switch to Tier0 LoggingSystem_*

This commit is contained in:
Lily Tsuru 2023-07-18 17:38:33 -04:00
parent 3d9f9139ff
commit 0d6646f8cb
4 changed files with 109 additions and 28 deletions

View File

@ -1,20 +1,56 @@
#include "SourceSink.hpp" #include "SourceSink.hpp"
#include <lucore/Library.hpp> #include <lucore/Library.hpp>
#include <lucore/Types.hpp>
namespace tier0 { namespace tier0 {
// TODO: For now this'll leak.. // TODO: For now this'll leak..
lucore::Library* library = nullptr; lucore::Library* library = nullptr;
// TODO: Switch this to using the LoggingSystem. It is available on the x86-64 branch of /// Minimally Source compatiable Color type
// gmod. /// (Even more POD than Source)
using Msg_t = void (*)(const char* format, ...); struct Color {
Msg_t Msg = nullptr; constexpr explicit Color(u8 r, u8 g, u8 b, u8 a) {
Msg_t Warning = nullptr; 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 tier0
namespace lcpu { namespace lcpu {
tier0::LoggingChannelID_t gSourceSinkLoggerChannel { -1 };
SourceSink& SourceSink::The() { SourceSink& SourceSink::The() {
static SourceSink sink; static SourceSink sink;
return 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 // 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.) // 32-bit GMod, and it also shares having only one tier0 library.)
tier0::library = lucore::Library::Open("tier0"); tier0::library = lucore::Library::Open("tier0");
tier0::Msg = tier0::library->Symbol<tier0::Msg_t>("Msg");
tier0::Warning = tier0::library->Symbol<tier0::Msg_t>("Warning"); #define GRAB_SYMBOL(name, T) tier0::name = tier0::library->Symbol<T>(#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) { void SourceSink::OutputMessage(const lucore::Logger::MessageData& data) {
auto formatted = auto formatted =
std::format("[LCPU Native/{}] [{}] {}\n", lucore::Logger::SeverityToString(data.severity), std::format("[LCPU Native/{}] [{}] {}\n", lucore::Logger::SeverityToString(data.severity),
data.time, std::vformat(data.format, data.args)); data.time, std::vformat(data.format, data.args));
if(data.severity < lucore::Logger::MessageSeverity::Warning) { auto lucore_to_source = [severity = data.severity]() -> tier0::LoggingSeverity_t {
tier0::Msg("%s", formatted.c_str()); using enum lucore::Logger::MessageSeverity;
} else { switch(severity) {
tier0::Warning("%s", formatted.c_str()); 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 } // namespace lcpu

View File

@ -11,7 +11,7 @@ GMOD_MODULE_OPEN() {
lucore::LogInfo("Hello Source World :) {} {}", 123.456, "This should work"); lucore::LogInfo("Hello Source World :) {} {}", 123.456, "This should work");
lucore::LogWarning("test"); lucore::LogWarning("test");
//LUCORE_CHECK(false, "this should bring the process down"); LUCORE_CHECK(false, "this should bring the process down");
return 0; return 0;
} }

View File

@ -0,0 +1,15 @@
#include <cstdint>
//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

View File

@ -1,26 +1,11 @@
//! Common types //! Common types
#pragma once #pragma once
#include <cassert> #include <lucore/Types.hpp>
#include <cstddef>
#include <cstdint>
namespace riscv { 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. /// A type that can repressent address space or address space offsets.
using AddressT = u32; using AddressT = u32;
} // namespace riscv } // namespace riscv