Elliott Hughes | 42ee142 | 2011-09-06 12:33:32 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Carl Shapiro | 6c21dc1 | 2011-06-20 15:20:52 -0700 | [diff] [blame] | 16 | |
Carl Shapiro | 6b6b5f0 | 2011-06-21 15:05:09 -0700 | [diff] [blame] | 17 | #ifndef ART_SRC_LOGGING_H_ |
| 18 | #define ART_SRC_LOGGING_H_ |
Carl Shapiro | 6c21dc1 | 2011-06-20 15:20:52 -0700 | [diff] [blame] | 19 | |
Elliott Hughes | eb4f614 | 2011-07-15 17:43:51 -0700 | [diff] [blame] | 20 | #include <cerrno> |
| 21 | #include <cstring> |
Carl Shapiro | 6c21dc1 | 2011-06-20 15:20:52 -0700 | [diff] [blame] | 22 | #include <iostream> // NOLINT |
Elliott Hughes | eb4f614 | 2011-07-15 17:43:51 -0700 | [diff] [blame] | 23 | #include <sstream> |
| 24 | #include "log_severity.h" |
| 25 | #include "macros.h" |
Carl Shapiro | 6c21dc1 | 2011-06-20 15:20:52 -0700 | [diff] [blame] | 26 | |
| 27 | #define CHECK(x) \ |
Ian Rogers | caab8c4 | 2011-10-12 12:11:18 -0700 | [diff] [blame^] | 28 | if (UNLIKELY(!(x))) \ |
Elliott Hughes | f5a7a47 | 2011-10-07 14:31:02 -0700 | [diff] [blame] | 29 | ::art::LogMessage(__FILE__, __LINE__, FATAL, -1).stream() \ |
Elliott Hughes | 710a0cb | 2011-08-16 14:32:37 -0700 | [diff] [blame] | 30 | << "Check failed: " #x << " " |
Elliott Hughes | eb4f614 | 2011-07-15 17:43:51 -0700 | [diff] [blame] | 31 | |
Elliott Hughes | 1f359b0 | 2011-07-17 14:27:17 -0700 | [diff] [blame] | 32 | #define CHECK_OP(LHS, RHS, OP) \ |
Ian Rogers | caab8c4 | 2011-10-12 12:11:18 -0700 | [diff] [blame^] | 33 | for (::art::EagerEvaluator<typeof(LHS), typeof(RHS)> _values(LHS, RHS); \ |
| 34 | UNLIKELY(!(_values.lhs OP _values.rhs)); ) \ |
Elliott Hughes | f5a7a47 | 2011-10-07 14:31:02 -0700 | [diff] [blame] | 35 | ::art::LogMessage(__FILE__, __LINE__, FATAL, -1).stream() \ |
| 36 | << "Check failed: " << #LHS << " " << #OP << " " << #RHS \ |
| 37 | << " (" #LHS "=" << _values.lhs << ", " #RHS "=" << _values.rhs << ") " |
Elliott Hughes | 1f359b0 | 2011-07-17 14:27:17 -0700 | [diff] [blame] | 38 | |
| 39 | #define CHECK_EQ(x, y) CHECK_OP(x, y, ==) |
| 40 | #define CHECK_NE(x, y) CHECK_OP(x, y, !=) |
| 41 | #define CHECK_LE(x, y) CHECK_OP(x, y, <=) |
| 42 | #define CHECK_LT(x, y) CHECK_OP(x, y, <) |
| 43 | #define CHECK_GE(x, y) CHECK_OP(x, y, >=) |
| 44 | #define CHECK_GT(x, y) CHECK_OP(x, y, >) |
Elliott Hughes | eb4f614 | 2011-07-15 17:43:51 -0700 | [diff] [blame] | 45 | |
| 46 | #define CHECK_STROP(s1, s2, sense) \ |
Ian Rogers | caab8c4 | 2011-10-12 12:11:18 -0700 | [diff] [blame^] | 47 | if (UNLIKELY((strcmp(s1, s2) == 0) != sense)) \ |
Elliott Hughes | f5a7a47 | 2011-10-07 14:31:02 -0700 | [diff] [blame] | 48 | LOG(FATAL) << "Check failed: " \ |
| 49 | << "\"" << s1 << "\"" \ |
| 50 | << (sense ? " == " : " != ") \ |
| 51 | << "\"" << s2 << "\"" |
Carl Shapiro | 6c21dc1 | 2011-06-20 15:20:52 -0700 | [diff] [blame] | 52 | |
Elliott Hughes | 1f359b0 | 2011-07-17 14:27:17 -0700 | [diff] [blame] | 53 | #define CHECK_STREQ(s1, s2) CHECK_STROP(s1, s2, true) |
| 54 | #define CHECK_STRNE(s1, s2) CHECK_STROP(s1, s2, false) |
| 55 | |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 56 | #define CHECK_PTHREAD_CALL(call, args, what) \ |
| 57 | do { \ |
| 58 | int rc = call args; \ |
| 59 | if (rc != 0) { \ |
| 60 | errno = rc; \ |
| 61 | PLOG(FATAL) << # call << " failed for " << what; \ |
| 62 | } \ |
| 63 | } while (false) |
| 64 | |
Carl Shapiro | 6c21dc1 | 2011-06-20 15:20:52 -0700 | [diff] [blame] | 65 | #ifndef NDEBUG |
| 66 | |
| 67 | #define DCHECK(x) CHECK(x) |
| 68 | #define DCHECK_EQ(x, y) CHECK_EQ(x, y) |
| 69 | #define DCHECK_NE(x, y) CHECK_NE(x, y) |
| 70 | #define DCHECK_LE(x, y) CHECK_LE(x, y) |
| 71 | #define DCHECK_LT(x, y) CHECK_LT(x, y) |
| 72 | #define DCHECK_GE(x, y) CHECK_GE(x, y) |
| 73 | #define DCHECK_GT(x, y) CHECK_GT(x, y) |
Elliott Hughes | eb4f614 | 2011-07-15 17:43:51 -0700 | [diff] [blame] | 74 | #define DCHECK_STREQ(s1, s2) CHECK_STREQ(s1, s2) |
| 75 | #define DCHECK_STRNE(s1, s2) CHECK_STRNE(s1, s2) |
Carl Shapiro | 6c21dc1 | 2011-06-20 15:20:52 -0700 | [diff] [blame] | 76 | |
| 77 | #else // NDEBUG |
| 78 | |
| 79 | #define DCHECK(condition) \ |
| 80 | while (false) \ |
| 81 | CHECK(condition) |
| 82 | |
| 83 | #define DCHECK_EQ(val1, val2) \ |
| 84 | while (false) \ |
| 85 | CHECK_EQ(val1, val2) |
| 86 | |
| 87 | #define DCHECK_NE(val1, val2) \ |
| 88 | while (false) \ |
| 89 | CHECK_NE(val1, val2) |
| 90 | |
| 91 | #define DCHECK_LE(val1, val2) \ |
| 92 | while (false) \ |
| 93 | CHECK_LE(val1, val2) |
| 94 | |
| 95 | #define DCHECK_LT(val1, val2) \ |
| 96 | while (false) \ |
| 97 | CHECK_LT(val1, val2) |
| 98 | |
| 99 | #define DCHECK_GE(val1, val2) \ |
| 100 | while (false) \ |
| 101 | CHECK_GE(val1, val2) |
| 102 | |
| 103 | #define DCHECK_GT(val1, val2) \ |
| 104 | while (false) \ |
| 105 | CHECK_GT(val1, val2) |
| 106 | |
| 107 | #define DCHECK_STREQ(str1, str2) \ |
| 108 | while (false) \ |
| 109 | CHECK_STREQ(str1, str2) |
| 110 | |
Elliott Hughes | eb4f614 | 2011-07-15 17:43:51 -0700 | [diff] [blame] | 111 | #define DCHECK_STRNE(str1, str2) \ |
| 112 | while (false) \ |
| 113 | CHECK_STRNE(str1, str2) |
| 114 | |
Carl Shapiro | 6c21dc1 | 2011-06-20 15:20:52 -0700 | [diff] [blame] | 115 | #endif |
| 116 | |
Elliott Hughes | f5a7a47 | 2011-10-07 14:31:02 -0700 | [diff] [blame] | 117 | #define LOG(severity) ::art::LogMessage(__FILE__, __LINE__, severity, -1).stream() |
| 118 | #define PLOG(severity) ::art::LogMessage(__FILE__, __LINE__, severity, errno).stream() |
Elliott Hughes | eb4f614 | 2011-07-15 17:43:51 -0700 | [diff] [blame] | 119 | |
Carl Shapiro | 6c21dc1 | 2011-06-20 15:20:52 -0700 | [diff] [blame] | 120 | #define LG LOG(INFO) |
| 121 | |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 122 | #define UNIMPLEMENTED(level) LOG(level) << __PRETTY_FUNCTION__ << " unimplemented " |
Elliott Hughes | 53b6131 | 2011-08-12 18:28:20 -0700 | [diff] [blame] | 123 | |
Elliott Hughes | 3ea7e99 | 2011-10-11 18:48:16 -0700 | [diff] [blame] | 124 | // |
| 125 | // Implementation details beyond this point. |
| 126 | // |
| 127 | |
| 128 | namespace art { |
| 129 | |
| 130 | template <typename LHS, typename RHS> |
| 131 | struct EagerEvaluator { |
| 132 | EagerEvaluator(LHS lhs, RHS rhs) : lhs(lhs), rhs(rhs) { } |
| 133 | LHS lhs; |
| 134 | RHS rhs; |
| 135 | }; |
| 136 | |
| 137 | class LogMessage { |
| 138 | public: |
| 139 | LogMessage(const char* file, int line, LogSeverity severity, int error); |
| 140 | ~LogMessage(); |
| 141 | std::ostream& stream(); |
| 142 | |
| 143 | private: |
| 144 | void LogLine(const char*); |
| 145 | |
| 146 | std::stringstream buffer_; |
| 147 | const char* file_; |
| 148 | int line_number_; |
| 149 | LogSeverity severity_; |
| 150 | int errno_; |
| 151 | |
| 152 | DISALLOW_COPY_AND_ASSIGN(LogMessage); |
| 153 | }; |
| 154 | |
| 155 | } // namespace art |
| 156 | |
Carl Shapiro | 6b6b5f0 | 2011-06-21 15:05:09 -0700 | [diff] [blame] | 157 | #endif // ART_SRC_LOGGING_H_ |