aboutsummaryrefslogtreecommitdiff
path: root/src/zencore
diff options
context:
space:
mode:
authorStefan Boberg <[email protected]>2026-03-09 10:50:47 +0100
committerGitHub Enterprise <[email protected]>2026-03-09 10:50:47 +0100
commit19a117889c2db6b817af9458c04c04f324162e75 (patch)
treefbbd0d01c5bf40be90cec88e1d02c6a3c529a2f5 /src/zencore
parentzenstore bug-fixes from static analysis pass (#815) (diff)
downloadzen-19a117889c2db6b817af9458c04c04f324162e75.tar.xz
zen-19a117889c2db6b817af9458c04c04f324162e75.zip
Eliminate spdlog dependency (#773)
Removes the vendored spdlog library (~12,000 lines) and replaces it with a purpose-built logging system in zencore (~1,800 lines). The new implementation provides the same functionality with fewer abstractions, no shared_ptr overhead, and full control over the logging pipeline. ### What changed **New logging core in zencore/logging/:** - LogMessage, Formatter, Sink, Logger, Registry - core abstractions matching spdlog's model but simplified - AnsiColorStdoutSink - ANSI color console output (replaces spdlog stdout_color_sink) - MsvcSink - OutputDebugString on Windows (replaces spdlog msvc_sink) - AsyncSink - async logging via BlockingQueue worker thread (replaces spdlog async_logger) - NullSink, MessageOnlyFormatter - utility types - Thread-safe timestamp caching in formatters using RwLock **Moved to zenutil/logging/:** - FullFormatter - full log formatting with timestamp, logger name, level, source location, multiline alignment - JsonFormatter - structured JSON log output - RotatingFileSink - rotating file sink with atomic size tracking **API changes:** - Log levels are now an enum (LogLevel) instead of int, eliminating the zen::logging::level namespace - LoggerRef no longer wraps shared_ptr - it holds a raw pointer with the registry owning lifetime - Logger error handler is wired through Registry and propagated to all loggers on registration - Logger::Log() now populates ThreadId on every message **Cleanup:** - Deleted thirdparty/spdlog/ entirely (110+ files) - Deleted full_test_formatter (was ~80% duplicate of FullFormatter) - Renamed snake_case classes to PascalCase (full_formatter -> FullFormatter, json_formatter -> JsonFormatter, sentry_sink -> SentrySink) - Removed spdlog from xmake dependency graph ### Build / test impact - zencore no longer depends on spdlog - zenutil and zenvfs xmake.lua updated to drop spdlog dep - zentelemetry xmake.lua updated to drop spdlog dep - All existing tests pass, no test changes required beyond formatter class renames
Diffstat (limited to 'src/zencore')
-rw-r--r--src/zencore/include/zencore/blockingqueue.h2
-rw-r--r--src/zencore/include/zencore/logbase.h113
-rw-r--r--src/zencore/include/zencore/logging.h214
-rw-r--r--src/zencore/include/zencore/logging/ansicolorsink.h26
-rw-r--r--src/zencore/include/zencore/logging/asyncsink.h30
-rw-r--r--src/zencore/include/zencore/logging/formatter.h20
-rw-r--r--src/zencore/include/zencore/logging/helpers.h122
-rw-r--r--src/zencore/include/zencore/logging/logger.h63
-rw-r--r--src/zencore/include/zencore/logging/logmsg.h66
-rw-r--r--src/zencore/include/zencore/logging/memorybuffer.h11
-rw-r--r--src/zencore/include/zencore/logging/messageonlyformatter.h22
-rw-r--r--src/zencore/include/zencore/logging/msvcsink.h30
-rw-r--r--src/zencore/include/zencore/logging/nullsink.h17
-rw-r--r--src/zencore/include/zencore/logging/registry.h70
-rw-r--r--src/zencore/include/zencore/logging/sink.h34
-rw-r--r--src/zencore/include/zencore/logging/tracesink.h23
-rw-r--r--src/zencore/include/zencore/sentryintegration.h8
-rw-r--r--src/zencore/logging.cpp328
-rw-r--r--src/zencore/logging/ansicolorsink.cpp178
-rw-r--r--src/zencore/logging/asyncsink.cpp212
-rw-r--r--src/zencore/logging/logger.cpp142
-rw-r--r--src/zencore/logging/msvcsink.cpp80
-rw-r--r--src/zencore/logging/registry.cpp330
-rw-r--r--src/zencore/logging/tracesink.cpp88
-rw-r--r--src/zencore/sentryintegration.cpp128
-rw-r--r--src/zencore/testing.cpp4
-rw-r--r--src/zencore/xmake.lua1
-rw-r--r--src/zencore/zencore.cpp2
28 files changed, 1912 insertions, 452 deletions
diff --git a/src/zencore/include/zencore/blockingqueue.h b/src/zencore/include/zencore/blockingqueue.h
index e91fdc659..b6c93e937 100644
--- a/src/zencore/include/zencore/blockingqueue.h
+++ b/src/zencore/include/zencore/blockingqueue.h
@@ -2,6 +2,8 @@
#pragma once
+#include <zencore/zencore.h> // For ZEN_ASSERT
+
#include <atomic>
#include <condition_variable>
#include <deque>
diff --git a/src/zencore/include/zencore/logbase.h b/src/zencore/include/zencore/logbase.h
index 00af68b0a..ece17a85e 100644
--- a/src/zencore/include/zencore/logbase.h
+++ b/src/zencore/include/zencore/logbase.h
@@ -4,96 +4,85 @@
#include <string_view>
-#define ZEN_LOG_LEVEL_TRACE 0
-#define ZEN_LOG_LEVEL_DEBUG 1
-#define ZEN_LOG_LEVEL_INFO 2
-#define ZEN_LOG_LEVEL_WARN 3
-#define ZEN_LOG_LEVEL_ERROR 4
-#define ZEN_LOG_LEVEL_CRITICAL 5
-#define ZEN_LOG_LEVEL_OFF 6
-
-#define ZEN_LEVEL_NAME_TRACE std::string_view("trace", 5)
-#define ZEN_LEVEL_NAME_DEBUG std::string_view("debug", 5)
-#define ZEN_LEVEL_NAME_INFO std::string_view("info", 4)
-#define ZEN_LEVEL_NAME_WARNING std::string_view("warning", 7)
-#define ZEN_LEVEL_NAME_ERROR std::string_view("error", 5)
-#define ZEN_LEVEL_NAME_CRITICAL std::string_view("critical", 8)
-#define ZEN_LEVEL_NAME_OFF std::string_view("off", 3)
-
-namespace zen::logging::level {
+namespace zen::logging {
enum LogLevel : int
{
- Trace = ZEN_LOG_LEVEL_TRACE,
- Debug = ZEN_LOG_LEVEL_DEBUG,
- Info = ZEN_LOG_LEVEL_INFO,
- Warn = ZEN_LOG_LEVEL_WARN,
- Err = ZEN_LOG_LEVEL_ERROR,
- Critical = ZEN_LOG_LEVEL_CRITICAL,
- Off = ZEN_LOG_LEVEL_OFF,
+ Trace,
+ Debug,
+ Info,
+ Warn,
+ Err,
+ Critical,
+ Off,
LogLevelCount
};
LogLevel ParseLogLevelString(std::string_view String);
std::string_view ToStringView(LogLevel Level);
-} // namespace zen::logging::level
-
-namespace zen::logging {
-
-void SetLogLevel(level::LogLevel NewLogLevel);
-level::LogLevel GetLogLevel();
+void SetLogLevel(LogLevel NewLogLevel);
+LogLevel GetLogLevel();
-} // namespace zen::logging
+struct SourceLocation
+{
+ constexpr SourceLocation() = default;
+ constexpr SourceLocation(const char* InFilename, int InLine) : Filename(InFilename), Line(InLine) {}
-namespace spdlog {
-class logger;
-}
+ constexpr operator bool() const noexcept { return Line != 0; }
-namespace zen::logging {
+ const char* Filename{nullptr};
+ int Line{0};
+};
-struct SourceLocation
+/** This encodes the constant parts of a log message which can be emitted once
+ * and then referred to by log events.
+ *
+ * It's *critical* that instances of this struct are permanent and never
+ * destroyed, as log messages will refer to them by pointer. The easiest way
+ * to ensure this is to create them as function-local statics.
+ *
+ * The logging macros already do this for you so this should not be something
+ * you normally would need to worry about.
+ */
+struct LogPoint
{
- constexpr SourceLocation() = default;
- constexpr SourceLocation(const char* filename_in, int line_in, const char* funcname_in)
- : filename(filename_in)
- , line(line_in)
- , funcname(funcname_in)
- {
- }
-
- constexpr bool empty() const noexcept { return line == 0; }
-
- // IMPORTANT NOTE: the layout of this class must match the spdlog::source_loc class
- // since we currently pass a pointer to it into spdlog after casting it to
- // spdlog::source_loc*
- //
- // This is intended to be an intermediate state, before we (probably) transition off
- // spdlog entirely
-
- const char* filename{nullptr};
- int line{0};
- const char* funcname{nullptr};
+ SourceLocation Location;
+ LogLevel Level;
+ std::string_view FormatString;
};
+class Logger;
+
} // namespace zen::logging
namespace zen {
+// Lightweight non-owning handle to a Logger. Loggers are owned by the Registry
+// via Ref<Logger>; LoggerRef exists as a cheap (raw pointer) handle that can be
+// stored in members and passed through logging macros without requiring the
+// complete Logger type or incurring refcount overhead on every log call.
struct LoggerRef
{
LoggerRef() = default;
- LoggerRef(spdlog::logger& InLogger) : SpdLogger(&InLogger) {}
+ LoggerRef(logging::Logger& InLogger) : m_Logger(&InLogger) {}
+ // This exists so that logging macros can pass LoggerRef or LogCategory
+ // to ZEN_LOG without needing to know which one it is
LoggerRef Logger() { return *this; }
- bool ShouldLog(int Level) const;
- inline operator bool() const { return SpdLogger != nullptr; }
+ bool ShouldLog(logging::LogLevel Level) const;
+ inline operator bool() const { return m_Logger != nullptr; }
+
+ inline logging::Logger* operator->() const { return m_Logger; }
+ inline logging::Logger& operator*() const { return *m_Logger; }
- void SetLogLevel(logging::level::LogLevel NewLogLevel);
- logging::level::LogLevel GetLogLevel();
+ void SetLogLevel(logging::LogLevel NewLogLevel);
+ logging::LogLevel GetLogLevel();
+ void Flush();
- spdlog::logger* SpdLogger = nullptr;
+private:
+ logging::Logger* m_Logger = nullptr;
};
} // namespace zen
diff --git a/src/zencore/include/zencore/logging.h b/src/zencore/include/zencore/logging.h
index 74a44d028..4b593c19e 100644
--- a/src/zencore/include/zencore/logging.h
+++ b/src/zencore/include/zencore/logging.h
@@ -9,16 +9,9 @@
#if ZEN_PLATFORM_WINDOWS
# define ZEN_LOG_SECTION(Id) ZEN_DATA_SECTION(Id)
-# pragma section(".zlog$f", read)
# pragma section(".zlog$l", read)
-# pragma section(".zlog$m", read)
-# pragma section(".zlog$s", read)
-# define ZEN_DECLARE_FUNCTION static constinit ZEN_LOG_SECTION(".zlog$f") char FuncName[] = __FUNCTION__;
-# define ZEN_LOG_FUNCNAME FuncName
#else
# define ZEN_LOG_SECTION(Id)
-# define ZEN_DECLARE_FUNCTION
-# define ZEN_LOG_FUNCNAME static_cast<const char*>(__func__)
#endif
namespace zen::logging {
@@ -37,34 +30,29 @@ LoggerRef ErrorLog();
void SetErrorLog(std::string_view LoggerId);
LoggerRef Get(std::string_view Name);
-void ConfigureLogLevels(level::LogLevel Level, std::string_view Loggers);
+void ConfigureLogLevels(LogLevel Level, std::string_view Loggers);
void RefreshLogLevels();
-void RefreshLogLevels(level::LogLevel DefaultLevel);
-
+void RefreshLogLevels(LogLevel DefaultLevel);
+
+/** LogCategory allows for the creation of log categories that can be used with
+ * the logging macros just like a logger reference. The main purpose of this is
+ * to allow for static log categories in global scope where we can't actually
+ * go ahead and instantiate a logger immediately because the logging system may
+ * not be initialized yet.
+ */
struct LogCategory
{
- inline LogCategory(std::string_view InCategory) : CategoryName(InCategory) {}
-
- inline zen::LoggerRef Logger()
- {
- if (LoggerRef)
- {
- return LoggerRef;
- }
+ inline LogCategory(std::string_view InCategory) : m_CategoryName(InCategory) {}
- LoggerRef = zen::logging::Get(CategoryName);
- return LoggerRef;
- }
+ LoggerRef Logger();
- std::string CategoryName;
- zen::LoggerRef LoggerRef;
+private:
+ std::string m_CategoryName;
+ LoggerRef m_LoggerRef;
};
-void EmitConsoleLogMessage(int LogLevel, std::string_view Format, fmt::format_args Args);
-void EmitLogMessage(LoggerRef& Logger, int LogLevel, std::string_view Message);
-void EmitLogMessage(LoggerRef& Logger, const SourceLocation& Location, int LogLevel, std::string_view Message);
-void EmitLogMessage(LoggerRef& Logger, int LogLevel, std::string_view Format, fmt::format_args Args);
-void EmitLogMessage(LoggerRef& Logger, const SourceLocation& Location, int LogLevel, std::string_view Format, fmt::format_args Args);
+void EmitConsoleLogMessage(const LogPoint& Lp, fmt::format_args Args);
+void EmitLogMessage(LoggerRef& Logger, const LogPoint& Lp, fmt::format_args Args);
template<typename... T>
auto
@@ -79,15 +67,14 @@ namespace zen {
extern LoggerRef TheDefaultLogger;
-inline LoggerRef
-Log()
-{
- if (TheDefaultLogger)
- {
- return TheDefaultLogger;
- }
- return zen::logging::ConsoleLog();
-}
+/**
+ * This is the default logger, which any ZEN_INFO et al will get if there's
+ * no Log() function declared in the current scope.
+ *
+ * Typically, classes which want to log to its own channel will declare a Log()
+ * member function which returns a LoggerRef created at construction time.
+ */
+LoggerRef Log();
using logging::ConsoleLog;
using logging::ErrorLog;
@@ -98,12 +85,6 @@ using zen::ConsoleLog;
using zen::ErrorLog;
using zen::Log;
-inline consteval bool
-LogIsErrorLevel(int LogLevel)
-{
- return (LogLevel == zen::logging::level::Err || LogLevel == zen::logging::level::Critical);
-};
-
#if ZEN_BUILD_DEBUG
# define ZEN_CHECK_FORMAT_STRING(fmtstr, ...) \
while (false) \
@@ -117,75 +98,66 @@ LogIsErrorLevel(int LogLevel)
}
#endif
-#define ZEN_LOG_WITH_LOCATION(InLogger, InLevel, fmtstr, ...) \
- do \
- { \
- using namespace std::literals; \
- ZEN_DECLARE_FUNCTION \
- static constinit ZEN_LOG_SECTION(".zlog$s") char FileName[] = __FILE__; \
- static constinit ZEN_LOG_SECTION(".zlog$m") char FormatString[] = fmtstr; \
- static constinit ZEN_LOG_SECTION(".zlog$l") zen::logging::SourceLocation Location{FileName, __LINE__, ZEN_LOG_FUNCNAME}; \
- zen::LoggerRef Logger = InLogger; \
- ZEN_CHECK_FORMAT_STRING(fmtstr##sv, ##__VA_ARGS__); \
- if (Logger.ShouldLog(InLevel)) \
- { \
- zen::logging::EmitLogMessage(Logger, \
- Location, \
- InLevel, \
- std::string_view(FormatString, sizeof FormatString - 1), \
- zen::logging::LogCaptureArguments(__VA_ARGS__)); \
- } \
+#define ZEN_LOG_WITH_LOCATION(InLogger, InLevel, fmtstr, ...) \
+ do \
+ { \
+ using namespace std::literals; \
+ static constinit ZEN_LOG_SECTION(".zlog$l") \
+ zen::logging::LogPoint LogPoint{zen::logging::SourceLocation{__FILE__, __LINE__}, InLevel, std::string_view(fmtstr)}; \
+ zen::LoggerRef Logger = InLogger; \
+ ZEN_CHECK_FORMAT_STRING(fmtstr##sv, ##__VA_ARGS__); \
+ if (Logger.ShouldLog(InLevel)) \
+ { \
+ zen::logging::EmitLogMessage(Logger, LogPoint, zen::logging::LogCaptureArguments(__VA_ARGS__)); \
+ } \
} while (false);
-#define ZEN_LOG(InLogger, InLevel, fmtstr, ...) \
- do \
- { \
- using namespace std::literals; \
- static constinit ZEN_LOG_SECTION(".zlog$m") char FormatString[] = fmtstr; \
- zen::LoggerRef Logger = InLogger; \
- ZEN_CHECK_FORMAT_STRING(fmtstr##sv, ##__VA_ARGS__); \
- if (Logger.ShouldLog(InLevel)) \
- { \
- zen::logging::EmitLogMessage(Logger, \
- InLevel, \
- std::string_view(FormatString, sizeof FormatString - 1), \
- zen::logging::LogCaptureArguments(__VA_ARGS__)); \
- } \
+#define ZEN_LOG(InLogger, InLevel, fmtstr, ...) \
+ do \
+ { \
+ using namespace std::literals; \
+ static constinit ZEN_LOG_SECTION(".zlog$l") zen::logging::LogPoint LogPoint{{}, InLevel, std::string_view(fmtstr)}; \
+ zen::LoggerRef Logger = InLogger; \
+ ZEN_CHECK_FORMAT_STRING(fmtstr##sv, ##__VA_ARGS__); \
+ if (Logger.ShouldLog(InLevel)) \
+ { \
+ zen::logging::EmitLogMessage(Logger, LogPoint, zen::logging::LogCaptureArguments(__VA_ARGS__)); \
+ } \
} while (false);
#define ZEN_DEFINE_LOG_CATEGORY_STATIC(Category, Name) \
static zen::logging::LogCategory Category { Name }
-#define ZEN_LOG_TRACE(Category, fmtstr, ...) ZEN_LOG(Category.Logger(), zen::logging::level::Trace, fmtstr, ##__VA_ARGS__)
-#define ZEN_LOG_DEBUG(Category, fmtstr, ...) ZEN_LOG(Category.Logger(), zen::logging::level::Debug, fmtstr, ##__VA_ARGS__)
-#define ZEN_LOG_INFO(Category, fmtstr, ...) ZEN_LOG(Category.Logger(), zen::logging::level::Info, fmtstr, ##__VA_ARGS__)
-#define ZEN_LOG_WARN(Category, fmtstr, ...) ZEN_LOG(Category.Logger(), zen::logging::level::Warn, fmtstr, ##__VA_ARGS__)
-#define ZEN_LOG_ERROR(Category, fmtstr, ...) ZEN_LOG_WITH_LOCATION(Category.Logger(), zen::logging::level::Err, fmtstr, ##__VA_ARGS__)
-#define ZEN_LOG_CRITICAL(Category, fmtstr, ...) \
- ZEN_LOG_WITH_LOCATION(Category.Logger(), zen::logging::level::Critical, fmtstr, ##__VA_ARGS__)
-
-#define ZEN_TRACE(fmtstr, ...) ZEN_LOG(Log(), zen::logging::level::Trace, fmtstr, ##__VA_ARGS__)
-#define ZEN_DEBUG(fmtstr, ...) ZEN_LOG(Log(), zen::logging::level::Debug, fmtstr, ##__VA_ARGS__)
-#define ZEN_INFO(fmtstr, ...) ZEN_LOG(Log(), zen::logging::level::Info, fmtstr, ##__VA_ARGS__)
-#define ZEN_WARN(fmtstr, ...) ZEN_LOG(Log(), zen::logging::level::Warn, fmtstr, ##__VA_ARGS__)
-#define ZEN_ERROR(fmtstr, ...) ZEN_LOG_WITH_LOCATION(Log(), zen::logging::level::Err, fmtstr, ##__VA_ARGS__)
-#define ZEN_CRITICAL(fmtstr, ...) ZEN_LOG_WITH_LOCATION(Log(), zen::logging::level::Critical, fmtstr, ##__VA_ARGS__)
-
-#define ZEN_CONSOLE_LOG(InLevel, fmtstr, ...) \
- do \
- { \
- using namespace std::literals; \
- ZEN_CHECK_FORMAT_STRING(fmtstr##sv, ##__VA_ARGS__); \
- zen::logging::EmitConsoleLogMessage(InLevel, fmtstr, zen::logging::LogCaptureArguments(__VA_ARGS__)); \
+#define ZEN_LOG_TRACE(Category, fmtstr, ...) ZEN_LOG(Category.Logger(), zen::logging::Trace, fmtstr, ##__VA_ARGS__)
+#define ZEN_LOG_DEBUG(Category, fmtstr, ...) ZEN_LOG(Category.Logger(), zen::logging::Debug, fmtstr, ##__VA_ARGS__)
+#define ZEN_LOG_INFO(Category, fmtstr, ...) ZEN_LOG(Category.Logger(), zen::logging::Info, fmtstr, ##__VA_ARGS__)
+#define ZEN_LOG_WARN(Category, fmtstr, ...) ZEN_LOG(Category.Logger(), zen::logging::Warn, fmtstr, ##__VA_ARGS__)
+#define ZEN_LOG_ERROR(Category, fmtstr, ...) ZEN_LOG_WITH_LOCATION(Category.Logger(), zen::logging::Err, fmtstr, ##__VA_ARGS__)
+#define ZEN_LOG_CRITICAL(Category, fmtstr, ...) ZEN_LOG_WITH_LOCATION(Category.Logger(), zen::logging::Critical, fmtstr, ##__VA_ARGS__)
+
+#define ZEN_TRACE(fmtstr, ...) ZEN_LOG(Log(), zen::logging::Trace, fmtstr, ##__VA_ARGS__)
+#define ZEN_DEBUG(fmtstr, ...) ZEN_LOG(Log(), zen::logging::Debug, fmtstr, ##__VA_ARGS__)
+#define ZEN_INFO(fmtstr, ...) ZEN_LOG(Log(), zen::logging::Info, fmtstr, ##__VA_ARGS__)
+#define ZEN_WARN(fmtstr, ...) ZEN_LOG(Log(), zen::logging::Warn, fmtstr, ##__VA_ARGS__)
+#define ZEN_ERROR(fmtstr, ...) ZEN_LOG_WITH_LOCATION(Log(), zen::logging::Err, fmtstr, ##__VA_ARGS__)
+#define ZEN_CRITICAL(fmtstr, ...) ZEN_LOG_WITH_LOCATION(Log(), zen::logging::Critical, fmtstr, ##__VA_ARGS__)
+
+#define ZEN_CONSOLE_LOG(InLevel, fmtstr, ...) \
+ do \
+ { \
+ using namespace std::literals; \
+ static constinit ZEN_LOG_SECTION(".zlog$l") zen::logging::LogPoint LogPoint{{}, InLevel, std::string_view(fmtstr)}; \
+ ZEN_CHECK_FORMAT_STRING(fmtstr##sv, ##__VA_ARGS__); \
+ zen::logging::EmitConsoleLogMessage(LogPoint, zen::logging::LogCaptureArguments(__VA_ARGS__)); \
} while (false)
-#define ZEN_CONSOLE(fmtstr, ...) ZEN_CONSOLE_LOG(zen::logging::level::Info, fmtstr, ##__VA_ARGS__)
-#define ZEN_CONSOLE_TRACE(fmtstr, ...) ZEN_CONSOLE_LOG(zen::logging::level::Trace, fmtstr, ##__VA_ARGS__)
-#define ZEN_CONSOLE_DEBUG(fmtstr, ...) ZEN_CONSOLE_LOG(zen::logging::level::Debug, fmtstr, ##__VA_ARGS__)
-#define ZEN_CONSOLE_INFO(fmtstr, ...) ZEN_CONSOLE_LOG(zen::logging::level::Info, fmtstr, ##__VA_ARGS__)
-#define ZEN_CONSOLE_WARN(fmtstr, ...) ZEN_CONSOLE_LOG(zen::logging::level::Warn, fmtstr, ##__VA_ARGS__)
-#define ZEN_CONSOLE_ERROR(fmtstr, ...) ZEN_CONSOLE_LOG(zen::logging::level::Err, fmtstr, ##__VA_ARGS__)
-#define ZEN_CONSOLE_CRITICAL(fmtstr, ...) ZEN_CONSOLE_LOG(zen::logging::level::Critical, fmtstr, ##__VA_ARGS__)
+#define ZEN_CONSOLE(fmtstr, ...) ZEN_CONSOLE_LOG(zen::logging::Info, fmtstr, ##__VA_ARGS__)
+#define ZEN_CONSOLE_TRACE(fmtstr, ...) ZEN_CONSOLE_LOG(zen::logging::Trace, fmtstr, ##__VA_ARGS__)
+#define ZEN_CONSOLE_DEBUG(fmtstr, ...) ZEN_CONSOLE_LOG(zen::logging::Debug, fmtstr, ##__VA_ARGS__)
+#define ZEN_CONSOLE_INFO(fmtstr, ...) ZEN_CONSOLE_LOG(zen::logging::Info, fmtstr, ##__VA_ARGS__)
+#define ZEN_CONSOLE_WARN(fmtstr, ...) ZEN_CONSOLE_LOG(zen::logging::Warn, fmtstr, ##__VA_ARGS__)
+#define ZEN_CONSOLE_ERROR(fmtstr, ...) ZEN_CONSOLE_LOG(zen::logging::Err, fmtstr, ##__VA_ARGS__)
+#define ZEN_CONSOLE_CRITICAL(fmtstr, ...) ZEN_CONSOLE_LOG(zen::logging::Critical, fmtstr, ##__VA_ARGS__)
//////////////////////////////////////////////////////////////////////////
@@ -240,28 +212,28 @@ std::string_view EmitActivitiesForLogging(StringBuilderBase& OutString);
#define ZEN_LOG_SCOPE(...) ScopedLazyActivity $Activity##__LINE__([&](StringBuilderBase& Out) { Out << fmt::format(__VA_ARGS__); })
-#define ZEN_SCOPED_WARN(fmtstr, ...) \
- do \
- { \
- ExtendableStringBuilder<256> ScopeString; \
- const std::string_view Scopes = EmitActivitiesForLogging(ScopeString); \
- ZEN_LOG(Log(), zen::logging::level::Warn, fmtstr "{}", ##__VA_ARGS__, Scopes); \
+#define ZEN_SCOPED_WARN(fmtstr, ...) \
+ do \
+ { \
+ ExtendableStringBuilder<256> ScopeString; \
+ const std::string_view Scopes = EmitActivitiesForLogging(ScopeString); \
+ ZEN_LOG(Log(), zen::logging::Warn, fmtstr "{}", ##__VA_ARGS__, Scopes); \
} while (false)
-#define ZEN_SCOPED_ERROR(fmtstr, ...) \
- do \
- { \
- ExtendableStringBuilder<256> ScopeString; \
- const std::string_view Scopes = EmitActivitiesForLogging(ScopeString); \
- ZEN_LOG_WITH_LOCATION(Log(), zen::logging::level::Err, fmtstr "{}", ##__VA_ARGS__, Scopes); \
+#define ZEN_SCOPED_ERROR(fmtstr, ...) \
+ do \
+ { \
+ ExtendableStringBuilder<256> ScopeString; \
+ const std::string_view Scopes = EmitActivitiesForLogging(ScopeString); \
+ ZEN_LOG_WITH_LOCATION(Log(), zen::logging::Err, fmtstr "{}", ##__VA_ARGS__, Scopes); \
} while (false)
-#define ZEN_SCOPED_CRITICAL(fmtstr, ...) \
- do \
- { \
- ExtendableStringBuilder<256> ScopeString; \
- const std::string_view Scopes = EmitActivitiesForLogging(ScopeString); \
- ZEN_LOG_WITH_LOCATION(Log(), zen::logging::level::Critical, fmtstr "{}", ##__VA_ARGS__, Scopes); \
+#define ZEN_SCOPED_CRITICAL(fmtstr, ...) \
+ do \
+ { \
+ ExtendableStringBuilder<256> ScopeString; \
+ const std::string_view Scopes = EmitActivitiesForLogging(ScopeString); \
+ ZEN_LOG_WITH_LOCATION(Log(), zen::logging::Critical, fmtstr "{}", ##__VA_ARGS__, Scopes); \
} while (false)
ScopedActivityBase* GetThreadActivity();
diff --git a/src/zencore/include/zencore/logging/ansicolorsink.h b/src/zencore/include/zencore/logging/ansicolorsink.h
new file mode 100644
index 000000000..9f859e8d7
--- /dev/null
+++ b/src/zencore/include/zencore/logging/ansicolorsink.h
@@ -0,0 +1,26 @@
+// Copyright Epic Games, Inc. All Rights Reserved.
+
+#pragma once
+
+#include <zencore/logging/sink.h>
+
+#include <memory>
+
+namespace zen::logging {
+
+class AnsiColorStdoutSink : public Sink
+{
+public:
+ AnsiColorStdoutSink();
+ ~AnsiColorStdoutSink() override;
+
+ void Log(const LogMessage& Msg) override;
+ void Flush() override;
+ void SetFormatter(std::unique_ptr<Formatter> InFormatter) override;
+
+private:
+ struct Impl;
+ std::unique_ptr<Impl> m_Impl;
+};
+
+} // namespace zen::logging
diff --git a/src/zencore/include/zencore/logging/asyncsink.h b/src/zencore/include/zencore/logging/asyncsink.h
new file mode 100644
index 000000000..c49a1ccce
--- /dev/null
+++ b/src/zencore/include/zencore/logging/asyncsink.h
@@ -0,0 +1,30 @@
+// Copyright Epic Games, Inc. All Rights Reserved.
+
+#pragma once
+
+#include <zencore/logging/sink.h>
+
+#include <memory>
+#include <vector>
+
+namespace zen::logging {
+
+class AsyncSink : public Sink
+{
+public:
+ explicit AsyncSink(std::vector<SinkPtr> InSinks);
+ ~AsyncSink() override;
+
+ AsyncSink(const AsyncSink&) = delete;
+ AsyncSink& operator=(const AsyncSink&) = delete;
+
+ void Log(const LogMessage& Msg) override;
+ void Flush() override;
+ void SetFormatter(std::unique_ptr<Formatter> InFormatter) override;
+
+private:
+ struct Impl;
+ std::unique_ptr<Impl> m_Impl;
+};
+
+} // namespace zen::logging
diff --git a/src/zencore/include/zencore/logging/formatter.h b/src/zencore/include/zencore/logging/formatter.h
new file mode 100644
index 000000000..11904d71d
--- /dev/null
+++ b/src/zencore/include/zencore/logging/formatter.h
@@ -0,0 +1,20 @@
+// Copyright Epic Games, Inc. All Rights Reserved.
+
+#pragma once
+
+#include <zencore/logging/logmsg.h>
+#include <zencore/logging/memorybuffer.h>
+
+#include <memory>
+
+namespace zen::logging {
+
+class Formatter
+{
+public:
+ virtual ~Formatter() = default;
+ virtual void Format(const LogMessage& Msg, MemoryBuffer& Dest) = 0;
+ virtual std::unique_ptr<Formatter> Clone() const = 0;
+};
+
+} // namespace zen::logging
diff --git a/src/zencore/include/zencore/logging/helpers.h b/src/zencore/include/zencore/logging/helpers.h
new file mode 100644
index 000000000..ce021e1a5
--- /dev/null
+++ b/src/zencore/include/zencore/logging/helpers.h
@@ -0,0 +1,122 @@
+// Copyright Epic Games, Inc. All Rights Reserved.
+
+#pragma once
+
+#include <zencore/logbase.h>
+#include <zencore/logging/memorybuffer.h>
+
+#include <chrono>
+#include <ctime>
+#include <string_view>
+
+namespace zen::logging::helpers {
+
+inline void
+AppendStringView(std::string_view Sv, MemoryBuffer& Dest)
+{
+ Dest.append(Sv.data(), Sv.data() + Sv.size());
+}
+
+inline void
+AppendInt(int N, MemoryBuffer& Dest)
+{
+ fmt::format_int Formatted(N);
+ Dest.append(Formatted.data(), Formatted.data() + Formatted.size());
+}
+
+inline void
+Pad2(int N, MemoryBuffer& Dest)
+{
+ if (N >= 0 && N < 100)
+ {
+ Dest.push_back(static_cast<char>('0' + N / 10));
+ Dest.push_back(static_cast<char>('0' + N % 10));
+ }
+ else
+ {
+ fmt::format_int Formatted(N);
+ Dest.append(Formatted.data(), Formatted.data() + Formatted.size());
+ }
+}
+
+inline void
+Pad3(uint32_t N, MemoryBuffer& Dest)
+{
+ if (N < 1000)
+ {
+ Dest.push_back(static_cast<char>('0' + N / 100));
+ Dest.push_back(static_cast<char>('0' + (N / 10) % 10));
+ Dest.push_back(static_cast<char>('0' + N % 10));
+ }
+ else
+ {
+ AppendInt(static_cast<int>(N), Dest);
+ }
+}
+
+inline void
+PadUint(size_t N, unsigned int Width, MemoryBuffer& Dest)
+{
+ fmt::format_int Formatted(N);
+ auto StrLen = static_cast<unsigned int>(Formatted.size());
+ if (Width > StrLen)
+ {
+ for (unsigned int Pad = 0; Pad < Width - StrLen; ++Pad)
+ {
+ Dest.push_back('0');
+ }
+ }
+ Dest.append(Formatted.data(), Formatted.data() + Formatted.size());
+}
+
+template<typename ToDuration>
+inline ToDuration
+TimeFraction(std::chrono::system_clock::time_point Tp)
+{
+ using std::chrono::duration_cast;
+ using std::chrono::seconds;
+ auto Duration = Tp.time_since_epoch();
+ auto Secs = duration_cast<seconds>(Duration);
+ return duration_cast<ToDuration>(Duration) - duration_cast<ToDuration>(Secs);
+}
+
+inline std::tm
+SafeLocaltime(std::time_t Time)
+{
+ std::tm Result{};
+#if defined(_WIN32)
+ localtime_s(&Result, &Time);
+#else
+ localtime_r(&Time, &Result);
+#endif
+ return Result;
+}
+
+inline const char*
+ShortFilename(const char* Path)
+{
+ if (Path == nullptr)
+ {
+ return Path;
+ }
+
+ const char* It = Path;
+ const char* LastSep = Path;
+ while (*It)
+ {
+ if (*It == '/' || *It == '\\')
+ {
+ LastSep = It + 1;
+ }
+ ++It;
+ }
+ return LastSep;
+}
+
+inline std::string_view
+LevelToShortString(LogLevel Level)
+{
+ return ToStringView(Level);
+}
+
+} // namespace zen::logging::helpers
diff --git a/src/zencore/include/zencore/logging/logger.h b/src/zencore/include/zencore/logging/logger.h
new file mode 100644
index 000000000..39d1139a5
--- /dev/null
+++ b/src/zencore/include/zencore/logging/logger.h
@@ -0,0 +1,63 @@
+// Copyright Epic Games, Inc. All Rights Reserved.
+
+#pragma once
+
+#include <zencore/logging/sink.h>
+
+#include <atomic>
+#include <memory>
+#include <string_view>
+
+namespace zen::logging {
+
+class ErrorHandler
+{
+public:
+ virtual ~ErrorHandler() = default;
+ virtual void HandleError(const std::string_view& Msg) = 0;
+};
+
+class Logger : public RefCounted
+{
+public:
+ Logger(std::string_view InName, SinkPtr InSink);
+ Logger(std::string_view InName, std::span<const SinkPtr> InSinks);
+ ~Logger();
+
+ Logger(const Logger&) = delete;
+ Logger& operator=(const Logger&) = delete;
+
+ void Log(const LogPoint& Point, fmt::format_args Args);
+
+ bool ShouldLog(LogLevel InLevel) const { return InLevel >= m_Level.load(std::memory_order_relaxed); }
+
+ void SetLevel(LogLevel InLevel) { m_Level.store(InLevel, std::memory_order_relaxed); }
+ LogLevel GetLevel() const { return m_Level.load(std::memory_order_relaxed); }
+
+ void SetFlushLevel(LogLevel InLevel) { m_FlushLevel.store(InLevel, std::memory_order_relaxed); }
+ LogLevel GetFlushLevel() const { return m_FlushLevel.load(std::memory_order_relaxed); }
+
+ std::string_view Name() const;
+
+ void SetSinks(std::vector<SinkPtr> InSinks);
+ void AddSink(SinkPtr InSink);
+
+ void SetFormatter(std::unique_ptr<Formatter> InFormatter);
+
+ void SetErrorHandler(ErrorHandler* Handler);
+
+ void Flush();
+
+ Ref<Logger> Clone(std::string_view NewName) const;
+
+private:
+ void SinkIt(const LogMessage& Msg);
+ void FlushIfNeeded(LogLevel InLevel);
+
+ struct Impl;
+ std::unique_ptr<Impl> m_Impl;
+ std::atomic<LogLevel> m_Level{Info};
+ std::atomic<LogLevel> m_FlushLevel{Off};
+};
+
+} // namespace zen::logging
diff --git a/src/zencore/include/zencore/logging/logmsg.h b/src/zencore/include/zencore/logging/logmsg.h
new file mode 100644
index 000000000..1d8b6b1b7
--- /dev/null
+++ b/src/zencore/include/zencore/logging/logmsg.h
@@ -0,0 +1,66 @@
+// Copyright Epic Games, Inc. All Rights Reserved.
+
+#pragma once
+
+#include <zencore/logbase.h>
+
+#include <chrono>
+#include <string_view>
+
+namespace zen::logging {
+
+using LogClock = std::chrono::system_clock;
+
+struct LogMessage
+{
+ LogMessage() = default;
+
+ LogMessage(const LogPoint& InPoint, std::string_view InLoggerName, std::string_view InPayload)
+ : m_LoggerName(InLoggerName)
+ , m_Level(InPoint.Level)
+ , m_Time(LogClock::now())
+ , m_Source(InPoint.Location)
+ , m_Payload(InPayload)
+ , m_Point(&InPoint)
+ {
+ }
+
+ std::string_view GetPayload() const { return m_Payload; }
+ int GetThreadId() const { return m_ThreadId; }
+ LogClock::time_point GetTime() const { return m_Time; }
+ LogLevel GetLevel() const { return m_Level; }
+ std::string_view GetLoggerName() const { return m_LoggerName; }
+ const SourceLocation& GetSource() const { return m_Source; }
+ const LogPoint& GetLogPoint() const { return *m_Point; }
+
+ void SetThreadId(int InThreadId) { m_ThreadId = InThreadId; }
+ void SetPayload(std::string_view InPayload) { m_Payload = InPayload; }
+ void SetLoggerName(std::string_view InName) { m_LoggerName = InName; }
+ void SetLevel(LogLevel InLevel) { m_Level = InLevel; }
+ void SetTime(LogClock::time_point InTime) { m_Time = InTime; }
+ void SetSource(const SourceLocation& InSource) { m_Source = InSource; }
+
+ mutable size_t ColorRangeStart = 0;
+ mutable size_t ColorRangeEnd = 0;
+
+private:
+ static constexpr LogPoint s_DefaultPoints[LogLevelCount] = {
+ {{}, Trace, {}},
+ {{}, Debug, {}},
+ {{}, Info, {}},
+ {{}, Warn, {}},
+ {{}, Err, {}},
+ {{}, Critical, {}},
+ {{}, Off, {}},
+ };
+
+ std::string_view m_LoggerName;
+ LogLevel m_Level = Off;
+ std::chrono::system_clock::time_point m_Time;
+ SourceLocation m_Source;
+ std::string_view m_Payload;
+ const LogPoint* m_Point = &s_DefaultPoints[Off];
+ int m_ThreadId = 0;
+};
+
+} // namespace zen::logging
diff --git a/src/zencore/include/zencore/logging/memorybuffer.h b/src/zencore/include/zencore/logging/memorybuffer.h
new file mode 100644
index 000000000..cd0ff324f
--- /dev/null
+++ b/src/zencore/include/zencore/logging/memorybuffer.h
@@ -0,0 +1,11 @@
+// Copyright Epic Games, Inc. All Rights Reserved.
+
+#pragma once
+
+#include <fmt/format.h>
+
+namespace zen::logging {
+
+using MemoryBuffer = fmt::basic_memory_buffer<char, 250>;
+
+} // namespace zen::logging
diff --git a/src/zencore/include/zencore/logging/messageonlyformatter.h b/src/zencore/include/zencore/logging/messageonlyformatter.h
new file mode 100644
index 000000000..ce25fe9a6
--- /dev/null
+++ b/src/zencore/include/zencore/logging/messageonlyformatter.h
@@ -0,0 +1,22 @@
+// Copyright Epic Games, Inc. All Rights Reserved.
+
+#pragma once
+
+#include <zencore/logging/formatter.h>
+#include <zencore/logging/helpers.h>
+
+namespace zen::logging {
+
+class MessageOnlyFormatter : public Formatter
+{
+public:
+ void Format(const LogMessage& Msg, MemoryBuffer& Dest) override
+ {
+ helpers::AppendStringView(Msg.GetPayload(), Dest);
+ Dest.push_back('\n');
+ }
+
+ std::unique_ptr<Formatter> Clone() const override { return std::make_unique<MessageOnlyFormatter>(); }
+};
+
+} // namespace zen::logging
diff --git a/src/zencore/include/zencore/logging/msvcsink.h b/src/zencore/include/zencore/logging/msvcsink.h
new file mode 100644
index 000000000..48ea1b915
--- /dev/null
+++ b/src/zencore/include/zencore/logging/msvcsink.h
@@ -0,0 +1,30 @@
+// Copyright Epic Games, Inc. All Rights Reserved.
+
+#pragma once
+
+#include <zencore/logging/sink.h>
+
+#if ZEN_PLATFORM_WINDOWS
+
+# include <mutex>
+
+namespace zen::logging {
+
+class MsvcSink : public Sink
+{
+public:
+ MsvcSink();
+ ~MsvcSink() override = default;
+
+ void Log(const LogMessage& Msg) override;
+ void Flush() override;
+ void SetFormatter(std::unique_ptr<Formatter> InFormatter) override;
+
+private:
+ std::mutex m_Mutex;
+ std::unique_ptr<Formatter> m_Formatter;
+};
+
+} // namespace zen::logging
+
+#endif // ZEN_PLATFORM_WINDOWS
diff --git a/src/zencore/include/zencore/logging/nullsink.h b/src/zencore/include/zencore/logging/nullsink.h
new file mode 100644
index 000000000..7ac5677c6
--- /dev/null
+++ b/src/zencore/include/zencore/logging/nullsink.h
@@ -0,0 +1,17 @@
+// Copyright Epic Games, Inc. All Rights Reserved.
+
+#pragma once
+
+#include <zencore/logging/sink.h>
+
+namespace zen::logging {
+
+class NullSink : public Sink
+{
+public:
+ void Log(const LogMessage& /*Msg*/) override {}
+ void Flush() override {}
+ void SetFormatter(std::unique_ptr<Formatter> /*InFormatter*/) override {}
+};
+
+} // namespace zen::logging
diff --git a/src/zencore/include/zencore/logging/registry.h b/src/zencore/include/zencore/logging/registry.h
new file mode 100644
index 000000000..a4d3692d2
--- /dev/null
+++ b/src/zencore/include/zencore/logging/registry.h
@@ -0,0 +1,70 @@
+// Copyright Epic Games, Inc. All Rights Reserved.
+
+#pragma once
+
+#include <zencore/logging/logger.h>
+
+#include <chrono>
+#include <memory>
+#include <span>
+#include <string>
+#include <type_traits>
+#include <utility>
+
+namespace zen::logging {
+
+class Registry
+{
+public:
+ using LogLevels = std::span<const std::pair<std::string, LogLevel>>;
+
+ static Registry& Instance();
+ void Shutdown();
+
+ void Register(Ref<Logger> InLogger);
+ void Drop(const std::string& Name);
+ Ref<Logger> Get(const std::string& Name);
+
+ void SetDefaultLogger(Ref<Logger> InLogger);
+ Logger* DefaultLoggerRaw();
+ Ref<Logger> DefaultLogger();
+
+ void SetGlobalLevel(LogLevel Level);
+ LogLevel GetGlobalLevel() const;
+ void SetLevels(LogLevels Levels, LogLevel* DefaultLevel);
+
+ void FlushAll();
+ void FlushOn(LogLevel Level);
+ void FlushEvery(std::chrono::seconds Interval);
+
+ // Change formatter on all registered loggers
+ void SetFormatter(std::unique_ptr<Formatter> InFormatter);
+
+ // Apply function to all registered loggers. Note that the function will
+ // be called while the registry mutex is held, so it should be fast and
+ // not attempt to call back into the registry.
+ template<typename Func>
+ void ApplyAll(Func&& F)
+ {
+ ApplyAllImpl([](void* Ctx, Ref<Logger> L) { (*static_cast<std::decay_t<Func>*>(Ctx))(std::move(L)); }, &F);
+ }
+
+ // Set error handler for all loggers in the registry. The handler is called
+ // if any logger encounters an error during logging or flushing.
+ // The caller must ensure the handler outlives the registry.
+ void SetErrorHandler(ErrorHandler* Handler);
+
+private:
+ void ApplyAllImpl(void (*Func)(void*, Ref<Logger>), void* Context);
+
+ Registry();
+ ~Registry();
+
+ Registry(const Registry&) = delete;
+ Registry& operator=(const Registry&) = delete;
+
+ struct Impl;
+ std::unique_ptr<Impl> m_Impl;
+};
+
+} // namespace zen::logging
diff --git a/src/zencore/include/zencore/logging/sink.h b/src/zencore/include/zencore/logging/sink.h
new file mode 100644
index 000000000..172176a4e
--- /dev/null
+++ b/src/zencore/include/zencore/logging/sink.h
@@ -0,0 +1,34 @@
+// Copyright Epic Games, Inc. All Rights Reserved.
+
+#pragma once
+
+#include <zenbase/refcount.h>
+#include <zencore/logging/formatter.h>
+#include <zencore/logging/logmsg.h>
+
+#include <atomic>
+#include <memory>
+
+namespace zen::logging {
+
+class Sink : public RefCounted
+{
+public:
+ virtual ~Sink() = default;
+
+ virtual void Log(const LogMessage& Msg) = 0;
+ virtual void Flush() = 0;
+
+ virtual void SetFormatter(std::unique_ptr<Formatter> InFormatter) = 0;
+
+ bool ShouldLog(LogLevel InLevel) const { return InLevel >= m_Level.load(std::memory_order_relaxed); }
+ void SetLevel(LogLevel InLevel) { m_Level.store(InLevel, std::memory_order_relaxed); }
+ LogLevel GetLevel() const { return m_Level.load(std::memory_order_relaxed); }
+
+protected:
+ std::atomic<LogLevel> m_Level{Trace};
+};
+
+using SinkPtr = Ref<Sink>;
+
+} // namespace zen::logging
diff --git a/src/zencore/include/zencore/logging/tracesink.h b/src/zencore/include/zencore/logging/tracesink.h
new file mode 100644
index 000000000..e63d838b4
--- /dev/null
+++ b/src/zencore/include/zencore/logging/tracesink.h
@@ -0,0 +1,23 @@
+// Copyright Epic Games, Inc. All Rights Reserved.
+
+#pragma once
+
+#include <zencore/logging/sink.h>
+
+namespace zen::logging {
+
+/**
+ * A logging sink that forwards log messages to the trace system.
+ *
+ * Work-in-progress, not fully implemented.
+ */
+
+class TraceSink : public Sink
+{
+public:
+ void Log(const LogMessage& Msg) override;
+ void Flush() override;
+ void SetFormatter(std::unique_ptr<Formatter> InFormatter) override;
+};
+
+} // namespace zen::logging
diff --git a/src/zencore/include/zencore/sentryintegration.h b/src/zencore/include/zencore/sentryintegration.h
index faf1238b7..a4e33d69e 100644
--- a/src/zencore/include/zencore/sentryintegration.h
+++ b/src/zencore/include/zencore/sentryintegration.h
@@ -11,11 +11,9 @@
#if ZEN_USE_SENTRY
-# include <memory>
+# include <zencore/logging/logger.h>
-ZEN_THIRD_PARTY_INCLUDES_START
-# include <spdlog/logger.h>
-ZEN_THIRD_PARTY_INCLUDES_END
+# include <memory>
namespace sentry {
@@ -53,7 +51,7 @@ private:
std::string m_SentryUserName;
std::string m_SentryHostName;
std::string m_SentryId;
- std::shared_ptr<spdlog::logger> m_SentryLogger;
+ Ref<logging::Logger> m_SentryLogger;
};
} // namespace zen
diff --git a/src/zencore/logging.cpp b/src/zencore/logging.cpp
index ebd68de09..099518637 100644
--- a/src/zencore/logging.cpp
+++ b/src/zencore/logging.cpp
@@ -2,208 +2,128 @@
#include "zencore/logging.h"
+#include <zencore/logging/ansicolorsink.h>
+#include <zencore/logging/logger.h>
+#include <zencore/logging/messageonlyformatter.h>
+#include <zencore/logging/nullsink.h>
+#include <zencore/logging/registry.h>
#include <zencore/string.h>
#include <zencore/testing.h>
#include <zencore/thread.h>
#include <zencore/memory/llm.h>
-ZEN_THIRD_PARTY_INCLUDES_START
-#include <spdlog/details/registry.h>
-#include <spdlog/sinks/null_sink.h>
-#include <spdlog/sinks/stdout_color_sinks.h>
-#include <spdlog/spdlog.h>
-ZEN_THIRD_PARTY_INCLUDES_END
+#include <mutex>
#if ZEN_PLATFORM_WINDOWS
# pragma section(".zlog$a", read)
-# pragma section(".zlog$f", read)
-# pragma section(".zlog$m", read)
-# pragma section(".zlog$s", read)
+# pragma section(".zlog$l", read)
# pragma section(".zlog$z", read)
#endif
namespace zen {
-// We shadow the underlying spdlog default logger, in order to avoid a bunch of overhead
LoggerRef TheDefaultLogger;
+LoggerRef
+Log()
+{
+ if (TheDefaultLogger)
+ {
+ return TheDefaultLogger;
+ }
+ return zen::logging::ConsoleLog();
+}
+
} // namespace zen
namespace zen::logging {
-using MemoryBuffer_t = fmt::basic_memory_buffer<char, 250>;
-
-struct LoggingContext
-{
- inline LoggingContext();
- inline ~LoggingContext();
-
- zen::logging::MemoryBuffer_t MessageBuffer;
-
- inline std::string_view Message() const { return std::string_view(MessageBuffer.data(), MessageBuffer.size()); }
-};
+//////////////////////////////////////////////////////////////////////////
-LoggingContext::LoggingContext()
+LoggerRef
+LogCategory::Logger()
{
-}
+ // This should be thread safe since zen::logging::Get() will return
+ // the same logger instance for the same category name. Also the
+ // LoggerRef is simply a pointer.
+ if (!m_LoggerRef)
+ {
+ m_LoggerRef = zen::logging::Get(m_CategoryName);
+ }
-LoggingContext::~LoggingContext()
-{
+ return m_LoggerRef;
}
-//////////////////////////////////////////////////////////////////////////
-
static inline bool
-IsErrorLevel(int LogLevel)
+IsErrorLevel(LogLevel InLevel)
{
- return (LogLevel == zen::logging::level::Err || LogLevel == zen::logging::level::Critical);
+ return (InLevel == Err || InLevel == Critical);
};
-static_assert(sizeof(spdlog::source_loc) == sizeof(SourceLocation));
-static_assert(offsetof(spdlog::source_loc, filename) == offsetof(SourceLocation, filename));
-static_assert(offsetof(spdlog::source_loc, line) == offsetof(SourceLocation, line));
-static_assert(offsetof(spdlog::source_loc, funcname) == offsetof(SourceLocation, funcname));
-
void
-EmitLogMessage(LoggerRef& Logger, int LogLevel, const std::string_view Message)
+EmitLogMessage(LoggerRef& Logger, const LogPoint& Lp, fmt::format_args Args)
{
ZEN_MEMSCOPE(ELLMTag::Logging);
- const spdlog::level::level_enum InLevel = (spdlog::level::level_enum)LogLevel;
- Logger.SpdLogger->log(InLevel, Message);
- if (IsErrorLevel(LogLevel))
- {
- if (LoggerRef ErrLogger = zen::logging::ErrorLog())
- {
- ErrLogger.SpdLogger->log(InLevel, Message);
- }
- }
-}
-void
-EmitLogMessage(LoggerRef& Logger, int LogLevel, std::string_view Format, fmt::format_args Args)
-{
- ZEN_MEMSCOPE(ELLMTag::Logging);
- zen::logging::LoggingContext LogCtx;
- fmt::vformat_to(fmt::appender(LogCtx.MessageBuffer), Format, Args);
- zen::logging::EmitLogMessage(Logger, LogLevel, LogCtx.Message());
-}
+ Logger->Log(Lp, Args);
-void
-EmitLogMessage(LoggerRef& Logger, const SourceLocation& InLocation, int LogLevel, const std::string_view Message)
-{
- ZEN_MEMSCOPE(ELLMTag::Logging);
- const spdlog::source_loc& Location = *reinterpret_cast<const spdlog::source_loc*>(&InLocation);
- const spdlog::level::level_enum InLevel = (spdlog::level::level_enum)LogLevel;
- Logger.SpdLogger->log(Location, InLevel, Message);
- if (IsErrorLevel(LogLevel))
+ if (IsErrorLevel(Lp.Level))
{
if (LoggerRef ErrLogger = zen::logging::ErrorLog())
{
- ErrLogger.SpdLogger->log(Location, InLevel, Message);
+ ErrLogger->Log(Lp, Args);
}
}
}
void
-EmitLogMessage(LoggerRef& Logger, const SourceLocation& InLocation, int LogLevel, std::string_view Format, fmt::format_args Args)
-{
- ZEN_MEMSCOPE(ELLMTag::Logging);
- zen::logging::LoggingContext LogCtx;
- fmt::vformat_to(fmt::appender(LogCtx.MessageBuffer), Format, Args);
- zen::logging::EmitLogMessage(Logger, InLocation, LogLevel, LogCtx.Message());
-}
-
-void
-EmitConsoleLogMessage(int LogLevel, const std::string_view Message)
-{
- ZEN_MEMSCOPE(ELLMTag::Logging);
- const spdlog::level::level_enum InLevel = (spdlog::level::level_enum)LogLevel;
- ConsoleLog().SpdLogger->log(InLevel, Message);
-}
-
-#define ZEN_COLOR_YELLOW "\033[0;33m"
-#define ZEN_COLOR_RED "\033[0;31m"
-#define ZEN_BRIGHT_COLOR_RED "\033[1;31m"
-#define ZEN_COLOR_RESET "\033[0m"
-
-void
-EmitConsoleLogMessage(int LogLevel, std::string_view Format, fmt::format_args Args)
+EmitConsoleLogMessage(const LogPoint& Lp, fmt::format_args Args)
{
ZEN_MEMSCOPE(ELLMTag::Logging);
- zen::logging::LoggingContext LogCtx;
-
- // We are not using a format option for console which include log level since it would interfere with normal console output
-
- const spdlog::level::level_enum InLevel = (spdlog::level::level_enum)LogLevel;
- switch (InLevel)
- {
- case spdlog::level::level_enum::warn:
- fmt::format_to(fmt::appender(LogCtx.MessageBuffer), ZEN_COLOR_YELLOW "Warning: " ZEN_COLOR_RESET);
- break;
- case spdlog::level::level_enum::err:
- fmt::format_to(fmt::appender(LogCtx.MessageBuffer), ZEN_BRIGHT_COLOR_RED "Error: " ZEN_COLOR_RESET);
- break;
- case spdlog::level::level_enum::critical:
- fmt::format_to(fmt::appender(LogCtx.MessageBuffer), ZEN_COLOR_RED "Critical: " ZEN_COLOR_RESET);
- break;
- default:
- break;
- }
- fmt::vformat_to(fmt::appender(LogCtx.MessageBuffer), Format, Args);
- zen::logging::EmitConsoleLogMessage(LogLevel, LogCtx.Message());
+ ConsoleLog()->Log(Lp, Args);
}
} // namespace zen::logging
-namespace zen::logging::level {
+namespace zen::logging {
-spdlog::level::level_enum
-to_spdlog_level(LogLevel NewLogLevel)
-{
- return static_cast<spdlog::level::level_enum>((int)NewLogLevel);
-}
+constinit std::string_view LevelNames[] = {std::string_view("trace", 5),
+ std::string_view("debug", 5),
+ std::string_view("info", 4),
+ std::string_view("warning", 7),
+ std::string_view("error", 5),
+ std::string_view("critical", 8),
+ std::string_view("off", 3)};
LogLevel
-to_logging_level(spdlog::level::level_enum NewLogLevel)
-{
- return static_cast<LogLevel>((int)NewLogLevel);
-}
-
-constinit std::string_view LevelNames[] = {ZEN_LEVEL_NAME_TRACE,
- ZEN_LEVEL_NAME_DEBUG,
- ZEN_LEVEL_NAME_INFO,
- ZEN_LEVEL_NAME_WARNING,
- ZEN_LEVEL_NAME_ERROR,
- ZEN_LEVEL_NAME_CRITICAL,
- ZEN_LEVEL_NAME_OFF};
-
-level::LogLevel
ParseLogLevelString(std::string_view Name)
{
- for (int Level = 0; Level < level::LogLevelCount; ++Level)
+ for (int Level = 0; Level < LogLevelCount; ++Level)
{
if (LevelNames[Level] == Name)
- return static_cast<level::LogLevel>(Level);
+ {
+ return static_cast<LogLevel>(Level);
+ }
}
if (Name == "warn")
{
- return level::Warn;
+ return Warn;
}
if (Name == "err")
{
- return level::Err;
+ return Err;
}
- return level::Off;
+ return Off;
}
std::string_view
-ToStringView(level::LogLevel Level)
+ToStringView(LogLevel Level)
{
- if (int(Level) < level::LogLevelCount)
+ if (int(Level) < LogLevelCount)
{
return LevelNames[int(Level)];
}
@@ -211,17 +131,17 @@ ToStringView(level::LogLevel Level)
return "None";
}
-} // namespace zen::logging::level
+} // namespace zen::logging
//////////////////////////////////////////////////////////////////////////
namespace zen::logging {
RwLock LogLevelsLock;
-std::string LogLevels[level::LogLevelCount];
+std::string LogLevels[LogLevelCount];
void
-ConfigureLogLevels(level::LogLevel Level, std::string_view Loggers)
+ConfigureLogLevels(LogLevel Level, std::string_view Loggers)
{
ZEN_MEMSCOPE(ELLMTag::Logging);
@@ -230,18 +150,18 @@ ConfigureLogLevels(level::LogLevel Level, std::string_view Loggers)
}
void
-RefreshLogLevels(level::LogLevel* DefaultLevel)
+RefreshLogLevels(LogLevel* DefaultLevel)
{
ZEN_MEMSCOPE(ELLMTag::Logging);
- spdlog::details::registry::log_levels Levels;
+ std::vector<std::pair<std::string, LogLevel>> Levels;
{
RwLock::SharedLockScope _(LogLevelsLock);
- for (int i = 0; i < level::LogLevelCount; ++i)
+ for (int i = 0; i < LogLevelCount; ++i)
{
- level::LogLevel CurrentLevel{i};
+ LogLevel CurrentLevel{i};
std::string_view Spec = LogLevels[i];
@@ -260,24 +180,16 @@ RefreshLogLevels(level::LogLevel* DefaultLevel)
Spec = {};
}
- Levels[LoggerName] = to_spdlog_level(CurrentLevel);
+ Levels.emplace_back(std::move(LoggerName), CurrentLevel);
}
}
}
- if (DefaultLevel)
- {
- spdlog::level::level_enum SpdDefaultLevel = to_spdlog_level(*DefaultLevel);
- spdlog::details::registry::instance().set_levels(Levels, &SpdDefaultLevel);
- }
- else
- {
- spdlog::details::registry::instance().set_levels(Levels, nullptr);
- }
+ Registry::Instance().SetLevels(Levels, DefaultLevel);
}
void
-RefreshLogLevels(level::LogLevel DefaultLevel)
+RefreshLogLevels(LogLevel DefaultLevel)
{
RefreshLogLevels(&DefaultLevel);
}
@@ -289,15 +201,15 @@ RefreshLogLevels()
}
void
-SetLogLevel(level::LogLevel NewLogLevel)
+SetLogLevel(LogLevel NewLogLevel)
{
- spdlog::set_level(to_spdlog_level(NewLogLevel));
+ Registry::Instance().SetGlobalLevel(NewLogLevel);
}
-level::LogLevel
+LogLevel
GetLogLevel()
{
- return level::to_logging_level(spdlog::get_level());
+ return Registry::Instance().GetGlobalLevel();
}
LoggerRef
@@ -312,10 +224,10 @@ SetDefault(std::string_view NewDefaultLoggerId)
{
ZEN_MEMSCOPE(ELLMTag::Logging);
- auto NewDefaultLogger = spdlog::get(std::string(NewDefaultLoggerId));
+ Ref<Logger> NewDefaultLogger = Registry::Instance().Get(std::string(NewDefaultLoggerId));
ZEN_ASSERT(NewDefaultLogger);
- spdlog::set_default_logger(NewDefaultLogger);
+ Registry::Instance().SetDefaultLogger(NewDefaultLogger);
TheDefaultLogger = LoggerRef(*NewDefaultLogger);
}
@@ -338,11 +250,11 @@ SetErrorLog(std::string_view NewErrorLoggerId)
}
else
{
- auto NewErrorLogger = spdlog::get(std::string(NewErrorLoggerId));
+ Ref<Logger> NewErrorLogger = Registry::Instance().Get(std::string(NewErrorLoggerId));
ZEN_ASSERT(NewErrorLogger);
- TheErrorLogger = LoggerRef(*NewErrorLogger.get());
+ TheErrorLogger = LoggerRef(*NewErrorLogger.Get());
}
}
@@ -353,39 +265,75 @@ Get(std::string_view Name)
{
ZEN_MEMSCOPE(ELLMTag::Logging);
- std::shared_ptr<spdlog::logger> Logger = spdlog::get(std::string(Name));
+ Ref<Logger> FoundLogger = Registry::Instance().Get(std::string(Name));
- if (!Logger)
+ if (!FoundLogger)
{
g_LoggerMutex.WithExclusiveLock([&] {
- Logger = spdlog::get(std::string(Name));
+ FoundLogger = Registry::Instance().Get(std::string(Name));
- if (!Logger)
+ if (!FoundLogger)
{
- Logger = Default().SpdLogger->clone(std::string(Name));
- spdlog::apply_logger_env_levels(Logger);
- spdlog::register_logger(Logger);
+ FoundLogger = Default()->Clone(std::string(Name));
+ Registry::Instance().Register(FoundLogger);
}
});
}
- return *Logger;
+ return *FoundLogger;
}
-std::once_flag ConsoleInitFlag;
-std::shared_ptr<spdlog::logger> ConLogger;
+std::once_flag ConsoleInitFlag;
+Ref<Logger> ConLogger;
void
SuppressConsoleLog()
{
+ ZEN_MEMSCOPE(ELLMTag::Logging);
+
if (ConLogger)
{
- spdlog::drop("console");
+ Registry::Instance().Drop("console");
ConLogger = {};
}
- ConLogger = spdlog::null_logger_mt("console");
+
+ SinkPtr NullSinkPtr(new NullSink());
+ ConLogger = Ref<Logger>(new Logger("console", std::vector<SinkPtr>{NullSinkPtr}));
+ Registry::Instance().Register(ConLogger);
}
+#define ZEN_COLOR_YELLOW "\033[0;33m"
+#define ZEN_COLOR_RED "\033[0;31m"
+#define ZEN_BRIGHT_COLOR_RED "\033[1;31m"
+#define ZEN_COLOR_RESET "\033[0m"
+
+class ConsoleFormatter : public Formatter
+{
+public:
+ void Format(const LogMessage& Msg, MemoryBuffer& Dest) override
+ {
+ switch (Msg.GetLevel())
+ {
+ case Warn:
+ fmt::format_to(fmt::appender(Dest), ZEN_COLOR_YELLOW "Warning: " ZEN_COLOR_RESET);
+ break;
+ case Err:
+ fmt::format_to(fmt::appender(Dest), ZEN_BRIGHT_COLOR_RED "Error: " ZEN_COLOR_RESET);
+ break;
+ case Critical:
+ fmt::format_to(fmt::appender(Dest), ZEN_COLOR_RED "Critical: " ZEN_COLOR_RESET);
+ break;
+ default:
+ break;
+ }
+
+ helpers::AppendStringView(Msg.GetPayload(), Dest);
+ Dest.push_back('\n');
+ }
+
+ std::unique_ptr<Formatter> Clone() const override { return std::make_unique<ConsoleFormatter>(); }
+};
+
LoggerRef
ConsoleLog()
{
@@ -394,10 +342,10 @@ ConsoleLog()
std::call_once(ConsoleInitFlag, [&] {
if (!ConLogger)
{
- ConLogger = spdlog::stdout_color_mt("console");
- spdlog::apply_logger_env_levels(ConLogger);
-
- ConLogger->set_pattern("%v");
+ SinkPtr ConsoleSink(new AnsiColorStdoutSink());
+ ConsoleSink->SetFormatter(std::make_unique<ConsoleFormatter>());
+ ConLogger = Ref<Logger>(new Logger("console", std::vector<SinkPtr>{ConsoleSink}));
+ Registry::Instance().Register(ConLogger);
}
});
@@ -407,9 +355,11 @@ ConsoleLog()
void
ResetConsoleLog()
{
+ ZEN_MEMSCOPE(ELLMTag::Logging);
+
LoggerRef ConLog = ConsoleLog();
- ConLog.SpdLogger->set_pattern("%v");
+ ConLog->SetFormatter(std::make_unique<ConsoleFormatter>());
}
void
@@ -417,13 +367,15 @@ InitializeLogging()
{
ZEN_MEMSCOPE(ELLMTag::Logging);
- TheDefaultLogger = *spdlog::default_logger_raw();
+ TheDefaultLogger = *Registry::Instance().DefaultLoggerRaw();
}
void
ShutdownLogging()
{
- spdlog::shutdown();
+ ZEN_MEMSCOPE(ELLMTag::Logging);
+
+ Registry::Instance().Shutdown();
TheDefaultLogger = {};
}
@@ -457,7 +409,7 @@ EnableVTMode()
void
FlushLogging()
{
- spdlog::details::registry::instance().flush_all();
+ Registry::Instance().FlushAll();
}
} // namespace zen::logging
@@ -465,21 +417,27 @@ FlushLogging()
namespace zen {
bool
-LoggerRef::ShouldLog(int Level) const
+LoggerRef::ShouldLog(logging::LogLevel Level) const
{
- return SpdLogger->should_log(static_cast<spdlog::level::level_enum>(Level));
+ return m_Logger->ShouldLog(Level);
}
void
-LoggerRef::SetLogLevel(logging::level::LogLevel NewLogLevel)
+LoggerRef::SetLogLevel(logging::LogLevel NewLogLevel)
{
- SpdLogger->set_level(to_spdlog_level(NewLogLevel));
+ m_Logger->SetLevel(NewLogLevel);
}
-logging::level::LogLevel
+logging::LogLevel
LoggerRef::GetLogLevel()
{
- return logging::level::to_logging_level(SpdLogger->level());
+ return m_Logger->GetLevel();
+}
+
+void
+LoggerRef::Flush()
+{
+ m_Logger->Flush();
}
thread_local ScopedActivityBase* t_ScopeStack = nullptr;
diff --git a/src/zencore/logging/ansicolorsink.cpp b/src/zencore/logging/ansicolorsink.cpp
new file mode 100644
index 000000000..9b9959862
--- /dev/null
+++ b/src/zencore/logging/ansicolorsink.cpp
@@ -0,0 +1,178 @@
+// Copyright Epic Games, Inc. All Rights Reserved.
+
+#include <zencore/logging/ansicolorsink.h>
+#include <zencore/logging/helpers.h>
+#include <zencore/logging/messageonlyformatter.h>
+
+#include <cstdio>
+#include <mutex>
+
+namespace zen::logging {
+
+// Default formatter replicating spdlog's %+ pattern:
+// [YYYY-MM-DD HH:MM:SS.mmm] [logger_name] [level] message\n
+class DefaultConsoleFormatter : public Formatter
+{
+public:
+ void Format(const LogMessage& Msg, MemoryBuffer& Dest) override
+ {
+ // timestamp
+ auto Secs = std::chrono::duration_cast<std::chrono::seconds>(Msg.GetTime().time_since_epoch());
+ if (Secs != m_LastLogSecs)
+ {
+ m_LastLogSecs = Secs;
+ m_CachedLocalTm = helpers::SafeLocaltime(LogClock::to_time_t(Msg.GetTime()));
+ }
+
+ Dest.push_back('[');
+ helpers::AppendInt(m_CachedLocalTm.tm_year + 1900, Dest);
+ Dest.push_back('-');
+ helpers::Pad2(m_CachedLocalTm.tm_mon + 1, Dest);
+ Dest.push_back('-');
+ helpers::Pad2(m_CachedLocalTm.tm_mday, Dest);
+ Dest.push_back(' ');
+ helpers::Pad2(m_CachedLocalTm.tm_hour, Dest);
+ Dest.push_back(':');
+ helpers::Pad2(m_CachedLocalTm.tm_min, Dest);
+ Dest.push_back(':');
+ helpers::Pad2(m_CachedLocalTm.tm_sec, Dest);
+ Dest.push_back('.');
+ auto Millis = helpers::TimeFraction<std::chrono::milliseconds>(Msg.GetTime());
+ helpers::Pad3(static_cast<uint32_t>(Millis.count()), Dest);
+ Dest.push_back(']');
+ Dest.push_back(' ');
+
+ // logger name
+ if (Msg.GetLoggerName().size() > 0)
+ {
+ Dest.push_back('[');
+ helpers::AppendStringView(Msg.GetLoggerName(), Dest);
+ Dest.push_back(']');
+ Dest.push_back(' ');
+ }
+
+ // level (colored range)
+ Dest.push_back('[');
+ Msg.ColorRangeStart = Dest.size();
+ helpers::AppendStringView(helpers::LevelToShortString(Msg.GetLevel()), Dest);
+ Msg.ColorRangeEnd = Dest.size();
+ Dest.push_back(']');
+ Dest.push_back(' ');
+
+ // message
+ helpers::AppendStringView(Msg.GetPayload(), Dest);
+ Dest.push_back('\n');
+ }
+
+ std::unique_ptr<Formatter> Clone() const override { return std::make_unique<DefaultConsoleFormatter>(); }
+
+private:
+ std::chrono::seconds m_LastLogSecs{0};
+ std::tm m_CachedLocalTm{};
+};
+
+static constexpr std::string_view s_Reset = "\033[m";
+
+static std::string_view
+GetColorForLevel(LogLevel InLevel)
+{
+ using namespace std::string_view_literals;
+ switch (InLevel)
+ {
+ case Trace:
+ return "\033[37m"sv; // white
+ case Debug:
+ return "\033[36m"sv; // cyan
+ case Info:
+ return "\033[32m"sv; // green
+ case Warn:
+ return "\033[33m\033[1m"sv; // bold yellow
+ case Err:
+ return "\033[31m\033[1m"sv; // bold red
+ case Critical:
+ return "\033[1m\033[41m"sv; // bold on red background
+ default:
+ return s_Reset;
+ }
+}
+
+struct AnsiColorStdoutSink::Impl
+{
+ Impl() : m_Formatter(std::make_unique<DefaultConsoleFormatter>()) {}
+
+ void Log(const LogMessage& Msg)
+ {
+ std::lock_guard<std::mutex> Lock(m_Mutex);
+
+ MemoryBuffer Formatted;
+ m_Formatter->Format(Msg, Formatted);
+
+ if (Msg.ColorRangeEnd > Msg.ColorRangeStart)
+ {
+ // Print pre-color range
+ fwrite(Formatted.data(), 1, Msg.ColorRangeStart, m_File);
+
+ // Print color
+ std::string_view Color = GetColorForLevel(Msg.GetLevel());
+ fwrite(Color.data(), 1, Color.size(), m_File);
+
+ // Print colored range
+ fwrite(Formatted.data() + Msg.ColorRangeStart, 1, Msg.ColorRangeEnd - Msg.ColorRangeStart, m_File);
+
+ // Reset color
+ fwrite(s_Reset.data(), 1, s_Reset.size(), m_File);
+
+ // Print remainder
+ fwrite(Formatted.data() + Msg.ColorRangeEnd, 1, Formatted.size() - Msg.ColorRangeEnd, m_File);
+ }
+ else
+ {
+ fwrite(Formatted.data(), 1, Formatted.size(), m_File);
+ }
+
+ fflush(m_File);
+ }
+
+ void Flush()
+ {
+ std::lock_guard<std::mutex> Lock(m_Mutex);
+ fflush(m_File);
+ }
+
+ void SetFormatter(std::unique_ptr<Formatter> InFormatter)
+ {
+ std::lock_guard<std::mutex> Lock(m_Mutex);
+ m_Formatter = std::move(InFormatter);
+ }
+
+private:
+ std::mutex m_Mutex;
+ std::unique_ptr<Formatter> m_Formatter;
+ FILE* m_File = stdout;
+};
+
+AnsiColorStdoutSink::AnsiColorStdoutSink() : m_Impl(std::make_unique<Impl>())
+{
+}
+
+AnsiColorStdoutSink::~AnsiColorStdoutSink() = default;
+
+void
+AnsiColorStdoutSink::Log(const LogMessage& Msg)
+{
+ m_Impl->Log(Msg);
+}
+
+void
+AnsiColorStdoutSink::Flush()
+{
+ m_Impl->Flush();
+}
+
+void
+AnsiColorStdoutSink::SetFormatter(std::unique_ptr<Formatter> InFormatter)
+{
+ m_Impl->SetFormatter(std::move(InFormatter));
+}
+
+} // namespace zen::logging
diff --git a/src/zencore/logging/asyncsink.cpp b/src/zencore/logging/asyncsink.cpp
new file mode 100644
index 000000000..02bf9f3ba
--- /dev/null
+++ b/src/zencore/logging/asyncsink.cpp
@@ -0,0 +1,212 @@
+// Copyright Epic Games, Inc. All Rights Reserved.
+
+#include <zencore/logging/asyncsink.h>
+
+#include <zencore/blockingqueue.h>
+#include <zencore/logging/logmsg.h>
+#include <zencore/thread.h>
+
+#include <future>
+#include <string>
+#include <thread>
+
+namespace zen::logging {
+
+struct AsyncLogMessage
+{
+ enum class Type : uint8_t
+ {
+ Log,
+ Flush,
+ Shutdown
+ };
+
+ Type MsgType = Type::Log;
+
+ // Points to the LogPoint from upstream logging code. LogMessage guarantees
+ // this is always valid (either a static LogPoint from ZEN_LOG macros or one
+ // of the per-level default LogPoints).
+ const LogPoint* Point = nullptr;
+
+ int ThreadId = 0;
+ std::string OwnedPayload;
+ std::string OwnedLoggerName;
+ std::chrono::system_clock::time_point Time;
+
+ std::shared_ptr<std::promise<void>> FlushPromise;
+};
+
+struct AsyncSink::Impl
+{
+ explicit Impl(std::vector<SinkPtr> InSinks) : m_Sinks(std::move(InSinks))
+ {
+ m_WorkerThread = std::thread([this]() {
+ zen::SetCurrentThreadName("AsyncLog");
+ WorkerLoop();
+ });
+ }
+
+ ~Impl()
+ {
+ AsyncLogMessage ShutdownMsg;
+ ShutdownMsg.MsgType = AsyncLogMessage::Type::Shutdown;
+ m_Queue.Enqueue(std::move(ShutdownMsg));
+
+ if (m_WorkerThread.joinable())
+ {
+ m_WorkerThread.join();
+ }
+ }
+
+ void Log(const LogMessage& Msg)
+ {
+ AsyncLogMessage AsyncMsg;
+ AsyncMsg.OwnedPayload = std::string(Msg.GetPayload());
+ AsyncMsg.OwnedLoggerName = std::string(Msg.GetLoggerName());
+ AsyncMsg.ThreadId = Msg.GetThreadId();
+ AsyncMsg.Time = Msg.GetTime();
+ AsyncMsg.Point = &Msg.GetLogPoint();
+ AsyncMsg.MsgType = AsyncLogMessage::Type::Log;
+
+ m_Queue.Enqueue(std::move(AsyncMsg));
+ }
+
+ void Flush()
+ {
+ auto Promise = std::make_shared<std::promise<void>>();
+ auto Future = Promise->get_future();
+
+ AsyncLogMessage FlushMsg;
+ FlushMsg.MsgType = AsyncLogMessage::Type::Flush;
+ FlushMsg.FlushPromise = std::move(Promise);
+
+ m_Queue.Enqueue(std::move(FlushMsg));
+
+ Future.get();
+ }
+
+ void SetFormatter(std::unique_ptr<Formatter> InFormatter)
+ {
+ for (auto& CurrentSink : m_Sinks)
+ {
+ CurrentSink->SetFormatter(InFormatter->Clone());
+ }
+ }
+
+private:
+ void ForwardLogToSinks(const AsyncLogMessage& AsyncMsg)
+ {
+ LogMessage Reconstructed(*AsyncMsg.Point, AsyncMsg.OwnedLoggerName, AsyncMsg.OwnedPayload);
+ Reconstructed.SetTime(AsyncMsg.Time);
+ Reconstructed.SetThreadId(AsyncMsg.ThreadId);
+
+ for (auto& CurrentSink : m_Sinks)
+ {
+ if (CurrentSink->ShouldLog(Reconstructed.GetLevel()))
+ {
+ try
+ {
+ CurrentSink->Log(Reconstructed);
+ }
+ catch (const std::exception&)
+ {
+ }
+ }
+ }
+ }
+
+ void FlushSinks()
+ {
+ for (auto& CurrentSink : m_Sinks)
+ {
+ try
+ {
+ CurrentSink->Flush();
+ }
+ catch (const std::exception&)
+ {
+ }
+ }
+ }
+
+ void WorkerLoop()
+ {
+ AsyncLogMessage Msg;
+ while (m_Queue.WaitAndDequeue(Msg))
+ {
+ switch (Msg.MsgType)
+ {
+ case AsyncLogMessage::Type::Log:
+ {
+ ForwardLogToSinks(Msg);
+ break;
+ }
+
+ case AsyncLogMessage::Type::Flush:
+ {
+ FlushSinks();
+ if (Msg.FlushPromise)
+ {
+ Msg.FlushPromise->set_value();
+ }
+ break;
+ }
+
+ case AsyncLogMessage::Type::Shutdown:
+ {
+ m_Queue.CompleteAdding();
+
+ AsyncLogMessage Remaining;
+ while (m_Queue.WaitAndDequeue(Remaining))
+ {
+ if (Remaining.MsgType == AsyncLogMessage::Type::Log)
+ {
+ ForwardLogToSinks(Remaining);
+ }
+ else if (Remaining.MsgType == AsyncLogMessage::Type::Flush)
+ {
+ FlushSinks();
+ if (Remaining.FlushPromise)
+ {
+ Remaining.FlushPromise->set_value();
+ }
+ }
+ }
+
+ FlushSinks();
+ return;
+ }
+ }
+ }
+ }
+
+ std::vector<SinkPtr> m_Sinks;
+ BlockingQueue<AsyncLogMessage> m_Queue;
+ std::thread m_WorkerThread;
+};
+
+AsyncSink::AsyncSink(std::vector<SinkPtr> InSinks) : m_Impl(std::make_unique<Impl>(std::move(InSinks)))
+{
+}
+
+AsyncSink::~AsyncSink() = default;
+
+void
+AsyncSink::Log(const LogMessage& Msg)
+{
+ m_Impl->Log(Msg);
+}
+
+void
+AsyncSink::Flush()
+{
+ m_Impl->Flush();
+}
+
+void
+AsyncSink::SetFormatter(std::unique_ptr<Formatter> InFormatter)
+{
+ m_Impl->SetFormatter(std::move(InFormatter));
+}
+
+} // namespace zen::logging
diff --git a/src/zencore/logging/logger.cpp b/src/zencore/logging/logger.cpp
new file mode 100644
index 000000000..dd1675bb1
--- /dev/null
+++ b/src/zencore/logging/logger.cpp
@@ -0,0 +1,142 @@
+// Copyright Epic Games, Inc. All Rights Reserved.
+
+#include <zencore/logging/logger.h>
+#include <zencore/thread.h>
+
+#include <string>
+#include <vector>
+
+namespace zen::logging {
+
+struct Logger::Impl
+{
+ std::string m_Name;
+ std::vector<SinkPtr> m_Sinks;
+ ErrorHandler* m_ErrorHandler = nullptr;
+};
+
+Logger::Logger(std::string_view InName, SinkPtr InSink) : m_Impl(std::make_unique<Impl>())
+{
+ m_Impl->m_Name = InName;
+ m_Impl->m_Sinks.push_back(std::move(InSink));
+}
+
+Logger::Logger(std::string_view InName, std::span<const SinkPtr> InSinks) : m_Impl(std::make_unique<Impl>())
+{
+ m_Impl->m_Name = InName;
+ m_Impl->m_Sinks.assign(InSinks.begin(), InSinks.end());
+}
+
+Logger::~Logger() = default;
+
+void
+Logger::Log(const LogPoint& Point, fmt::format_args Args)
+{
+ if (!ShouldLog(Point.Level))
+ {
+ return;
+ }
+
+ fmt::basic_memory_buffer<char, 250> Buffer;
+ fmt::vformat_to(fmt::appender(Buffer), Point.FormatString, Args);
+
+ LogMessage LogMsg(Point, m_Impl->m_Name, std::string_view(Buffer.data(), Buffer.size()));
+ LogMsg.SetThreadId(GetCurrentThreadId());
+ SinkIt(LogMsg);
+ FlushIfNeeded(Point.Level);
+}
+
+void
+Logger::SinkIt(const LogMessage& Msg)
+{
+ for (auto& CurrentSink : m_Impl->m_Sinks)
+ {
+ if (CurrentSink->ShouldLog(Msg.GetLevel()))
+ {
+ try
+ {
+ CurrentSink->Log(Msg);
+ }
+ catch (const std::exception& Ex)
+ {
+ if (m_Impl->m_ErrorHandler)
+ {
+ m_Impl->m_ErrorHandler->HandleError(Ex.what());
+ }
+ }
+ }
+ }
+}
+
+void
+Logger::FlushIfNeeded(LogLevel InLevel)
+{
+ if (InLevel >= m_FlushLevel.load(std::memory_order_relaxed))
+ {
+ Flush();
+ }
+}
+
+void
+Logger::Flush()
+{
+ for (auto& CurrentSink : m_Impl->m_Sinks)
+ {
+ try
+ {
+ CurrentSink->Flush();
+ }
+ catch (const std::exception& Ex)
+ {
+ if (m_Impl->m_ErrorHandler)
+ {
+ m_Impl->m_ErrorHandler->HandleError(Ex.what());
+ }
+ }
+ }
+}
+
+void
+Logger::SetSinks(std::vector<SinkPtr> InSinks)
+{
+ m_Impl->m_Sinks = std::move(InSinks);
+}
+
+void
+Logger::AddSink(SinkPtr InSink)
+{
+ m_Impl->m_Sinks.push_back(std::move(InSink));
+}
+
+void
+Logger::SetErrorHandler(ErrorHandler* Handler)
+{
+ m_Impl->m_ErrorHandler = Handler;
+}
+
+void
+Logger::SetFormatter(std::unique_ptr<Formatter> InFormatter)
+{
+ for (auto& CurrentSink : m_Impl->m_Sinks)
+ {
+ CurrentSink->SetFormatter(InFormatter->Clone());
+ }
+}
+
+std::string_view
+Logger::Name() const
+{
+ return m_Impl->m_Name;
+}
+
+Ref<Logger>
+Logger::Clone(std::string_view NewName) const
+{
+ Ref<Logger> Cloned(new Logger(NewName, m_Impl->m_Sinks));
+ Cloned->SetLevel(m_Level.load(std::memory_order_relaxed));
+ Cloned->SetFlushLevel(m_FlushLevel.load(std::memory_order_relaxed));
+ Cloned->SetErrorHandler(m_Impl->m_ErrorHandler);
+ return Cloned;
+}
+
+} // namespace zen::logging
diff --git a/src/zencore/logging/msvcsink.cpp b/src/zencore/logging/msvcsink.cpp
new file mode 100644
index 000000000..457a4d6e1
--- /dev/null
+++ b/src/zencore/logging/msvcsink.cpp
@@ -0,0 +1,80 @@
+// Copyright Epic Games, Inc. All Rights Reserved.
+
+#include <zencore/zencore.h>
+
+#if ZEN_PLATFORM_WINDOWS
+
+# include <zencore/logging/helpers.h>
+# include <zencore/logging/messageonlyformatter.h>
+# include <zencore/logging/msvcsink.h>
+
+ZEN_THIRD_PARTY_INCLUDES_START
+# include <Windows.h>
+ZEN_THIRD_PARTY_INCLUDES_END
+
+namespace zen::logging {
+
+// Default formatter for MSVC debug output: [level] message\n
+// For error/critical messages with source info, prepends file(line): so that
+// the message is clickable in the Visual Studio Output window.
+class DefaultMsvcFormatter : public Formatter
+{
+public:
+ void Format(const LogMessage& Msg, MemoryBuffer& Dest) override
+ {
+ const auto& Source = Msg.GetSource();
+ if (Msg.GetLevel() >= LogLevel::Err && Source)
+ {
+ helpers::AppendStringView(Source.Filename, Dest);
+ Dest.push_back('(');
+ helpers::AppendInt(Source.Line, Dest);
+ Dest.push_back(')');
+ Dest.push_back(':');
+ Dest.push_back(' ');
+ }
+
+ Dest.push_back('[');
+ helpers::AppendStringView(helpers::LevelToShortString(Msg.GetLevel()), Dest);
+ Dest.push_back(']');
+ Dest.push_back(' ');
+ helpers::AppendStringView(Msg.GetPayload(), Dest);
+ Dest.push_back('\n');
+ }
+
+ std::unique_ptr<Formatter> Clone() const override { return std::make_unique<DefaultMsvcFormatter>(); }
+};
+
+MsvcSink::MsvcSink() : m_Formatter(std::make_unique<DefaultMsvcFormatter>())
+{
+}
+
+void
+MsvcSink::Log(const LogMessage& Msg)
+{
+ std::lock_guard<std::mutex> Lock(m_Mutex);
+
+ MemoryBuffer Formatted;
+ m_Formatter->Format(Msg, Formatted);
+
+ // Null-terminate for OutputDebugStringA
+ Formatted.push_back('\0');
+
+ OutputDebugStringA(Formatted.data());
+}
+
+void
+MsvcSink::Flush()
+{
+ // Nothing to flush for OutputDebugString
+}
+
+void
+MsvcSink::SetFormatter(std::unique_ptr<Formatter> InFormatter)
+{
+ std::lock_guard<std::mutex> Lock(m_Mutex);
+ m_Formatter = std::move(InFormatter);
+}
+
+} // namespace zen::logging
+
+#endif // ZEN_PLATFORM_WINDOWS
diff --git a/src/zencore/logging/registry.cpp b/src/zencore/logging/registry.cpp
new file mode 100644
index 000000000..3ed1fb0df
--- /dev/null
+++ b/src/zencore/logging/registry.cpp
@@ -0,0 +1,330 @@
+// Copyright Epic Games, Inc. All Rights Reserved.
+
+#include <zencore/logging/registry.h>
+
+#include <zencore/logging/ansicolorsink.h>
+#include <zencore/logging/messageonlyformatter.h>
+
+#include <atomic>
+#include <condition_variable>
+#include <mutex>
+#include <thread>
+#include <unordered_map>
+
+namespace zen::logging {
+
+struct Registry::Impl
+{
+ Impl()
+ {
+ // Create default logger with a stdout color sink
+ SinkPtr DefaultSink(new AnsiColorStdoutSink());
+ m_DefaultLogger = Ref<Logger>(new Logger("", DefaultSink));
+ m_Loggers[""] = m_DefaultLogger;
+ }
+
+ ~Impl() { StopPeriodicFlush(); }
+
+ void Register(Ref<Logger> InLogger)
+ {
+ std::lock_guard<std::mutex> Lock(m_Mutex);
+ if (m_ErrorHandler)
+ {
+ InLogger->SetErrorHandler(m_ErrorHandler);
+ }
+ m_Loggers[std::string(InLogger->Name())] = std::move(InLogger);
+ }
+
+ void Drop(const std::string& Name)
+ {
+ std::lock_guard<std::mutex> Lock(m_Mutex);
+ m_Loggers.erase(Name);
+ }
+
+ Ref<Logger> Get(const std::string& Name)
+ {
+ std::lock_guard<std::mutex> Lock(m_Mutex);
+ auto It = m_Loggers.find(Name);
+ if (It != m_Loggers.end())
+ {
+ return It->second;
+ }
+ return {};
+ }
+
+ void SetDefaultLogger(Ref<Logger> InLogger)
+ {
+ std::lock_guard<std::mutex> Lock(m_Mutex);
+ if (InLogger)
+ {
+ m_Loggers[std::string(InLogger->Name())] = InLogger;
+ }
+ m_DefaultLogger = std::move(InLogger);
+ }
+
+ Logger* DefaultLoggerRaw() { return m_DefaultLogger.Get(); }
+
+ Ref<Logger> DefaultLogger()
+ {
+ std::lock_guard<std::mutex> Lock(m_Mutex);
+ return m_DefaultLogger;
+ }
+
+ void SetGlobalLevel(LogLevel Level)
+ {
+ m_GlobalLevel.store(Level, std::memory_order_relaxed);
+ std::lock_guard<std::mutex> Lock(m_Mutex);
+ for (auto& [Name, CurLogger] : m_Loggers)
+ {
+ CurLogger->SetLevel(Level);
+ }
+ }
+
+ LogLevel GetGlobalLevel() const { return m_GlobalLevel.load(std::memory_order_relaxed); }
+
+ void SetLevels(Registry::LogLevels Levels, LogLevel* DefaultLevel)
+ {
+ std::lock_guard<std::mutex> Lock(m_Mutex);
+
+ if (DefaultLevel)
+ {
+ m_GlobalLevel.store(*DefaultLevel, std::memory_order_relaxed);
+ for (auto& [Name, CurLogger] : m_Loggers)
+ {
+ CurLogger->SetLevel(*DefaultLevel);
+ }
+ }
+
+ for (auto& [LoggerName, Level] : Levels)
+ {
+ auto It = m_Loggers.find(LoggerName);
+ if (It != m_Loggers.end())
+ {
+ It->second->SetLevel(Level);
+ }
+ }
+ }
+
+ void FlushAll()
+ {
+ std::lock_guard<std::mutex> Lock(m_Mutex);
+ for (auto& [Name, CurLogger] : m_Loggers)
+ {
+ try
+ {
+ CurLogger->Flush();
+ }
+ catch (const std::exception&)
+ {
+ }
+ }
+ }
+
+ void FlushOn(LogLevel Level)
+ {
+ std::lock_guard<std::mutex> Lock(m_Mutex);
+ m_FlushLevel = Level;
+ for (auto& [Name, CurLogger] : m_Loggers)
+ {
+ CurLogger->SetFlushLevel(Level);
+ }
+ }
+
+ void FlushEvery(std::chrono::seconds Interval)
+ {
+ StopPeriodicFlush();
+
+ m_PeriodicFlushRunning.store(true, std::memory_order_relaxed);
+
+ m_FlushThread = std::thread([this, Interval] {
+ while (m_PeriodicFlushRunning.load(std::memory_order_relaxed))
+ {
+ {
+ std::unique_lock<std::mutex> Lock(m_PeriodicFlushMutex);
+ m_PeriodicFlushCv.wait_for(Lock, Interval, [this] { return !m_PeriodicFlushRunning.load(std::memory_order_relaxed); });
+ }
+
+ if (m_PeriodicFlushRunning.load(std::memory_order_relaxed))
+ {
+ FlushAll();
+ }
+ }
+ });
+ }
+
+ void SetFormatter(std::unique_ptr<Formatter> InFormatter)
+ {
+ std::lock_guard<std::mutex> Lock(m_Mutex);
+ for (auto& [Name, CurLogger] : m_Loggers)
+ {
+ CurLogger->SetFormatter(InFormatter->Clone());
+ }
+ }
+
+ void ApplyAll(void (*Func)(void*, Ref<Logger>), void* Context)
+ {
+ std::lock_guard<std::mutex> Lock(m_Mutex);
+ for (auto& [Name, CurLogger] : m_Loggers)
+ {
+ Func(Context, CurLogger);
+ }
+ }
+
+ void SetErrorHandler(ErrorHandler* Handler)
+ {
+ std::lock_guard<std::mutex> Lock(m_Mutex);
+ m_ErrorHandler = Handler;
+ for (auto& [Name, CurLogger] : m_Loggers)
+ {
+ CurLogger->SetErrorHandler(Handler);
+ }
+ }
+
+ void Shutdown()
+ {
+ StopPeriodicFlush();
+ FlushAll();
+
+ std::lock_guard<std::mutex> Lock(m_Mutex);
+ m_Loggers.clear();
+ m_DefaultLogger = nullptr;
+ }
+
+private:
+ void StopPeriodicFlush()
+ {
+ if (m_FlushThread.joinable())
+ {
+ m_PeriodicFlushRunning.store(false, std::memory_order_relaxed);
+ {
+ std::lock_guard<std::mutex> Lock(m_PeriodicFlushMutex);
+ m_PeriodicFlushCv.notify_one();
+ }
+ m_FlushThread.join();
+ }
+ }
+
+ std::mutex m_Mutex;
+ std::unordered_map<std::string, Ref<Logger>> m_Loggers;
+ Ref<Logger> m_DefaultLogger;
+ std::atomic<LogLevel> m_GlobalLevel{Trace};
+ LogLevel m_FlushLevel{Off};
+ ErrorHandler* m_ErrorHandler = nullptr;
+
+ // Periodic flush
+ std::atomic<bool> m_PeriodicFlushRunning{false};
+ std::mutex m_PeriodicFlushMutex;
+ std::condition_variable m_PeriodicFlushCv;
+ std::thread m_FlushThread;
+};
+
+Registry&
+Registry::Instance()
+{
+ static Registry s_Instance;
+ return s_Instance;
+}
+
+Registry::Registry() : m_Impl(std::make_unique<Impl>())
+{
+}
+
+Registry::~Registry() = default;
+
+void
+Registry::Register(Ref<Logger> InLogger)
+{
+ m_Impl->Register(std::move(InLogger));
+}
+
+void
+Registry::Drop(const std::string& Name)
+{
+ m_Impl->Drop(Name);
+}
+
+Ref<Logger>
+Registry::Get(const std::string& Name)
+{
+ return m_Impl->Get(Name);
+}
+
+void
+Registry::SetDefaultLogger(Ref<Logger> InLogger)
+{
+ m_Impl->SetDefaultLogger(std::move(InLogger));
+}
+
+Logger*
+Registry::DefaultLoggerRaw()
+{
+ return m_Impl->DefaultLoggerRaw();
+}
+
+Ref<Logger>
+Registry::DefaultLogger()
+{
+ return m_Impl->DefaultLogger();
+}
+
+void
+Registry::SetGlobalLevel(LogLevel Level)
+{
+ m_Impl->SetGlobalLevel(Level);
+}
+
+LogLevel
+Registry::GetGlobalLevel() const
+{
+ return m_Impl->GetGlobalLevel();
+}
+
+void
+Registry::SetLevels(LogLevels Levels, LogLevel* DefaultLevel)
+{
+ m_Impl->SetLevels(Levels, DefaultLevel);
+}
+
+void
+Registry::FlushAll()
+{
+ m_Impl->FlushAll();
+}
+
+void
+Registry::FlushOn(LogLevel Level)
+{
+ m_Impl->FlushOn(Level);
+}
+
+void
+Registry::FlushEvery(std::chrono::seconds Interval)
+{
+ m_Impl->FlushEvery(Interval);
+}
+
+void
+Registry::SetFormatter(std::unique_ptr<Formatter> InFormatter)
+{
+ m_Impl->SetFormatter(std::move(InFormatter));
+}
+
+void
+Registry::ApplyAllImpl(void (*Func)(void*, Ref<Logger>), void* Context)
+{
+ m_Impl->ApplyAll(Func, Context);
+}
+
+void
+Registry::SetErrorHandler(ErrorHandler* Handler)
+{
+ m_Impl->SetErrorHandler(Handler);
+}
+
+void
+Registry::Shutdown()
+{
+ m_Impl->Shutdown();
+}
+
+} // namespace zen::logging
diff --git a/src/zencore/logging/tracesink.cpp b/src/zencore/logging/tracesink.cpp
new file mode 100644
index 000000000..e3533327b
--- /dev/null
+++ b/src/zencore/logging/tracesink.cpp
@@ -0,0 +1,88 @@
+
+// Copyright Epic Games, Inc. All Rights Reserved.
+
+#include <zencore/logbase.h>
+#include <zencore/logging/tracesink.h>
+#include <zencore/string.h>
+#include <zencore/timer.h>
+#include <zencore/trace.h>
+
+namespace zen::logging {
+
+UE_TRACE_CHANNEL_DEFINE(LogChannel)
+
+UE_TRACE_EVENT_BEGIN(Logging, LogCategory, NoSync | Important)
+ UE_TRACE_EVENT_FIELD(const void*, CategoryPointer)
+ UE_TRACE_EVENT_FIELD(uint8_t, DefaultVerbosity)
+ UE_TRACE_EVENT_FIELD(UE::Trace::AnsiString, Name)
+UE_TRACE_EVENT_END()
+
+UE_TRACE_EVENT_BEGIN(Logging, LogMessageSpec, NoSync | Important)
+ UE_TRACE_EVENT_FIELD(const void*, LogPoint)
+ UE_TRACE_EVENT_FIELD(const void*, CategoryPointer)
+ UE_TRACE_EVENT_FIELD(int32_t, Line)
+ UE_TRACE_EVENT_FIELD(uint8_t, Verbosity)
+ UE_TRACE_EVENT_FIELD(UE::Trace::AnsiString, FileName)
+ UE_TRACE_EVENT_FIELD(UE::Trace::AnsiString, FormatString)
+UE_TRACE_EVENT_END()
+
+UE_TRACE_EVENT_BEGIN(Logging, LogMessage, NoSync)
+ UE_TRACE_EVENT_FIELD(const void*, LogPoint)
+ UE_TRACE_EVENT_FIELD(uint64_t, Cycle)
+ UE_TRACE_EVENT_FIELD(uint8_t[], FormatArgs)
+UE_TRACE_EVENT_END()
+
+void
+TraceLogCategory(const logging::Logger* Category, const char* Name, logging::LogLevel DefaultVerbosity)
+{
+ uint16_t NameLen = uint16_t(strlen(Name));
+ UE_TRACE_LOG(Logging, LogCategory, LogChannel, NameLen * sizeof(ANSICHAR))
+ << LogCategory.CategoryPointer(Category) << LogCategory.DefaultVerbosity(uint8_t(DefaultVerbosity))
+ << LogCategory.Name(Name, NameLen);
+}
+
+void
+TraceLogMessageSpec(const void* LogPoint,
+ const logging::Logger* Category,
+ logging::LogLevel Verbosity,
+ const std::string_view File,
+ int32_t Line,
+ const std::string_view Format)
+{
+ uint16_t FileNameLen = uint16_t(File.size());
+ uint16_t FormatStringLen = uint16_t(Format.size());
+ uint32_t DataSize = (FileNameLen * sizeof(ANSICHAR)) + (FormatStringLen * sizeof(ANSICHAR));
+ UE_TRACE_LOG(Logging, LogMessageSpec, LogChannel, DataSize)
+ << LogMessageSpec.LogPoint(LogPoint) << LogMessageSpec.CategoryPointer(Category) << LogMessageSpec.Line(Line)
+ << LogMessageSpec.Verbosity(uint8_t(Verbosity)) << LogMessageSpec.FileName(File.data(), FileNameLen)
+ << LogMessageSpec.FormatString(Format.data(), FormatStringLen);
+}
+
+void
+TraceLogMessageInternal(const void* LogPoint, int32_t EncodedFormatArgsSize, const uint8_t* EncodedFormatArgs)
+{
+ UE_TRACE_LOG(Logging, LogMessage, LogChannel) << LogMessage.LogPoint(LogPoint) << LogMessage.Cycle(GetHifreqTimerValue())
+ << LogMessage.FormatArgs(EncodedFormatArgs, EncodedFormatArgsSize);
+}
+
+//////////////////////////////////////////////////////////////////////////
+
+void
+TraceSink::Log(const LogMessage& Msg)
+{
+ ZEN_UNUSED(Msg);
+}
+
+void
+TraceSink::Flush()
+{
+}
+
+void
+TraceSink::SetFormatter(std::unique_ptr<Formatter> /*InFormatter*/)
+{
+ // This sink doesn't use a formatter since it just forwards the raw format
+ // args to the trace system
+}
+
+} // namespace zen::logging
diff --git a/src/zencore/sentryintegration.cpp b/src/zencore/sentryintegration.cpp
index bfff114c3..e39b8438d 100644
--- a/src/zencore/sentryintegration.cpp
+++ b/src/zencore/sentryintegration.cpp
@@ -4,29 +4,23 @@
#include <zencore/config.h>
#include <zencore/logging.h>
+#include <zencore/logging/registry.h>
+#include <zencore/logging/sink.h>
#include <zencore/session.h>
#include <zencore/uid.h>
#include <stdarg.h>
#include <stdio.h>
-#if ZEN_PLATFORM_LINUX
+#if ZEN_PLATFORM_LINUX || ZEN_PLATFORM_MAC
# include <pwd.h>
+# include <unistd.h>
#endif
-#if ZEN_PLATFORM_MAC
-# include <pwd.h>
-#endif
-
-ZEN_THIRD_PARTY_INCLUDES_START
-#include <spdlog/spdlog.h>
-ZEN_THIRD_PARTY_INCLUDES_END
-
#if ZEN_USE_SENTRY
# define SENTRY_BUILD_STATIC 1
ZEN_THIRD_PARTY_INCLUDES_START
# include <sentry.h>
-# include <spdlog/sinks/base_sink.h>
ZEN_THIRD_PARTY_INCLUDES_END
namespace sentry {
@@ -44,76 +38,58 @@ struct SentryAssertImpl : zen::AssertImpl
const zen::CallstackFrames* Callstack) override;
};
-class sentry_sink final : public spdlog::sinks::base_sink<spdlog::details::null_mutex>
+static constexpr sentry_level_t MapToSentryLevel[zen::logging::LogLevelCount] = {SENTRY_LEVEL_DEBUG,
+ SENTRY_LEVEL_DEBUG,
+ SENTRY_LEVEL_INFO,
+ SENTRY_LEVEL_WARNING,
+ SENTRY_LEVEL_ERROR,
+ SENTRY_LEVEL_FATAL,
+ SENTRY_LEVEL_DEBUG};
+
+class SentrySink final : public zen::logging::Sink
{
public:
- sentry_sink();
- ~sentry_sink();
+ SentrySink() = default;
+ ~SentrySink() = default;
+
+ void Log(const zen::logging::LogMessage& Msg) override
+ {
+ if (Msg.GetLevel() != zen::logging::Err && Msg.GetLevel() != zen::logging::Critical)
+ {
+ return;
+ }
+ try
+ {
+ std::string Message = fmt::format("{}\n{}({})", Msg.GetPayload(), Msg.GetSource().Filename, Msg.GetSource().Line);
+ sentry_value_t Event = sentry_value_new_message_event(
+ /* level */ MapToSentryLevel[Msg.GetLevel()],
+ /* logger */ nullptr,
+ /* message */ Message.c_str());
+ sentry_event_value_add_stacktrace(Event, NULL, 0);
+ sentry_capture_event(Event);
+ }
+ catch (const std::exception&)
+ {
+ // If our logging with Message formatting fails we do a non-allocating version and just post the payload raw
+ char TmpBuffer[256];
+ size_t MaxCopy = zen::Min<size_t>(Msg.GetPayload().size(), size_t(255));
+ memcpy(TmpBuffer, Msg.GetPayload().data(), MaxCopy);
+ TmpBuffer[MaxCopy] = '\0';
+ sentry_value_t Event = sentry_value_new_message_event(
+ /* level */ SENTRY_LEVEL_ERROR,
+ /* logger */ nullptr,
+ /* message */ TmpBuffer);
+ sentry_event_value_add_stacktrace(Event, NULL, 0);
+ sentry_capture_event(Event);
+ }
+ }
-protected:
- void sink_it_(const spdlog::details::log_msg& msg) override;
- void flush_() override;
+ void Flush() override {}
+ void SetFormatter(std::unique_ptr<zen::logging::Formatter>) override {}
};
//////////////////////////////////////////////////////////////////////////
-static constexpr sentry_level_t MapToSentryLevel[spdlog::level::level_enum::n_levels] = {SENTRY_LEVEL_DEBUG,
- SENTRY_LEVEL_DEBUG,
- SENTRY_LEVEL_INFO,
- SENTRY_LEVEL_WARNING,
- SENTRY_LEVEL_ERROR,
- SENTRY_LEVEL_FATAL,
- SENTRY_LEVEL_DEBUG};
-
-sentry_sink::sentry_sink()
-{
-}
-sentry_sink::~sentry_sink()
-{
-}
-
-void
-sentry_sink::sink_it_(const spdlog::details::log_msg& msg)
-{
- if (msg.level != spdlog::level::err && msg.level != spdlog::level::critical)
- {
- return;
- }
- try
- {
- auto MaybeNullString = [](const char* Ptr) { return Ptr ? Ptr : "<null>"; };
- std::string Message = fmt::format("{}\n{}({}) [{}]",
- msg.payload,
- MaybeNullString(msg.source.filename),
- msg.source.line,
- MaybeNullString(msg.source.funcname));
- sentry_value_t event = sentry_value_new_message_event(
- /* level */ MapToSentryLevel[msg.level],
- /* logger */ nullptr,
- /* message */ Message.c_str());
- sentry_event_value_add_stacktrace(event, NULL, 0);
- sentry_capture_event(event);
- }
- catch (const std::exception&)
- {
- // If our logging with Message formatting fails we do a non-allocating version and just post the msg.payload raw
- char TmpBuffer[256];
- size_t MaxCopy = zen::Min<size_t>(msg.payload.size(), size_t(255));
- memcpy(TmpBuffer, msg.payload.data(), MaxCopy);
- TmpBuffer[MaxCopy] = '\0';
- sentry_value_t event = sentry_value_new_message_event(
- /* level */ SENTRY_LEVEL_ERROR,
- /* logger */ nullptr,
- /* message */ TmpBuffer);
- sentry_event_value_add_stacktrace(event, NULL, 0);
- sentry_capture_event(event);
- }
-}
-void
-sentry_sink::flush_()
-{
-}
-
void
SentryAssertImpl::OnAssert(const char* Filename,
int LineNumber,
@@ -340,7 +316,9 @@ SentryIntegration::Initialize(const Config& Conf, const std::string& CommandLine
sentry_set_user(SentryUserObject);
- m_SentryLogger = spdlog::create<sentry::sentry_sink>("sentry");
+ logging::SinkPtr SentrySink(new sentry::SentrySink());
+ m_SentryLogger = Ref<logging::Logger>(new logging::Logger("sentry", std::vector<logging::SinkPtr>{SentrySink}));
+ logging::Registry::Instance().Register(m_SentryLogger);
logging::SetErrorLog("sentry");
m_SentryAssert = std::make_unique<sentry::SentryAssertImpl>();
@@ -354,7 +332,7 @@ SentryIntegration::LogStartupInformation()
{
// Initialize the sentry-sdk log category at Warn level to reduce startup noise.
// The level can be overridden via --log-debug=sentry-sdk or --log-info=sentry-sdk
- LogSentry.Logger().SetLogLevel(logging::level::Warn);
+ LogSentry.Logger().SetLogLevel(logging::Warn);
if (m_IsInitialized)
{
diff --git a/src/zencore/testing.cpp b/src/zencore/testing.cpp
index 0bae139bd..089e376bb 100644
--- a/src/zencore/testing.cpp
+++ b/src/zencore/testing.cpp
@@ -143,11 +143,11 @@ TestRunner::ApplyCommandLine(int Argc, char const* const* Argv)
{
if (Argv[i] == "--debug"sv)
{
- zen::logging::SetLogLevel(zen::logging::level::Debug);
+ zen::logging::SetLogLevel(zen::logging::Debug);
}
else if (Argv[i] == "--verbose"sv)
{
- zen::logging::SetLogLevel(zen::logging::level::Trace);
+ zen::logging::SetLogLevel(zen::logging::Trace);
}
}
diff --git a/src/zencore/xmake.lua b/src/zencore/xmake.lua
index 2f81b7ec8..171f4c533 100644
--- a/src/zencore/xmake.lua
+++ b/src/zencore/xmake.lua
@@ -26,7 +26,6 @@ target('zencore')
end
add_deps("zenbase")
- add_deps("spdlog")
add_deps("utfcpp")
add_deps("oodle")
add_deps("blake3")
diff --git a/src/zencore/zencore.cpp b/src/zencore/zencore.cpp
index d82474705..8c29a8962 100644
--- a/src/zencore/zencore.cpp
+++ b/src/zencore/zencore.cpp
@@ -147,7 +147,7 @@ AssertImpl::OnAssert(const char* Filename, int LineNumber, const char* FunctionN
Message.push_back('\0');
// We use direct ZEN_LOG here instead of ZEN_ERROR as we don't care about *this* code location in the log
- ZEN_LOG(Log(), zen::logging::level::Err, "{}", Message.data());
+ ZEN_LOG(Log(), zen::logging::Err, "{}", Message.data());
zen::logging::FlushLogging();
}