Fork me on GitHub
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
debug.h
Go to the documentation of this file.
1 
12 #ifndef _JANUS_DEBUG_H
13 #define _JANUS_DEBUG_H
14 
15 #include <glib.h>
16 #include <glib/gprintf.h>
17 
18 extern int janus_log_level;
19 extern gboolean janus_log_timestamps;
20 extern gboolean janus_log_colors;
21 
24 
25 #define ANSI_COLOR_RED "\x1b[31m"
26 #define ANSI_COLOR_GREEN "\x1b[32m"
27 #define ANSI_COLOR_YELLOW "\x1b[33m"
28 #define ANSI_COLOR_BLUE "\x1b[34m"
29 #define ANSI_COLOR_MAGENTA "\x1b[35m"
30 #define ANSI_COLOR_CYAN "\x1b[36m"
31 #define ANSI_COLOR_RESET "\x1b[0m"
32 
33 
36 
37 
38 #define LOG_NONE (0)
39 
40 #define LOG_FATAL (1)
41 
42 #define LOG_ERR (2)
43 
44 #define LOG_WARN (3)
45 
46 #define LOG_INFO (4)
47 
48 #define LOG_VERB (5)
49 
50 #define LOG_HUGE (6)
51 
52 #define LOG_DBG (7)
53 
54 #define LOG_MAX LOG_DBG
55 
57 static const char *janus_log_prefix[] = {
58  "",
60  ANSI_COLOR_RED"[ERR]"ANSI_COLOR_RESET" ",
61  ANSI_COLOR_YELLOW"[WARN]"ANSI_COLOR_RESET" ",
62  "",
63  "",
64  "",
65  ""
66 };
67 
69 static const char *janus_log_nocolors_prefix[] = {
70  "",
71  "[FATAL] ",
72  "[ERR] ",
73  "[WARN] ",
74  "",
75  "",
76  "",
77  ""
78 };
80 
83 
84 
85 #define JANUS_PRINT g_print
86 
89 #define JANUS_LOG(level, format, ...) \
90 do { \
91  if (level > LOG_NONE && level <= LOG_MAX && level <= janus_log_level) { \
92  char janus_log_ts[32] = ""; \
93  char janus_log_src[64] = ""; \
94  if (janus_log_timestamps) { \
95  struct tm janustmresult; \
96  time_t janusltime = time(NULL); \
97  localtime_r(&janusltime, &janustmresult); \
98  strftime(janus_log_ts, sizeof(janus_log_ts), \
99  "[%a %b %e %T %Y] ", &janustmresult); \
100  } \
101  if (level == LOG_FATAL || level == LOG_ERR || level == LOG_DBG) { \
102  snprintf(janus_log_src, sizeof(janus_log_src), \
103  "[%s:%s:%d] ", __FILE__, __FUNCTION__, __LINE__); \
104  } \
105  g_print("%s%s%s" format, \
106  janus_log_ts, janus_log_colors ? janus_log_prefix[level] : janus_log_nocolors_prefix[level], janus_log_src, \
107  ##__VA_ARGS__); \
108  } \
109 } while (0)
110 
111 
112 #endif