-
Notifications
You must be signed in to change notification settings - Fork 6
/
log.hh
144 lines (128 loc) · 6.9 KB
/
log.hh
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
141
142
143
144
/*
* FreeGuard: A Faster Secure Heap Allocator
* Copyright (C) 2017 Sam Silvestro, Hongyu Liu, Corey Crosser,
* Zhiqiang Lin, and Tongping Liu
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* @file log.hh: logging and debug printing macros.
* Color codes from SNAPPLE: http://sourceforge.net/projects/snapple/
* @author Tongping Liu <http://www.cs.utsa.edu/~tongpingliu/>, Charlie Curtsinger
* @author Sam Silvestro <[email protected]>
*/
#ifndef __LOG_HH__
#define __LOG_HH__
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "xdefines.hh"
#ifndef DEBUG_LEVEL
#define DEBUG_LEVEL 0
#endif
#define NORMAL_CYAN "\033[36m"
#define NORMAL_MAGENTA "\033[35m"
#define NORMAL_BLUE "\033[34m"
#define NORMAL_YELLOW "\033[33m"
#define NORMAL_GREEN "\033[32m"
#define NORMAL_RED "\033[31m"
#define BRIGHT_CYAN "\033[1m\033[36m"
#define BRIGHT_MAGENTA "\033[1m\033[35m"
#define BRIGHT_BLUE "\033[1m\033[34m"
#define BRIGHT_YELLOW "\033[1m\033[33m"
#define BRIGHT_GREEN "\033[1m\033[32m"
#define BRIGHT_RED "\033[1m\033[31m"
#define ESC_INF NORMAL_CYAN
#define ESC_DBG NORMAL_GREEN
#define ESC_WRN BRIGHT_YELLOW
#define ESC_ERR BRIGHT_RED
#define ESC_END "\033[0m"
#define OUTPUT write
#ifndef NDEBUG
/**
* Print status-information message: level 0
*/
#define PRINF(fmt, ...) \
{ \
if(DEBUG_LEVEL < 1) { \
::snprintf(getThreadBuffer(), LOG_SIZE, \
ESC_INF "%lx [INFO]: %20s:%-4d: " fmt ESC_END "\n", pthread_self(), \
__FILE__, __LINE__, ##__VA_ARGS__); \
OUTPUT(OUTFD, getThreadBuffer(), strlen(getThreadBuffer())); \
} \
}
/**
* Print status-information message: level 1
*/
#define PRDBG(fmt, ...) \
{ \
if(DEBUG_LEVEL < 2) { \
::snprintf(getThreadBuffer(), LOG_SIZE, \
ESC_DBG "%lx [DBG]: %20s:%-4d: " fmt ESC_END "\n", pthread_self(), \
__FILE__, __LINE__, ##__VA_ARGS__); \
OUTPUT(OUTFD, getThreadBuffer(), strlen(getThreadBuffer())); \
} \
}
/**
* Print warning message: level 2
*/
#define PRWRN(fmt, ...) \
{ \
if(DEBUG_LEVEL < 3) { \
::snprintf(getThreadBuffer(), LOG_SIZE, \
ESC_WRN "%lx [WRN]: %20s:%-4d: " fmt ESC_END "\n", pthread_self(), \
__FILE__, __LINE__, ##__VA_ARGS__); \
OUTPUT(OUTFD, getThreadBuffer(), strlen(getThreadBuffer())); \
} \
}
#else
#define PRINF(fmt, ...)
#define PRDBG(fmt, ...)
#define PRWRN(fmt, ...)
#endif
/**
* Print error message: level 3
*/
#define PRERR(fmt, ...) \
{ \
if(DEBUG_LEVEL < 4) { \
::snprintf(getThreadBuffer(), LOG_SIZE, \
ESC_ERR "%lx [ERR]: %20s:%-4d: " fmt ESC_END "\n", pthread_self(), \
__FILE__, __LINE__, ##__VA_ARGS__); \
OUTPUT(OUTFD, getThreadBuffer(), strlen(getThreadBuffer())); \
} \
}
// Can't be turned off. But we don't want to output those line number information.
#define PRINT(fmt, ...) \
{ \
::snprintf(getThreadBuffer(), LOG_SIZE, BRIGHT_MAGENTA fmt ESC_END "\n", ##__VA_ARGS__); \
OUTPUT(OUTFD, getThreadBuffer(), strlen(getThreadBuffer())); \
}
/**
* Print fatal error message, the program is going to exit.
*/
#define FATAL(fmt, ...) \
{ \
::snprintf(getThreadBuffer(), LOG_SIZE, \
ESC_ERR "%lx [FATALERROR]: %20s:%-4d: " fmt ESC_END "\n", \
pthread_self(), __FILE__, __LINE__, ##__VA_ARGS__); \
OUTPUT(OUTFD, getThreadBuffer(), strlen(getThreadBuffer())); \
exit(-1); \
}
// Check a condition. If false, print an error message and abort
#define REQUIRE(cond, ...) \
if(!(cond)) { \
FATAL(__VA_ARGS__) \
}
#endif