-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdbug.h
140 lines (120 loc) · 4.54 KB
/
dbug.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*! \file dbug.h
Debug macros, disabled and cost-free in release builds.
\details
These macros should cost nothing when building without DEBUG.
\author Christopher Welborn
\date 11-12-2017
*/
#ifndef DBUG_H
#define DBUG_H
/* Tell gcc to ignore this unused inclusion macro. */
#pragma GCC diagnostic ignored "-Wunused-macros"
/* Tell gcc to ignore clang pragmas, for linting. */
#pragma GCC diagnostic ignored "-Wunknown-pragmas"
/* Tell clang to ignore this unused inclusion macro. */
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused-macros"
//! Name of this library.
#define DBUG_NAME "dbug"
//! Version for this library.
#define DBUG_VERSION "0.0.4"
//! Long version string for this library.
#define DBUG_VERSION_STR DBUG_NAME " v. " DBUG_VERSION
#include <stdarg.h>
#include <stdio.h>
#ifdef DEBUG
#ifndef DOXYGEN_SKIP
#define DBUG_COLOR_NONE "\x1b[0m"
#define DBUG_COLOR_BLUE "\x1b[34m"
#define DBUG_COLOR_CYAN "\x1b[36m"
#define DBUG_COLOR_GREEN "\x1b[32m"
#define DBUG_COLOR_RED "\x1b[31m"
#define DBUG_COLOR_YELLOW "\x1b[33m"
#define DBUG_STYLE_BRIGHT "\x1b[1m"
#endif
//! Number of characters in the file, line, and lineno info.
#define DBUG_DEBUGWIDTH 45
//! Number of characters in the maximum line-count/line-number.
#define DBUG_LINELEN 4
//! Number of "other characters", such as ':', '()', and ' ' in the line info.
#define DBUG_EXTRACHARS 6
//! Computed max width for file name in line info.
#define DBUG_FILELEN ((DBUG_DEBUGWIDTH - DBUG_LINELEN - DBUG_EXTRACHARS) / 2)
//! Computed max width for function name in line info.
#define DBUG_FUNCLEN ((DBUG_DEBUGWIDTH - DBUG_LINELEN - DBUG_EXTRACHARS) / 2)
//! Macro for printing line information only, with no newline.
#define dbug_lineinfo() \
fprintf(stderr, "%s%*s%s:%s%s%-*d%s %s%*s()%s: " \
,DBUG_COLOR_YELLOW \
,DBUG_FILELEN, __FILE__ \
,DBUG_COLOR_NONE \
,DBUG_COLOR_BLUE \
,DBUG_STYLE_BRIGHT \
,DBUG_LINELEN, __LINE__ \
,DBUG_COLOR_NONE \
,DBUG_COLOR_BLUE \
,DBUG_FUNCLEN, __func__ \
,DBUG_COLOR_NONE \
);
/*! Macro for printing a regular message.
\details
Writes a debug message with no line information, and no newline.
\pi ... Format string and all other arguments to satisfy the format string.
*/
#define dbug_msg(...) \
do { \
fprintf(stderr, "%s", DBUG_COLOR_GREEN); \
fprintf(stderr, __VA_ARGS__); \
fprintf(stderr, "%s", DBUG_COLOR_NONE); \
} while (0)
/*! Macro for printing an error message.
\details
File and line information will be included.
\pi ... Format string and all other arguments to satisfy the format string.
*/
#define dbug_msg_err(...) \
do { \
fprintf(stderr, "%s", DBUG_COLOR_RED); \
fprintf(stderr, __VA_ARGS__); \
fprintf(stderr, "%s", DBUG_COLOR_NONE); \
} while (0)
/*! Macro for printing debug information.
\details
File and line information will be included.
\pi ... Format string and all other arguments to satisfy the format string.
*/
#define dbug(...) \
do { \
dbug_lineinfo(); \
dbug_msg(__VA_ARGS__); \
} while (0)
/*! Macro for printing debug error information.
\details
File and line information will be included.
\pi ... Format string and all other arguments to satisfy the format string.
*/
#define debugerr(...) \
do { \
dbug_lineinfo(); \
dbug_msg_err(__VA_ARGS__); \
} while (0)
/* Warn for any other unused macros, for gcc and clang. */
#pragma clang diagnostic pop
#pragma GCC diagnostic warning "-Wunused-macros"
#pragma clang diagnostic push
#pragma clang diagnostic warning "-Wunused-macros"
#else
// Ignore unused macros (for linting).
#pragma GCC diagnostic ignored "-Wunknown-pragmas"
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused-macros"
// In non-debug builds, any calls to the debug* macros are no-ops.
#define dbug_lineinfo(...) ((void)0)
#define dbug_msg(...) ((void)0)
#define dbug_msg_err(...) ((void)0)
#define dbug(...) ((void)0)
#define dbugerr(...) ((void)0)
#pragma clang diagnostic pop /* end ignore -Wunused-macros */
#endif
#pragma clang diagnostic pop /* end warning -Wunused-macros */
#endif /* DBUG_H */