OLD | NEW |
1 // Copyright 2009 The RE2 Authors. All Rights Reserved. | 1 // Copyright 2009 The RE2 Authors. All Rights Reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 // Simplified version of Google's logging. | 5 // Simplified version of Google's logging. |
6 | 6 |
7 #ifndef RE2_UTIL_LOGGING_H__ | 7 #ifndef RE2_UTIL_LOGGING_H__ |
8 #define RE2_UTIL_LOGGING_H__ | 8 #define RE2_UTIL_LOGGING_H__ |
9 | 9 |
10 #include <stdio.h> /* for fwrite */ | 10 #ifndef WIN32 |
| 11 #include <unistd.h> /* for write */ |
| 12 #endif |
11 #include <sstream> | 13 #include <sstream> |
12 | 14 #ifdef WIN32 |
13 #include "util/util.h" | 15 #include <io.h> |
14 #include "util/flags.h" | 16 #endif |
15 | |
16 DECLARE_int32(minloglevel); | |
17 | 17 |
18 // Debug-only checking. | 18 // Debug-only checking. |
19 #define DCHECK(condition) assert(condition) | 19 #define DCHECK(condition) assert(condition) |
20 #define DCHECK_EQ(val1, val2) assert((val1) == (val2)) | 20 #define DCHECK_EQ(val1, val2) assert((val1) == (val2)) |
21 #define DCHECK_NE(val1, val2) assert((val1) != (val2)) | 21 #define DCHECK_NE(val1, val2) assert((val1) != (val2)) |
22 #define DCHECK_LE(val1, val2) assert((val1) <= (val2)) | 22 #define DCHECK_LE(val1, val2) assert((val1) <= (val2)) |
23 #define DCHECK_LT(val1, val2) assert((val1) < (val2)) | 23 #define DCHECK_LT(val1, val2) assert((val1) < (val2)) |
24 #define DCHECK_GE(val1, val2) assert((val1) >= (val2)) | 24 #define DCHECK_GE(val1, val2) assert((val1) >= (val2)) |
25 #define DCHECK_GT(val1, val2) assert((val1) > (val2)) | 25 #define DCHECK_GT(val1, val2) assert((val1) > (val2)) |
26 | 26 |
27 // Always-on checking | 27 // Always-on checking |
28 #define CHECK(x) if(x){}else LogMessageFatal(__FILE__, __LINE__).stream()
<< "Check failed: " #x | 28 #define CHECK(x) if(x){}else LogMessageFatal(__FILE__, __LINE__).stream()
<< "Check failed: " #x |
29 #define CHECK_LT(x, y) CHECK((x) < (y)) | 29 #define CHECK_LT(x, y) CHECK((x) < (y)) |
30 #define CHECK_GT(x, y) CHECK((x) > (y)) | 30 #define CHECK_GT(x, y) CHECK((x) > (y)) |
31 #define CHECK_LE(x, y) CHECK((x) <= (y)) | 31 #define CHECK_LE(x, y) CHECK((x) <= (y)) |
32 #define CHECK_GE(x, y) CHECK((x) >= (y)) | 32 #define CHECK_GE(x, y) CHECK((x) >= (y)) |
33 #define CHECK_EQ(x, y) CHECK((x) == (y)) | 33 #define CHECK_EQ(x, y) CHECK((x) == (y)) |
34 #define CHECK_NE(x, y) CHECK((x) != (y)) | 34 #define CHECK_NE(x, y) CHECK((x) != (y)) |
35 | 35 |
36 #define LOG_INFO LogMessage(__FILE__, __LINE__, 0) | 36 #define LOG_INFO LogMessage(__FILE__, __LINE__) |
37 #define LOG_WARNING LogMessage(__FILE__, __LINE__, 1) | 37 #define LOG_ERROR LOG_INFO |
38 #define LOG_ERROR LogMessage(__FILE__, __LINE__, 2) | 38 #define LOG_WARNING LOG_INFO |
39 #define LOG_FATAL LogMessageFatal(__FILE__, __LINE__) | 39 #define LOG_FATAL LogMessageFatal(__FILE__, __LINE__) |
40 #define LOG_QFATAL LOG_FATAL | 40 #define LOG_QFATAL LOG_FATAL |
41 | 41 |
42 // It seems that one of the Windows header files defines ERROR as 0. | 42 #define VLOG(x) if((x)>0){}else LOG_INFO.stream() |
43 #ifdef _WIN32 | |
44 #define LOG_0 LOG_INFO | |
45 #endif | |
46 | 43 |
47 #ifdef NDEBUG | 44 #ifdef NDEBUG |
48 #define DEBUG_MODE 0 | 45 #define DEBUG_MODE 0 |
49 #define LOG_DFATAL LOG_ERROR | 46 #define LOG_DFATAL LOG_ERROR |
50 #else | 47 #else |
51 #define DEBUG_MODE 1 | 48 #define DEBUG_MODE 1 |
52 #define LOG_DFATAL LOG_FATAL | 49 #define LOG_DFATAL LOG_FATAL |
53 #endif | 50 #endif |
54 | 51 |
55 #define LOG(severity) LOG_ ## severity.stream() | 52 #define LOG(severity) LOG_ ## severity.stream() |
56 | 53 |
57 #define VLOG(x) if((x)>0){}else LOG_INFO.stream() | |
58 | |
59 class LogMessage { | 54 class LogMessage { |
60 public: | 55 public: |
61 LogMessage(const char* file, int line, int severity) | 56 LogMessage(const char* file, int line) : flushed_(false) { |
62 : severity_(severity), flushed_(false) { | |
63 stream() << file << ":" << line << ": "; | 57 stream() << file << ":" << line << ": "; |
64 } | 58 } |
65 void Flush() { | 59 void Flush() { |
66 stream() << "\n"; | 60 stream() << "\n"; |
67 if (severity_ >= re2::FLAGS_minloglevel) { | 61 string s = str_.str(); |
68 string s = str_.str(); | 62 int n = (int)s.size(); // shut up msvc |
69 size_t n = s.size(); | 63 if(write(2, s.data(), n) < 0) {} // shut up gcc |
70 if (fwrite(s.data(), 1, n, stderr) < n) {} // shut up gcc | |
71 } | |
72 flushed_ = true; | 64 flushed_ = true; |
73 } | 65 } |
74 ~LogMessage() { | 66 ~LogMessage() { |
75 if (!flushed_) { | 67 if (!flushed_) { |
76 Flush(); | 68 Flush(); |
77 } | 69 } |
78 } | 70 } |
79 ostream& stream() { return str_; } | 71 ostream& stream() { return str_; } |
80 | 72 |
81 private: | 73 private: |
82 const int severity_; | |
83 bool flushed_; | 74 bool flushed_; |
84 std::ostringstream str_; | 75 std::ostringstream str_; |
85 DISALLOW_COPY_AND_ASSIGN(LogMessage); | 76 DISALLOW_EVIL_CONSTRUCTORS(LogMessage); |
86 }; | 77 }; |
87 | 78 |
88 #ifdef _WIN32 | |
89 #pragma warning(push) | |
90 #pragma warning(disable: 4722) // destructor never returns | |
91 #endif | |
92 | |
93 class LogMessageFatal : public LogMessage { | 79 class LogMessageFatal : public LogMessage { |
94 public: | 80 public: |
95 LogMessageFatal(const char* file, int line) | 81 LogMessageFatal(const char* file, int line) |
96 : LogMessage(file, line, 3) {} | 82 : LogMessage(file, line) { } |
97 ~LogMessageFatal() { | 83 ~LogMessageFatal() { |
98 Flush(); | 84 Flush(); |
99 abort(); | 85 abort(); |
100 } | 86 } |
101 private: | 87 private: |
102 DISALLOW_COPY_AND_ASSIGN(LogMessageFatal); | 88 DISALLOW_EVIL_CONSTRUCTORS(LogMessageFatal); |
103 }; | 89 }; |
104 | 90 |
105 #ifdef _WIN32 | |
106 #pragma warning(pop) | |
107 #endif | |
108 | |
109 #endif // RE2_UTIL_LOGGING_H__ | 91 #endif // RE2_UTIL_LOGGING_H__ |
OLD | NEW |