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 | |
Carl Shapiro | 6b6b5f0 | 2011-06-21 15:05:09 -0700 | [diff] [blame] | 14 | #ifndef ART_SRC_LOGGING_H_ |
| 15 | #define ART_SRC_LOGGING_H_ |
Carl Shapiro | 6c21dc1 | 2011-06-20 15:20:52 -0700 | [diff] [blame] | 16 | |
Elliott Hughes | eb4f614 | 2011-07-15 17:43:51 -0700 | [diff] [blame^] | 17 | #include <cerrno> |
| 18 | #include <cstring> |
Carl Shapiro | 6c21dc1 | 2011-06-20 15:20:52 -0700 | [diff] [blame] | 19 | #include <iostream> // NOLINT |
Elliott Hughes | eb4f614 | 2011-07-15 17:43:51 -0700 | [diff] [blame^] | 20 | #include <sstream> |
| 21 | #include "log_severity.h" |
| 22 | #include "macros.h" |
Carl Shapiro | 6c21dc1 | 2011-06-20 15:20:52 -0700 | [diff] [blame] | 23 | |
| 24 | #define CHECK(x) \ |
Elliott Hughes | eb4f614 | 2011-07-15 17:43:51 -0700 | [diff] [blame^] | 25 | if (!(x)) \ |
| 26 | LogMessage(__FILE__, __LINE__, FATAL, -1).stream() << "Check failed: " #x |
| 27 | |
Carl Shapiro | 6c21dc1 | 2011-06-20 15:20:52 -0700 | [diff] [blame] | 28 | #define CHECK_EQ(x, y) CHECK((x) == (y)) |
| 29 | #define CHECK_NE(x, y) CHECK((x) != (y)) |
| 30 | #define CHECK_LE(x, y) CHECK((x) <= (y)) |
| 31 | #define CHECK_LT(x, y) CHECK((x) < (y)) |
| 32 | #define CHECK_GE(x, y) CHECK((x) >= (y)) |
| 33 | #define CHECK_GT(x, y) CHECK((x) > (y)) |
Elliott Hughes | eb4f614 | 2011-07-15 17:43:51 -0700 | [diff] [blame^] | 34 | #define CHECK_STREQ(s1, s2) CHECK_STROP(s1, s2, true) |
| 35 | #define CHECK_STRNE(s1, s2) CHECK_STROP(s1, s2, false) |
| 36 | |
| 37 | #define CHECK_STROP(s1, s2, sense) \ |
| 38 | do { \ |
| 39 | if ((strcmp(s1, s2) == 0) != sense) { \ |
| 40 | LOG(FATAL) << "Check failed: " \ |
| 41 | << "\"" << s1 << "\"" \ |
| 42 | << (sense ? " == " : " != ") \ |
| 43 | << "\"" << s2 << "\""; \ |
| 44 | } \ |
| 45 | } while (false) |
Carl Shapiro | 6c21dc1 | 2011-06-20 15:20:52 -0700 | [diff] [blame] | 46 | |
| 47 | #ifndef NDEBUG |
| 48 | |
| 49 | #define DCHECK(x) CHECK(x) |
| 50 | #define DCHECK_EQ(x, y) CHECK_EQ(x, y) |
| 51 | #define DCHECK_NE(x, y) CHECK_NE(x, y) |
| 52 | #define DCHECK_LE(x, y) CHECK_LE(x, y) |
| 53 | #define DCHECK_LT(x, y) CHECK_LT(x, y) |
| 54 | #define DCHECK_GE(x, y) CHECK_GE(x, y) |
| 55 | #define DCHECK_GT(x, y) CHECK_GT(x, y) |
Elliott Hughes | eb4f614 | 2011-07-15 17:43:51 -0700 | [diff] [blame^] | 56 | #define DCHECK_STREQ(s1, s2) CHECK_STREQ(s1, s2) |
| 57 | #define DCHECK_STRNE(s1, s2) CHECK_STRNE(s1, s2) |
Carl Shapiro | 6c21dc1 | 2011-06-20 15:20:52 -0700 | [diff] [blame] | 58 | |
| 59 | #else // NDEBUG |
| 60 | |
| 61 | #define DCHECK(condition) \ |
| 62 | while (false) \ |
| 63 | CHECK(condition) |
| 64 | |
| 65 | #define DCHECK_EQ(val1, val2) \ |
| 66 | while (false) \ |
| 67 | CHECK_EQ(val1, val2) |
| 68 | |
| 69 | #define DCHECK_NE(val1, val2) \ |
| 70 | while (false) \ |
| 71 | CHECK_NE(val1, val2) |
| 72 | |
| 73 | #define DCHECK_LE(val1, val2) \ |
| 74 | while (false) \ |
| 75 | CHECK_LE(val1, val2) |
| 76 | |
| 77 | #define DCHECK_LT(val1, val2) \ |
| 78 | while (false) \ |
| 79 | CHECK_LT(val1, val2) |
| 80 | |
| 81 | #define DCHECK_GE(val1, val2) \ |
| 82 | while (false) \ |
| 83 | CHECK_GE(val1, val2) |
| 84 | |
| 85 | #define DCHECK_GT(val1, val2) \ |
| 86 | while (false) \ |
| 87 | CHECK_GT(val1, val2) |
| 88 | |
| 89 | #define DCHECK_STREQ(str1, str2) \ |
| 90 | while (false) \ |
| 91 | CHECK_STREQ(str1, str2) |
| 92 | |
Elliott Hughes | eb4f614 | 2011-07-15 17:43:51 -0700 | [diff] [blame^] | 93 | #define DCHECK_STRNE(str1, str2) \ |
| 94 | while (false) \ |
| 95 | CHECK_STRNE(str1, str2) |
| 96 | |
Carl Shapiro | 6c21dc1 | 2011-06-20 15:20:52 -0700 | [diff] [blame] | 97 | #endif |
| 98 | |
Elliott Hughes | eb4f614 | 2011-07-15 17:43:51 -0700 | [diff] [blame^] | 99 | #define LOG(severity) LogMessage(__FILE__, __LINE__, severity, -1).stream() |
| 100 | #define PLOG(severity) LogMessage(__FILE__, __LINE__, severity, errno).stream() |
| 101 | |
Carl Shapiro | 6c21dc1 | 2011-06-20 15:20:52 -0700 | [diff] [blame] | 102 | #define LG LOG(INFO) |
| 103 | |
| 104 | class LogMessage { |
| 105 | public: |
Elliott Hughes | eb4f614 | 2011-07-15 17:43:51 -0700 | [diff] [blame^] | 106 | LogMessage(const char* file, int line, LogSeverity severity, int error); |
| 107 | ~LogMessage(); |
| 108 | std::ostream& stream(); |
Carl Shapiro | 6c21dc1 | 2011-06-20 15:20:52 -0700 | [diff] [blame] | 109 | |
Carl Shapiro | 6c21dc1 | 2011-06-20 15:20:52 -0700 | [diff] [blame] | 110 | private: |
Elliott Hughes | eb4f614 | 2011-07-15 17:43:51 -0700 | [diff] [blame^] | 111 | std::stringstream buffer_; |
| 112 | LogSeverity severity_; |
| 113 | int errno_; |
| 114 | |
| 115 | DISALLOW_COPY_AND_ASSIGN(LogMessage); |
Carl Shapiro | 6c21dc1 | 2011-06-20 15:20:52 -0700 | [diff] [blame] | 116 | }; |
| 117 | |
Carl Shapiro | 6b6b5f0 | 2011-06-21 15:05:09 -0700 | [diff] [blame] | 118 | #endif // ART_SRC_LOGGING_H_ |