From ced73e43af27e99d312d74846fdee1de9da4e48c Mon Sep 17 00:00:00 2001 From: Patrick Wuttke Date: Sat, 22 Nov 2025 12:50:14 +0100 Subject: [PATCH] Logging: added default formatter and makeLogFormatter() utility function. --- source/mijin/logging/formatting.hpp | 14 ++++++++++++-- source/mijin/logging/stdio_sink.hpp | 6 +++++- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/source/mijin/logging/formatting.hpp b/source/mijin/logging/formatting.hpp index 395be21..072c7aa 100644 --- a/source/mijin/logging/formatting.hpp +++ b/source/mijin/logging/formatting.hpp @@ -64,6 +64,15 @@ MIJIN_DEFINE_CHAR_VERSIONS_TMPL(SimpleLogFormatter, FORMATTER_COMMON_ARGS, FORMA #undef FORMATTER_COMMON_ARGS #undef FORMATTER_SET_ARGS +template> +DynamicPointer makeLogFormatter(typename TFormatter::string_t&& format) +{ + return makeDynamic(std::move(format)); +} + +template> +inline TFormatter DEFAULT_FORMATTER("[{level}] {text}"); + #define MIJIN_FORMATTING_SINK_COMMON_ARGS(chr_type) \ typename TTraits = std::char_traits, \ template typename TAllocator = MIJIN_DEFAULT_ALLOCATOR, \ @@ -95,10 +104,11 @@ private: formatter_ptr_t mFormatter; string_t mBuffer; public: - explicit BaseFormattingLogSink(formatter_ptr_t formatter, allocator_t allocator = {}) + explicit BaseFormattingLogSink(formatter_ptr_t formatter = wrapDynamic(&DEFAULT_FORMATTER), allocator_t allocator = {}) MIJIN_NOEXCEPT_IF(std::is_nothrow_move_constructible_v) : mFormatter(std::move(formatter)), mBuffer(std::move(allocator)) - {} + { + } virtual void handleMessageFormatted(const message_t& message, const string_t& formatted) MIJIN_NOEXCEPT = 0; diff --git a/source/mijin/logging/stdio_sink.hpp b/source/mijin/logging/stdio_sink.hpp index b00609d..dada535 100644 --- a/source/mijin/logging/stdio_sink.hpp +++ b/source/mijin/logging/stdio_sink.hpp @@ -9,6 +9,9 @@ namespace mijin { +template> +inline TFormatter DEFAULT_STDIO_FORMATTER("[{ansi:level_color}{level}{ansi:reset}] {text} <{file}:{line}>"); + template requires(allocator_type>) class BaseStdioSink : public BaseFormattingLogSink @@ -16,6 +19,7 @@ class BaseStdioSink : public BaseFormattingLogSink; using typename base_t::char_t; + using typename base_t::traits_t; using typename base_t::allocator_t; using typename base_t::formatter_ptr_t; using typename base_t::message_t; @@ -23,7 +27,7 @@ public: private: int mMinStderrLevel = MIJIN_LOG_LEVEL_VALUE_WARNING; public: - explicit BaseStdioSink(formatter_ptr_t formatter, allocator_t allocator = {}) + explicit BaseStdioSink(formatter_ptr_t formatter = wrapDynamic(&DEFAULT_STDIO_FORMATTER), allocator_t allocator = {}) MIJIN_NOEXCEPT_IF(std::is_nothrow_move_constructible_v) : base_t(std::move(formatter), std::move(allocator)) {}