From 0d6646f8cb59d0817a0bef209ed0d1bfd4a91622 Mon Sep 17 00:00:00 2001 From: modeco80 Date: Tue, 18 Jul 2023 17:38:33 -0400 Subject: [PATCH] switch to Tier0 LoggingSystem_* --- native/projects/lcpu/src/SourceSink.cpp | 103 ++++++++++++++++-- native/projects/lcpu/src/main.cpp | 2 +- .../projects/lucore/include/lucore/Types.hpp | 15 +++ native/projects/riscv/include/riscv/Types.hpp | 17 +-- 4 files changed, 109 insertions(+), 28 deletions(-) create mode 100644 native/projects/lucore/include/lucore/Types.hpp diff --git a/native/projects/lcpu/src/SourceSink.cpp b/native/projects/lcpu/src/SourceSink.cpp index dbee2c4..da72bd0 100644 --- a/native/projects/lcpu/src/SourceSink.cpp +++ b/native/projects/lcpu/src/SourceSink.cpp @@ -1,20 +1,56 @@ #include "SourceSink.hpp" #include +#include namespace tier0 { // TODO: For now this'll leak.. lucore::Library* library = nullptr; - // TODO: Switch this to using the LoggingSystem. It is available on the x86-64 branch of - // gmod. - using Msg_t = void (*)(const char* format, ...); - Msg_t Msg = nullptr; - Msg_t Warning = nullptr; + /// Minimally Source compatiable Color type + /// (Even more POD than Source) + struct Color { + constexpr explicit Color(u8 r, u8 g, u8 b, u8 a) { + colors[0] = r; + colors[1] = g; + colors[2] = b; + colors[3] = a; + } + + u8 colors[4]; + }; + + using LoggingChannelID_t = s32; + enum LoggingSeverity_t { + LS_MESSAGE, + LS_WARNING, + LS_ASSERT, + LS_ERROR, + + LS_HIGHEST_SEVERITY + }; + + enum LoggingChannelFlags_t { LCF_CONSOLE_ONLY = 1, LCF_DO_NOT_ECHO = 2 }; + + using RegisterTagsFunc = void (*)(); + + // LoggingSystem_ APIs + using RegisterLoggingChannel_t = LoggingChannelID_t (*)(const char*, RegisterTagsFunc, + int flags, LoggingSeverity_t severity, + Color color); + using Log_t = s32 (*)(LoggingChannelID_t, LoggingSeverity_t, const char*, ...); + using GetChannelColor_t = Color (*)(LoggingChannelID_t); + + RegisterLoggingChannel_t LoggingSystem_RegisterLoggingChannel {}; + Log_t LoggingSystem_Log {}; + GetChannelColor_t LoggingSystem_GetChannelColor {}; + } // namespace tier0 namespace lcpu { + tier0::LoggingChannelID_t gSourceSinkLoggerChannel { -1 }; + SourceSink& SourceSink::The() { static SourceSink sink; return sink; @@ -29,19 +65,64 @@ namespace lcpu { // weirdly. You can build the module for this too, but not the non-x86-64 branch // 32-bit GMod, and it also shares having only one tier0 library.) tier0::library = lucore::Library::Open("tier0"); - tier0::Msg = tier0::library->Symbol("Msg"); - tier0::Warning = tier0::library->Symbol("Warning"); + +#define GRAB_SYMBOL(name, T) tier0::name = tier0::library->Symbol(#name); + + GRAB_SYMBOL(LoggingSystem_RegisterLoggingChannel, tier0::RegisterLoggingChannel_t); + GRAB_SYMBOL(LoggingSystem_Log, tier0::Log_t); + GRAB_SYMBOL(LoggingSystem_GetChannelColor, tier0::GetChannelColor_t); + + gSourceSinkLoggerChannel = tier0::LoggingSystem_RegisterLoggingChannel( + "LCPU Native", nullptr, 0, tier0::LS_MESSAGE, tier0::Color(0xff, 0x99, 0x00, 0xff)); } void SourceSink::OutputMessage(const lucore::Logger::MessageData& data) { auto formatted = std::format("[LCPU Native/{}] [{}] {}\n", lucore::Logger::SeverityToString(data.severity), data.time, std::vformat(data.format, data.args)); - if(data.severity < lucore::Logger::MessageSeverity::Warning) { - tier0::Msg("%s", formatted.c_str()); - } else { - tier0::Warning("%s", formatted.c_str()); + auto lucore_to_source = [severity = data.severity]() -> tier0::LoggingSeverity_t { + using enum lucore::Logger::MessageSeverity; + switch(severity) { + case Info: + return tier0::LS_MESSAGE; + case Warning: + return tier0::LS_WARNING; + case Error: + return tier0::LS_WARNING; + case Fatal: + return tier0::LS_ASSERT; + default: + return tier0::LS_MESSAGE; + } + }(); + +#if 0 + // This is a pretty stupid hack for console colors on Linux. + // I don't really like it that much tbh, wish tier0 supported this. + auto color = tier0::LoggingSystem_GetChannelColor(gSourceSinkLoggerChannel); + + { + using enum lucore::Logger::MessageSeverity; + switch(data.severity) { + case Warning: + color = tier0::Color(0xff, 0xff, 0x0, 0xff); + break; + case Error: + case Fatal: + color = tier0::Color(0xff, 0x0, 0x0, 0xff); + break; + default: + break; + } } + + tier0::LoggingSystem_Log(gSourceSinkLoggerChannel, lucore_to_source, + "\033[38;2;%d;%d;%dm%s\033[0m", color.colors[0], color.colors[1], + color.colors[2], formatted.c_str()); +#else + tier0::LoggingSystem_Log(gSourceSinkLoggerChannel, lucore_to_source, "%s", + formatted.c_str()); +#endif } } // namespace lcpu diff --git a/native/projects/lcpu/src/main.cpp b/native/projects/lcpu/src/main.cpp index 669c302..9d10ac9 100644 --- a/native/projects/lcpu/src/main.cpp +++ b/native/projects/lcpu/src/main.cpp @@ -11,7 +11,7 @@ GMOD_MODULE_OPEN() { lucore::LogInfo("Hello Source World :) {} {}", 123.456, "This should work"); lucore::LogWarning("test"); - //LUCORE_CHECK(false, "this should bring the process down"); + LUCORE_CHECK(false, "this should bring the process down"); return 0; } diff --git a/native/projects/lucore/include/lucore/Types.hpp b/native/projects/lucore/include/lucore/Types.hpp new file mode 100644 index 0000000..98a77fe --- /dev/null +++ b/native/projects/lucore/include/lucore/Types.hpp @@ -0,0 +1,15 @@ +#include + +//namespace lucore { + using u8 = std::uint8_t; + using s8 = std::int8_t; + using u16 = std::uint16_t; + using s16 = std::int16_t; + using u32 = std::uint32_t; + using s32 = std::int32_t; + using u64 = std::uint64_t; + using s64 = std::int64_t; + using usize = std::size_t; + using ssize = std::intptr_t; + +//} // namespace lucore diff --git a/native/projects/riscv/include/riscv/Types.hpp b/native/projects/riscv/include/riscv/Types.hpp index cf17f59..32ca17c 100644 --- a/native/projects/riscv/include/riscv/Types.hpp +++ b/native/projects/riscv/include/riscv/Types.hpp @@ -1,26 +1,11 @@ //! Common types #pragma once -#include -#include -#include +#include namespace riscv { - using u8 = std::uint8_t; - using s8 = std::int8_t; - using u16 = std::uint16_t; - using s16 = std::int16_t; - using u32 = std::uint32_t; - using s32 = std::int32_t; - using u64 = std::uint64_t; - using s64 = std::int64_t; - using usize = std::size_t; - using ssize = std::intptr_t; - /// A type that can repressent address space or address space offsets. using AddressT = u32; - - } // namespace riscv