Files
Jungfraujoch/common/Logger.cpp
T
leonarski_fandClaude Opus 5.5 48c14b6d1b broker: the journal keeps operational events; rugnux's processing log goes to it only when verbose
The live rugnux run wrote its whole processing log into the broker's journal - every
[Rugnux] line of the library (tables included) plus a "rugnux:" line per step and note -
about 300 lines per collection, all of them results the user already gets through
/rugnux/status, the end message and the written files.

- Logger::ServiceLevel(service, level): every Logger of that name made afterwards keeps only
  that level and above. The library makes its own Logger("Rugnux") (and
  Logger("ScaleOnTheFly")) wherever it logs, so this is the one switch: the broker sets them
  to error, or to debug with the existing `verbose` setting. The per-Logger "Version" line goes
  with it. The rugnux CLI routes every Logger to its own sinks (RouteAll) and never calls
  this, so its console and _log.txt are unchanged.
- The live observer feeds steps and notes to the live status only.
- The run's outcome is one line at the end: state, START -> first image and frame rate,
  done after the last image, last image -> all written. The probe result, the warm-up time
  and the cut reason go to debug (the cut is in the outcome line).
- Per-collection step chatter goes to debug: receiver init/teardown, thread start/stop,
  "Wait for receiver done" and friends, the per-image mean processing time. The JFJochBroker
  logger now follows `verbose` too, so these are available again with it.
- Kept: Start request, measurement start, receiving started/finished, receiver efficiency
  (and the per-module packet counts on loss), calibration sequence, all warnings and errors.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PXc1zD1yxHqaR1DGdCTHL4
2026-10-19 22:12:07 +02:00

119 lines
3.7 KiB
C++

// SPDX-FileCopyrightText: 2024 Filip Leonarski, Paul Scherrer Institute <filip.leonarski@psi.ch>
// SPDX-License-Identifier: GPL-3.0-only
#include "spdlog/sinks/daily_file_sink.h"
#include "spdlog/sinks/stdout_color_sinks.h"
#include "spdlog/sinks/base_sink.h"
#include "spdlog/fmt/bin_to_hex.h"
#include <map>
#include "Logger.h"
#include "GitInfo.h"
#ifdef JFJOCH_USE_NVTX
#include <nvtx3/nvToolsExt.h>
uint64_t NvtxRangeStart(const std::string &name) {
return nvtxRangeStartA(name.c_str());
}
void NvtxRangeEnd(uint64_t id) {
nvtxRangeEnd(id);
}
#else
uint64_t NvtxRangeStart(const std::string &) {
return 0;
}
void NvtxRangeEnd(uint64_t) {}
#endif
namespace {
spdlog::sink_ptr routed_sink; // set once, at start-up, before any thread logs
std::map<std::string, spdlog::level::level_enum> service_levels; // the same
}
void Logger::RouteAll(spdlog::sink_ptr sink) {
routed_sink = std::move(sink);
}
void Logger::ServiceLevel(const std::string &service, spdlog::level::level_enum level) {
service_levels[service] = level;
}
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";
#endif
#ifdef JFJOCH_USE_FFTW
variant += "F";
#endif
std::vector<spdlog::sink_ptr> sinks;
sinks.push_back(std::make_shared<spdlog::sinks::stdout_color_sink_mt>());
if (!file_name.empty())
sinks.push_back(std::make_shared<spdlog::sinks::daily_file_sink_mt>(file_name, 23, 59));
spdlog_logger = std::make_shared<spdlog::logger>(service_name, std::begin(sinks), std::end(sinks));
if (const auto it = service_levels.find(service_name); it != service_levels.end())
spdlog_logger->set_level(it->second);
if (jfjoch_git_sha1().empty())
spdlog_logger->info("Version {} {}", jfjoch_version(), variant);
else
spdlog_logger->info("Version {} (git {} {}) {}",
jfjoch_version(),
jfjoch_git_sha1().substr(0, 6),
jfjoch_git_date(),
variant);
}
class LogBufferSink : public spdlog::sinks::base_sink<std::mutex> {
public:
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.push_back({msg.time, msg.level, std::string(msg.payload.data(), msg.payload.size())});
}
void flush_() override {}
};
Logger::Logger(std::shared_ptr<LogBufferSink> sink) : buffer(std::move(sink)) {
spdlog_logger = std::make_shared<spdlog::logger>("buffer", buffer);
spdlog_logger->set_level(spdlog::level::trace); // the logger it is replayed into filters
}
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. 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 &l : buffer->lines)
to.spdlog_logger->log(l.time, spdlog::source_loc{}, l.level, l.text);
}
void Logger::ErrorException(const std::exception &e) {
spdlog_logger->error(e.what());
}
Logger &Logger::Verbose(bool input) {
if (input)
spdlog_logger->set_level(spdlog::level::debug);
else
spdlog_logger->set_level(spdlog::level::info);
return *this;
}