logging.h (3477B)
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 public: 38 Logger(LogSeverity severity, const char* file, int line) 39 : mSeverity(severity), mFile(file), mLine(line) {} 40 41 ~Logger(); 42 43 // not private so that the operator<< overloads can get to it 44 void printf(const char* fmt, ...) MOZ_FORMAT_PRINTF(2, 3); 45 46 private: 47 static mozilla::LazyLogModule gChromiumPRLog; 48 // static PRLogModuleInfo* GetLog(); 49 50 LogSeverity mSeverity; 51 const char* mFile; 52 int mLine; 53 SmprintfPointer mMsg; 54 55 DISALLOW_EVIL_CONSTRUCTORS(Logger); 56 }; 57 58 class LogWrapper { 59 public: 60 LogWrapper(LogSeverity severity, const char* file, int line) 61 : log(severity, file, line) {} 62 63 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 mozilla::Logger& operator<<(mozilla::Logger& log, const char* s); 74 mozilla::Logger& operator<<(mozilla::Logger& log, const std::string& s); 75 mozilla::Logger& operator<<(mozilla::Logger& log, int i); 76 mozilla::Logger& operator<<(mozilla::Logger& log, const std::wstring& s); 77 mozilla::Logger& operator<<(mozilla::Logger& log, void* p); 78 79 template <class T> 80 const mozilla::EmptyLog& operator<<(const mozilla::EmptyLog& log, const T&) { 81 return log; 82 } 83 84 } // namespace mozilla 85 86 #ifdef NO_CHROMIUM_LOGGING 87 # define CHROMIUM_LOG(info) std::stringstream() 88 # define LOG_IF(info, condition) \ 89 if (!(condition)) std::stringstream() 90 #else 91 # define CHROMIUM_LOG(info) \ 92 mozilla::LogWrapper(mozilla::LOG_##info, __FILE__, __LINE__) 93 # define LOG_IF(info, condition) \ 94 if (!(condition)) \ 95 mozilla::LogWrapper(mozilla::LOG_##info, __FILE__, __LINE__) 96 #endif 97 98 #ifdef DEBUG 99 # define DLOG(info) CHROMIUM_LOG(info) 100 # define DLOG_IF(info, condition) LOG_IF(info, condition) 101 # define DCHECK(condition) CHECK(condition) 102 #else 103 # define DLOG(info) mozilla::EmptyLog() 104 # define DLOG_IF(info, condition) mozilla::EmptyLog() 105 # define DCHECK(condition) \ 106 while (false && (condition)) mozilla::EmptyLog() 107 #endif 108 109 #define DVLOG(level) DLOG(INFO) 110 111 #undef LOG_ASSERT 112 #define LOG_ASSERT(cond) CHECK(0) 113 #define DLOG_ASSERT(cond) DCHECK(0) 114 115 #define NOTREACHED() CHROMIUM_LOG(ERROR) 116 #define NOTIMPLEMENTED() CHROMIUM_LOG(ERROR) 117 118 #undef CHECK 119 #ifdef FUZZING 120 # define CHECK(condition) LOG_IF(WARNING, condition) 121 #else 122 # define CHECK(condition) LOG_IF(FATAL, condition) 123 #endif 124 125 #define DCHECK_EQ(v1, v2) DCHECK((v1) == (v2)) 126 #define DCHECK_NE(v1, v2) DCHECK((v1) != (v2)) 127 #define DCHECK_LE(v1, v2) DCHECK((v1) <= (v2)) 128 #define DCHECK_LT(v1, v2) DCHECK((v1) < (v2)) 129 #define DCHECK_GE(v1, v2) DCHECK((v1) >= (v2)) 130 #define DCHECK_GT(v1, v2) DCHECK((v1) > (v2)) 131 132 #endif // BASE_LOGGING_H_