Fork me on GitHub
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 #include "log.h"
18 
19 extern int janus_log_level;
20 extern gboolean janus_log_timestamps;
21 extern gboolean janus_log_colors;
22 
25 #define ANSI_COLOR_RED "\x1b[31m"
27 #define ANSI_COLOR_GREEN "\x1b[32m"
28 #define ANSI_COLOR_YELLOW "\x1b[33m"
29 #define ANSI_COLOR_BLUE "\x1b[34m"
30 #define ANSI_COLOR_MAGENTA "\x1b[35m"
31 #define ANSI_COLOR_CYAN "\x1b[36m"
32 #define ANSI_COLOR_RESET "\x1b[0m"
33 
37 
39 #define LOG_NONE (0)
40 
41 #define LOG_FATAL (1)
42 
43 #define LOG_ERR (2)
44 
45 #define LOG_WARN (3)
46 
47 #define LOG_INFO (4)
48 
49 #define LOG_VERB (5)
50 
51 #define LOG_HUGE (6)
52 
53 #define LOG_DBG (7)
54 
55 #define LOG_MAX LOG_DBG
56 
58 static const char *janus_log_prefix[] = {
59 /* no colors */
60  "",
61  "[FATAL] ",
62  "[ERR] ",
63  "[WARN] ",
64  "",
65  "",
66  "",
67  "",
68 /* with colors */
69  "",
71  ANSI_COLOR_RED "[ERR]" ANSI_COLOR_RESET " ",
72  ANSI_COLOR_YELLOW "[WARN]" ANSI_COLOR_RESET " ",
73  "",
74  "",
75  "",
76  ""
77 };
79 
82 
84 #define JANUS_PRINT janus_vprintf
85 
88 #define JANUS_LOG(level, format, ...) \
89 do { \
90  if (level > LOG_NONE && level <= LOG_MAX && level <= janus_log_level) { \
91  char janus_log_ts[64] = ""; \
92  char janus_log_src[128] = ""; \
93  if (janus_log_timestamps) { \
94  struct tm janustmresult; \
95  time_t janusltime = time(NULL); \
96  localtime_r(&janusltime, &janustmresult); \
97  strftime(janus_log_ts, sizeof(janus_log_ts), \
98  "[%a %b %e %T %Y] ", &janustmresult); \
99  } \
100  if (level == LOG_FATAL || level == LOG_ERR || level == LOG_DBG) { \
101  snprintf(janus_log_src, sizeof(janus_log_src), \
102  "[%s:%s:%d] ", __FILE__, __FUNCTION__, __LINE__); \
103  } \
104  JANUS_PRINT("%s%s%s" format, \
105  janus_log_ts, \
106  janus_log_prefix[level | ((int)janus_log_colors << 3)], \
107  janus_log_src, \
108  ##__VA_ARGS__); \
109  } \
110 } while (0)
111 
113 #endif
#define ANSI_COLOR_RED
Definition: debug.h:26
#define ANSI_COLOR_YELLOW
Definition: debug.h:28
#define ANSI_COLOR_RESET
Definition: debug.h:32
gboolean janus_log_colors
Definition: janus.c:333
int janus_log_level
Definition: janus.c:331
gboolean janus_log_timestamps
Definition: janus.c:332
#define ANSI_COLOR_MAGENTA
Definition: debug.h:30
Buffered logging (headers)