ComPWA
Common Partial-Wave-Analysis Framework
Logging.cpp
Go to the documentation of this file.
1 // Copyright (c) 2015, 2017 The ComPWA Team.
2 // This file is part of the ComPWA framework, check
3 // https://github.com/ComPWA/ComPWA/license.txt for details.
4 
5 #include <array>
6 #include <chrono>
7 #include <ctime>
8 
9 #include "Core/Logging.hpp"
10 
11 INITIALIZE_EASYLOGGINGPP
12 
13 namespace ComPWA {
14 
15 Logging::Logging(std::string level, std::string filename) {
16  el::Configurations DefaultConfig;
17  // initialize with default values
18  DefaultConfig.setToDefault();
19  DefaultConfig.setGlobally(el::ConfigurationType::Format,
20  "%datetime [%level] %msg");
21  if (filename.empty()) {
22  DefaultConfig.setGlobally(el::ConfigurationType::ToFile, "0");
23  LOG(INFO) << "Logging to file disabled!";
24  } else {
25  DefaultConfig.setGlobally(el::ConfigurationType::Filename, filename);
26  DefaultConfig.setGlobally(el::ConfigurationType::ToFile, "1");
27  LOG(INFO) << "Log file: " << filename;
28  }
29  // reconfigure the default logger
30  el::Loggers::reconfigureLogger("default", DefaultConfig);
31 
32  setLogLevel(level);
33 
34  LOG(INFO) << "Log level: " << level;
35 
36  // Print local time and date at the beginning
37  auto time =
38  std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
39 
40  LOG(INFO) << "Current date and time: " << std::ctime(&time);
41 };
42 
46 void enableDisableLvl(el::Logger *logger, std::array<std::string, 5> levels) {
47  logger->configurations()->set(el::Level::Trace,
48  el::ConfigurationType::Enabled, levels.at(0));
49  logger->configurations()->set(el::Level::Debug,
50  el::ConfigurationType::Enabled, levels.at(1));
51  logger->configurations()->set(el::Level::Info, el::ConfigurationType::Enabled,
52  levels.at(2));
53  logger->configurations()->set(el::Level::Warning,
54  el::ConfigurationType::Enabled, levels.at(3));
55  logger->configurations()->set(el::Level::Error,
56  el::ConfigurationType::Enabled, levels.at(4));
57 }
58 
59 void Logging::setLogLevel(std::string level) {
60  // Capitalize string
61  std::transform(level.begin(), level.end(), level.begin(), ::toupper);
62 
63  Level = level;
64 
65  el::Logger *logger =
66  ELPP->registeredLoggers()->get(el::base::consts::kDefaultLoggerId);
67 
68  // Normally use the hierarchy mode of easyloggingcpp, e.g.
69  // el::Loggers::addFlag(el::LoggingFlag::HierarchicalLogging);
70  // el::Loggers::setLoggingLevel(el::Level::Fatal);
71  // However, the hierarchy of the easyloggingcpp log levels is currently not
72  // convenient and has to be manually reordered to:
73  // TRACE, DEBUG, INFO, WARNING, ERROR, FATAL
74 
75  if (level == "TRACE")
76  enableDisableLvl(logger, {"1", "1", "1", "1", "1"});
77  else if (level == "DEBUG")
78  enableDisableLvl(logger, {"0", "1", "1", "1", "1"});
79  else if (level == "INFO")
80  enableDisableLvl(logger, {"0", "0", "1", "1", "1"});
81  else if (level == "WARNING")
82  enableDisableLvl(logger, {"0", "0", "0", "1", "1"});
83  else if (level == "ERROR")
84  enableDisableLvl(logger, {"0", "0", "0", "0", "1"});
85  else if (level == "FATAL")
86  enableDisableLvl(logger, {"0", "0", "0", "0", "0"});
87  else {
88  enableDisableLvl(logger, {"0", "0", "1", "1", "1"});
89  Level = "INFO";
90  LOG(WARNING)
91  << "Logging::setLogLevel() | Unknown log level " + level +
92  ". Available levels are: \"TRACE\", \"DEBUG\", \"INFO\", "
93  "\"WARNING\", \"ERROR\", \"FATAL\". Setting log level "
94  "to [INFO]!";
95  }
96  logger->reconfigure();
97 };
98 
99 } // namespace ComPWA
void enableDisableLvl(el::Logger *logger, std::array< std::string, 5 > levels)
Enable or disable levels TRACE, DEBUG, INFO, WARNING, ERROR, FATAL.
Definition: Logging.cpp:46
std::string Level
Definition: Logging.hpp:25
void setLogLevel(std::string level)
Definition: Logging.cpp:59
Logging(std::string level="INFO", std::string filename="")
Definition: Logging.cpp:15