4 #include "gutsConventions.h"
8 #include "G4UIsession.hh"
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);
63 template <
typename... Args>
65 if (debug_level == 0)
return;
67 std::ostringstream oss;
68 (oss << ... << std::forward<Args>(args));
72 G4cout << KCYN << header_string() <<
"DEBUG " << oss.str() << RST << G4endl;
75 G4cout << KCYN << header_string() <<
"DEBUG " <<
76 CONSTRUCTORLOG <<
" " << oss.str() <<
" " << CONSTRUCTORLOG << RST << G4endl;
79 G4cout << KCYN << header_string() <<
"DEBUG " <<
80 DESTRUCTORLOG <<
" " << oss.str() <<
" " << DESTRUCTORLOG << RST << G4endl;
94 template <
typename... Args>
95 void info(
int level, Args&&... args)
const {
97 if (level != 0 && level != 1 && level != 2) {
98 G4cerr << FATALERRORL << header_string() << GWARNING <<
" Invalid verbosity level requested: " << level <<
100 exit(EC_WRONG_VERBOSITY_LEVEL);
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;
116 template <
typename... Args>
117 void info(Args&&... args)
const {
info(0, std::forward<Args>(args)...); }
127 template <
typename... Args>
129 std::ostringstream oss;
130 (oss << ... << std::forward<Args>(args));
131 G4cout << KYEL << header_string() << GWARNING << oss.str() << RST << G4endl;
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);
158 template <
typename... Args>
160 std::ostringstream oss;
161 (oss << ... << std::forward<Args>(args));
162 G4cout << KBOLD << header_string() << RST << oss.str() << G4endl;
169 std::string verbosity_name;
170 std::string calling_class;
171 int verbosity_level{};
174 mutable std::atomic<int> log_counter{};
183 [[nodiscard]] std::string header_string()
const {
185 return calling_class +
" [" + std::to_string(log_counter.load()) +
"]: ";
Handles structured logging with verbosity and debug levels.
void warning(Args &&... args) const
Logs a warning message.
void debug(debug_type type, Args &&... args) const
Logs a debug message if the debug level is nonzero.
GLogger(GOptions *gopts, std::string vname, std::string cc)
Constructs a GLogger instance.
std::string get_verbosity_name() const
void info(Args &&... args) const
Overloaded version of info() with default level = 0.
void critical(Args &&... args) const
Logs a critical message. Always printed.
void info(int level, Args &&... args) const
Logs an info message, conditionally based on verbosity level.
void error(int exit_code, Args &&... args) const
Logs an error message and exits the application.
debug_type
Enumerates debug message types.