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