From e0879bed083eab09cfa28043a3b210714d0884b9 Mon Sep 17 00:00:00 2001 From: Stefan Boberg Date: Mon, 13 Apr 2026 12:09:05 +0200 Subject: Logging and diagnostics improvements (#941) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Core logging and system diagnostics improvements, extracted from the compute branch. ### Logging - **Elapsed timestamps**: Console log now shows elapsed time since launch `[HH:MM:SS.mmm]` instead of full date/time; file logging is unchanged - **Short level names**: 3-letter short level names (`trc`/`dbg`/`inf`/`wrn`/`err`/`crt`) used by both console and file formatters via `ShortToStringView()` - **Consistent field order**: Standardized to `[timestamp] [level] [logger]` across both console and file formatters - **Slim LogMessage/LogPoint**: Remove redundant fields from `LogMessage` (derive level/source from `LogPoint`), flatten `LogPoint` to inline filename/line fields, shrink `LogLevel` to `int8_t` with `static_assert(sizeof(LogPoint) <= 32)` - **Remove default member initializers** and static default `LogPoint` from `LogMessage` — all fields initialized by constructor - **LoggerRef string constructor**: Convenience constructor accepting a string directly - **Fix SendMessage macro collision**: Replace `thread.h` include in `logmsg.h` with a forward declaration of `GetCurrentThreadId()` to avoid pulling in `windows.h` transitively ### System Diagnostics - **Cache static system metrics**: Add `RefreshDynamicSystemMetrics()` that only queries values that change at runtime (available memory, uptime, swap). `SystemMetricsTracker` snapshots full `GetSystemMetrics()` once at construction and reuses cached topology/total memory on each `Query()`, avoiding repeated `GetLogicalProcessorInformationEx` traversal on Windows, `/proc/cpuinfo` parsing on Linux, and `sysctl` topology calls on macOS --- src/zencore/logging.cpp | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) (limited to 'src/zencore/logging.cpp') diff --git a/src/zencore/logging.cpp b/src/zencore/logging.cpp index 5ada0cac7..ca34b7d0e 100644 --- a/src/zencore/logging.cpp +++ b/src/zencore/logging.cpp @@ -112,6 +112,14 @@ constinit std::string_view LevelNames[] = {std::string_view("trace", 5), std::string_view("critical", 8), std::string_view("off", 3)}; +constinit std::string_view ShortNames[] = {std::string_view("trc", 3), + std::string_view("dbg", 3), + std::string_view("inf", 3), + std::string_view("wrn", 3), + std::string_view("err", 3), + std::string_view("crt", 3), + std::string_view("off", 3)}; + LogLevel ParseLogLevelString(std::string_view Name) { @@ -139,12 +147,27 @@ ParseLogLevelString(std::string_view Name) std::string_view ToStringView(LogLevel Level) { + using namespace std::literals; + if (int(Level) < LogLevelCount) { return LevelNames[int(Level)]; } - return "None"; + return "None"sv; +} + +std::string_view +ShortToStringView(LogLevel Level) +{ + using namespace std::literals; + + if (int(Level) < LogLevelCount) + { + return ShortNames[int(Level)]; + } + + return "None"sv; } } // namespace zen::logging @@ -476,6 +499,10 @@ LoggerRef::LoggerRef(logging::Logger& InLogger) : m_Logger(static_cast