glogging
Loading...
Searching...
No Matches
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
14
15// cross-platform function to return the function name
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(" > ")
20#else
21#define FUNCTION_NAME __func__ + std::string(" > ") // fallback
22#endif
23
24#define SFUNCTION_NAME __func__ + std::string(" > ") // Always portable and standard since C++11
25
26
34
43class GLogger {
44public:
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);
55 debug(CONSTRUCTOR, logger_name, " logger");
56 }
57
58 // default constructor
59 GLogger() = default;
60
61 ~GLogger() { debug(DESTRUCTOR, logger_name, " logger"); }
62
77 template <typename... Args>
78 void debug(debug_type type, Args&&... args) const {
79 if (debug_level == 0) return;
80
81 std::ostringstream oss;
82 (oss << ... << std::forward<Args>(args));
83
84 switch (type) {
85 case NORMAL:
86 G4cout << KCYN << header_string() << "DEBUG: " << oss.str() << RST << G4endl;
87 break;
88 case CONSTRUCTOR:
89 G4cout << KCYN << header_string() << "DEBUG: " <<
90 CONSTRUCTORLOG << " " << oss.str() << " " << CONSTRUCTORLOG << RST << G4endl;
91 break;
92 case DESTRUCTOR:
93 G4cout << KCYN << header_string() << "DEBUG: " <<
94 DESTRUCTORLOG << " " << oss.str() << " " << DESTRUCTORLOG << RST << G4endl;
95 break;
96 }
97 }
98
108 template <typename... Args>
109 void info(int level, Args&&... args) const {
110 // error if the level is not 0, 1 or 2
111 if (level != 0 && level != 1 && level != 2) {
112 G4cerr << FATALERRORL << header_string() << GWARNING << " Invalid verbosity level requested: " << level <<
113 RST << G4endl;
114 exit(EC_WRONG_VERBOSITY_LEVEL);
115 }
116
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;
121 }
122 }
123
130 template <typename... Args>
131 void info(Args&&... args) const { info(0, std::forward<Args>(args)...); }
132
141 template <typename... Args>
142 void warning(Args&&... args) const {
143 std::ostringstream oss;
144 (oss << ... << std::forward<Args>(args));
145 G4cout << KYEL << header_string() << GWARNING << KYEL << oss.str() << RST << G4endl;
146 }
147
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);
164 }
165
172 template <typename... Args>
173 void critical(Args&&... args) const {
174 std::ostringstream oss;
175 (oss << ... << std::forward<Args>(args));
176 G4cout << KBOLD << header_string() << RST << oss.str() << G4endl;
177 }
178
179
180 [[nodiscard]] std::string get_class_name() const { return class_name; }
181
182private:
183 std::string class_name;
184 std::string logger_name;
185 int verbosity_level{};
186 int debug_level{};
187
188 mutable std::atomic<int> log_counter{};
189
197 [[nodiscard]] std::string header_string() const {
198 ++log_counter;
199 return " [ " + logger_name + " - " + std::to_string(log_counter.load()) + " ] ";
200 }
201};
Handles structured logging with verbosity and debug levels.
Definition glogger.h:43
void warning(Args &&... args) const
Logs a warning message.
Definition glogger.h:142
GLogger(const std::shared_ptr< GOptions > &gopts, const std::string &cname, const std::string &lname="")
Constructs a GLogger instance.
Definition glogger.h:51
void debug(debug_type type, Args &&... args) const
Logs a debug message if the debug level is nonzero.
Definition glogger.h:78
GLogger()=default
void info(Args &&... args) const
Overloaded version of info() with the default level = 0.
Definition glogger.h:131
std::string get_class_name() const
Definition glogger.h:180
~GLogger()
Definition glogger.h:61
void critical(Args &&... args) const
Logs a critical message. Always printed.
Definition glogger.h:173
void info(int level, Args &&... args) const
Logs an info message, conditionally based on verbosity level.
Definition glogger.h:109
void error(int exit_code, Args &&... args) const
Logs an error message and exits the application.
Definition glogger.h:158
debug_type
Enumerates debug message types.
Definition glogger.h:33
@ CONSTRUCTOR
Definition glogger.h:33
@ NORMAL
Definition glogger.h:33
@ DESTRUCTOR
Definition glogger.h:33