aboutsummaryrefslogtreecommitdiff
path: root/src/zencore/logging
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/zencore/logging
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/zencore/logging')
-rw-r--r--src/zencore/logging/ansicolorsink.cpp287
1 files changed, 163 insertions, 124 deletions
diff --git a/src/zencore/logging/ansicolorsink.cpp b/src/zencore/logging/ansicolorsink.cpp
index 540d22359..03aae068a 100644
--- a/src/zencore/logging/ansicolorsink.cpp
+++ b/src/zencore/logging/ansicolorsink.cpp
@@ -4,12 +4,14 @@
#include <zencore/logging/helpers.h>
#include <zencore/logging/messageonlyformatter.h>
+#include <zencore/thread.h>
+
#include <cstdio>
#include <cstdlib>
-#include <mutex>
#if defined(_WIN32)
# include <io.h>
+# include <zencore/windows.h>
# define ZEN_ISATTY _isatty
# define ZEN_FILENO _fileno
#else
@@ -62,188 +64,225 @@ public:
Dest.push_back(' ');
}
- // level (colored range)
+ // level
Dest.push_back('[');
- Msg.ColorRangeStart = Dest.size();
+ if (IsColorEnabled())
+ {
+ helpers::AppendAnsiColor(Msg.GetLevel(), Dest);
+ }
helpers::AppendStringView(helpers::LevelToShortString(Msg.GetLevel()), Dest);
- Msg.ColorRangeEnd = Dest.size();
+ if (IsColorEnabled())
+ {
+ helpers::AppendAnsiReset(Dest);
+ }
Dest.push_back(']');
Dest.push_back(' ');
- // message
- helpers::AppendStringView(Msg.GetPayload(), Dest);
+ // message (align continuation lines with the first line)
+ size_t AnsiBytes = IsColorEnabled() ? (helpers::AnsiColorForLevel(Msg.GetLevel()).size() + helpers::kAnsiReset.size()) : 0;
+ size_t LinePrefixCount = Dest.size() - AnsiBytes;
+
+ 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
+ {
+ for (size_t i = 0; i < LinePrefixCount; ++i)
+ {
+ Dest.push_back(' ');
+ }
+ }
+ helpers::AppendStringView(std::string_view(&*ItLineBegin, ItLineEnd - ItLineBegin), Dest);
+ };
+
+ while (ItLineEnd != ItMessageEnd)
+ {
+ if (*ItLineEnd++ == '\n')
+ {
+ EmitLine();
+ ItLineBegin = ItLineEnd;
+ }
+ }
+
+ if (ItLineBegin != ItMessageEnd)
+ {
+ EmitLine();
+ }
Dest.push_back('\n');
}
- std::unique_ptr<Formatter> Clone() const override { return std::make_unique<DefaultConsoleFormatter>(); }
+ std::unique_ptr<Formatter> Clone() const override
+ {
+ auto Copy = std::make_unique<DefaultConsoleFormatter>();
+ Copy->SetColorEnabled(IsColorEnabled());
+ return Copy;
+ }
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)
+bool
+IsColorTerminal()
{
- using namespace std::string_view_literals;
- switch (InLevel)
+ // If stdout is not a TTY, no color
+ if (ZEN_ISATTY(ZEN_FILENO(stdout)) == 0)
{
- 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;
+ return false;
}
-}
-struct AnsiColorStdoutSink::Impl
-{
- explicit Impl(ColorMode Mode) : m_Formatter(std::make_unique<DefaultConsoleFormatter>()), m_UseColor(ResolveColorMode(Mode)) {}
+ // NO_COLOR convention (https://no-color.org/)
+ if (std::getenv("NO_COLOR") != nullptr)
+ {
+ return false;
+ }
- static bool IsColorTerminal()
+ // COLORTERM is set by terminals that support color (e.g. "truecolor", "24bit")
+ if (std::getenv("COLORTERM") != nullptr)
{
- // If stdout is not a TTY, no color
- if (ZEN_ISATTY(ZEN_FILENO(stdout)) == 0)
- {
- return false;
- }
+ return true;
+ }
- // NO_COLOR convention (https://no-color.org/)
- if (std::getenv("NO_COLOR") != nullptr)
+ // Check TERM for known color-capable values
+ const char* Term = std::getenv("TERM");
+ if (Term != nullptr)
+ {
+ std::string_view TermView(Term);
+ // "dumb" terminals do not support color
+ if (TermView == "dumb")
{
return false;
}
-
- // COLORTERM is set by terminals that support color (e.g. "truecolor", "24bit")
- if (std::getenv("COLORTERM") != nullptr)
+ // Match against known color-capable terminal types.
+ // TERM often includes suffixes like "-256color", so we use substring matching.
+ constexpr std::string_view ColorTerms[] = {
+ "alacritty",
+ "ansi",
+ "color",
+ "console",
+ "cygwin",
+ "gnome",
+ "konsole",
+ "kterm",
+ "linux",
+ "msys",
+ "putty",
+ "rxvt",
+ "screen",
+ "tmux",
+ "vt100",
+ "vt102",
+ "xterm",
+ };
+ for (std::string_view Candidate : ColorTerms)
{
- return true;
- }
-
- // Check TERM for known color-capable values
- const char* Term = std::getenv("TERM");
- if (Term != nullptr)
- {
- std::string_view TermView(Term);
- // "dumb" terminals do not support color
- if (TermView == "dumb")
+ if (TermView.find(Candidate) != std::string_view::npos)
{
- return false;
- }
- // Match against known color-capable terminal types.
- // TERM often includes suffixes like "-256color", so we use substring matching.
- constexpr std::string_view ColorTerms[] = {
- "alacritty",
- "ansi",
- "color",
- "console",
- "cygwin",
- "gnome",
- "konsole",
- "kterm",
- "linux",
- "msys",
- "putty",
- "rxvt",
- "screen",
- "tmux",
- "vt100",
- "vt102",
- "xterm",
- };
- for (std::string_view Candidate : ColorTerms)
- {
- if (TermView.find(Candidate) != std::string_view::npos)
- {
- return true;
- }
+ return true;
}
}
+ }
#if defined(_WIN32)
- // Windows console supports ANSI color by default in modern versions
- return true;
+ // Windows console supports ANSI color by default in modern versions
+ return true;
#else
- // Unknown terminal — be conservative
- return false;
+ // Unknown terminal — be conservative
+ return false;
#endif
- }
+}
- static bool ResolveColorMode(ColorMode Mode)
+bool
+ResolveColorMode(ColorMode Mode)
+{
+ switch (Mode)
{
- switch (Mode)
- {
- case ColorMode::On:
- return true;
- case ColorMode::Off:
- return false;
- case ColorMode::Auto:
- default:
- return IsColorTerminal();
- }
+ case ColorMode::On:
+ return true;
+ case ColorMode::Off:
+ return false;
+ case ColorMode::Auto:
+ default:
+ return IsColorTerminal();
}
+}
- void Log(const LogMessage& Msg)
+struct AnsiColorStdoutSink::Impl
+{
+ explicit Impl(ColorMode Mode) : m_Formatter(std::make_unique<DefaultConsoleFormatter>()), m_UseColor(ResolveColorMode(Mode))
{
- std::lock_guard<std::mutex> Lock(m_Mutex);
-
- MemoryBuffer Formatted;
- m_Formatter->Format(Msg, Formatted);
+ m_Formatter->SetColorEnabled(m_UseColor);
+ }
- if (m_UseColor && Msg.ColorRangeEnd > Msg.ColorRangeStart)
- {
- // Print pre-color range
- fwrite(Formatted.data(), 1, Msg.ColorRangeStart, m_File);
+ void WriteOutput(const MemoryBuffer& Buf)
+ {
+ RwLock::ExclusiveLockScope Lock(m_Lock);
- // Print color
- std::string_view Color = GetColorForLevel(Msg.GetLevel());
- fwrite(Color.data(), 1, Color.size(), m_File);
+#if defined(_WIN32)
+ DWORD Written;
+ WriteFile(m_Handle, Buf.data(), static_cast<DWORD>(Buf.size()), &Written, nullptr);
+#else
+ fwrite(Buf.data(), 1, Buf.size(), m_File);
+#endif
- // Print colored range
- fwrite(Formatted.data() + Msg.ColorRangeStart, 1, Msg.ColorRangeEnd - Msg.ColorRangeStart, m_File);
+ m_Dirty.store(false, std::memory_order_relaxed);
+ }
- // Reset color
- fwrite(s_Reset.data(), 1, s_Reset.size(), m_File);
+ void Log(const LogMessage& Msg)
+ {
+ MemoryBuffer Formatted;
+ m_Formatter->Format(Msg, Formatted);
- // Print remainder
- fwrite(Formatted.data() + Msg.ColorRangeEnd, 1, Formatted.size() - Msg.ColorRangeEnd, m_File);
- }
- else
+ if (!m_UseColor)
{
- fwrite(Formatted.data(), 1, Formatted.size(), m_File);
+ helpers::StripAnsiSgrSequences(Formatted);
}
- fflush(m_File);
+ WriteOutput(Formatted);
}
void Flush()
{
- std::lock_guard<std::mutex> Lock(m_Mutex);
+ if (!m_Dirty.load(std::memory_order_relaxed))
+ {
+ return;
+ }
+ RwLock::ExclusiveLockScope Lock(m_Lock);
+ m_Dirty.store(false, std::memory_order_relaxed);
+#if defined(_WIN32)
+ FlushFileBuffers(m_Handle);
+#else
fflush(m_File);
+#endif
}
void SetFormatter(std::unique_ptr<Formatter> InFormatter)
{
- std::lock_guard<std::mutex> Lock(m_Mutex);
+ RwLock::ExclusiveLockScope Lock(m_Lock);
+ InFormatter->SetColorEnabled(m_UseColor);
m_Formatter = std::move(InFormatter);
}
private:
- std::mutex m_Mutex;
+ RwLock m_Lock;
std::unique_ptr<Formatter> m_Formatter;
- FILE* m_File = stdout;
- bool m_UseColor = true;
+#if defined(_WIN32)
+ HANDLE m_Handle = GetStdHandle(STD_OUTPUT_HANDLE);
+#else
+ FILE* m_File = stdout;
+#endif
+ bool m_UseColor = true;
+ std::atomic<bool> m_Dirty = false;
};
AnsiColorStdoutSink::AnsiColorStdoutSink(ColorMode Mode) : m_Impl(std::make_unique<Impl>(Mode))