32 #ifndef HERMES_LOG_LOGGING_H
33 #define HERMES_LOG_LOGGING_H
79 template<
typename ...Ts>
83 #if defined(__CUDA_ARCH__) && __CUDA_ARCH__ > 0
87 message_options = options_ | message_options;
89 u8 color, label_color;
90 configFrom(message_options, color, label_color);
95 s += label(message_options);
99 processPath(message_options, abbreviate(message_options,
location.file_name)),
location.line,
100 abbreviate(message_options,
location.function_name));
111 printf(
"%s\n", s.
c_str());
119 template<
typename ...Ts>
121 #if defined(__CUDA_ARCH__) && __CUDA_ARCH__ > 0
125 message_options = options_ | message_options;
127 u8 color, label_color;
128 configFrom(message_options, color, label_color);
133 s += label(message_options);
144 printf(
"%s\n", s.
c_str());
151 template<
typename ...Ts>
153 #if (defined(__CUDA_ARCH__) && (__CUDA_ARCH__ > 0))
154 printf(fmt, std::forward<Ts>(args)...);
156 auto message_options = options_ | logging_options::info;
161 s += label(message_options);
170 printf(
"%s\n", s.
c_str());
177 template<
typename ...Ts>
179 #if (defined(__CUDA_ARCH__) && (__CUDA_ARCH__ > 0))
180 printf(fmt, std::forward<Ts>(args)...);
182 auto message_options = options_ | logging_options::warn;
187 s += label(message_options);
196 printf(
"%s\n", s.
c_str());
203 template<
typename ...Ts>
205 #if (defined(__CUDA_ARCH__) && (__CUDA_ARCH__ > 0))
206 printf(fmt, std::forward<Ts>(args)...);
208 auto message_options = options_ | logging_options::error;
213 s += label(message_options);
222 printf(
"%s\n", s.
c_str());
229 template<
typename ...Ts>
231 #if (defined(__CUDA_ARCH__) && (__CUDA_ARCH__ > 0))
232 printf(
"[%s][%d][%s] CRITICAL\n", std::forward<Ts>(args)...);
234 auto message_options = options_ | logging_options::critical;
239 s += label(message_options);
248 printf(
"%s\n", s.
c_str());
254 options_ = options_ | options_to_add;
259 options_ = options_ & ~options_to_remove;
283 const std::chrono::time_point<std::chrono::system_clock> now =
284 std::chrono::system_clock::now();
285 const std::time_t t_c = std::chrono::system_clock::to_time_t(now);
287 s += std::put_time(std::localtime(&t_c),
"[%F %T]");
311 }
else if (
HERMES_MASK_BIT(message_options, logging_options::critical)) {
317 static inline Str abbreviate(logging_options message_options,
const char *str) {
320 size_t l = std::strlen(str);
330 static inline Str processPath(logging_options options,
const hermes::Path &path) {
352 #define HERMES_PING hermes::Log::info("[{}][{}][{}]", __FILE__, __LINE__, __FUNCTION__);
363 #define HERMES_LOG(FMT, ...) hermes::Log::logMessage(hermes::logging_options::info, FMT, \
364 hermes::Log::Location{__FILE__, __LINE__, __FUNCTION__} __VA_OPT__(,) __VA_ARGS__)
373 #ifndef HERMES_LOG_WARNING
374 #define HERMES_LOG_WARNING(FMT, ...) hermes::Log::logMessage(hermes::logging_options::warn, FMT, \
375 hermes::Log::Location{__FILE__, __LINE__, __FUNCTION__} __VA_OPT__(,) __VA_ARGS__)
384 #ifndef HERMES_LOG_ERROR
385 #define HERMES_LOG_ERROR(FMT, ...) hermes::Log::logMessage(hermes::logging_options::error, FMT, \
386 hermes::Log::Location{__FILE__, __LINE__, __FUNCTION__} __VA_OPT__(,) __VA_ARGS__)
395 #ifndef HERMES_LOG_CRITICAL
396 #define HERMES_LOG_CRITICAL(FMT, ...) hermes::Log::logMessage(hermes::logging_options::critical, FMT, \
397 hermes::Log::Location{__FILE__, __LINE__, __FUNCTION__} __VA_OPT__(,) __VA_ARGS__)
400 #ifndef HERMES_LOG_VARIABLE
404 #define HERMES_LOG_VARIABLE(A) hermes::Log::logMessage(hermes::logging_options::info, "{} = {}", \
405 hermes::Log::Location{__FILE__, __LINE__, __FUNCTION__}, #A, A)
408 #ifndef HERMES_LOG_ARRAY
412 #define HERMES_LOG_ARRAY(A) \
413 HERMES_LOG("values of \"{}\":", #A); \
414 for(const auto& hermes_log_array_element : A) \
415 HERMES_LOG(" {}", hermes_log_array_element)
422 static inline void hermes_log_variables_r(std::stringstream &s,
const T &first) {
431 template<
typename T,
typename ...Args>
432 static inline void hermes_log_variables_r(std::stringstream &s,
const T &first, Args &&...rest) {
434 if constexpr(
sizeof ...(rest) > 0)
435 hermes_log_variables_r(s, std::forward<Args>(rest) ...);
441 template<class... Args>
442 static inline std::
string hermes_log_variables(Args &&... args) {
444 if constexpr(
sizeof...(args) > 0) {
445 hermes_log_variables_r(s, std::forward<Args>(args) ...);
451 #ifndef HERMES_LOG_VARIABLES
455 #define HERMES_LOG_VARIABLES(...) \
456 hermes::Log::info("[{}][{}][{}]: {}", __FILE__, __LINE__, __FUNCTION__, hermes_log_variables(__VA_ARGS__))
467 #define HERMES_C_LOG(FMT, ...) \
468 fprintf(stdout, "[%s][%d][%s]: ", __FILE__, __LINE__, __FUNCTION__); \
469 fprintf(stdout, FMT __VA_OPT__(,) __VA_ARGS__); \
470 fprintf(stdout, "\n")
472 #ifndef HERMES_C_LOG_ERROR
480 #define HERMES_C_LOG_ERROR(FMT, ...) \
481 fprintf(stderr, "[%s][%d][%s]: ", __FILE__, __LINE__, __FUNCTION__); \
482 fprintf(stderr, FMT __VA_OPT__(,) __VA_ARGS__); \
483 fprintf(stderr, "\n")
485 #ifndef HERMES_C_DEVICE_LOG
493 #define HERMES_C_DEVICE_LOG(FMT, ...) \
494 printf("[%s][%d][%s]: ", __FILE__, __LINE__, __FUNCTION__); \
495 printf(FMT __VA_OPT__(,) __VA_ARGS__); \
498 #ifndef HERMES_C_DEVICE_ERROR
506 #define HERMES_C_DEVICE_ERROR(FMT, ...) \
507 printf("[%s][%d][%s]: ", __FILE__, __LINE__, __FUNCTION__); \
508 printf(FMT __VA_OPT__(,) __VA_ARGS__); \
516 #define HERMES_LOG_VARIABLE
Support of bitwise operations for compatible enum classes.
#define HERMES_MASK_BIT(MASK, BIT)
Tests if enum class value is enabled.
Definition: bitmask_operators.h:84
#define HERMES_ENABLE_BITMASK_OPERATORS(x)
Adds bitwise operation support to a given enum class.
Definition: bitmask_operators.h:58
static std::string color(u8 color_number)
Get 88/256 color code.
Definition: console_colors.h:99
static char reset[5]
"\e[0m"
Definition: console_colors.h:53
Static class that manages logging messages.
Definition: logging.h:64
static u8 info_color
info stream messages color
Definition: logging.h:262
static void removeOptions(logging_options options_to_remove)
Disables logging options.
Definition: logging.h:258
static HERMES_DEVICE_CALLABLE void critical(const std::string &fmt, Ts &&...args)
Logs into critical stream.
Definition: logging.h:230
static HERMES_DEVICE_CALLABLE void warn(const char *fmt, Ts &&...args)
Logs into warn stream.
Definition: logging.h:178
static std::function< void(const Str &)> info_callback
info stream redirection callback
Definition: logging.h:275
static size_t abbreviation_size
size after abbreviation (in characters)
Definition: logging.h:272
static u8 critical_color
critical stream messages color
Definition: logging.h:265
static u8 critical_label_color
critical stream label color
Definition: logging.h:270
static HERMES_DEVICE_CALLABLE void logMessage(logging_options message_options, const char *fmt, Location location, Ts &&...args)
Logs a formatted message with code location information.
Definition: logging.h:80
static u8 warn_label_color
warn stream label color
Definition: logging.h:268
static u8 warn_color
warn stream messages color
Definition: logging.h:263
static HERMES_DEVICE_CALLABLE void error(const char *fmt, Ts &&...args)
Logs into error stream.
Definition: logging.h:204
static HERMES_DEVICE_CALLABLE void logMessage(logging_options message_options, const char *fmt, Ts &&...args)
Logs a formatted message.
Definition: logging.h:120
static u8 error_label_color
error stream label color
Definition: logging.h:269
static u8 info_label_color
info stream label color
Definition: logging.h:267
static std::function< void(const Str &)> critical_callback
critical stream redirection callback
Definition: logging.h:278
static u8 error_color
error stream messages color
Definition: logging.h:264
static HERMES_DEVICE_CALLABLE void info(const char *fmt, Ts &&...args)
Logs into info stream.
Definition: logging.h:152
static std::function< void(const Str &)> warn_callback
warn stream redirection callback
Definition: logging.h:276
static std::function< void(const Str &, logging_options)> log_callback
redirection callback
Definition: logging.h:274
static std::function< void(const Str &)> error_callback
error stream redirection callback
Definition: logging.h:277
static void addOptions(logging_options options_to_add)
Enables logging options.
Definition: logging.h:253
Representation of a directory/file in the filesystem.
Definition: file_system.h:48
const std::string & fullName() const
Gets this full path string.
Definition: file_system.cpp:143
std::string name() const
Gets last folder/file name.
Definition: file_system.cpp:139
String class and set of string functions.
Definition: str.h:53
const char * c_str() const
Get const char* pointer.
Definition: str.h:384
static std::string format(const std::string &fmt, Ts &&... args)
Definition: str.h:170
Set of 256-terminal supported color codes.
#define HERMES_DEVICE_CALLABLE
Specifies that the function can be called from both host and device sides.
Definition: defs.h:45
uint8_t u8
8 bit size unsigned integer type
Definition: defs.h:86
logging_options
Options for logging output.
Definition: logging.h:47
@ callback_only
redirect output to callback only
@ time
logs message time point
@ warn
logs into warning stream
@ full_path_location
output full path locations
@ critical
logs into critical stream
@ abbreviate
abbreviate long paths
@ info
logs into info stream
@ error
logs into error stream
@ location
logs code location
@ use_colors
output colored messages
Holds information about log code location.
Definition: logging.h:67
int line
file line number
Definition: logging.h:69
const char * function_name
scope name
Definition: logging.h:70
const char * file_name
file path
Definition: logging.h:68