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 <gemc/guts/gutsConventions.h>
5#include <gemc/goptions/goptions.h>
7
8// Geant4
9#include "G4UIsession.hh"
10#include <array>
11#include <atomic>
12#include <stdexcept>
13#include <string>
14#include <string_view>
15#include <sstream>
16#include <type_traits>
17#include <utility>
18
19
32#if defined(__clang__) || defined(__GNUC__)
33#define FUNCTION_NAME std::string(__PRETTY_FUNCTION__) + std::string(" > ")
34#elif defined(_MSC_VER)
35#define FUNCTION_NAME std::string(__FUNCSIG__) + std::string(" > ")
36#else
37#define FUNCTION_NAME std::string(__func__) + std::string(" > ")
38#endif
39
51#define SFUNCTION_NAME std::string(__func__) + std::string(" > ")
52
66
92{
93public:
110 explicit GLogger(const std::shared_ptr<GOptions>& gopts, const std::string& cname,
111 const std::string& lname = "")
112 : class_name(cname), logger_name(lname), log_counter{0}, catalog(GMessageCatalog::load(gopts.get())) {
113 // Resolve the runtime policy for this subsystem name from the global options.
114 verbosity_level = gopts->getVerbosityFor(logger_name);
115 debug_level = gopts->getDebugFor(logger_name);
116
117 // Optional lifetime trace to help identify logger ownership and construction order.
118 debug(CONSTRUCTOR, "%1 logger", logger_name);
119 }
120
130 GLogger() : catalog(GMessageCatalog::load()) {}
131
138 ~GLogger();
139
153 template <typename... Args>
154 void debug(debug_type type, std::string_view message, Args&&... args) const {
155 if (debug_level == 0) return; // Debug disabled for this logger instance.
156 const auto text = format_message(message, std::forward<Args>(args)...);
157
158 switch (type) {
159 case NORMAL:
160 G4cout << guts::KCYN << header_string() << "DEBUG: " << text << guts::RST << G4endl;
161 break;
162 case CONSTRUCTOR:
163 G4cout << guts::KCYN << header_string() << "DEBUG: " <<
164 guts::CONSTRUCTORLOG << " " << text << " " << guts::CONSTRUCTORLOG << guts::RST << G4endl;
165 break;
166 case DESTRUCTOR:
167 G4cout << guts::KCYN << header_string() << "DEBUG: " <<
168 guts::DESTRUCTORLOG << " " << text << " " << guts::DESTRUCTORLOG << guts::RST << G4endl;
169 break;
170 }
171 }
172
201 template <typename... Args>
202 void info(int level, std::string_view message, Args&&... args) const {
203 if (info_enabled(level)) {
204 const auto text = format_message(message, std::forward<Args>(args)...);
205 G4cout << header_string() << "INFO L" << level << ": " << text << G4endl;
206 }
207 }
208
219 template <typename Message, typename... Args>
220 requires std::is_convertible_v<Message, std::string_view>
221 void info(Message&& message, Args&&... args) const {
222 info(0, std::string_view(std::forward<Message>(message)), std::forward<Args>(args)...);
223 }
224
237 template <typename... Args>
238 void warning(std::string_view message, Args&&... args) const {
239 const auto text = format_message(message, std::forward<Args>(args)...);
240 G4cout << guts::KYEL << header_string() << guts::GWARNING << guts::KYEL << text
241 << guts::RST << G4endl;
242 }
243
260 template <typename... Args>
261 [[noreturn]] void error(int exit_code, std::string_view message, Args&&... args) const {
262 const auto text = format_message(message, std::forward<Args>(args)...);
263 G4cerr << guts::FATALERRORL << header_string() << guts::KRED << text << guts::RST << G4endl;
264 G4cerr << guts::FATALERRORL << header_string() << guts::KRED << "Exit Code: " << exit_code << guts::RST
265 << G4endl;
266 std::exit(exit_code);
267 }
268
279 template <typename... Args>
280 void critical(std::string_view message, Args&&... args) const {
281 const auto text = format_message(message, std::forward<Args>(args)...);
282 G4cout << guts::KBOLD << header_string() << guts::RST << text << G4endl;
283 }
284
291 template <typename... Args>
292 void infoTemplate(int level, std::string_view message, Args&&... args) const {
293 info(level, message, std::forward<Args>(args)...);
294 }
295
297 template <typename Message, typename... Args>
298 requires std::is_convertible_v<Message, std::string_view>
299 void infoTemplate(Message&& message, Args&&... args) const {
300 info(0, std::string_view(std::forward<Message>(message)), std::forward<Args>(args)...);
301 }
302
304 template <typename... Args>
305 void debugTemplate(debug_type type, std::string_view message, Args&&... args) const {
306 debug(type, message, std::forward<Args>(args)...);
307 }
308
310 template <typename... Args>
311 void warningTemplate(std::string_view message, Args&&... args) const {
312 warning(message, std::forward<Args>(args)...);
313 }
314
316 template <typename... Args>
317 [[noreturn]] void errorTemplate(int exit_code, std::string_view message, Args&&... args) const {
318 error(exit_code, message, std::forward<Args>(args)...);
319 }
320
322 template <typename... Args>
323 void criticalTemplate(std::string_view message, Args&&... args) const {
324 critical(message, std::forward<Args>(args)...);
325 }
326
335 [[nodiscard]] std::string get_class_name() const { return class_name; }
336
338 [[nodiscard]] const std::string& get_language() const { return catalog->language(); }
339
340private:
341 std::string class_name;
342 std::string logger_name;
344 int verbosity_level{};
345 int debug_level{};
346
347 mutable std::atomic<int> log_counter{};
348 std::shared_ptr<const GMessageCatalog> catalog;
349
351 [[nodiscard]] bool info_enabled(int level) const {
352 if (level != 0 && level != 1 && level != 2) {
353 G4cerr << guts::FATALERRORL << header_string() << guts::GWARNING
354 << " Invalid verbosity level requested: " << level << guts::RST << G4endl;
356 }
357 return level == 0 || (level == 1 && verbosity_level > 0) || (level == 2 && verbosity_level > 1);
358 }
359
361 template <typename... Args>
362 [[nodiscard]] std::string format_message(std::string_view message, Args&&... args) const {
363 return catalog->format(message, std::forward<Args>(args)...);
364 }
365
377 [[nodiscard]] std::string header_string() const {
378 ++log_counter; // Increment first so the first emitted message is "1" rather than "0".
379 return " [ " + logger_name + " - " + std::to_string(log_counter.load()) + " ] ";
380 }
381};
void info(int level, std::string_view message, Args &&... args) const
Logs a complete message template, optionally gated by verbosity level.
Definition glogger.h:202
void warningTemplate(std::string_view message, Args &&... args) const
Transitional alias for warning().
Definition glogger.h:311
GLogger(const std::shared_ptr< GOptions > &gopts, const std::string &cname, const std::string &lname="")
Constructs a GLogger instance and resolves its runtime configuration.
Definition glogger.h:110
void info(Message &&message, Args &&... args) const
Convenience overload of info() that defaults to level 0.
Definition glogger.h:221
void criticalTemplate(std::string_view message, Args &&... args) const
Transitional alias for critical().
Definition glogger.h:323
void debug(debug_type type, std::string_view message, Args &&... args) const
Logs a debug message if the debug level is nonzero.
Definition glogger.h:154
void errorTemplate(int exit_code, std::string_view message, Args &&... args) const
Transitional alias for error().
Definition glogger.h:317
void critical(std::string_view message, Args &&... args) const
Logs a critical message.
Definition glogger.h:280
void infoTemplate(int level, std::string_view message, Args &&... args) const
Transitional alias for info(), with identical template and filtering semantics.
Definition glogger.h:292
void error(int exit_code, std::string_view message, Args &&... args) const
Logs an error message and terminates the process.
Definition glogger.h:261
GLogger()
Default constructor.
Definition glogger.h:130
std::string get_class_name() const
Returns the caller-provided class name associated with this logger instance.
Definition glogger.h:335
void infoTemplate(Message &&message, Args &&... args) const
Logs a message template at info level 0; see infoTemplate(int, std::string_view, Args&&....
Definition glogger.h:299
void warning(std::string_view message, Args &&... args) const
Logs a warning message.
Definition glogger.h:238
~GLogger()
Destructor.
Definition glogger.cc:3
void debugTemplate(debug_type type, std::string_view message, Args &&... args) const
Transitional alias for debug().
Definition glogger.h:305
const std::string & get_language() const
Normalized language selected from options, environment, or system settings.
Definition glogger.h:338
Immutable, cached translations of complete English message templates.
debug_type
Classifies debug messages by intent.
Definition glogger.h:65
@ CONSTRUCTOR
Definition glogger.h:65
@ NORMAL
Definition glogger.h:65
@ DESTRUCTOR
Definition glogger.h:65
constexpr int EC_WRONG_VERBOSITY_LEVEL
constexpr char RST[]
constexpr char FATALERRORL[]
constexpr char GWARNING[]
constexpr char DESTRUCTORLOG[]
constexpr char CONSTRUCTORLOG[]
constexpr char KCYN[]
constexpr char KRED[]
constexpr char KYEL[]
constexpr char KBOLD[]