forked from webrtc-gateway/janus-gateway-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
debug.h
120 lines (110 loc) · 3.02 KB
/
debug.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*! \file debug.h
* \author Lorenzo Miniero <[email protected]>
* \copyright GNU General Public License v3
* \brief Logging and Debugging
* \details Implementation of a wrapper on printf (or g_print) to either log or debug.
* \todo Improve this wrappers to optionally save logs on file
*
* \ingroup core
* \ref core
*/
#ifndef _JANUS_DEBUG_H
#define _JANUS_DEBUG_H
#include <glib.h>
#include <glib/gprintf.h>
#include "log.h"
JANUS_API extern int janus_log_level;
JANUS_API extern gboolean janus_log_timestamps;
JANUS_API extern gboolean janus_log_colors;
/** @name Janus log colors
*/
///@{
#define ANSI_COLOR_RED "\x1b[31m"
#define ANSI_COLOR_GREEN "\x1b[32m"
#define ANSI_COLOR_YELLOW "\x1b[33m"
#define ANSI_COLOR_BLUE "\x1b[34m"
#define ANSI_COLOR_MAGENTA "\x1b[35m"
#define ANSI_COLOR_CYAN "\x1b[36m"
#define ANSI_COLOR_RESET "\x1b[0m"
///@}
/** @name Janus log levels
*/
///@{
/*! \brief No debugging */
#define LOG_NONE (0)
/*! \brief Fatal error */
#define LOG_FATAL (1)
/*! \brief Non-fatal error */
#define LOG_ERR (2)
/*! \brief Warning */
#define LOG_WARN (3)
/*! \brief Informational message */
#define LOG_INFO (4)
/*! \brief Verbose message */
#define LOG_VERB (5)
/*! \brief Overly verbose message */
#define LOG_HUGE (6)
/*! \brief Debug message (includes .c filename, function and line number) */
#define LOG_DBG (7)
/*! \brief Maximum level of debugging */
#define LOG_MAX LOG_DBG
/*! \brief Coloured prefixes for errors and warnings logging. */
static const char *janus_log_prefix[] = {
/* no colors */
"",
"[FATAL] ",
"[ERR] ",
"[WARN] ",
"",
"",
"",
"",
/* with colors */
"",
ANSI_COLOR_MAGENTA"[FATAL]"ANSI_COLOR_RESET" ",
ANSI_COLOR_RED"[ERR]"ANSI_COLOR_RESET" ",
ANSI_COLOR_YELLOW"[WARN]"ANSI_COLOR_RESET" ",
"",
"",
"",
""
};
#ifdef _WIN32
#define DATE_TIME_FORMAT "[%a %b %d %H:%M:%S %Y] "
#else
#define DATE_TIME_FORMAT "[%a %b %e %T %Y] "
#endif
///@}
/** @name Janus log wrappers
*/
///@{
/*! \brief Simple wrapper to g_print/printf */
#define JANUS_PRINT janus_vprintf
/*! \brief Logger based on different levels, which can either be displayed
* or not according to the configuration of the gateway.
* The format must be a string literal. */
#define JANUS_LOG(level, format, ...) \
do { \
if (level > LOG_NONE && level <= LOG_MAX && level <= janus_log_level) { \
char janus_log_ts[64] = ""; \
char janus_log_src[128] = ""; \
if (janus_log_timestamps) { \
struct tm janustmresult; \
time_t janusltime = time(NULL); \
localtime_r(&janusltime, &janustmresult); \
strftime(janus_log_ts, sizeof(janus_log_ts), \
DATE_TIME_FORMAT, &janustmresult); \
} \
if (level == LOG_FATAL || level == LOG_ERR || level == LOG_DBG) { \
snprintf(janus_log_src, sizeof(janus_log_src), \
"[%s:%s:%d] ", __FILE__, __FUNCTION__, __LINE__); \
} \
JANUS_PRINT("%s%s%s" format, \
janus_log_ts, \
janus_log_prefix[level | ((int)janus_log_colors << 3)], \
janus_log_src, \
##__VA_ARGS__); \
} \
} while (0)
///@}
#endif