83 #define LOG_LEVEL LOG_LEVEL_DEBUG
93#ifndef _POSIX_C_SOURCE
94 #define _POSIX_C_SOURCE 200809UL
106#if defined(__has_builtin)
107 #if __has_builtin(__builtin_fprintf)
108 #define LOG_PRINTF(...) __builtin_fprintf(__VA_ARGS__)
110 #define LOG_PRINTF(...) fprintf(__VA_ARGS__)
113 #if defined(__GNUC__)
114 #define LOG_PRINTF(...) __builtin_fprintf(__VA_ARGS__)
116 #define LOG_PRINTF(...) fprintf(__VA_ARGS__)
129#if defined(__has_builtin)
130 #if __has_builtin(__builtin_vfprintf)
131 #define LOG_VPRINTF(...) __builtin_vfprintf(__VA_ARGS__)
133 #define LOG_VPRINTF(...) vfprintf(__VA_ARGS__)
136 #if defined(__GNUC__)
137 #define LOG_VPRINTF(...) __builtin_vfprintf(__VA_ARGS__)
139 #define LOG_VPRINTF(...) vfprintf(__VA_ARGS__)
147#define PREFIX_ERROR "[ERROR]"
148#define PREFIX_WARNING "[WARNING]"
149#define PREFIX_INFO "[INFO]"
150#define PREFIX_DEBUG "[DEBUG]"
157#define COLOR_RED "\033[0;31m"
158#define COLOR_YELLOW "\033[0;33m"
159#define COLOR_BLUE "\033[0;34m"
160#define COLOR_GREEN "\033[0;32m"
161#define COLOR_RESET "\033[0m"
172 #if defined(__has_attribute)
173 #if __has_attribute(format)
174 #define ATTR_PRINTF(fmt_idx, var_idx) \
175 __attribute__((format(printf, fmt_idx, var_idx)))
177 #define ATTR_PRINTF(fmt_idx, var_idx)
179 #elif defined(__GNUC__)
180 #define ATTR_PRINTF(fmt_idx, var_idx) \
181 __attribute__((format(printf, fmt_idx, var_idx)))
183 #define ATTR_PRINTF(fmt_idx, var_idx)
273 int ret = EXIT_SUCCESS;
275 FILE *out = (FILE *)NULL;
277 static pthread_mutex_t log_mutex = PTHREAD_MUTEX_INITIALIZER;
280 struct tm *ptm = (
struct tm *)NULL;
289 goto function_output;
292 pthread_mutex_lock(&log_mutex);
294 memset(&ts, 0,
sizeof(ts));
296 clock_gettime(CLOCK_REALTIME, &ts);
299 ptm = localtime(&now);
302 msec = ts.tv_nsec / 1000000L;
305 "[%02d:%02d:%02d.%03ld] ",
311 if (isatty(fileno(out)))
321 LOG_PRINTF(out,
" (at %s:%d:%s())\n", file, line, func);
322 pthread_mutex_unlock(&log_mutex);
336#define LOG_ERROR(...) \
337 LOG_output(LOG_LEVEL_ERROR, \
349#define LOG_WARNING(...) \
350 LOG_output(LOG_LEVEL_WARNING, \
362#define LOG_INFO(...) \
363 LOG_output(LOG_LEVEL_INFO, \
375#define LOG_DEBUG(...) \
376 LOG_output(LOG_LEVEL_DEBUG, \
#define LOG_LEVEL
Default log level if not defined.
Definition logs.h:80
#define COLOR_RESET
Definition logs.h:158
#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:180
#define LOG_PRINTF(...)
Compiler-specific printf abstraction (variadic).
Definition logs.h:113
log_level_t
Defines log levels for the logging system.
Definition logs.h:64
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:260
#define LOG_VPRINTF(...)
Compiler-specific vprintf abstraction.
Definition logs.h:136
@ LOG_LEVEL_DEBUG
Definition logs.h:69
@ LOG_LEVEL_ERROR
Definition logs.h:66
@ LOG_LEVEL_WARNING
Definition logs.h:67
@ LOG_LEVEL_NONE
Definition logs.h:65
@ LOG_LEVEL_INFO
Definition logs.h:68