File indexing completed on 2025-01-18 09:11:31
0001
0002
0003
0004
0005
0006
0007
0008
0009 #include "Acts/Utilities/Logger.hpp"
0010
0011 #include <algorithm>
0012 #include <cstdlib>
0013
0014 namespace Acts {
0015
0016 namespace Logging {
0017
0018 #if defined(ACTS_ENABLE_LOG_FAILURE_THRESHOLD) and \
0019 not defined(ACTS_LOG_FAILURE_THRESHOLD)
0020 namespace {
0021 Level& getFailureThresholdMutable() {
0022 static Level _level = []() {
0023 Level level = Level::MAX;
0024
0025 const char* envvar = std::getenv("ACTS_LOG_FAILURE_THRESHOLD");
0026 if (envvar == nullptr) {
0027 return level;
0028 }
0029
0030 std::string slevel = envvar;
0031 if (slevel == "VERBOSE") {
0032 level = std::min(level, Level::VERBOSE);
0033 } else if (slevel == "DEBUG") {
0034 level = std::min(level, Level::DEBUG);
0035 } else if (slevel == "INFO") {
0036 level = std::min(level, Level::INFO);
0037 } else if (slevel == "WARNING") {
0038 level = std::min(level, Level::WARNING);
0039 } else if (slevel == "ERROR") {
0040 level = std::min(level, Level::ERROR);
0041 } else if (slevel == "FATAL") {
0042 level = std::min(level, Level::FATAL);
0043 } else {
0044 std::cerr << "ACTS_LOG_FAILURE_THRESHOLD environment variable is set to "
0045 "unknown value: "
0046 << slevel << std::endl;
0047 }
0048 return level;
0049 }();
0050
0051 return _level;
0052 }
0053 }
0054
0055 Level getFailureThreshold() {
0056 return getFailureThresholdMutable();
0057 }
0058
0059 void setFailureThreshold(Level level) {
0060 getFailureThresholdMutable() = level;
0061 }
0062
0063 ScopedFailureThreshold::~ScopedFailureThreshold() noexcept {
0064 try {
0065 setFailureThreshold(m_previousLevel);
0066 } catch (const std::bad_alloc&) {
0067
0068 std::cerr << "Failed to reset log failure threshold (bad_alloc)"
0069 << std::endl;
0070 std::terminate();
0071 }
0072 }
0073
0074 #else
0075
0076 void setFailureThreshold(Level ) {
0077 throw std::logic_error{
0078 "Compile-time log failure threshold defined (ACTS_LOG_FAILURE_THRESHOLD "
0079 "is set or ACTS_ENABLE_LOG_FAILURE_THRESHOLD is OFF), unable to "
0080 "override. See "
0081 "https://acts.readthedocs.io/en/latest/core/misc/"
0082 "logging.html#logging-thresholds"};
0083 }
0084
0085 ScopedFailureThreshold::~ScopedFailureThreshold() noexcept = default;
0086
0087 #endif
0088
0089 namespace {
0090 class NeverFilterPolicy final : public OutputFilterPolicy {
0091 public:
0092 ~NeverFilterPolicy() override = default;
0093
0094 bool doPrint(const Level& ) const override { return false; }
0095
0096 Level level() const override { return Level::MAX; }
0097
0098 std::unique_ptr<OutputFilterPolicy> clone(Level ) const override {
0099 return std::make_unique<NeverFilterPolicy>();
0100 }
0101 };
0102
0103 class DummyPrintPolicy final : public OutputPrintPolicy {
0104 public:
0105 void flush(const Level& , const std::string& ) override {}
0106
0107 const std::string& name() const override {
0108 const static std::string s_name = "Dummy";
0109 return s_name;
0110 }
0111
0112 std::unique_ptr<OutputPrintPolicy> clone(
0113 const std::string& ) const override {
0114 return std::make_unique<DummyPrintPolicy>();
0115 }
0116 };
0117
0118 std::unique_ptr<const Logger> makeDummyLogger() {
0119 using namespace Logging;
0120 auto output = std::make_unique<DummyPrintPolicy>();
0121 auto print = std::make_unique<NeverFilterPolicy>();
0122 return std::make_unique<const Logger>(std::move(output), std::move(print));
0123 }
0124
0125 }
0126 }
0127
0128 std::unique_ptr<const Logger> getDefaultLogger(const std::string& name,
0129 const Logging::Level& lvl,
0130 std::ostream* log_stream) {
0131 using namespace Logging;
0132 auto output = std::make_unique<LevelOutputDecorator>(
0133 std::make_unique<NamedOutputDecorator>(
0134 std::make_unique<TimedOutputDecorator>(
0135 std::make_unique<DefaultPrintPolicy>(log_stream)),
0136 name));
0137 auto print = std::make_unique<DefaultFilterPolicy>(lvl);
0138 return std::make_unique<const Logger>(std::move(output), std::move(print));
0139 }
0140
0141 const Logger& getDummyLogger() {
0142 static const std::unique_ptr<const Logger> logger =
0143 Logging::makeDummyLogger();
0144
0145 return *logger;
0146 }
0147 }