Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Logger.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file Logger.cpp
1 // This file is part of the Acts project.
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 
10 
11 #include <algorithm>
12 #include <cstdlib>
13 
14 namespace Acts {
15 
16 namespace Logging {
17 
18 #if defined(ACTS_ENABLE_LOG_FAILURE_THRESHOLD) and \
19  not defined(ACTS_LOG_FAILURE_THRESHOLD)
20 namespace {
21 Level& getFailureThresholdMutable() {
22  static Level _level = []() {
24 
25  const char* envvar = std::getenv("ACTS_LOG_FAILURE_THRESHOLD");
26  if (envvar == nullptr) {
27  return level;
28  }
29  std::string slevel = envvar;
30  if (slevel == "VERBOSE") {
31  level = std::min(level, Level::VERBOSE);
32  } else if (slevel == "DEBUG") {
33  level = std::min(level, Level::DEBUG);
34  } else if (slevel == "INFO") {
35  level = std::min(level, Level::INFO);
36  } else if (slevel == "WARNING") {
37  level = std::min(level, Level::WARNING);
38  } else if (slevel == "ERROR") {
39  level = std::min(level, Level::ERROR);
40  } else if (slevel == "FATAL") {
41  level = std::min(level, Level::FATAL);
42  } else {
43  std::cerr << "ACTS_LOG_FAILURE_THRESHOLD environment variable is set to "
44  "unknown value: "
45  << slevel << std::endl;
46  }
47  return level;
48  }();
49 
50  return _level;
51 }
52 } // namespace
53 
55  return getFailureThresholdMutable();
56 }
57 
58 void setFailureThreshold(Level level) {
59  getFailureThresholdMutable() = level;
60 }
61 
62 #else
63 
64 void setFailureThreshold(Level /*lvl*/) {
65  throw std::logic_error{
66  "Compile-time log failure threshold defined (ACTS_LOG_FAILURE_THRESHOLD "
67  "is set or ACTS_ENABLE_LOG_FAILURE_THRESHOLD is OFF), unable to "
68  "override. See "
69  "https://acts.readthedocs.io/en/latest/core/"
70  "logging.html#logging-thresholds"};
71 }
72 
73 #endif
74 
75 namespace {
76 class NeverFilterPolicy final : public OutputFilterPolicy {
77  public:
78  ~NeverFilterPolicy() override = default;
79 
80  bool doPrint(const Level& /*lvl*/) const override { return false; }
81 
82  Level level() const override { return Level::MAX; }
83 
84  std::unique_ptr<OutputFilterPolicy> clone(Level /*level*/) const override {
85  return std::make_unique<NeverFilterPolicy>();
86  }
87 };
88 
89 std::unique_ptr<const Logger> makeDummyLogger() {
90  using namespace Logging;
91  auto output = std::make_unique<DefaultPrintPolicy>(&std::cout);
92  auto print = std::make_unique<NeverFilterPolicy>();
93  return std::make_unique<const Logger>(std::move(output), std::move(print));
94 }
95 
96 } // namespace
97 } // namespace Logging
98 
99 std::unique_ptr<const Logger> getDefaultLogger(const std::string& name,
100  const Logging::Level& lvl,
101  std::ostream* log_stream) {
102  using namespace Logging;
103  auto output = std::make_unique<LevelOutputDecorator>(
104  std::make_unique<NamedOutputDecorator>(
105  std::make_unique<TimedOutputDecorator>(
106  std::make_unique<DefaultPrintPolicy>(log_stream)),
107  name));
108  auto print = std::make_unique<DefaultFilterPolicy>(lvl);
109  return std::make_unique<const Logger>(std::move(output), std::move(print));
110 }
111 
113  static const std::unique_ptr<const Logger> logger =
114  Logging::makeDummyLogger();
115 
116  return *logger;
117 }
118 } // namespace Acts