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) \ |
Elliott Hughes | eb4f614 | 2011-07-15 17:43:51 -0700 | [diff] [blame] | 28 | if (!(x)) \ |
Elliott Hughes | 710a0cb | 2011-08-16 14:32:37 -0700 | [diff] [blame] | 29 | LogMessage(__FILE__, __LINE__, FATAL, -1).stream() \ |
| 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) \ |
| 33 | do { \ |
| 34 | typeof (LHS) _lhs = (LHS); \ |
| 35 | typeof (RHS) _rhs = (RHS); \ |
| 36 | if (!(_lhs OP _rhs)) { \ |
| 37 | LogMessage(__FILE__, __LINE__, FATAL, -1).stream() \ |
| 38 | << "Check failed: " << #LHS << " " << #OP << " " << #RHS \ |
| 39 | << " (" #LHS "=" << _lhs << ", " #RHS "=" << _rhs << ")"; \ |
| 40 | } \ |
| 41 | } while (false) |
| 42 | |
| 43 | #define CHECK_EQ(x, y) CHECK_OP(x, y, ==) |
| 44 | #define CHECK_NE(x, y) CHECK_OP(x, y, !=) |
| 45 | #define CHECK_LE(x, y) CHECK_OP(x, y, <=) |
| 46 | #define CHECK_LT(x, y) CHECK_OP(x, y, <) |
| 47 | #define CHECK_GE(x, y) CHECK_OP(x, y, >=) |
| 48 | #define CHECK_GT(x, y) CHECK_OP(x, y, >) |
Elliott Hughes | eb4f614 | 2011-07-15 17:43:51 -0700 | [diff] [blame] | 49 | |
| 50 | #define CHECK_STROP(s1, s2, sense) \ |
| 51 | do { \ |
| 52 | if ((strcmp(s1, s2) == 0) != sense) { \ |
| 53 | LOG(FATAL) << "Check failed: " \ |
| 54 | << "\"" << s1 << "\"" \ |
| 55 | << (sense ? " == " : " != ") \ |
| 56 | << "\"" << s2 << "\""; \ |
| 57 | } \ |
| 58 | } while (false) |
Carl Shapiro | 6c21dc1 | 2011-06-20 15:20:52 -0700 | [diff] [blame] | 59 | |
Elliott Hughes | 1f359b0 | 2011-07-17 14:27:17 -0700 | [diff] [blame] | 60 | #define CHECK_STREQ(s1, s2) CHECK_STROP(s1, s2, true) |
| 61 | #define CHECK_STRNE(s1, s2) CHECK_STROP(s1, s2, false) |
| 62 | |
Carl Shapiro | 6c21dc1 | 2011-06-20 15:20:52 -0700 | [diff] [blame] | 63 | #ifndef NDEBUG |
| 64 | |
| 65 | #define DCHECK(x) CHECK(x) |
| 66 | #define DCHECK_EQ(x, y) CHECK_EQ(x, y) |
| 67 | #define DCHECK_NE(x, y) CHECK_NE(x, y) |
| 68 | #define DCHECK_LE(x, y) CHECK_LE(x, y) |
| 69 | #define DCHECK_LT(x, y) CHECK_LT(x, y) |
| 70 | #define DCHECK_GE(x, y) CHECK_GE(x, y) |
| 71 | #define DCHECK_GT(x, y) CHECK_GT(x, y) |
Elliott Hughes | eb4f614 | 2011-07-15 17:43:51 -0700 | [diff] [blame] | 72 | #define DCHECK_STREQ(s1, s2) CHECK_STREQ(s1, s2) |
| 73 | #define DCHECK_STRNE(s1, s2) CHECK_STRNE(s1, s2) |
Carl Shapiro | 6c21dc1 | 2011-06-20 15:20:52 -0700 | [diff] [blame] | 74 | |
| 75 | #else // NDEBUG |
| 76 | |
| 77 | #define DCHECK(condition) \ |
| 78 | while (false) \ |
| 79 | CHECK(condition) |
| 80 | |
| 81 | #define DCHECK_EQ(val1, val2) \ |
| 82 | while (false) \ |
| 83 | CHECK_EQ(val1, val2) |
| 84 | |
| 85 | #define DCHECK_NE(val1, val2) \ |
| 86 | while (false) \ |
| 87 | CHECK_NE(val1, val2) |
| 88 | |
| 89 | #define DCHECK_LE(val1, val2) \ |
| 90 | while (false) \ |
| 91 | CHECK_LE(val1, val2) |
| 92 | |
| 93 | #define DCHECK_LT(val1, val2) \ |
| 94 | while (false) \ |
| 95 | CHECK_LT(val1, val2) |
| 96 | |
| 97 | #define DCHECK_GE(val1, val2) \ |
| 98 | while (false) \ |
| 99 | CHECK_GE(val1, val2) |
| 100 | |
| 101 | #define DCHECK_GT(val1, val2) \ |
| 102 | while (false) \ |
| 103 | CHECK_GT(val1, val2) |
| 104 | |
| 105 | #define DCHECK_STREQ(str1, str2) \ |
| 106 | while (false) \ |
| 107 | CHECK_STREQ(str1, str2) |
| 108 | |
Elliott Hughes | eb4f614 | 2011-07-15 17:43:51 -0700 | [diff] [blame] | 109 | #define DCHECK_STRNE(str1, str2) \ |
| 110 | while (false) \ |
| 111 | CHECK_STRNE(str1, str2) |
| 112 | |
Carl Shapiro | 6c21dc1 | 2011-06-20 15:20:52 -0700 | [diff] [blame] | 113 | #endif |
| 114 | |
Elliott Hughes | eb4f614 | 2011-07-15 17:43:51 -0700 | [diff] [blame] | 115 | #define LOG(severity) LogMessage(__FILE__, __LINE__, severity, -1).stream() |
| 116 | #define PLOG(severity) LogMessage(__FILE__, __LINE__, severity, errno).stream() |
| 117 | |
Carl Shapiro | 6c21dc1 | 2011-06-20 15:20:52 -0700 | [diff] [blame] | 118 | #define LG LOG(INFO) |
| 119 | |
Elliott Hughes | 710a0cb | 2011-08-16 14:32:37 -0700 | [diff] [blame] | 120 | #define UNIMPLEMENTED(level) LOG(level) << __FUNCTION__ << " unimplemented " |
Elliott Hughes | 53b6131 | 2011-08-12 18:28:20 -0700 | [diff] [blame] | 121 | |
Carl Shapiro | 6c21dc1 | 2011-06-20 15:20:52 -0700 | [diff] [blame] | 122 | class LogMessage { |
| 123 | public: |
Elliott Hughes | eb4f614 | 2011-07-15 17:43:51 -0700 | [diff] [blame] | 124 | LogMessage(const char* file, int line, LogSeverity severity, int error); |
| 125 | ~LogMessage(); |
| 126 | std::ostream& stream(); |
Carl Shapiro | 6c21dc1 | 2011-06-20 15:20:52 -0700 | [diff] [blame] | 127 | |
Carl Shapiro | 6c21dc1 | 2011-06-20 15:20:52 -0700 | [diff] [blame] | 128 | private: |
Elliott Hughes | 42ee142 | 2011-09-06 12:33:32 -0700 | [diff] [blame^] | 129 | void LogLine(const char*); |
| 130 | |
Elliott Hughes | eb4f614 | 2011-07-15 17:43:51 -0700 | [diff] [blame] | 131 | std::stringstream buffer_; |
Elliott Hughes | ffe6736 | 2011-07-17 12:09:27 -0700 | [diff] [blame] | 132 | const char* file_; |
Elliott Hughes | 42ee142 | 2011-09-06 12:33:32 -0700 | [diff] [blame^] | 133 | int line_number_; |
Elliott Hughes | eb4f614 | 2011-07-15 17:43:51 -0700 | [diff] [blame] | 134 | LogSeverity severity_; |
| 135 | int errno_; |
| 136 | |
| 137 | DISALLOW_COPY_AND_ASSIGN(LogMessage); |
Carl Shapiro | 6c21dc1 | 2011-06-20 15:20:52 -0700 | [diff] [blame] | 138 | }; |
| 139 | |
Carl Shapiro | 6b6b5f0 | 2011-06-21 15:05:09 -0700 | [diff] [blame] | 140 | #endif // ART_SRC_LOGGING_H_ |