|
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
|
2 // Use of this source code is governed by a BSD-style license that can be |
|
3 // found in the LICENSE file. |
|
4 |
|
5 #ifndef BASE_LOGGING_H_ |
|
6 #define BASE_LOGGING_H_ |
|
7 |
|
8 #include <string> |
|
9 #include <cstring> |
|
10 |
|
11 #include "base/basictypes.h" |
|
12 #include "prlog.h" |
|
13 |
|
14 #ifdef NO_CHROMIUM_LOGGING |
|
15 #include <sstream> |
|
16 #endif |
|
17 |
|
18 // Replace the Chromium logging code with NSPR-based logging code and |
|
19 // some C++ wrappers to emulate std::ostream |
|
20 |
|
21 namespace mozilla { |
|
22 |
|
23 enum LogSeverity { |
|
24 LOG_INFO, |
|
25 LOG_WARNING, |
|
26 LOG_ERROR, |
|
27 LOG_ERROR_REPORT, |
|
28 LOG_FATAL, |
|
29 LOG_0 = LOG_ERROR |
|
30 }; |
|
31 |
|
32 class Logger |
|
33 { |
|
34 public: |
|
35 Logger(LogSeverity severity, const char* file, int line) |
|
36 : mSeverity(severity) |
|
37 , mFile(file) |
|
38 , mLine(line) |
|
39 , mMsg(NULL) |
|
40 { } |
|
41 |
|
42 ~Logger(); |
|
43 |
|
44 // not private so that the operator<< overloads can get to it |
|
45 void printf(const char* fmt, ...); |
|
46 |
|
47 private: |
|
48 static PRLogModuleInfo* gChromiumPRLog; |
|
49 static PRLogModuleInfo* GetLog(); |
|
50 |
|
51 LogSeverity mSeverity; |
|
52 const char* mFile; |
|
53 int mLine; |
|
54 char* mMsg; |
|
55 |
|
56 DISALLOW_EVIL_CONSTRUCTORS(Logger); |
|
57 }; |
|
58 |
|
59 class LogWrapper |
|
60 { |
|
61 public: |
|
62 LogWrapper(LogSeverity severity, const char* file, int line) : |
|
63 log(severity, file, line) { } |
|
64 |
|
65 operator Logger&() const { return log; } |
|
66 |
|
67 private: |
|
68 mutable Logger log; |
|
69 |
|
70 DISALLOW_EVIL_CONSTRUCTORS(LogWrapper); |
|
71 }; |
|
72 |
|
73 struct EmptyLog |
|
74 { |
|
75 }; |
|
76 |
|
77 } // namespace mozilla |
|
78 |
|
79 mozilla::Logger& operator<<(mozilla::Logger& log, const char* s); |
|
80 mozilla::Logger& operator<<(mozilla::Logger& log, const std::string& s); |
|
81 mozilla::Logger& operator<<(mozilla::Logger& log, int i); |
|
82 mozilla::Logger& operator<<(mozilla::Logger& log, const std::wstring& s); |
|
83 mozilla::Logger& operator<<(mozilla::Logger& log, void* p); |
|
84 |
|
85 template<class T> |
|
86 const mozilla::EmptyLog& operator <<(const mozilla::EmptyLog& log, const T&) |
|
87 { |
|
88 return log; |
|
89 } |
|
90 |
|
91 #ifdef NO_CHROMIUM_LOGGING |
|
92 #define CHROMIUM_LOG(info) std::stringstream() |
|
93 #define LOG_IF(info, condition) if (!(condition)) std::stringstream() |
|
94 #else |
|
95 #define CHROMIUM_LOG(info) mozilla::LogWrapper(mozilla::LOG_ ## info, __FILE__, __LINE__) |
|
96 #define LOG_IF(info, condition) \ |
|
97 if (!(condition)) mozilla::LogWrapper(mozilla::LOG_ ## info, __FILE__, __LINE__) |
|
98 #endif |
|
99 |
|
100 |
|
101 #ifdef DEBUG |
|
102 #define DLOG(info) CHROMIUM_LOG(info) |
|
103 #define DLOG_IF(info) LOG_IF(info) |
|
104 #define DCHECK(condition) CHECK(condition) |
|
105 #else |
|
106 #define DLOG(info) mozilla::EmptyLog() |
|
107 #define DLOG_IF(info, condition) mozilla::EmptyLog() |
|
108 #define DCHECK(condition) while (false && (condition)) mozilla::EmptyLog() |
|
109 #endif |
|
110 |
|
111 #define LOG_ASSERT(cond) CHECK(0) |
|
112 #define DLOG_ASSERT(cond) DCHECK(0) |
|
113 |
|
114 #define NOTREACHED() CHROMIUM_LOG(ERROR) |
|
115 #define NOTIMPLEMENTED() CHROMIUM_LOG(ERROR) |
|
116 |
|
117 #define CHECK(condition) LOG_IF(FATAL, condition) |
|
118 |
|
119 #define DCHECK_EQ(v1, v2) DCHECK((v1) == (v2)) |
|
120 #define DCHECK_NE(v1, v2) DCHECK((v1) != (v2)) |
|
121 #define DCHECK_LE(v1, v2) DCHECK((v1) <= (v2)) |
|
122 #define DCHECK_LT(v1, v2) DCHECK((v1) < (v2)) |
|
123 #define DCHECK_GE(v1, v2) DCHECK((v1) >= (v2)) |
|
124 #define DCHECK_GT(v1, v2) DCHECK((v1) > (v2)) |
|
125 |
|
126 #endif // BASE_LOGGING_H_ |