Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-10 08:19:00

0001 // This file is part of the ACTS project.
0002 //
0003 // Copyright (C) 2016 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 https://mozilla.org/MPL/2.0/.
0008 
0009 #include "Acts/Utilities/Logger.hpp"
0010 
0011 #include <algorithm>
0012 #include <cstdlib>
0013 
0014 namespace Acts {
0015 
0016 namespace Logging {
0017 
0018 #ifdef ACTS_ENABLE_LOG_FAILURE_THRESHOLD
0019 namespace {
0020 Level& getFailureThresholdMutable() {
0021   static Level _level = []() {
0022     Level level = Level::MAX;
0023 
0024     const char* envvar = std::getenv("ACTS_LOG_FAILURE_THRESHOLD");
0025     if (envvar == nullptr) {
0026       return level;
0027     }
0028 
0029     std::string slevel = envvar;
0030     if (slevel == "VERBOSE") {
0031       level = std::min(level, Level::VERBOSE);
0032     } else if (slevel == "DEBUG") {
0033       level = std::min(level, Level::DEBUG);
0034     } else if (slevel == "INFO") {
0035       level = std::min(level, Level::INFO);
0036     } else if (slevel == "WARNING") {
0037       level = std::min(level, Level::WARNING);
0038     } else if (slevel == "ERROR") {
0039       level = std::min(level, Level::ERROR);
0040     } else if (slevel == "FATAL") {
0041       level = std::min(level, Level::FATAL);
0042     } else {
0043       std::cerr << "ACTS_LOG_FAILURE_THRESHOLD environment variable is set to "
0044                    "unknown value: "
0045                 << slevel << std::endl;
0046     }
0047     return level;
0048   }();
0049 
0050   return _level;
0051 }
0052 }  // namespace
0053 
0054 Level getFailureThreshold() {
0055   return getFailureThresholdMutable();
0056 }
0057 
0058 void setFailureThreshold(Level level) {
0059   getFailureThresholdMutable() = level;
0060 }
0061 
0062 ScopedFailureThreshold::~ScopedFailureThreshold() noexcept {
0063   try {
0064     setFailureThreshold(m_previousLevel);
0065   } catch (const std::bad_alloc&) {
0066     // bad alloc can be thrown when initializing the global static variable
0067     std::cerr << "Failed to reset log failure threshold (bad_alloc)"
0068               << std::endl;
0069     std::terminate();
0070   }
0071 }
0072 
0073 #else
0074 
0075 void setFailureThreshold(Level /*lvl*/) {
0076   throw std::logic_error{
0077       "The log failure threshold is not compiled in "
0078       "(ACTS_ENABLE_LOG_FAILURE_THRESHOLD is OFF), unable to override. See "
0079       "https://cern.ch/acts-log-thresh"};
0080 }
0081 
0082 ScopedFailureThreshold::~ScopedFailureThreshold() noexcept = default;
0083 
0084 #endif
0085 
0086 namespace {
0087 class NeverFilterPolicy final : public OutputFilterPolicy {
0088  public:
0089   ~NeverFilterPolicy() override = default;
0090 
0091   bool doPrint(const Level& /*lvl*/) const override { return false; }
0092 
0093   Level level() const override { return Level::MAX; }
0094 
0095   std::unique_ptr<OutputFilterPolicy> clone(Level /*level*/) const override {
0096     return std::make_unique<NeverFilterPolicy>();
0097   }
0098 };
0099 
0100 class DummyPrintPolicy final : public OutputPrintPolicy {
0101  public:
0102   void flush(const Level& /*lvl*/, const std::string& /*input*/) override {}
0103 
0104   const std::string& name() const override {
0105     const static std::string s_name = "Dummy";
0106     return s_name;
0107   }
0108 
0109   std::unique_ptr<OutputPrintPolicy> clone(
0110       const std::string& /*name*/) const override {
0111     return std::make_unique<DummyPrintPolicy>();
0112   }
0113 };
0114 
0115 std::unique_ptr<const Logger> makeDummyLogger() {
0116   using namespace Logging;
0117   auto output = std::make_unique<DummyPrintPolicy>();
0118   auto print = std::make_unique<NeverFilterPolicy>();
0119   return std::make_unique<const Logger>(std::move(output), std::move(print));
0120 }
0121 
0122 }  // namespace
0123 }  // namespace Logging
0124 
0125 std::unique_ptr<const Logger> getDefaultLogger(const std::string& name,
0126                                                const Logging::Level& lvl,
0127                                                std::ostream* log_stream) {
0128   using namespace Logging;
0129   auto output = std::make_unique<LevelOutputDecorator>(
0130       std::make_unique<NamedOutputDecorator>(
0131           std::make_unique<TimedOutputDecorator>(
0132               std::make_unique<DefaultPrintPolicy>(log_stream)),
0133           name));
0134   auto print = std::make_unique<DefaultFilterPolicy>(lvl);
0135   return std::make_unique<const Logger>(std::move(output), std::move(print));
0136 }
0137 
0138 const Logger& getDummyLogger() {
0139   static const std::unique_ptr<const Logger> logger =
0140       Logging::makeDummyLogger();
0141 
0142   return *logger;
0143 }
0144 }  // namespace Acts