| 1 | // Copyright 2010 Google |
| 2 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | // you may not use this file except in compliance with the License. |
| 4 | // You may obtain a copy of the License at |
| 5 | // |
| 6 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | // |
| 8 | // Unless required by applicable law or agreed to in writing, software |
| 9 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | // See the License for the specific language governing permissions and |
| 12 | // limitations under the License. |
| 13 | |
| 14 | #ifndef BASE_LOGGING_H |
| 15 | #define BASE_LOGGING_H |
| 16 | |
| 17 | #include <stdlib.h> |
| 18 | #include <iostream> |
| 19 | using std::ostream; |
| 20 | using std::cout; |
| 21 | using std::endl; |
| 22 | |
| 23 | #include "base/macros.h" |
| 24 | |
| 25 | // Always-on checking |
| 26 | #define CHECK(x) if(x){}else LogMessageFatal(__FILE__, __LINE__).stream() << "Check failed: " #x |
| 27 | #define CHECK_LT(x, y) CHECK((x) < (y)) |
| 28 | #define CHECK_GT(x, y) CHECK((x) > (y)) |
| 29 | #define CHECK_LE(x, y) CHECK((x) <= (y)) |
| 30 | #define CHECK_GE(x, y) CHECK((x) >= (y)) |
| 31 | #define CHECK_EQ(x, y) CHECK((x) == (y)) |
| 32 | #define CHECK_NE(x, y) CHECK((x) != (y)) |
| 33 | #define CHECK_NOTNULL(x) CHECK((x) != NULL) |
| 34 | |
| 35 | #ifndef NDEBUG |
| 36 | // Debug-only checking. |
| 37 | #define DCHECK(condition) CHECK(condition) |
| 38 | #define DCHECK_EQ(val1, val2) CHECK_EQ(val1, val2) |
| 39 | #define DCHECK_NE(val1, val2) CHECK_NE(val1, val2) |
| 40 | #define DCHECK_LE(val1, val2) CHECK_LE(val1, val2) |
| 41 | #define DCHECK_LT(val1, val2) CHECK_LT(val1, val2) |
| 42 | #define DCHECK_GE(val1, val2) CHECK_GE(val1, val2) |
| 43 | #define DCHECK_GT(val1, val2) CHECK_GT(val1, val2) |
| 44 | #else |
| 45 | #define DCHECK(condition) CHECK(true) |
| 46 | #define DCHECK_EQ(val1, val2) CHECK(true) |
| 47 | #define DCHECK_NE(val1, val2) CHECK(true) |
| 48 | #define DCHECK_LE(val1, val2) CHECK(true) |
| 49 | #define DCHECK_LT(val1, val2) CHECK(true) |
| 50 | #define DCHECK_GE(val1, val2) CHECK(true) |
| 51 | #define DCHECK_GT(val1, val2) CHECK(true) |
| 52 | #endif |
| 53 | |
| 54 | #define LOG_INFO LogMessage(__FILE__, __LINE__) |
| 55 | #define LOG_ERROR LOG_INFO |
| 56 | #define LOG_WARNING LOG_INFO |
| 57 | #define LOG_FATAL LogMessageFatal(__FILE__, __LINE__) |
| 58 | #define LOG_QFATAL LOG_FATAL |
| 59 | |
| 60 | #define VLOG(x) if((x)>0){} else LOG_INFO.stream() |
| 61 | |
| 62 | #ifdef NDEBUG |
| 63 | #define DEBUG_MODE false |
| 64 | #define LOG_DFATAL LOG_ERROR |
| 65 | #else |
| 66 | #define DEBUG_MODE true |
| 67 | #define LOG_DFATAL LOG_FATAL |
| 68 | #endif |
| 69 | |
| 70 | #define LOG(severity) LOG_ ## severity.stream() |
| 71 | #define LG LOG_INFO.stream() |
| 72 | |
| 73 | namespace google_base { |
| 74 | class DateLogger { |
| 75 | public: |
| 76 | DateLogger(); |
| 77 | char* const HumanDate(); |
| 78 | private: |
| 79 | char buffer_[9]; |
| 80 | }; |
| 81 | } // namespace google_base |
| 82 | |
| 83 | class LogMessage { |
| 84 | public: |
| 85 | LogMessage(const char* file, int line) { |
| 86 | std::cerr << "[" << pretty_date_.HumanDate() << "] " |
| 87 | << file << ":" << line << ": " ; |
| 88 | } |
| 89 | ~LogMessage() { std::cerr << "\n" ; } |
| 90 | std::ostream& stream() { return std::cerr; } |
| 91 | |
| 92 | private: |
| 93 | google_base::DateLogger pretty_date_; |
| 94 | DISALLOW_COPY_AND_ASSIGN(LogMessage); |
| 95 | }; |
| 96 | |
| 97 | class LogMessageFatal : public LogMessage { |
| 98 | public: |
| 99 | LogMessageFatal(const char* file, int line) |
| 100 | : LogMessage(file, line) { } |
| 101 | ~LogMessageFatal() { |
| 102 | std::cerr << "\n" ; |
| 103 | abort(); |
| 104 | } |
| 105 | private: |
| 106 | DISALLOW_COPY_AND_ASSIGN(LogMessageFatal); |
| 107 | }; |
| 108 | |
| 109 | #endif // BASE_LOGGING_H |
| 110 | |