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