#pragma once #ifdef __cplusplus extern "C" { #endif // __cplusplus #include /** * The verbosity level of a log event. */ enum log_Level { Trace, //!< Designates very low priority, often extremely verbose, information. Debug, //!< Designates lower priority information. Info, //!< Designates useful information. Warn, //!< Designates hazardous situations. Error, //!< Designates very serious errors. }; /** * The type of a log event function. * * @param level The verbosity level of the event. * @param msg The string message to be logged. */ typedef void (*log_LogFn)(enum log_Level level, const char *msg); /** * Set the global event logging function. * * @param fn_ptr A pointer to the event logging function. If `NULL`, the global logger is unset. */ void log_set(log_LogFn fn_ptr); /** * The inner logging function, this should not be called directly, instead the macros defined in the same header * should be used.. * * @param level The verbosity level of the event. * @param msg The string message to be logged. */ void inner_log(enum log_Level level, const char *msg); #define LOG_MSG_SIZE(message, ...) sizeof(message) #define INNER_LOG(level, ...) { char tmp[LOG_MSG_SIZE(__VA_ARGS__) + 256]; snprintf(tmp, sizeof(tmp), __VA_ARGS__); inner_log(level, tmp); }(0) #define LOG_TRACE(...) INNER_LOG(Trace, __VA_ARGS__) #define LOG_DEBUG(...) INNER_LOG(Debug, __VA_ARGS__) #define LOG_INFO(...) INNER_LOG(Info, __VA_ARGS__) #define LOG_WARN(...) INNER_LOG(Warn, __VA_ARGS__) #define LOG_ERROR(...) INNER_LOG(Error, __VA_ARGS__) #ifdef __cplusplus } #endif // __cplusplus