glogging
glogger.h
Go to the documentation of this file.
1 #ifndef GLOGGER_H
2 #define GLOGGER_H
3 
4 // Color conventions and logging macros
5 #include "gutsConventions.h"
6 #include "goptions.h"
7 
8 // Geant4
9 #include "G4UIsession.hh"
10 #include <atomic>
11 #include <string>
12 #include <sstream>
13 #include <utility>
14 
22 
31 class GLogger {
32 public:
38  explicit GLogger(GOptions* gopts, std::string vname)
39  : name(vname), log_counter{0} {
40  verbosity_level = gopts->getVerbosityFor(name);
41  debug_level = gopts->getDebugFor(name);
42  debug(CONSTRUCTOR, name, " logger");
43  }
44 
45  // default constructor
46  GLogger() = default;
47 
48  ~GLogger() { debug(DESTRUCTOR, name, " logger"); }
49 
64  template <typename... Args>
65  void debug(debug_type type, Args&&... args) const {
66  if (debug_level == 0) return;
67 
68  std::ostringstream oss;
69  (oss << ... << std::forward<Args>(args));
70 
71  switch (type) {
72  case NORMAL:
73  G4cout << KMAG << header_string() << oss.str() << RST << G4endl;
74  break;
75  case CONSTRUCTOR:
76  G4cout << KCYN << header_string() << CONSTRUCTORLOG << " constructor " << CONSTRUCTORLOG << " " << RST <<
77  oss.str() << G4endl;
78  break;
79  case DESTRUCTOR:
80  G4cout << KCYN << header_string() << DESTRUCTORLOG << " destructor " << DESTRUCTORLOG << " " << RST << oss.
81  str() << G4endl;
82  break;
83  }
84  }
85 
95  template <typename... Args>
96  void info(int level, Args&&... args) const {
97  // error if level is not 0, 1 or 2
98  if (level != 0 && level != 1 && level != 2) {
99  G4cerr << FATALERRORL << header_string() << GWARNING << " Invalid verbosity level requested: " << level <<
100  RST << G4endl;
101  exit(EC_WRONG_VERBOSITY_LEVEL);
102  }
103 
104  if (level == 0 || (level == 1 && verbosity_level > 0) || (level == 2 && verbosity_level > 1)) {
105  std::ostringstream oss;
106  (oss << ... << std::forward<Args>(args));
107  G4cout << header_string() << oss.str() << G4endl;
108  }
109  }
110 
117  template <typename... Args>
118  void info(Args&&... args) const { info(0, std::forward<Args>(args)...); }
119 
128  template <typename... Args>
129  void warning(Args&&... args) const {
130  std::ostringstream oss;
131  (oss << ... << std::forward<Args>(args));
132  G4cout << KYEL << header_string() << GWARNING << oss.str() << RST << G4endl;
133  }
134 
144  template <typename... Args>
145  [[noreturn]] void error(int exit_code, Args&&... args) const {
146  std::ostringstream oss;
147  (oss << ... << std::forward<Args>(args));
148  G4cerr << FATALERRORL << header_string() << KRED << oss.str() << RST << G4endl;
149  G4cerr << FATALERRORL << header_string() << KRED << "Exit Code: " << exit_code << RST << G4endl;
150  std::exit(exit_code);
151  }
152 
159  template <typename... Args>
160  void critical(Args&&... args) const {
161  std::ostringstream oss;
162  (oss << ... << std::forward<Args>(args));
163  G4cout << KBOLD << header_string() << RST << oss.str() << G4endl;
164  }
165 
166 private:
167  std::string name;
168  int verbosity_level{};
169  int debug_level{};
170 
171  mutable std::atomic<int> log_counter{};
172 
180  [[nodiscard]] std::string header_string() const {
181  log_counter++;
182  return name + " [" + std::to_string(log_counter.load()) + "] ";
183  }
184 };
185 
186 #endif // GLOGGER_H
Handles structured logging with verbosity and debug levels.
Definition: glogger.h:31
void warning(Args &&... args) const
Logs a warning message.
Definition: glogger.h:129
void debug(debug_type type, Args &&... args) const
Logs a debug message if the debug level is nonzero.
Definition: glogger.h:65
GLogger(GOptions *gopts, std::string vname)
Constructs a GLogger instance.
Definition: glogger.h:38
GLogger()=default
void info(Args &&... args) const
Overloaded version of info() with default level = 0.
Definition: glogger.h:118
~GLogger()
Definition: glogger.h:48
void critical(Args &&... args) const
Logs a critical message. Always printed.
Definition: glogger.h:160
void info(int level, Args &&... args) const
Logs an info message, conditionally based on verbosity level.
Definition: glogger.h:96
void error(int exit_code, Args &&... args) const
Logs an error message and exits the application.
Definition: glogger.h:145
debug_type
Enumerates debug message types.
Definition: glogger.h:21
@ CONSTRUCTOR
Definition: glogger.h:21
@ NORMAL
Definition: glogger.h:21
@ DESTRUCTOR
Definition: glogger.h:21