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