EICrecon
JANA based reconstruction for the EPIC detector
Loading...
Searching...
No Matches
SpdlogToActs.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MPL-2.0
2//
3// Copyright (C) 2016-2018 CERN for the benefit of the Acts project
4//
5// This Source Code Form is subject to the terms of the Mozilla Public
6// License, v. 2.0. If a copy of the MPL was not distributed with this
7// file, You can obtain one at http://mozilla.org/MPL/2.0/.
8//
9// Based on code from Acts at Core/include/Acts/Utilities/Logger.hpp
10//
11
12#pragma once
13
14#include <boost/assign.hpp>
15#include <boost/bimap.hpp>
16
17#include <stdexcept>
18
19#include <spdlog/spdlog.h>
20#include <spdlog/fmt/fmt.h>
21
22#include <Acts/Utilities/Logger.hpp>
23
24namespace eicrecon {
25
26using namespace Acts::Logging;
27
28using SpdlogToActsLevel_t = boost::bimap<spdlog::level::level_enum, Acts::Logging::Level>;
29static SpdlogToActsLevel_t kSpdlogToActsLevel =
30 boost::assign::list_of<SpdlogToActsLevel_t::relation>(
31 spdlog::level::trace, Acts::Logging::VERBOSE)(spdlog::level::debug, Acts::Logging::DEBUG)(
32 spdlog::level::info, Acts::Logging::INFO)(spdlog::level::warn, Acts::Logging::WARNING)(
33 spdlog::level::err, Acts::Logging::ERROR)(spdlog::level::critical, Acts::Logging::FATAL);
34
35inline Acts::Logging::Level SpdlogToActsLevel(spdlog::level::level_enum input) {
36 try {
37 return kSpdlogToActsLevel.left.at(input);
38 } catch (...) {
39 auto err_msg =
40 fmt::format("SpdlogToActsLevel doesn't know this log level: '{}'", fmt::underlying(input));
41 throw std::runtime_error(err_msg);
42 }
43}
44
45inline spdlog::level::level_enum ActsToSpdlogLevel(Acts::Logging::Level input) {
46 try {
47 return kSpdlogToActsLevel.right.at(input);
48 } catch (...) {
49 auto err_msg =
50 fmt::format("ActsToSpdlogLevel doesn't know this log level: '{}'", fmt::underlying(input));
51 throw std::runtime_error(err_msg);
52 }
53}
54
59class SpdlogPrintPolicy final : public Acts::Logging::OutputPrintPolicy {
60public:
66 explicit SpdlogPrintPolicy(std::shared_ptr<spdlog::logger> out) : m_out(out) {}
67
69 ~SpdlogPrintPolicy() = default;
70
75 void flush(const Level& lvl, const std::string& input) final {
76 m_out->log(ActsToSpdlogLevel(lvl), input);
77 if (lvl >= getFailureThreshold()) {
78 throw ThresholdFailure("Previous debug message exceeds the "
79 "ACTS_LOG_FAILURE_THRESHOLD=" +
80 std::string{levelName(getFailureThreshold())} +
81 " configuration, bailing out. See "
82 "https://acts.readthedocs.io/en/latest/core/"
83 "logging.html#logging-thresholds");
84 }
85 }
86
93 const std::string& name() const override {
94 throw std::runtime_error{
95 "Default print policy doesn't have a name. Is there no named output in "
96 "the decorator chain?"};
97 };
98
102 std::unique_ptr<OutputPrintPolicy> clone(const std::string& name) const override {
103 (void)name;
104 return std::make_unique<SpdlogPrintPolicy>(m_out);
105 };
106
107private:
109 std::shared_ptr<spdlog::logger> m_out;
110};
111
112inline std::unique_ptr<const Acts::Logger> getSpdlogLogger(const std::string& name,
113 std::shared_ptr<spdlog::logger> log) {
114
115 const Acts::Logging::Level lvl = SpdlogToActsLevel(log->level());
116 auto output = std::make_unique<Acts::Logging::NamedOutputDecorator>(
117 std::make_unique<SpdlogPrintPolicy>(log), name);
118 auto print = std::make_unique<DefaultFilterPolicy>(lvl);
119 return std::make_unique<const Acts::Logger>(std::move(output), std::move(print));
120}
121
122} // namespace eicrecon
default print policy for debug messages
Definition SpdlogToActs.h:59
SpdlogPrintPolicy(std::shared_ptr< spdlog::logger > out)
constructor
Definition SpdlogToActs.h:66
std::unique_ptr< OutputPrintPolicy > clone(const std::string &name) const override
Definition SpdlogToActs.h:102
~SpdlogPrintPolicy()=default
destructor
const std::string & name() const override
Definition SpdlogToActs.h:93
void flush(const Level &lvl, const std::string &input) final
flush the debug message to the destination stream
Definition SpdlogToActs.h:75
-client
Definition CalorimeterClusterRecoCoG.cc:37
spdlog::level::level_enum ActsToSpdlogLevel(Acts::Logging::Level input)
Definition SpdlogToActs.h:45
boost::bimap< spdlog::level::level_enum, Acts::Logging::Level > SpdlogToActsLevel_t
Definition SpdlogToActs.h:28
std::unique_ptr< const Acts::Logger > getSpdlogLogger(const std::string &name, std::shared_ptr< spdlog::logger > log)
Definition SpdlogToActs.h:112
Acts::Logging::Level SpdlogToActsLevel(spdlog::level::level_enum input)
Definition SpdlogToActs.h:35