Skip to content

Commit

Permalink
C4LogSystem: Swap order of logger name and log level in messages
Browse files Browse the repository at this point in the history
  • Loading branch information
Fulgen301 committed Nov 16, 2024
1 parent 923243c commit ddc0792
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
17 changes: 12 additions & 5 deletions src/C4Log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ class LoggerNameIfExistsFormatterFlag : public spdlog::custom_flag_formatter

}

C4LogSystem::LogSink::LogSink()
C4LogSystem::LogSink::LogSink(std::unique_ptr<spdlog::formatter> formatter)
: base_sink{std::move(formatter)}
{
std::string logFileName{C4CFN_Log};
// open
Expand Down Expand Up @@ -262,10 +263,16 @@ C4LogSystem::C4LogSystem()
{
spdlog::set_automatic_registration(false);

defaultPatternFormatter = std::make_unique<spdlog::pattern_formatter>();
defaultPatternFormatter->add_flag<LoggerNameIfExistsFormatterFlag>('~');
defaultPatternFormatter->set_pattern("[%Y-%m-%d %H:%M:%S.%e] [%^%l%$] %~%v");

loggerSilentGuiSink = std::make_shared<GuiSink>(spdlog::level::warn, true);

#ifdef _WIN32
debugSink = std::make_shared<spdlog::sinks::msvc_sink_mt>();
debugSink->set_formatter(defaultPatternFormatter->clone());

loggerSilent = std::make_shared<spdlog::logger>("", std::initializer_list<spdlog::sink_ptr>{loggerSilentGuiSink, debugSink});
#else
loggerSilent = std::make_shared<spdlog::logger>("", loggerSilentGuiSink);
Expand All @@ -285,7 +292,7 @@ C4LogSystem::C4LogSystem()
#endif
loggerDebug->set_level(spdlog::level::trace);

ringbufferSink = std::make_shared<RingbufferSink>(100);
ringbufferSink = std::make_shared<RingbufferSink>(defaultPatternFormatter->clone(), 100);
logger->sinks().emplace_back(ringbufferSink);
}

Expand All @@ -296,9 +303,10 @@ void C4LogSystem::OpenLog()
#endif

stdoutSink = std::static_pointer_cast<spdlog::sinks::sink>(std::make_shared<spdlog::sinks::stdout_color_sink_mt>());
stdoutSink->set_formatter(defaultPatternFormatter->clone());
stdoutSink->set_level(spdlog::level::info);

auto clonkLogSink = std::make_shared<LogSink>();
auto clonkLogSink = std::make_shared<LogSink>(defaultPatternFormatter->clone());
clonkLogFD = clonkLogSink->GetFD();

clonkToUtf8Sink = std::make_shared<ClonkToUtf8Sink>(std::initializer_list<spdlog::sink_ptr>{stdoutSink, std::static_pointer_cast<spdlog::sinks::sink>(clonkLogSink)});
Expand All @@ -308,8 +316,7 @@ void C4LogSystem::OpenLog()
logger = std::make_shared<spdlog::logger>("", loggerSilent->sinks().begin() + 1, loggerSilent->sinks().end());
logger->set_level(spdlog::level::trace);

auto guiSink = std::make_shared<GuiSink>();
guiSink->set_pattern("%v");
auto guiSink = std::make_shared<GuiSink>(std::make_unique<spdlog::pattern_formatter>("%v"));

logger->sinks().insert(logger->sinks().begin(), std::move(guiSink));

Expand Down
8 changes: 5 additions & 3 deletions src/C4Log.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

#include <fmt/printf.h>
#include <spdlog/spdlog.h>
#include <spdlog/pattern_formatter.h>
#include <spdlog/sinks/base_sink.h>


Expand All @@ -44,7 +45,7 @@ class C4LogSystem
class LogSink : public spdlog::sinks::base_sink<std::mutex>
{
public:
LogSink();
LogSink(std::unique_ptr<spdlog::formatter> formatter);
~LogSink();

LogSink(const LogSink &) = delete;
Expand All @@ -67,7 +68,7 @@ class C4LogSystem
class GuiSink : public spdlog::sinks::base_sink<spdlog::details::null_mutex>, public std::enable_shared_from_this<GuiSink>
{
public:
GuiSink() = default;
GuiSink(std::unique_ptr<spdlog::formatter> formatter) : base_sink{std::move(formatter)} {}
GuiSink(spdlog::level::level_enum level, bool showLoggerNameInGui);

protected:
Expand All @@ -81,7 +82,7 @@ class C4LogSystem
class RingbufferSink : public spdlog::sinks::base_sink<std::mutex>
{
public:
RingbufferSink(const std::size_t size) : size{size}, ringbuffer{size} {}
RingbufferSink(std::unique_ptr<spdlog::formatter> formatter, const std::size_t size) : base_sink{std::move(formatter)}, size{size}, ringbuffer{size} {}

public:
std::vector<std::string> TakeMessages();
Expand Down Expand Up @@ -146,6 +147,7 @@ class C4LogSystem
#endif

private:
std::unique_ptr<spdlog::pattern_formatter> defaultPatternFormatter;
std::shared_ptr<spdlog::logger> logger;
std::shared_ptr<spdlog::logger> loggerSilent;
std::shared_ptr<spdlog::logger> loggerDebug;
Expand Down

0 comments on commit ddc0792

Please sign in to comment.