File indexing completed on 2026-03-28 08:19:06
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012 #pragma once
0013
0014 #include <boost/assign.hpp>
0015 #include <boost/bimap.hpp>
0016
0017 #include <stdexcept>
0018
0019 #include <spdlog/spdlog.h>
0020 #include <spdlog/fmt/fmt.h>
0021
0022 #include <Acts/Utilities/Logger.hpp>
0023
0024 namespace eicrecon {
0025
0026 using namespace Acts::Logging;
0027
0028 using SpdlogToActsLevel_t = boost::bimap<spdlog::level::level_enum, Acts::Logging::Level>;
0029 static SpdlogToActsLevel_t kSpdlogToActsLevel =
0030 boost::assign::list_of<SpdlogToActsLevel_t::relation>(
0031 spdlog::level::trace, Acts::Logging::VERBOSE)(spdlog::level::debug, Acts::Logging::DEBUG)(
0032 spdlog::level::info, Acts::Logging::INFO)(spdlog::level::warn, Acts::Logging::WARNING)(
0033 spdlog::level::err, Acts::Logging::ERROR)(spdlog::level::critical, Acts::Logging::FATAL);
0034
0035 inline Acts::Logging::Level SpdlogToActsLevel(spdlog::level::level_enum input) {
0036 try {
0037 return kSpdlogToActsLevel.left.at(input);
0038 } catch (...) {
0039 auto err_msg =
0040 fmt::format("SpdlogToActsLevel doesn't know this log level: '{}'", fmt::underlying(input));
0041 throw std::runtime_error(err_msg);
0042 }
0043 }
0044
0045 inline spdlog::level::level_enum ActsToSpdlogLevel(Acts::Logging::Level input) {
0046 try {
0047 return kSpdlogToActsLevel.right.at(input);
0048 } catch (...) {
0049 auto err_msg =
0050 fmt::format("ActsToSpdlogLevel doesn't know this log level: '{}'", fmt::underlying(input));
0051 throw std::runtime_error(err_msg);
0052 }
0053 }
0054
0055
0056
0057
0058
0059 class SpdlogPrintPolicy final : public Acts::Logging::OutputPrintPolicy {
0060 public:
0061
0062
0063
0064
0065
0066 explicit SpdlogPrintPolicy(std::shared_ptr<spdlog::logger> out) : m_out(out) {}
0067
0068
0069 ~SpdlogPrintPolicy() = default;
0070
0071
0072
0073
0074
0075 void flush(const Level& lvl, const std::string& input) final {
0076 m_out->log(ActsToSpdlogLevel(lvl), input);
0077 if (lvl >= getFailureThreshold()) {
0078 throw ThresholdFailure("Previous debug message exceeds the "
0079 "ACTS_LOG_FAILURE_THRESHOLD=" +
0080 std::string{levelName(getFailureThreshold())} +
0081 " configuration, bailing out. See "
0082 "https://acts.readthedocs.io/en/latest/core/"
0083 "logging.html#logging-thresholds");
0084 }
0085 }
0086
0087
0088
0089
0090
0091
0092
0093 const std::string& name() const override {
0094 throw std::runtime_error{
0095 "Default print policy doesn't have a name. Is there no named output in "
0096 "the decorator chain?"};
0097 };
0098
0099
0100
0101
0102 std::unique_ptr<OutputPrintPolicy> clone(const std::string& name) const override {
0103 (void)name;
0104 return std::make_unique<SpdlogPrintPolicy>(m_out);
0105 };
0106
0107 private:
0108
0109 std::shared_ptr<spdlog::logger> m_out;
0110 };
0111
0112 inline std::unique_ptr<const Acts::Logger> getSpdlogLogger(const std::string& name,
0113 std::shared_ptr<spdlog::logger> log) {
0114
0115 const Acts::Logging::Level lvl = SpdlogToActsLevel(log->level());
0116 auto output = std::make_unique<Acts::Logging::NamedOutputDecorator>(
0117 std::make_unique<SpdlogPrintPolicy>(log), name);
0118 auto print = std::make_unique<DefaultFilterPolicy>(lvl);
0119 return std::make_unique<const Acts::Logger>(std::move(output), std::move(print));
0120 }
0121
0122 }