35
35
#ifndef _LOGGING_H_
36
36
#define _LOGGING_H_
38
39
#include <stdarg.h>
43
#include <unistd.h> // for write()
45
#include <string.h> // for strlen()
47
#include <errno.h> // for errno
48
#include "base/commandlineflags.h"
50
// On some systems (like freebsd), we can't call write() at all in a
51
// global constructor, perhaps because errno hasn't been set up.
52
// Calling the write syscall is safer (it doesn't set errno), so we
53
// prefer that. Note we don't care about errno for logging: we just
54
// do logging on a best-effort basis.
55
#ifdef HAVE_SYS_SYSCALL_H
56
#include <sys/syscall.h>
57
#define WRITE_TO_STDERR(buf, len) syscall(SYS_write, STDERR_FILENO, buf, len)
59
#define WRITE_TO_STDERR(buf, len) write(STDERR_FILENO, buf, len)
63
// We log all messages at this log-level and below.
64
// INFO == -1, WARNING == -2, ERROR == -3, FATAL == -4
65
DECLARE_int32(verbose);
42
67
// CHECK dies with a fatal error if condition is not true. It is *not*
43
68
// controlled by NDEBUG, so the check will be executed regardless of
44
69
// compilation mode. Therefore, it is safe to do things like:
45
70
// CHECK(fp->Write(x) == 4)
46
#define CHECK(condition) \
49
fprintf(stderr, "Check failed: %s\n", #condition); \
71
// Note we use write instead of printf/puts to avoid the risk we'll
73
#define CHECK(condition) \
76
WRITE_TO_STDERR("Check failed: " #condition "\n", \
77
sizeof("Check failed: " #condition "\n")-1); \
82
// This takes a message to print. The name is historical.
83
#define RAW_CHECK(condition, message) \
86
WRITE_TO_STDERR("Check failed: " #condition ": " message "\n", \
87
sizeof("Check failed: " #condition ": " message "\n")-1);\
92
// This is like RAW_CHECK, but only in debug-mode
94
enum { DEBUG_MODE = 0 };
95
#define RAW_DCHECK(condition, message)
97
enum { DEBUG_MODE = 1 };
98
#define RAW_DCHECK(condition, message) RAW_CHECK(condition, message)
101
// This prints errno as well. Note we use write instead of printf/puts to
102
// avoid the risk we'll call malloc().
103
#define PCHECK(condition) \
105
if (!(condition)) { \
106
const int err_no = errno; \
107
WRITE_TO_STDERR("Check failed: " #condition ": ", \
108
sizeof("Check failed: " #condition ": ")-1); \
109
WRITE_TO_STDERR(strerror(err_no), strlen(strerror(err_no))); \
110
WRITE_TO_STDERR("\n", sizeof("\n")-1); \
54
115
// Helper macro for binary operators; prints the two values on error
55
116
// Don't use this macro directly in your code, use CHECK_EQ et al below
61
122
// TODO(jandrews): Also print the values in case of failure. Requires some
62
123
// sort of type-sensitive ToString() function.
63
#define CHECK_OP(op, val1, val2) \
65
if (!((val1) op (val2))) { \
66
fprintf(stderr, "Check failed: %s %s %s\n", #val1, #op, #val2); \
124
#define CHECK_OP(op, val1, val2) \
126
if (!((val1) op (val2))) { \
127
fprintf(stderr, "Check failed: %s %s %s\n", #val1, #op, #val2); \
71
132
#define CHECK_EQ(val1, val2) CHECK_OP(==, val1, val2)
75
136
#define CHECK_GE(val1, val2) CHECK_OP(>=, val1, val2)
76
137
#define CHECK_GT(val1, val2) CHECK_OP(> , val1, val2)
78
enum {INFO, WARNING, ERROR, FATAL, NUM_SEVERITIES};
80
inline void LogPrintf(int severity, const char* pat, ...) {
83
vfprintf(stderr, pat, ap);
85
fprintf(stderr, "\n");
139
// Used for (libc) functions that return -1 and set errno
140
#define CHECK_ERR(invocation) PCHECK((invocation) != -1)
142
// A few more checks that only happen in debug mode
144
#define DCHECK_EQ(val1, val2)
145
#define DCHECK_NE(val1, val2)
146
#define DCHECK_LE(val1, val2)
147
#define DCHECK_LT(val1, val2)
148
#define DCHECK_GE(val1, val2)
149
#define DCHECK_GT(val1, val2)
151
#define DCHECK_EQ(val1, val2) CHECK_EQ(val1, val2)
152
#define DCHECK_NE(val1, val2) CHECK_NE(val1, val2)
153
#define DCHECK_LE(val1, val2) CHECK_LE(val1, val2)
154
#define DCHECK_LT(val1, val2) CHECK_LT(val1, val2)
155
#define DCHECK_GE(val1, val2) CHECK_GE(val1, val2)
156
#define DCHECK_GT(val1, val2) CHECK_GT(val1, val2)
161
#undef ERROR // may conflict with ERROR macro on windows
163
enum LogSeverity {INFO = -1, WARNING = -2, ERROR = -3, FATAL = -4};
165
// NOTE: we add a newline to the end of the output if it's not there already
166
inline void LogPrintf(int severity, const char* pat, va_list ap) {
167
// We write directly to the stderr file descriptor and avoid FILE
168
// buffering because that may invoke malloc()
170
vsnprintf(buf, sizeof(buf)-1, pat, ap);
171
if (buf[0] != '\0' && buf[strlen(buf)-1] != '\n') {
172
assert(strlen(buf)+1 < sizeof(buf));
175
WRITE_TO_STDERR(buf, strlen(buf));
86
176
if ((severity) == FATAL)
177
abort(); // LOG(FATAL) indicates a big problem, so don't run atexit() calls
180
// Note that since the order of global constructors is unspecified,
181
// global code that calls RAW_LOG may execute before FLAGS_verbose is set.
182
// Such code will run with verbosity == 0 no matter what.
183
#define VLOG_IS_ON(severity) (FLAGS_verbose >= severity)
185
// In a better world, we'd use __VA_ARGS__, but VC++ 7 doesn't support it.
186
#define LOG_PRINTF(severity, pat) do { \
187
if (VLOG_IS_ON(severity)) { \
190
LogPrintf(severity, pat, ap); \
195
// RAW_LOG is the main function; some synonyms are used in unittests.
196
inline void RAW_LOG(int lvl, const char* pat, ...) { LOG_PRINTF(lvl, pat); }
197
inline void RAW_VLOG(int lvl, const char* pat, ...) { LOG_PRINTF(lvl, pat); }
198
inline void LOG(int lvl, const char* pat, ...) { LOG_PRINTF(lvl, pat); }
199
inline void VLOG(int lvl, const char* pat, ...) { LOG_PRINTF(lvl, pat); }
200
inline void LOG_IF(int lvl, bool cond, const char* pat, ...) {
201
if (cond) LOG_PRINTF(lvl, pat);
90
204
#endif // _LOGGING_H_