Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 | */ |
| 16 | |
| 17 | #ifndef BASE_LOGGING_H |
| 18 | #define BASE_LOGGING_H |
| 19 | |
Dan Albert | 2f638c0 | 2015-05-04 16:40:38 -0700 | [diff] [blame] | 20 | #ifdef ERROR |
| 21 | #error ERROR is already defined. If this is Windows code, #define NOGDI before \ |
| 22 | including anything. |
| 23 | #endif |
| 24 | |
| 25 | #ifdef _WIN32 |
Spencer Low | 5d6ee46 | 2015-05-06 16:13:42 -0700 | [diff] [blame^] | 26 | #ifndef NOGDI |
Dan Albert | 2f638c0 | 2015-05-04 16:40:38 -0700 | [diff] [blame] | 27 | #define NOGDI // Suppress the evil ERROR macro. |
| 28 | #endif |
Spencer Low | 5d6ee46 | 2015-05-06 16:13:42 -0700 | [diff] [blame^] | 29 | #endif |
Dan Albert | 2f638c0 | 2015-05-04 16:40:38 -0700 | [diff] [blame] | 30 | |
Dan Albert | 1f65c49 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 31 | #include <functional> |
Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 32 | #include <memory> |
| 33 | #include <ostream> |
| 34 | |
| 35 | #include "base/macros.h" |
| 36 | |
| 37 | namespace android { |
| 38 | namespace base { |
| 39 | |
| 40 | enum LogSeverity { |
| 41 | VERBOSE, |
| 42 | DEBUG, |
| 43 | INFO, |
| 44 | WARNING, |
| 45 | ERROR, |
| 46 | FATAL, |
| 47 | }; |
| 48 | |
Dan Albert | ab5c882 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 49 | enum LogId { |
Dan Albert | 1f65c49 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 50 | DEFAULT, |
Dan Albert | ab5c882 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 51 | MAIN, |
| 52 | SYSTEM, |
| 53 | }; |
| 54 | |
Dan Albert | 1f65c49 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 55 | typedef std::function<void(LogId, LogSeverity, const char*, const char*, |
| 56 | unsigned int, const char*)> LogFunction; |
| 57 | |
| 58 | extern void StderrLogger(LogId, LogSeverity, const char*, const char*, |
| 59 | unsigned int, const char*); |
| 60 | |
| 61 | #ifdef __ANDROID__ |
| 62 | // We expose this even though it is the default because a user that wants to |
| 63 | // override the default log buffer will have to construct this themselves. |
| 64 | class LogdLogger { |
| 65 | public: |
| 66 | explicit LogdLogger(LogId default_log_id = android::base::MAIN); |
| 67 | |
| 68 | void operator()(LogId, LogSeverity, const char* tag, const char* file, |
| 69 | unsigned int line, const char* message); |
| 70 | |
| 71 | private: |
| 72 | LogId default_log_id_; |
| 73 | }; |
| 74 | #endif |
| 75 | |
Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 76 | // Configure logging based on ANDROID_LOG_TAGS environment variable. |
| 77 | // We need to parse a string that looks like |
| 78 | // |
| 79 | // *:v jdwp:d dalvikvm:d dalvikvm-gc:i dalvikvmi:i |
| 80 | // |
| 81 | // The tag (or '*' for the global level) comes first, followed by a colon and a |
| 82 | // letter indicating the minimum priority level we're expected to log. This can |
| 83 | // be used to reveal or conceal logs with specific tags. |
Dan Albert | 1f65c49 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 84 | extern void InitLogging(char* argv[], LogFunction&& logger); |
| 85 | |
| 86 | // Configures logging using the default logger (logd for the device, stderr for |
| 87 | // the host). |
Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 88 | extern void InitLogging(char* argv[]); |
| 89 | |
Dan Albert | 1f65c49 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 90 | // Replace the current logger. |
| 91 | extern void SetLogger(LogFunction&& logger); |
| 92 | |
Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 93 | // Logs a message to logcat on Android otherwise to stderr. If the severity is |
| 94 | // FATAL it also causes an abort. For example: |
| 95 | // |
| 96 | // LOG(FATAL) << "We didn't expect to reach here"; |
Dan Albert | 1f65c49 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 97 | #define LOG(severity) \ |
| 98 | ::android::base::LogMessage(__FILE__, __LINE__, ::android::base::DEFAULT, \ |
Dan Albert | ab5c882 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 99 | ::android::base::severity, -1).stream() |
| 100 | |
| 101 | // Logs a message to logcat with the specified log ID on Android otherwise to |
| 102 | // stderr. If the severity is FATAL it also causes an abort. |
| 103 | #define LOG_TO(dest, severity) \ |
| 104 | ::android::base::LogMessage(__FILE__, __LINE__, ::android::base::dest, \ |
| 105 | ::android::base::severity, -1).stream() |
Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 106 | |
| 107 | // A variant of LOG that also logs the current errno value. To be used when |
| 108 | // library calls fail. |
Dan Albert | 1f65c49 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 109 | #define PLOG(severity) \ |
| 110 | ::android::base::LogMessage(__FILE__, __LINE__, ::android::base::DEFAULT, \ |
Dan Albert | ab5c882 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 111 | ::android::base::severity, errno).stream() |
| 112 | |
| 113 | // Behaves like PLOG, but logs to the specified log ID. |
| 114 | #define PLOG_TO(dest, severity) \ |
| 115 | ::android::base::LogMessage(__FILE__, __LINE__, ::android::base::dest, \ |
| 116 | ::android::base::severity, errno).stream() |
Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 117 | |
| 118 | // Marker that code is yet to be implemented. |
| 119 | #define UNIMPLEMENTED(level) \ |
| 120 | LOG(level) << __PRETTY_FUNCTION__ << " unimplemented " |
| 121 | |
| 122 | // Check whether condition x holds and LOG(FATAL) if not. The value of the |
| 123 | // expression x is only evaluated once. Extra logging can be appended using << |
| 124 | // after. For example: |
| 125 | // |
| 126 | // CHECK(false == true) results in a log message of |
| 127 | // "Check failed: false == true". |
Dan Albert | 1f65c49 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 128 | #define CHECK(x) \ |
| 129 | if (UNLIKELY(!(x))) \ |
| 130 | ::android::base::LogMessage(__FILE__, __LINE__, ::android::base::DEFAULT, \ |
| 131 | ::android::base::FATAL, -1).stream() \ |
Dan Albert | ab5c882 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 132 | << "Check failed: " #x << " " |
Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 133 | |
| 134 | // Helper for CHECK_xx(x,y) macros. |
Dan Albert | 1f65c49 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 135 | #define CHECK_OP(LHS, RHS, OP) \ |
| 136 | for (auto _values = ::android::base::MakeEagerEvaluator(LHS, RHS); \ |
| 137 | UNLIKELY(!(_values.lhs OP _values.rhs)); \ |
| 138 | /* empty */) \ |
| 139 | ::android::base::LogMessage(__FILE__, __LINE__, ::android::base::DEFAULT, \ |
| 140 | ::android::base::FATAL, -1).stream() \ |
| 141 | << "Check failed: " << #LHS << " " << #OP << " " << #RHS \ |
Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 142 | << " (" #LHS "=" << _values.lhs << ", " #RHS "=" << _values.rhs << ") " |
| 143 | |
| 144 | // Check whether a condition holds between x and y, LOG(FATAL) if not. The value |
| 145 | // of the expressions x and y is evaluated once. Extra logging can be appended |
| 146 | // using << after. For example: |
| 147 | // |
| 148 | // CHECK_NE(0 == 1, false) results in |
| 149 | // "Check failed: false != false (0==1=false, false=false) ". |
| 150 | #define CHECK_EQ(x, y) CHECK_OP(x, y, == ) |
| 151 | #define CHECK_NE(x, y) CHECK_OP(x, y, != ) |
| 152 | #define CHECK_LE(x, y) CHECK_OP(x, y, <= ) |
| 153 | #define CHECK_LT(x, y) CHECK_OP(x, y, < ) |
| 154 | #define CHECK_GE(x, y) CHECK_OP(x, y, >= ) |
| 155 | #define CHECK_GT(x, y) CHECK_OP(x, y, > ) |
| 156 | |
| 157 | // Helper for CHECK_STRxx(s1,s2) macros. |
| 158 | #define CHECK_STROP(s1, s2, sense) \ |
| 159 | if (UNLIKELY((strcmp(s1, s2) == 0) != sense)) \ |
| 160 | LOG(FATAL) << "Check failed: " \ |
| 161 | << "\"" << s1 << "\"" \ |
| 162 | << (sense ? " == " : " != ") << "\"" << s2 << "\"" |
| 163 | |
| 164 | // Check for string (const char*) equality between s1 and s2, LOG(FATAL) if not. |
| 165 | #define CHECK_STREQ(s1, s2) CHECK_STROP(s1, s2, true) |
| 166 | #define CHECK_STRNE(s1, s2) CHECK_STROP(s1, s2, false) |
| 167 | |
| 168 | // Perform the pthread function call(args), LOG(FATAL) on error. |
| 169 | #define CHECK_PTHREAD_CALL(call, args, what) \ |
| 170 | do { \ |
| 171 | int rc = call args; \ |
| 172 | if (rc != 0) { \ |
| 173 | errno = rc; \ |
| 174 | PLOG(FATAL) << #call << " failed for " << what; \ |
| 175 | } \ |
| 176 | } while (false) |
| 177 | |
| 178 | // CHECK that can be used in a constexpr function. For example: |
| 179 | // |
| 180 | // constexpr int half(int n) { |
| 181 | // return |
| 182 | // DCHECK_CONSTEXPR(n >= 0, , 0) |
| 183 | // CHECK_CONSTEXPR((n & 1) == 0), |
| 184 | // << "Extra debugging output: n = " << n, 0) |
| 185 | // n / 2; |
| 186 | // } |
| 187 | #define CHECK_CONSTEXPR(x, out, dummy) \ |
| 188 | (UNLIKELY(!(x))) \ |
| 189 | ? (LOG(FATAL) << "Check failed: " << #x out, dummy) \ |
| 190 | : |
| 191 | |
| 192 | // DCHECKs are debug variants of CHECKs only enabled in debug builds. Generally |
| 193 | // CHECK should be used unless profiling identifies a CHECK as being in |
| 194 | // performance critical code. |
| 195 | #if defined(NDEBUG) |
| 196 | static constexpr bool kEnableDChecks = false; |
| 197 | #else |
| 198 | static constexpr bool kEnableDChecks = true; |
| 199 | #endif |
| 200 | |
| 201 | #define DCHECK(x) \ |
| 202 | if (::android::base::kEnableDChecks) CHECK(x) |
| 203 | #define DCHECK_EQ(x, y) \ |
| 204 | if (::android::base::kEnableDChecks) CHECK_EQ(x, y) |
| 205 | #define DCHECK_NE(x, y) \ |
| 206 | if (::android::base::kEnableDChecks) CHECK_NE(x, y) |
| 207 | #define DCHECK_LE(x, y) \ |
| 208 | if (::android::base::kEnableDChecks) CHECK_LE(x, y) |
| 209 | #define DCHECK_LT(x, y) \ |
| 210 | if (::android::base::kEnableDChecks) CHECK_LT(x, y) |
| 211 | #define DCHECK_GE(x, y) \ |
| 212 | if (::android::base::kEnableDChecks) CHECK_GE(x, y) |
| 213 | #define DCHECK_GT(x, y) \ |
| 214 | if (::android::base::kEnableDChecks) CHECK_GT(x, y) |
| 215 | #define DCHECK_STREQ(s1, s2) \ |
| 216 | if (::android::base::kEnableDChecks) CHECK_STREQ(s1, s2) |
| 217 | #define DCHECK_STRNE(s1, s2) \ |
| 218 | if (::android::base::kEnableDChecks) CHECK_STRNE(s1, s2) |
| 219 | #if defined(NDEBUG) |
| 220 | #define DCHECK_CONSTEXPR(x, out, dummy) |
| 221 | #else |
| 222 | #define DCHECK_CONSTEXPR(x, out, dummy) CHECK_CONSTEXPR(x, out, dummy) |
| 223 | #endif |
| 224 | |
| 225 | // Temporary class created to evaluate the LHS and RHS, used with |
| 226 | // MakeEagerEvaluator to infer the types of LHS and RHS. |
| 227 | template <typename LHS, typename RHS> |
| 228 | struct EagerEvaluator { |
| 229 | EagerEvaluator(LHS l, RHS r) : lhs(l), rhs(r) { |
| 230 | } |
| 231 | LHS lhs; |
| 232 | RHS rhs; |
| 233 | }; |
| 234 | |
| 235 | // Helper function for CHECK_xx. |
| 236 | template <typename LHS, typename RHS> |
| 237 | static inline EagerEvaluator<LHS, RHS> MakeEagerEvaluator(LHS lhs, RHS rhs) { |
| 238 | return EagerEvaluator<LHS, RHS>(lhs, rhs); |
| 239 | } |
| 240 | |
| 241 | // Explicitly instantiate EagerEvalue for pointers so that char*s aren't treated |
| 242 | // as strings. To compare strings use CHECK_STREQ and CHECK_STRNE. We rely on |
| 243 | // signed/unsigned warnings to protect you against combinations not explicitly |
| 244 | // listed below. |
| 245 | #define EAGER_PTR_EVALUATOR(T1, T2) \ |
| 246 | template <> \ |
| 247 | struct EagerEvaluator<T1, T2> { \ |
| 248 | EagerEvaluator(T1 l, T2 r) \ |
| 249 | : lhs(reinterpret_cast<const void*>(l)), \ |
| 250 | rhs(reinterpret_cast<const void*>(r)) { \ |
| 251 | } \ |
| 252 | const void* lhs; \ |
| 253 | const void* rhs; \ |
| 254 | } |
| 255 | EAGER_PTR_EVALUATOR(const char*, const char*); |
| 256 | EAGER_PTR_EVALUATOR(const char*, char*); |
| 257 | EAGER_PTR_EVALUATOR(char*, const char*); |
| 258 | EAGER_PTR_EVALUATOR(char*, char*); |
| 259 | EAGER_PTR_EVALUATOR(const unsigned char*, const unsigned char*); |
| 260 | EAGER_PTR_EVALUATOR(const unsigned char*, unsigned char*); |
| 261 | EAGER_PTR_EVALUATOR(unsigned char*, const unsigned char*); |
| 262 | EAGER_PTR_EVALUATOR(unsigned char*, unsigned char*); |
| 263 | EAGER_PTR_EVALUATOR(const signed char*, const signed char*); |
| 264 | EAGER_PTR_EVALUATOR(const signed char*, signed char*); |
| 265 | EAGER_PTR_EVALUATOR(signed char*, const signed char*); |
| 266 | EAGER_PTR_EVALUATOR(signed char*, signed char*); |
| 267 | |
| 268 | // Data for the log message, not stored in LogMessage to avoid increasing the |
| 269 | // stack size. |
| 270 | class LogMessageData; |
| 271 | |
| 272 | // A LogMessage is a temporarily scoped object used by LOG and the unlikely part |
| 273 | // of a CHECK. The destructor will abort if the severity is FATAL. |
| 274 | class LogMessage { |
| 275 | public: |
Dan Albert | ab5c882 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 276 | LogMessage(const char* file, unsigned int line, LogId id, |
| 277 | LogSeverity severity, int error); |
Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 278 | |
| 279 | ~LogMessage(); |
| 280 | |
| 281 | // Returns the stream associated with the message, the LogMessage performs |
| 282 | // output when it goes out of scope. |
| 283 | std::ostream& stream(); |
| 284 | |
| 285 | // The routine that performs the actual logging. |
Dan Albert | ab5c882 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 286 | static void LogLine(const char* file, unsigned int line, LogId id, |
| 287 | LogSeverity severity, const char* msg); |
Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 288 | |
Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 289 | private: |
| 290 | const std::unique_ptr<LogMessageData> data_; |
| 291 | |
| 292 | DISALLOW_COPY_AND_ASSIGN(LogMessage); |
| 293 | }; |
| 294 | |
| 295 | // Allows to temporarily change the minimum severity level for logging. |
| 296 | class ScopedLogSeverity { |
| 297 | public: |
| 298 | explicit ScopedLogSeverity(LogSeverity level); |
| 299 | ~ScopedLogSeverity(); |
| 300 | |
| 301 | private: |
| 302 | LogSeverity old_; |
| 303 | }; |
| 304 | |
| 305 | } // namespace base |
| 306 | } // namespace android |
| 307 | |
| 308 | #endif // BASE_LOGGING_H |