Files
Jungfraujoch/common/Logger.h
T
leonarski_fandClaude Opus 5.5 91bfb4ae51 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
2026-10-07 12:11:21 +02:00

71 lines
2.4 KiB
C++

// SPDX-FileCopyrightText: 2024 Filip Leonarski, Paul Scherrer Institute <filip.leonarski@psi.ch>
// SPDX-License-Identifier: GPL-3.0-only
#pragma once
#include <cstdint>
#include <chrono>
#include <iostream>
#include <mutex>
#include <memory>
#include <spdlog/spdlog.h>
#include <spdlog/fmt/fmt.h>
namespace spdlog {
class logger;
}
class LogBufferSink;
class Logger {
std::shared_ptr<spdlog::logger> spdlog_logger;
std::shared_ptr<LogBufferSink> buffer; // only on a Buffered() logger
explicit Logger(std::shared_ptr<LogBufferSink> sink);
std::string service;
std::string hostname;
public:
Logger(const std::string &service_name, const std::string &file_name = "");
void ErrorException(const std::exception &e);
void Info(const std::string& msg) { spdlog_logger->info(msg); }
void Warning(const std::string& msg) { spdlog_logger->warn(msg); }
void Error(const std::string& msg) { spdlog_logger->error(msg); }
void Debug(const std::string& msg) { spdlog_logger->debug(msg); }
template<typename... Args>
void Info(fmt::format_string<Args...> fmt, Args &&... args) {
spdlog_logger->info(fmt, std::forward<Args>(args)...);
}
template<typename... Args>
void Error(fmt::format_string<Args...> fmt, Args &&... args) {
spdlog_logger->error(fmt, std::forward<Args>(args)...);
}
template<typename... Args>
void Debug(fmt::format_string<Args...> fmt, Args &&... args) {
spdlog_logger->debug(fmt, std::forward<Args>(args)...);
}
template<typename... Args>
void Warning(fmt::format_string<Args...> fmt, Args &&... args) {
spdlog_logger->warn(fmt, std::forward<Args>(args)...);
}
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.
static Logger Buffered();
void ReplayInto(Logger &to) const;
};