Loading...
Searching...
No Matches
debug.h
Go to the documentation of this file.
1
9
10#ifndef IMQUIC_DEBUG_H
11#define IMQUIC_DEBUG_H
12
13#include <inttypes.h>
14
15#include <glib.h>
16#include <glib/gprintf.h>
17
18extern int imquic_log_level;
19extern gboolean imquic_log_timestamps;
20extern gboolean imquic_log_colors;
21extern void (* imquic_log_function)(int level, const char *format, ...);
22
23#define IMQUIC_MAX_VARINT (((uint64_t)1 << 62) - 1)
24
28#define IMQUIC_ANSI_COLOR_RED "\x1b[31m"
29#define IMQUIC_ANSI_COLOR_GREEN "\x1b[32m"
30#define IMQUIC_ANSI_COLOR_YELLOW "\x1b[33m"
31#define IMQUIC_ANSI_COLOR_BLUE "\x1b[34m"
32#define IMQUIC_ANSI_COLOR_MAGENTA "\x1b[35m"
33#define IMQUIC_ANSI_COLOR_CYAN "\x1b[36m"
34#define IMQUIC_ANSI_COLOR_RESET "\x1b[0m"
36
40
41#define IMQUIC_LOG_NONE (0)
43#define IMQUIC_LOG_FATAL (1)
45#define IMQUIC_LOG_ERR (2)
47#define IMQUIC_LOG_WARN (3)
49#define IMQUIC_LOG_INFO (4)
51#define IMQUIC_LOG_VERB (5)
53#define IMQUIC_LOG_HUGE (6)
55#define IMQUIC_LOG_DBG (7)
57#define IMQUIC_LOG_MAX IMQUIC_LOG_DBG
58
59#pragma GCC diagnostic push
60#pragma GCC diagnostic ignored "-Wunused-variable"
62static const char *imquic_log_prefix[] = {
63/* no colors */
64 "",
65 "[FATAL] ",
66 "[ERR] ",
67 "[WARN] ",
68 "",
69 "",
70 "",
71 "",
72/* with colors */
73 "",
77 "",
78 "",
79 "",
80 ""
81};
83#pragma GCC diagnostic pop
84
88
89#define IMQUIC_PRINT(format, ...) \
90do { \
91 if(imquic_log_function == NULL) { \
92 g_print(format, \
93 ##__VA_ARGS__); \
94 } else { \
95 imquic_log_function(IMQUIC_LOG_NONE, format, \
96 ##__VA_ARGS__); \
97 } \
98} while (0)
99
102#define IMQUIC_LOG(level, format, ...) \
103do { \
104 if (level > IMQUIC_LOG_NONE && level <= IMQUIC_LOG_MAX && level <= imquic_log_level) { \
105 if(imquic_log_function == NULL) { \
106 char imquic_log_ts[64] = ""; \
107 char imquic_log_src[128] = ""; \
108 if (imquic_log_timestamps) { \
109 struct tm imquictmresult; \
110 time_t imquicltime = time(NULL); \
111 localtime_r(&imquicltime, &imquictmresult); \
112 strftime(imquic_log_ts, sizeof(imquic_log_ts), \
113 "[%a %b %e %T %Y] ", &imquictmresult); \
114 } \
115 if (level == IMQUIC_LOG_FATAL || level == IMQUIC_LOG_ERR || level == IMQUIC_LOG_DBG) { \
116 snprintf(imquic_log_src, sizeof(imquic_log_src), \
117 "[%s:%s:%d] ", __FILE__, __FUNCTION__, __LINE__); \
118 } \
119 g_print("%s%s%s" format, \
120 imquic_log_ts, \
121 imquic_log_prefix[level | ((int)imquic_log_colors << 3)], \
122 imquic_log_src, \
123 ##__VA_ARGS__); \
124 } else { \
125 imquic_log_function(level, format, \
126 ##__VA_ARGS__); \
127 } \
128 } \
129} while (0)
130
131
132#endif
#define IMQUIC_ANSI_COLOR_RESET
Definition debug.h:34
#define IMQUIC_ANSI_COLOR_YELLOW
Definition debug.h:30
#define IMQUIC_ANSI_COLOR_MAGENTA
Definition debug.h:32
#define IMQUIC_ANSI_COLOR_RED
Definition debug.h:28
int imquic_log_level
Definition imquic.c:26
gboolean imquic_log_timestamps
Definition imquic.c:27
gboolean imquic_log_colors
Definition imquic.c:29
void(* imquic_log_function)(int level, const char *format,...)
Definition imquic.c:28