Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-03-28 08:19:06

0001 // SPDX-License-Identifier: MPL-2.0
0002 //
0003 // Copyright (C) 2016-2018 CERN for the benefit of the Acts project
0004 //
0005 // This Source Code Form is subject to the terms of the Mozilla Public
0006 // License, v. 2.0. If a copy of the MPL was not distributed with this
0007 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
0008 //
0009 // Based on code from Acts at Core/include/Acts/Utilities/Logger.hpp
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 /// @brief default print policy for debug messages
0056 ///
0057 /// This class allows to print debug messages without further modifications to
0058 /// a specified output stream.
0059 class SpdlogPrintPolicy final : public Acts::Logging::OutputPrintPolicy {
0060 public:
0061   /// @brief constructor
0062   ///
0063   /// @param [in] out pointer to output stream object
0064   ///
0065   /// @pre @p out is non-zero
0066   explicit SpdlogPrintPolicy(std::shared_ptr<spdlog::logger> out) : m_out(out) {}
0067 
0068   /// @brief destructor
0069   ~SpdlogPrintPolicy() = default;
0070 
0071   /// @brief flush the debug message to the destination stream
0072   ///
0073   /// @param [in] lvl   debug level of debug message
0074   /// @param [in] input text of debug message
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   /// Fulfill @c OutputPrintPolicy interface. This policy doesn't actually have a
0088   /// name, so the assumption is that somewhere in the decorator hierarchy,
0089   /// there is something that returns a name without delegating to a wrappee,
0090   /// before reaching this overload.
0091   /// @note This method will throw an exception
0092   /// @return the name, but it never returns
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   /// Make a copy of this print policy with a new name
0100   /// @param name the new name
0101   /// @return the copy
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   /// pointer to destination output stream
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 } // namespace eicrecon