5 #include "gutsConventions.h"
9 #include "G4UIsession.hh"
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);
64 template <
typename... Args>
66 if (debug_level == 0)
return;
68 std::ostringstream oss;
69 (oss << ... << std::forward<Args>(args));
73 G4cout << KMAG << header_string() << oss.str() << RST << G4endl;
76 G4cout << KCYN << header_string() << CONSTRUCTORLOG <<
" constructor " << CONSTRUCTORLOG <<
" " << RST <<
80 G4cout << KCYN << header_string() << DESTRUCTORLOG <<
" destructor " << DESTRUCTORLOG <<
" " << RST << oss.
95 template <
typename... Args>
96 void info(
int level, Args&&... args)
const {
98 if (level != 0 && level != 1 && level != 2) {
99 G4cerr << FATALERRORL << header_string() << GWARNING <<
" Invalid verbosity level requested: " << level <<
101 exit(EC_WRONG_VERBOSITY_LEVEL);
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;
117 template <
typename... Args>
118 void info(Args&&... args)
const {
info(0, std::forward<Args>(args)...); }
128 template <
typename... Args>
130 std::ostringstream oss;
131 (oss << ... << std::forward<Args>(args));
132 G4cout << KYEL << header_string() << GWARNING << oss.str() << RST << G4endl;
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);
159 template <
typename... Args>
161 std::ostringstream oss;
162 (oss << ... << std::forward<Args>(args));
163 G4cout << KBOLD << header_string() << RST << oss.str() << G4endl;
168 int verbosity_level{};
171 mutable std::atomic<int> log_counter{};
180 [[nodiscard]] std::string header_string()
const {
182 return name +
" [" + 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)
Constructs a GLogger instance.
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.