Logger: route every logger to one sink; buffered lines keep their own time

Logger::RouteAll lets a program that owns its terminal send every Logger made after the call
to a single sink, at debug level, without the version line each one opened with. rugnux uses it
to write the whole log to a file and show only warnings and errors among its own step lines.

A Buffered logger now replays each line with the time it was logged at rather than the time of
the replay, so a timestamped log shows when the work happened (the P1 cross-check merge runs
beside the analyses and its lines used to carry the replay time).

HDF5DataFile's destructor reports a failed close through the logger instead of std::cerr.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SVmAWnzCmRKAXVUCdc4iNi
This commit is contained in:
leonarski_fandClaude Opus 5.5 committed 2026-10-07 12:11:21 +02:00
1 parent 5282dd6199
commit 91bfb4ae51
3 files changed
+34 -7

No files matched your search

+25 -5
View File
@@ -9,7 +9,20 @@
#include "Logger.h"
#include "GitInfo.h"
namespace {
spdlog::sink_ptr routed_sink; // set once, at start-up, before any thread logs
}
void Logger::RouteAll(spdlog::sink_ptr sink) {
routed_sink = std::move(sink);
}
Logger::Logger(const std::string &service_name, const std::string &file_name) {
if (routed_sink) {
spdlog_logger = std::make_shared<spdlog::logger>(service_name, routed_sink);
spdlog_logger->set_level(spdlog::level::debug);
return;
}
std::string variant = "";
#ifdef JFJOCH_USE_CUDA
variant += "C";
@@ -36,10 +49,15 @@ Logger::Logger(const std::string &service_name, const std::string &file_name) {
class LogBufferSink : public spdlog::sinks::base_sink<std::mutex> {
public:
std::vector<std::pair<spdlog::level::level_enum, std::string>> lines;
struct Line {
spdlog::log_clock::time_point time;
spdlog::level::level_enum level;
std::string text;
};
std::vector<Line> lines;
protected:
void sink_it_(const spdlog::details::log_msg &msg) override {
lines.emplace_back(msg.level, std::string(msg.payload.data(), msg.payload.size()));
lines.push_back({msg.time, msg.level, std::string(msg.payload.data(), msg.payload.size())});
}
void flush_() override {}
};
@@ -53,10 +71,12 @@ Logger Logger::Buffered() {
return Logger(std::make_shared<LogBufferSink>());
}
// Called once the work that logged is finished, so nothing is still writing to the buffer.
// Called once the work that logged is finished, so nothing is still writing to the buffer. Each line
// keeps the time it was logged at, not the time of the replay, so a timestamped log still shows when
// the work happened.
void Logger::ReplayInto(Logger &to) const {
for (const auto &[level, line] : buffer->lines)
to.spdlog_logger->log(level, line);
for (const auto &l : buffer->lines)
to.spdlog_logger->log(l.time, spdlog::source_loc{}, l.level, l.text);
}
void Logger::ErrorException(const std::exception &e) {
+6
View File
@@ -56,6 +56,12 @@ public:
Logger& Verbose(bool input);
// Every Logger made after this call writes to `sink` alone instead of the console, keeps every
// level down to debug (the sink decides what it shows), and skips the version line each Logger
// otherwise opens with. For a program that owns its terminal: rugnux sends the whole log to a file
// next to its outputs and shows only warnings and errors among its own step lines.
static void RouteAll(spdlog::sink_ptr sink);
// A logger that keeps its lines instead of printing them, for work run beside other work that
// logs: ReplayInto hands them to `to` afterwards, in order and as one block, so the two do not
// interleave.
+3 -2
View File
@@ -18,6 +18,7 @@
#include "../include/spdlog/fmt/fmt.h"
#include "HDF5NXmx.h"
#include "../common/time_utc.h"
#include "../common/Logger.h"
HDF5DataFile::HDF5DataFile(const StartMessage &msg, uint64_t file_number, const std::string &filename) :
filename(filename),
@@ -123,9 +124,9 @@ HDF5DataFile::~HDF5DataFile() {
std::filesystem::remove(tmp_filename, ec);
}
} catch (const std::exception &e) {
std::cerr << "HDF5DataFile::~HDF5DataFile: " << e.what() << std::endl;
Logger("HDF5DataFile").Error("Closing the data file failed: {}", e.what());
} catch (...) {
std::cerr << "HDF5DataFile::~HDF5DataFile: Unknown error " << std::endl;
Logger("HDF5DataFile").Error("Closing the data file failed with an unknown error");
}
}
}