michael@0: // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: michael@0: #ifndef BASE_LOGGING_H_ michael@0: #define BASE_LOGGING_H_ michael@0: michael@0: #include michael@0: #include michael@0: michael@0: #include "base/basictypes.h" michael@0: #include "prlog.h" michael@0: michael@0: #ifdef NO_CHROMIUM_LOGGING michael@0: #include michael@0: #endif michael@0: michael@0: // Replace the Chromium logging code with NSPR-based logging code and michael@0: // some C++ wrappers to emulate std::ostream michael@0: michael@0: namespace mozilla { michael@0: michael@0: enum LogSeverity { michael@0: LOG_INFO, michael@0: LOG_WARNING, michael@0: LOG_ERROR, michael@0: LOG_ERROR_REPORT, michael@0: LOG_FATAL, michael@0: LOG_0 = LOG_ERROR michael@0: }; michael@0: michael@0: class Logger michael@0: { michael@0: public: michael@0: Logger(LogSeverity severity, const char* file, int line) michael@0: : mSeverity(severity) michael@0: , mFile(file) michael@0: , mLine(line) michael@0: , mMsg(NULL) michael@0: { } michael@0: michael@0: ~Logger(); michael@0: michael@0: // not private so that the operator<< overloads can get to it michael@0: void printf(const char* fmt, ...); michael@0: michael@0: private: michael@0: static PRLogModuleInfo* gChromiumPRLog; michael@0: static PRLogModuleInfo* GetLog(); michael@0: michael@0: LogSeverity mSeverity; michael@0: const char* mFile; michael@0: int mLine; michael@0: char* mMsg; michael@0: michael@0: DISALLOW_EVIL_CONSTRUCTORS(Logger); michael@0: }; michael@0: michael@0: class LogWrapper michael@0: { michael@0: public: michael@0: LogWrapper(LogSeverity severity, const char* file, int line) : michael@0: log(severity, file, line) { } michael@0: michael@0: operator Logger&() const { return log; } michael@0: michael@0: private: michael@0: mutable Logger log; michael@0: michael@0: DISALLOW_EVIL_CONSTRUCTORS(LogWrapper); michael@0: }; michael@0: michael@0: struct EmptyLog michael@0: { michael@0: }; michael@0: michael@0: } // namespace mozilla michael@0: michael@0: mozilla::Logger& operator<<(mozilla::Logger& log, const char* s); michael@0: mozilla::Logger& operator<<(mozilla::Logger& log, const std::string& s); michael@0: mozilla::Logger& operator<<(mozilla::Logger& log, int i); michael@0: mozilla::Logger& operator<<(mozilla::Logger& log, const std::wstring& s); michael@0: mozilla::Logger& operator<<(mozilla::Logger& log, void* p); michael@0: michael@0: template michael@0: const mozilla::EmptyLog& operator <<(const mozilla::EmptyLog& log, const T&) michael@0: { michael@0: return log; michael@0: } michael@0: michael@0: #ifdef NO_CHROMIUM_LOGGING michael@0: #define CHROMIUM_LOG(info) std::stringstream() michael@0: #define LOG_IF(info, condition) if (!(condition)) std::stringstream() michael@0: #else michael@0: #define CHROMIUM_LOG(info) mozilla::LogWrapper(mozilla::LOG_ ## info, __FILE__, __LINE__) michael@0: #define LOG_IF(info, condition) \ michael@0: if (!(condition)) mozilla::LogWrapper(mozilla::LOG_ ## info, __FILE__, __LINE__) michael@0: #endif michael@0: michael@0: michael@0: #ifdef DEBUG michael@0: #define DLOG(info) CHROMIUM_LOG(info) michael@0: #define DLOG_IF(info) LOG_IF(info) michael@0: #define DCHECK(condition) CHECK(condition) michael@0: #else michael@0: #define DLOG(info) mozilla::EmptyLog() michael@0: #define DLOG_IF(info, condition) mozilla::EmptyLog() michael@0: #define DCHECK(condition) while (false && (condition)) mozilla::EmptyLog() michael@0: #endif michael@0: michael@0: #define LOG_ASSERT(cond) CHECK(0) michael@0: #define DLOG_ASSERT(cond) DCHECK(0) michael@0: michael@0: #define NOTREACHED() CHROMIUM_LOG(ERROR) michael@0: #define NOTIMPLEMENTED() CHROMIUM_LOG(ERROR) michael@0: michael@0: #define CHECK(condition) LOG_IF(FATAL, condition) michael@0: michael@0: #define DCHECK_EQ(v1, v2) DCHECK((v1) == (v2)) michael@0: #define DCHECK_NE(v1, v2) DCHECK((v1) != (v2)) michael@0: #define DCHECK_LE(v1, v2) DCHECK((v1) <= (v2)) michael@0: #define DCHECK_LT(v1, v2) DCHECK((v1) < (v2)) michael@0: #define DCHECK_GE(v1, v2) DCHECK((v1) >= (v2)) michael@0: #define DCHECK_GT(v1, v2) DCHECK((v1) > (v2)) michael@0: michael@0: #endif // BASE_LOGGING_H_