48#if !defined(_WIN32) && !defined(_WIN64)
49 #if defined(__STDC_HOSTED__) && (__STDC_HOSTED__ == 1)
63#if !defined(_WIN32) && !defined(_WIN64)
64 #if defined(__STDC_HOSTED__) && (__STDC_HOSTED__ == 1)
65 #if defined(__unix__) || defined(__unix) || defined(__APPLE__) \
67 #ifndef _POSIX_C_SOURCE
68 #define _POSIX_C_SOURCE 200809UL
89 #define LOG_LEVEL LOG_LEVEL_DEBUG
146#if defined(__has_builtin)
147 #if __has_builtin(__builtin_fprintf)
148 #define LOG_PRINTF(...) __builtin_fprintf(__VA_ARGS__)
150 #define LOG_PRINTF(...) fprintf(__VA_ARGS__)
153 #if defined(__GNUC__)
154 #define LOG_PRINTF(...) __builtin_fprintf(__VA_ARGS__)
156 #define LOG_PRINTF(...) fprintf(__VA_ARGS__)
169#if defined(__has_builtin)
170 #if __has_builtin(__builtin_vfprintf)
171 #define LOG_VPRINTF(...) __builtin_vfprintf(__VA_ARGS__)
173 #define LOG_VPRINTF(...) vfprintf(__VA_ARGS__)
176 #if defined(__GNUC__)
177 #define LOG_VPRINTF(...) __builtin_vfprintf(__VA_ARGS__)
179 #define LOG_VPRINTF(...) vfprintf(__VA_ARGS__)
187#define PREFIX_ERROR "[ERROR]"
188#define PREFIX_WARNING "[WARNING]"
189#define PREFIX_INFO "[INFO]"
190#define PREFIX_DEBUG "[DEBUG]"
197#define COLOR_RED "\033[0;31m"
198#define COLOR_YELLOW "\033[0;33m"
199#define COLOR_BLUE "\033[0;34m"
200#define COLOR_GREEN "\033[0;32m"
201#define COLOR_RESET "\033[0m"
212 #if defined(__has_attribute)
213 #if __has_attribute(format)
214 #define ATTR_PRINTF(fmt_idx, var_idx) \
215 __attribute__((format(printf, fmt_idx, var_idx)))
217 #define ATTR_PRINTF(fmt_idx, var_idx)
219 #elif defined(__GNUC__)
220 #define ATTR_PRINTF(fmt_idx, var_idx) \
221 __attribute__((format(printf, fmt_idx, var_idx)))
223 #define ATTR_PRINTF(fmt_idx, var_idx)
313 int ret = EXIT_SUCCESS;
315 FILE *out = (FILE *)NULL;
317 static pthread_mutex_t log_mutex = PTHREAD_MUTEX_INITIALIZER;
321 struct tm *ptm = (
struct tm *)NULL;
330 goto function_output;
333 pthread_mutex_lock(&log_mutex);
335 memset(&ts, 0,
sizeof(ts));
337 clock_gettime(CLOCK_REALTIME, &ts);
340 memset(&tm_buf, 0,
sizeof(tm_buf));
342#if defined(_WIN32) || defined(_WIN64)
343 if (localtime_s(&tm_buf, &now) != 0)
344 memset(&tm_buf, 0,
sizeof(tm_buf));
346 if (localtime_r(&now, &tm_buf) == NULL)
347 memset(&tm_buf, 0,
sizeof(tm_buf));
353 msec = ts.tv_nsec / 1000000L;
356 "[%02d:%02d:%02d.%03ld] ",
362 if (isatty(fileno(out)))
372 LOG_PRINTF(out,
" (at %s:%d:%s())\n", file, line, func);
373 pthread_mutex_unlock(&log_mutex);
387#define LOG_ERROR(...) \
388 LOG_output(LOG_LEVEL_ERROR, \
400#define LOG_WARNING(...) \
401 LOG_output(LOG_LEVEL_WARNING, \
413#define LOG_INFO(...) \
414 LOG_output(LOG_LEVEL_INFO, \
426#define LOG_DEBUG(...) \
427 LOG_output(LOG_LEVEL_DEBUG, \
#define LOG_LEVEL
Logging verbosity threshold for this module.
Definition logs.h:89
#define COLOR_RESET
Definition logs.h:198
#define ATTR_PRINTF(fmt_idx, var_idx)
Macro to apply printf-style format checking on custom functions when supported by the compiler.
Definition logs.h:220
#define LOG_PRINTF(...)
Compiler-specific printf abstraction (variadic).
Definition logs.h:153
log_level_t
Defines log levels for the logging system.
Definition logs.h:123
static int LOG_output(log_level_t level, const char *color, const char *prefix, const char *file, const char *func, int line, const char *fmt,...)
Internal logging implementation: threadâsafe, prints timestamp, optional ANSI color,...
Definition logs.h:300
#define LOG_VPRINTF(...)
Compiler-specific vprintf abstraction.
Definition logs.h:176
@ LOG_LEVEL_DEBUG
Definition logs.h:128
@ LOG_LEVEL_ERROR
Definition logs.h:125
@ LOG_LEVEL_WARNING
Definition logs.h:126
@ LOG_LEVEL_NONE
Definition logs.h:124
@ LOG_LEVEL_INFO
Definition logs.h:127