Carl Shapiro | 6c21dc1 | 2011-06-20 15:20:52 -0700 | [diff] [blame^] | 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 SRC_LOGGING_H_ |
| 15 | #define SRC_LOGGING_H_ |
| 16 | |
| 17 | #include <cstdlib> |
| 18 | #include <iostream> // NOLINT |
| 19 | #include "src/macros.h" |
| 20 | |
| 21 | #define CHECK(x) \ |
| 22 | if (!(x)) LogMessageFatal(__FILE__, __LINE__).stream() << "Check failed: " #x |
| 23 | #define CHECK_EQ(x, y) CHECK((x) == (y)) |
| 24 | #define CHECK_NE(x, y) CHECK((x) != (y)) |
| 25 | #define CHECK_LE(x, y) CHECK((x) <= (y)) |
| 26 | #define CHECK_LT(x, y) CHECK((x) < (y)) |
| 27 | #define CHECK_GE(x, y) CHECK((x) >= (y)) |
| 28 | #define CHECK_GT(x, y) CHECK((x) > (y)) |
| 29 | |
| 30 | #ifndef NDEBUG |
| 31 | |
| 32 | #define DCHECK(x) CHECK(x) |
| 33 | #define DCHECK_EQ(x, y) CHECK_EQ(x, y) |
| 34 | #define DCHECK_NE(x, y) CHECK_NE(x, y) |
| 35 | #define DCHECK_LE(x, y) CHECK_LE(x, y) |
| 36 | #define DCHECK_LT(x, y) CHECK_LT(x, y) |
| 37 | #define DCHECK_GE(x, y) CHECK_GE(x, y) |
| 38 | #define DCHECK_GT(x, y) CHECK_GT(x, y) |
| 39 | |
| 40 | #else // NDEBUG |
| 41 | |
| 42 | #define DCHECK(condition) \ |
| 43 | while (false) \ |
| 44 | CHECK(condition) |
| 45 | |
| 46 | #define DCHECK_EQ(val1, val2) \ |
| 47 | while (false) \ |
| 48 | CHECK_EQ(val1, val2) |
| 49 | |
| 50 | #define DCHECK_NE(val1, val2) \ |
| 51 | while (false) \ |
| 52 | CHECK_NE(val1, val2) |
| 53 | |
| 54 | #define DCHECK_LE(val1, val2) \ |
| 55 | while (false) \ |
| 56 | CHECK_LE(val1, val2) |
| 57 | |
| 58 | #define DCHECK_LT(val1, val2) \ |
| 59 | while (false) \ |
| 60 | CHECK_LT(val1, val2) |
| 61 | |
| 62 | #define DCHECK_GE(val1, val2) \ |
| 63 | while (false) \ |
| 64 | CHECK_GE(val1, val2) |
| 65 | |
| 66 | #define DCHECK_GT(val1, val2) \ |
| 67 | while (false) \ |
| 68 | CHECK_GT(val1, val2) |
| 69 | |
| 70 | #define DCHECK_STREQ(str1, str2) \ |
| 71 | while (false) \ |
| 72 | CHECK_STREQ(str1, str2) |
| 73 | |
| 74 | #endif |
| 75 | |
| 76 | #define LOG_INFO LogMessage(__FILE__, __LINE__) |
| 77 | #define LOG(severity) LOG_ ## severity.stream() |
| 78 | #define LG LOG(INFO) |
| 79 | |
| 80 | class LogMessage { |
| 81 | public: |
| 82 | LogMessage(const char* file, int line) {} |
| 83 | ~LogMessage() { std::cerr << "\n"; } |
| 84 | std::ostream& stream() { return std::cerr; } |
| 85 | private: |
| 86 | DISALLOW_COPY_AND_ASSIGN(LogMessage); |
| 87 | }; |
| 88 | |
| 89 | class LogMessageFatal : public LogMessage { |
| 90 | public: |
| 91 | LogMessageFatal(const char* file, int line) |
| 92 | : LogMessage(file, line) {} |
| 93 | ~LogMessageFatal() { |
| 94 | std::cerr << "\n"; |
| 95 | std::abort(); |
| 96 | } |
| 97 | private: |
| 98 | DISALLOW_COPY_AND_ASSIGN(LogMessageFatal); |
| 99 | }; |
| 100 | |
| 101 | #endif // SRC_LOGGING_H_ |