aboutsummaryrefslogtreecommitdiff
path: root/src/zenutil/logging/fullformatter.cpp
diff options
context:
space:
mode:
authorStefan Boberg <[email protected]>2026-03-18 11:19:10 +0100
committerGitHub Enterprise <[email protected]>2026-03-18 11:19:10 +0100
commiteba410c4168e23d7908827eb34b7cf0c58a5dc48 (patch)
tree3cda8e8f3f81941d3bb5b84a8155350c5bb2068c /src/zenutil/logging/fullformatter.cpp
parentbugfix release - v5.7.23 (#851) (diff)
downloadzen-eba410c4168e23d7908827eb34b7cf0c58a5dc48.tar.xz
zen-eba410c4168e23d7908827eb34b7cf0c58a5dc48.zip
Compute batching (#849)
### Compute Batch Submission - Consolidate duplicated action submission logic in `httpcomputeservice` into a single `HandleSubmitAction` supporting both single-action and batch (actions array) payloads - Group actions by queue in `RemoteHttpRunner` and submit as batches with configurable chunk size, falling back to individual submission on failure - Extract shared helpers: `MakeErrorResult`, `ValidateQueueForEnqueue`, `ActivateActionInQueue`, `RemoveActionFromActiveMaps` ### Retracted Action State - Add `Retracted` state to `RunnerAction` for retry-free rescheduling — an explicit request to pull an action back and reschedule it on a different runner without incrementing `RetryCount` - Implement idempotent `RetractAction()` on `RunnerAction` and `ComputeServiceSession` - Add `POST jobs/{lsn}/retract` and `queues/{queueref}/jobs/{lsn}/retract` HTTP endpoints - Add state machine documentation and per-state comments to `RunnerAction` ### Compute Race Fixes - Fix race in `HandleActionUpdates` where actions enqueued between session abandon and scheduler tick were never abandoned, causing `GetActionResult` to return 202 indefinitely - Fix queue `ActiveCount` race where `NotifyQueueActionComplete` was called after releasing `m_ResultsLock`, allowing callers to observe stale counters immediately after `GetActionResult` returned OK ### Logging Optimization and ANSI improvements - Improve `AnsiColorStdoutSink` write efficiency — single write call, dirty-flag flush, `RwLock` instead of `std::mutex` - Move ANSI color emission from sink into formatters via `Formatter::SetColorEnabled()`; remove `ColorRangeStart`/`End` from `LogMessage` - Extract color helpers (`AnsiColorForLevel`, `StripAnsiSgrSequences`) into `helpers.h` - Strip upstream ANSI SGR escapes in non-color output mode. This enables colour in log messages without polluting log files with ANSI control sequences - Move `RotatingFileSink`, `JsonFormatter`, and `FullFormatter` from header-only to pimpl with `.cpp` files ### CLI / Exec Refactoring - Extract `ExecSessionRunner` class from ~920-line `ExecUsingSession` into focused methods and a `ExecSessionConfig` struct - Replace monolithic `ExecCommand` with subcommand-based architecture (`http`, `inproc`, `beacon`, `dump`, `buildlog`) - Allow parent options to appear after subcommand name by parsing subcommand args permissively and forwarding unmatched tokens to the parent parser ### Testing Improvements - Fix `--test-suite` filter being ignored due to accumulation with default wildcard filter - Add test suite banners to test listener output - Made `function.session.abandon_pending` test more robust ### Startup / Reliability Fixes - Fix silent exit when a second zenserver instance detects a port conflict — use `ZEN_CONSOLE_*` for log calls that precede `InitializeLogging()` - Fix two potential SIGSEGV paths during early startup: guard `sentry_options_new()` returning nullptr, and throw on `ZenServerState::Register()` returning nullptr instead of dereferencing - Fail on unrecognized zenserver `--mode` instead of silently defaulting to store ### Other - Show host details (hostname, platform, CPU count, memory) when discovering new compute workers - Move frontend `html.zip` from source tree into build directory - Add format specifications for Compact Binary and Compressed Buffer wire formats - Add `WriteCompactBinaryObject` to zencore - Extended `ConsoleTui` with additional functionality - Add `--vscode` option to `xmake sln` for clangd / `compile_commands.json` support - Disable compute/horde/nomad in release builds (not yet production-ready) - Disable unintended `ASIO_HAS_IO_URING` enablement - Fix crashpad patch missing leading whitespace - Clean up code triggering gcc false positives
Diffstat (limited to 'src/zenutil/logging/fullformatter.cpp')
-rw-r--r--src/zenutil/logging/fullformatter.cpp235
1 files changed, 235 insertions, 0 deletions
diff --git a/src/zenutil/logging/fullformatter.cpp b/src/zenutil/logging/fullformatter.cpp
new file mode 100644
index 000000000..2a4840241
--- /dev/null
+++ b/src/zenutil/logging/fullformatter.cpp
@@ -0,0 +1,235 @@
+// Copyright Epic Games, Inc. All Rights Reserved.
+
+#include <zenutil/logging/fullformatter.h>
+
+#include <zencore/intmath.h>
+#include <zencore/logging/helpers.h>
+#include <zencore/logging/memorybuffer.h>
+#include <zencore/memory/llm.h>
+#include <zencore/thread.h>
+#include <zencore/zencore.h>
+
+#include <atomic>
+#include <chrono>
+#include <string>
+
+namespace zen::logging {
+
+struct FullFormatter::Impl
+{
+ Impl(std::string_view LogId, std::chrono::time_point<std::chrono::system_clock> Epoch)
+ : m_Epoch(Epoch)
+ , m_LogId(LogId)
+ , m_LinePrefix(128, ' ')
+ , m_UseFullDate(false)
+ {
+ }
+
+ explicit Impl(std::string_view LogId) : m_LogId(LogId), m_LinePrefix(128, ' '), m_UseFullDate(true) {}
+
+ std::chrono::time_point<std::chrono::system_clock> m_Epoch;
+ std::tm m_CachedLocalTm{};
+ std::chrono::seconds m_LastLogSecs{std::chrono::seconds(87654321)};
+ std::atomic<std::chrono::seconds> m_CacheTimestamp{std::chrono::seconds(87654321)};
+ MemoryBuffer m_CachedDatetime;
+ std::string m_LogId;
+ std::string m_LinePrefix;
+ bool m_UseFullDate = true;
+ RwLock m_TimestampLock;
+};
+
+FullFormatter::FullFormatter(std::string_view LogId, std::chrono::time_point<std::chrono::system_clock> Epoch)
+: m_Impl(std::make_unique<Impl>(LogId, Epoch))
+{
+}
+
+FullFormatter::FullFormatter(std::string_view LogId) : m_Impl(std::make_unique<Impl>(LogId))
+{
+}
+
+FullFormatter::~FullFormatter() = default;
+
+std::unique_ptr<Formatter>
+FullFormatter::Clone() const
+{
+ ZEN_MEMSCOPE(ELLMTag::Logging);
+ std::unique_ptr<FullFormatter> Copy;
+ if (m_Impl->m_UseFullDate)
+ {
+ Copy = std::make_unique<FullFormatter>(m_Impl->m_LogId);
+ }
+ else
+ {
+ Copy = std::make_unique<FullFormatter>(m_Impl->m_LogId, m_Impl->m_Epoch);
+ }
+ Copy->SetColorEnabled(IsColorEnabled());
+ return Copy;
+}
+
+void
+FullFormatter::Format(const LogMessage& Msg, MemoryBuffer& OutBuffer)
+{
+ ZEN_MEMSCOPE(ELLMTag::Logging);
+
+ // Note that the sink is responsible for ensuring there is only ever a
+ // single caller in here
+
+ using namespace std::literals;
+
+ std::chrono::seconds TimestampSeconds;
+
+ std::chrono::milliseconds Millis;
+
+ if (m_Impl->m_UseFullDate)
+ {
+ TimestampSeconds = std::chrono::duration_cast<std::chrono::seconds>(Msg.GetTime().time_since_epoch());
+ if (TimestampSeconds != m_Impl->m_LastLogSecs)
+ {
+ RwLock::ExclusiveLockScope _(m_Impl->m_TimestampLock);
+ m_Impl->m_LastLogSecs = TimestampSeconds;
+
+ m_Impl->m_CachedLocalTm = helpers::SafeLocaltime(LogClock::to_time_t(Msg.GetTime()));
+ m_Impl->m_CachedDatetime.clear();
+ m_Impl->m_CachedDatetime.push_back('[');
+ helpers::Pad2(m_Impl->m_CachedLocalTm.tm_year % 100, m_Impl->m_CachedDatetime);
+ m_Impl->m_CachedDatetime.push_back('-');
+ helpers::Pad2(m_Impl->m_CachedLocalTm.tm_mon + 1, m_Impl->m_CachedDatetime);
+ m_Impl->m_CachedDatetime.push_back('-');
+ helpers::Pad2(m_Impl->m_CachedLocalTm.tm_mday, m_Impl->m_CachedDatetime);
+ m_Impl->m_CachedDatetime.push_back(' ');
+ helpers::Pad2(m_Impl->m_CachedLocalTm.tm_hour, m_Impl->m_CachedDatetime);
+ m_Impl->m_CachedDatetime.push_back(':');
+ helpers::Pad2(m_Impl->m_CachedLocalTm.tm_min, m_Impl->m_CachedDatetime);
+ m_Impl->m_CachedDatetime.push_back(':');
+ helpers::Pad2(m_Impl->m_CachedLocalTm.tm_sec, m_Impl->m_CachedDatetime);
+ m_Impl->m_CachedDatetime.push_back('.');
+ }
+
+ Millis = helpers::TimeFraction<std::chrono::milliseconds>(Msg.GetTime());
+ }
+ else
+ {
+ auto ElapsedTime = Msg.GetTime() - m_Impl->m_Epoch;
+ TimestampSeconds = std::chrono::duration_cast<std::chrono::seconds>(ElapsedTime);
+
+ if (m_Impl->m_CacheTimestamp.load() != TimestampSeconds)
+ {
+ RwLock::ExclusiveLockScope _(m_Impl->m_TimestampLock);
+
+ m_Impl->m_CacheTimestamp = TimestampSeconds;
+
+ int Count = int(TimestampSeconds.count());
+ const int LogSecs = Count % 60;
+ Count /= 60;
+ const int LogMins = Count % 60;
+ Count /= 60;
+ const int LogHours = Count;
+
+ m_Impl->m_CachedDatetime.clear();
+ m_Impl->m_CachedDatetime.push_back('[');
+ helpers::Pad2(LogHours, m_Impl->m_CachedDatetime);
+ m_Impl->m_CachedDatetime.push_back(':');
+ helpers::Pad2(LogMins, m_Impl->m_CachedDatetime);
+ m_Impl->m_CachedDatetime.push_back(':');
+ helpers::Pad2(LogSecs, m_Impl->m_CachedDatetime);
+ m_Impl->m_CachedDatetime.push_back('.');
+ }
+
+ Millis = std::chrono::duration_cast<std::chrono::milliseconds>(ElapsedTime - TimestampSeconds);
+ }
+
+ {
+ RwLock::SharedLockScope _(m_Impl->m_TimestampLock);
+ OutBuffer.append(m_Impl->m_CachedDatetime.begin(), m_Impl->m_CachedDatetime.end());
+ }
+
+ helpers::Pad3(static_cast<uint32_t>(Millis.count()), OutBuffer);
+ OutBuffer.push_back(']');
+ OutBuffer.push_back(' ');
+
+ if (!m_Impl->m_LogId.empty())
+ {
+ OutBuffer.push_back('[');
+ helpers::AppendStringView(m_Impl->m_LogId, OutBuffer);
+ OutBuffer.push_back(']');
+ OutBuffer.push_back(' ');
+ }
+
+ // append logger name if exists
+ if (Msg.GetLoggerName().size() > 0)
+ {
+ OutBuffer.push_back('[');
+ helpers::AppendStringView(Msg.GetLoggerName(), OutBuffer);
+ OutBuffer.push_back(']');
+ OutBuffer.push_back(' ');
+ }
+
+ OutBuffer.push_back('[');
+ if (IsColorEnabled())
+ {
+ helpers::AppendAnsiColor(Msg.GetLevel(), OutBuffer);
+ }
+ helpers::AppendStringView(helpers::LevelToShortString(Msg.GetLevel()), OutBuffer);
+ if (IsColorEnabled())
+ {
+ helpers::AppendAnsiReset(OutBuffer);
+ }
+ OutBuffer.push_back(']');
+ OutBuffer.push_back(' ');
+
+ // add source location if present
+ if (Msg.GetSource())
+ {
+ OutBuffer.push_back('[');
+ const char* Filename = helpers::ShortFilename(Msg.GetSource().Filename);
+ helpers::AppendStringView(Filename, OutBuffer);
+ OutBuffer.push_back(':');
+ helpers::AppendInt(Msg.GetSource().Line, OutBuffer);
+ OutBuffer.push_back(']');
+ OutBuffer.push_back(' ');
+ }
+
+ // Handle newlines in single log call by prefixing each additional line to make
+ // subsequent lines align with the first line in the message
+
+ size_t AnsiBytes = IsColorEnabled() ? (helpers::AnsiColorForLevel(Msg.GetLevel()).size() + helpers::kAnsiReset.size()) : 0;
+ const size_t LinePrefixCount = Min<size_t>(OutBuffer.size() - AnsiBytes, m_Impl->m_LinePrefix.size());
+
+ auto MsgPayload = Msg.GetPayload();
+ auto ItLineBegin = MsgPayload.begin();
+ auto ItMessageEnd = MsgPayload.end();
+ bool IsFirstline = true;
+
+ {
+ auto ItLineEnd = ItLineBegin;
+
+ auto EmitLine = [&] {
+ if (IsFirstline)
+ {
+ IsFirstline = false;
+ }
+ else
+ {
+ helpers::AppendStringView(std::string_view(m_Impl->m_LinePrefix.data(), LinePrefixCount), OutBuffer);
+ }
+ helpers::AppendStringView(std::string_view(&*ItLineBegin, ItLineEnd - ItLineBegin), OutBuffer);
+ };
+
+ while (ItLineEnd != ItMessageEnd)
+ {
+ if (*ItLineEnd++ == '\n')
+ {
+ EmitLine();
+ ItLineBegin = ItLineEnd;
+ }
+ }
+
+ if (ItLineBegin != ItMessageEnd)
+ {
+ EmitLine();
+ helpers::AppendStringView("\n"sv, OutBuffer);
+ }
+ }
+}
+
+} // namespace zen::logging