4#include "gutsConventions.h"
8#include "G4UIsession.hh"
16#if defined(__clang__) || defined(__GNUC__)
17#define FUNCTION_NAME std::string(__PRETTY_FUNCTION__) + std::string(" > ")
18#elif defined(_MSC_VER)
19#define FUNCTION_NAME __FUNCSIG__ + std::string(" > ")
21#define FUNCTION_NAME __func__ + std::string(" > ")
24#define SFUNCTION_NAME __func__ + std::string(" > ")
51 explicit GLogger(
const std::shared_ptr<GOptions>& gopts,
const std::string& cname,
const std::string& lname =
"")
52 : class_name(cname), logger_name(lname), log_counter{0} {
53 verbosity_level = gopts->getVerbosityFor(logger_name);
54 debug_level = gopts->getDebugFor(logger_name);
77 template <
typename... Args>
79 if (debug_level == 0)
return;
81 std::ostringstream oss;
82 (oss << ... << std::forward<Args>(args));
86 G4cout << KCYN << header_string() <<
"DEBUG: " << oss.str() << RST << G4endl;
89 G4cout << KCYN << header_string() <<
"DEBUG: " <<
90 CONSTRUCTORLOG <<
" " << oss.str() <<
" " << CONSTRUCTORLOG << RST << G4endl;
93 G4cout << KCYN << header_string() <<
"DEBUG: " <<
94 DESTRUCTORLOG <<
" " << oss.str() <<
" " << DESTRUCTORLOG << RST << G4endl;
108 template <
typename... Args>
109 void info(
int level, Args&&... args)
const {
111 if (level != 0 && level != 1 && level != 2) {
112 G4cerr << FATALERRORL << header_string() << GWARNING <<
" Invalid verbosity level requested: " << level <<
114 exit(EC_WRONG_VERBOSITY_LEVEL);
117 if (level == 0 || (level == 1 && verbosity_level > 0) || (level == 2 && verbosity_level > 1)) {
118 std::ostringstream oss;
119 (oss << ... << std::forward<Args>(args));
120 G4cout << header_string() <<
"INFO L" << level <<
": " << oss.str() << G4endl;
130 template <
typename... Args>
131 void info(Args&&... args)
const {
info(0, std::forward<Args>(args)...); }
141 template <
typename... Args>
143 std::ostringstream oss;
144 (oss << ... << std::forward<Args>(args));
145 G4cout << KYEL << header_string() << GWARNING << KYEL << oss.str() << RST << G4endl;
157 template <
typename... Args>
158 [[noreturn]]
void error(
int exit_code, Args&&... args)
const {
159 std::ostringstream oss;
160 (oss << ... << std::forward<Args>(args));
161 G4cerr << FATALERRORL << header_string() << KRED << oss.str() << RST << G4endl;
162 G4cerr << FATALERRORL << header_string() << KRED <<
"Exit Code: " << exit_code << RST << G4endl;
163 std::exit(exit_code);
172 template <
typename... Args>
174 std::ostringstream oss;
175 (oss << ... << std::forward<Args>(args));
176 G4cout << KBOLD << header_string() << RST << oss.str() << G4endl;
183 std::string class_name;
184 std::string logger_name;
185 int verbosity_level{};
188 mutable std::atomic<int> log_counter{};
197 [[nodiscard]] std::string header_string()
const {
199 return " [ " + logger_name +
" - " + std::to_string(log_counter.load()) +
" ] ";
Handles structured logging with verbosity and debug levels.
void warning(Args &&... args) const
Logs a warning message.
GLogger(const std::shared_ptr< GOptions > &gopts, const std::string &cname, const std::string &lname="")
Constructs a GLogger instance.
void debug(debug_type type, Args &&... args) const
Logs a debug message if the debug level is nonzero.
void info(Args &&... args) const
Overloaded version of info() with the default level = 0.
std::string get_class_name() const
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.