1*8f0ba417SAndroid Build Coastguard Worker /* 2*8f0ba417SAndroid Build Coastguard Worker * Copyright (C) 2015 The Android Open Source Project 3*8f0ba417SAndroid Build Coastguard Worker * 4*8f0ba417SAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License"); 5*8f0ba417SAndroid Build Coastguard Worker * you may not use this file except in compliance with the License. 6*8f0ba417SAndroid Build Coastguard Worker * You may obtain a copy of the License at 7*8f0ba417SAndroid Build Coastguard Worker * 8*8f0ba417SAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0 9*8f0ba417SAndroid Build Coastguard Worker * 10*8f0ba417SAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software 11*8f0ba417SAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS, 12*8f0ba417SAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13*8f0ba417SAndroid Build Coastguard Worker * See the License for the specific language governing permissions and 14*8f0ba417SAndroid Build Coastguard Worker * limitations under the License. 15*8f0ba417SAndroid Build Coastguard Worker */ 16*8f0ba417SAndroid Build Coastguard Worker 17*8f0ba417SAndroid Build Coastguard Worker #pragma once 18*8f0ba417SAndroid Build Coastguard Worker 19*8f0ba417SAndroid Build Coastguard Worker // 20*8f0ba417SAndroid Build Coastguard Worker // Google-style C++ logging. 21*8f0ba417SAndroid Build Coastguard Worker // 22*8f0ba417SAndroid Build Coastguard Worker 23*8f0ba417SAndroid Build Coastguard Worker // This header provides a C++ stream interface to logging. 24*8f0ba417SAndroid Build Coastguard Worker // 25*8f0ba417SAndroid Build Coastguard Worker // To log: 26*8f0ba417SAndroid Build Coastguard Worker // 27*8f0ba417SAndroid Build Coastguard Worker // LOG(INFO) << "Some text; " << some_value; 28*8f0ba417SAndroid Build Coastguard Worker // 29*8f0ba417SAndroid Build Coastguard Worker // Replace `INFO` with any severity from `enum LogSeverity`. 30*8f0ba417SAndroid Build Coastguard Worker // Most devices filter out VERBOSE logs by default, run 31*8f0ba417SAndroid Build Coastguard Worker // `adb shell setprop log.tag.<TAG> V` to see them in adb logcat. 32*8f0ba417SAndroid Build Coastguard Worker // 33*8f0ba417SAndroid Build Coastguard Worker // To log the result of a failed function and include the string 34*8f0ba417SAndroid Build Coastguard Worker // representation of `errno` at the end: 35*8f0ba417SAndroid Build Coastguard Worker // 36*8f0ba417SAndroid Build Coastguard Worker // PLOG(ERROR) << "Write failed"; 37*8f0ba417SAndroid Build Coastguard Worker // 38*8f0ba417SAndroid Build Coastguard Worker // The output will be something like `Write failed: I/O error`. 39*8f0ba417SAndroid Build Coastguard Worker // Remember this as 'P' as in perror(3). 40*8f0ba417SAndroid Build Coastguard Worker // 41*8f0ba417SAndroid Build Coastguard Worker // To output your own types, simply implement operator<< as normal. 42*8f0ba417SAndroid Build Coastguard Worker // 43*8f0ba417SAndroid Build Coastguard Worker // By default, output goes to logcat on Android and stderr on the host. 44*8f0ba417SAndroid Build Coastguard Worker // A process can use `SetLogger` to decide where all logging goes. 45*8f0ba417SAndroid Build Coastguard Worker // Implementations are provided for logcat, stderr, and dmesg. 46*8f0ba417SAndroid Build Coastguard Worker // 47*8f0ba417SAndroid Build Coastguard Worker // By default, the process' name is used as the log tag. 48*8f0ba417SAndroid Build Coastguard Worker // Code can choose a specific log tag by defining LOG_TAG 49*8f0ba417SAndroid Build Coastguard Worker // before including this header. 50*8f0ba417SAndroid Build Coastguard Worker 51*8f0ba417SAndroid Build Coastguard Worker // This header also provides assertions: 52*8f0ba417SAndroid Build Coastguard Worker // 53*8f0ba417SAndroid Build Coastguard Worker // CHECK(must_be_true); 54*8f0ba417SAndroid Build Coastguard Worker // CHECK_EQ(a, b) << z_is_interesting_too; 55*8f0ba417SAndroid Build Coastguard Worker 56*8f0ba417SAndroid Build Coastguard Worker // NOTE: For Windows, you must include logging.h after windows.h to allow the 57*8f0ba417SAndroid Build Coastguard Worker // following code to suppress the evil ERROR macro: 58*8f0ba417SAndroid Build Coastguard Worker #ifdef _WIN32 59*8f0ba417SAndroid Build Coastguard Worker // windows.h includes wingdi.h which defines an evil macro ERROR. 60*8f0ba417SAndroid Build Coastguard Worker #ifdef ERROR 61*8f0ba417SAndroid Build Coastguard Worker #undef ERROR 62*8f0ba417SAndroid Build Coastguard Worker #endif 63*8f0ba417SAndroid Build Coastguard Worker #endif 64*8f0ba417SAndroid Build Coastguard Worker 65*8f0ba417SAndroid Build Coastguard Worker #include <functional> 66*8f0ba417SAndroid Build Coastguard Worker #include <memory> 67*8f0ba417SAndroid Build Coastguard Worker #include <ostream> 68*8f0ba417SAndroid Build Coastguard Worker 69*8f0ba417SAndroid Build Coastguard Worker #include "android-base/errno_restorer.h" 70*8f0ba417SAndroid Build Coastguard Worker #include "android-base/macros.h" 71*8f0ba417SAndroid Build Coastguard Worker 72*8f0ba417SAndroid Build Coastguard Worker // Note: DO NOT USE DIRECTLY. Use LOG_TAG instead. 73*8f0ba417SAndroid Build Coastguard Worker #ifdef _LOG_TAG_INTERNAL 74*8f0ba417SAndroid Build Coastguard Worker #error "_LOG_TAG_INTERNAL must not be defined" 75*8f0ba417SAndroid Build Coastguard Worker #endif 76*8f0ba417SAndroid Build Coastguard Worker #ifdef LOG_TAG 77*8f0ba417SAndroid Build Coastguard Worker #define _LOG_TAG_INTERNAL LOG_TAG 78*8f0ba417SAndroid Build Coastguard Worker #else 79*8f0ba417SAndroid Build Coastguard Worker #define _LOG_TAG_INTERNAL nullptr 80*8f0ba417SAndroid Build Coastguard Worker #endif 81*8f0ba417SAndroid Build Coastguard Worker 82*8f0ba417SAndroid Build Coastguard Worker namespace android { 83*8f0ba417SAndroid Build Coastguard Worker namespace base { 84*8f0ba417SAndroid Build Coastguard Worker 85*8f0ba417SAndroid Build Coastguard Worker enum LogSeverity { 86*8f0ba417SAndroid Build Coastguard Worker VERBOSE, 87*8f0ba417SAndroid Build Coastguard Worker DEBUG, 88*8f0ba417SAndroid Build Coastguard Worker INFO, 89*8f0ba417SAndroid Build Coastguard Worker WARNING, 90*8f0ba417SAndroid Build Coastguard Worker ERROR, 91*8f0ba417SAndroid Build Coastguard Worker FATAL_WITHOUT_ABORT, // For loggability tests, this is considered identical to FATAL. 92*8f0ba417SAndroid Build Coastguard Worker FATAL, 93*8f0ba417SAndroid Build Coastguard Worker }; 94*8f0ba417SAndroid Build Coastguard Worker 95*8f0ba417SAndroid Build Coastguard Worker enum LogId { 96*8f0ba417SAndroid Build Coastguard Worker DEFAULT, 97*8f0ba417SAndroid Build Coastguard Worker MAIN, 98*8f0ba417SAndroid Build Coastguard Worker SYSTEM, 99*8f0ba417SAndroid Build Coastguard Worker RADIO, 100*8f0ba417SAndroid Build Coastguard Worker CRASH, 101*8f0ba417SAndroid Build Coastguard Worker }; 102*8f0ba417SAndroid Build Coastguard Worker 103*8f0ba417SAndroid Build Coastguard Worker using LogFunction = std::function<void(LogId /*log_buffer_id*/, 104*8f0ba417SAndroid Build Coastguard Worker LogSeverity /*severity*/, 105*8f0ba417SAndroid Build Coastguard Worker const char* /*tag*/, 106*8f0ba417SAndroid Build Coastguard Worker const char* /*file*/, 107*8f0ba417SAndroid Build Coastguard Worker unsigned int /*line*/, 108*8f0ba417SAndroid Build Coastguard Worker const char* /*message*/)>; 109*8f0ba417SAndroid Build Coastguard Worker using AbortFunction = std::function<void(const char* /*abort_message*/)>; 110*8f0ba417SAndroid Build Coastguard Worker 111*8f0ba417SAndroid Build Coastguard Worker // Loggers for use with InitLogging/SetLogger. 112*8f0ba417SAndroid Build Coastguard Worker 113*8f0ba417SAndroid Build Coastguard Worker // Log to the kernel log (dmesg). 114*8f0ba417SAndroid Build Coastguard Worker // Note that you'll likely need to inherit a /dev/kmsg fd from init. 115*8f0ba417SAndroid Build Coastguard Worker // Add `file /dev/kmsg w` to your .rc file. 116*8f0ba417SAndroid Build Coastguard Worker // You'll also need to `allow <your_domain> kmsg_device:chr_file w_file_perms;` 117*8f0ba417SAndroid Build Coastguard Worker // in `system/sepolocy/private/<your_domain>.te`. 118*8f0ba417SAndroid Build Coastguard Worker void KernelLogger(LogId log_buffer_id, LogSeverity severity, const char* tag, const char* file, unsigned int line, const char* message); 119*8f0ba417SAndroid Build Coastguard Worker // Log to stderr in the full logcat format (with pid/tid/time/tag details). 120*8f0ba417SAndroid Build Coastguard Worker void StderrLogger(LogId log_buffer_id, LogSeverity severity, const char* tag, const char* file, unsigned int line, const char* message); 121*8f0ba417SAndroid Build Coastguard Worker // Log just the message to stdout/stderr (without pid/tid/time/tag details). 122*8f0ba417SAndroid Build Coastguard Worker // The choice of stdout versus stderr is based on the severity. 123*8f0ba417SAndroid Build Coastguard Worker // Errors are also prefixed by the program name (as with err(3)/error(3)). 124*8f0ba417SAndroid Build Coastguard Worker // Useful for replacing printf(3)/perror(3)/err(3)/error(3) in command-line tools. 125*8f0ba417SAndroid Build Coastguard Worker void StdioLogger(LogId log_buffer_id, LogSeverity severity, const char* tag, const char* file, unsigned int line, const char* message); 126*8f0ba417SAndroid Build Coastguard Worker // Returns a log function which tees (outputs to both) log streams. 127*8f0ba417SAndroid Build Coastguard Worker // For example: InitLogging(argv, TeeLogger(&StderrLogger, LogdLogger())) 128*8f0ba417SAndroid Build Coastguard Worker LogFunction TeeLogger(LogFunction&& l1, LogFunction&& l2); 129*8f0ba417SAndroid Build Coastguard Worker 130*8f0ba417SAndroid Build Coastguard Worker void DefaultAborter(const char* abort_message); 131*8f0ba417SAndroid Build Coastguard Worker 132*8f0ba417SAndroid Build Coastguard Worker void SetDefaultTag(const std::string& tag); 133*8f0ba417SAndroid Build Coastguard Worker 134*8f0ba417SAndroid Build Coastguard Worker // The LogdLogger sends chunks of up to ~4000 bytes at a time to logd. It does not prevent other 135*8f0ba417SAndroid Build Coastguard Worker // threads from writing to logd between sending each chunk, so other threads may interleave their 136*8f0ba417SAndroid Build Coastguard Worker // messages. If preventing interleaving is required, then a custom logger that takes a lock before 137*8f0ba417SAndroid Build Coastguard Worker // calling this logger should be provided. 138*8f0ba417SAndroid Build Coastguard Worker class LogdLogger { 139*8f0ba417SAndroid Build Coastguard Worker public: 140*8f0ba417SAndroid Build Coastguard Worker explicit LogdLogger(LogId default_log_id = android::base::MAIN); 141*8f0ba417SAndroid Build Coastguard Worker 142*8f0ba417SAndroid Build Coastguard Worker void operator()(LogId, LogSeverity, const char* tag, const char* file, 143*8f0ba417SAndroid Build Coastguard Worker unsigned int line, const char* message); 144*8f0ba417SAndroid Build Coastguard Worker 145*8f0ba417SAndroid Build Coastguard Worker private: 146*8f0ba417SAndroid Build Coastguard Worker LogId default_log_id_; 147*8f0ba417SAndroid Build Coastguard Worker }; 148*8f0ba417SAndroid Build Coastguard Worker 149*8f0ba417SAndroid Build Coastguard Worker // Configure logging based on ANDROID_LOG_TAGS environment variable. 150*8f0ba417SAndroid Build Coastguard Worker // We need to parse a string that looks like 151*8f0ba417SAndroid Build Coastguard Worker // 152*8f0ba417SAndroid Build Coastguard Worker // *:v jdwp:d dalvikvm:d dalvikvm-gc:i dalvikvmi:i 153*8f0ba417SAndroid Build Coastguard Worker // 154*8f0ba417SAndroid Build Coastguard Worker // The tag (or '*' for the global level) comes first, followed by a colon and a 155*8f0ba417SAndroid Build Coastguard Worker // letter indicating the minimum priority level we're expected to log. This can 156*8f0ba417SAndroid Build Coastguard Worker // be used to reveal or conceal logs with specific tags. 157*8f0ba417SAndroid Build Coastguard Worker #ifdef __ANDROID__ 158*8f0ba417SAndroid Build Coastguard Worker #define INIT_LOGGING_DEFAULT_LOGGER LogdLogger() 159*8f0ba417SAndroid Build Coastguard Worker #else 160*8f0ba417SAndroid Build Coastguard Worker #define INIT_LOGGING_DEFAULT_LOGGER StderrLogger 161*8f0ba417SAndroid Build Coastguard Worker #endif 162*8f0ba417SAndroid Build Coastguard Worker void InitLogging(char* argv[], 163*8f0ba417SAndroid Build Coastguard Worker LogFunction&& logger = INIT_LOGGING_DEFAULT_LOGGER, 164*8f0ba417SAndroid Build Coastguard Worker AbortFunction&& aborter = DefaultAborter); 165*8f0ba417SAndroid Build Coastguard Worker #undef INIT_LOGGING_DEFAULT_LOGGER 166*8f0ba417SAndroid Build Coastguard Worker 167*8f0ba417SAndroid Build Coastguard Worker // Replace the current logger and return the old one. 168*8f0ba417SAndroid Build Coastguard Worker LogFunction SetLogger(LogFunction&& logger); 169*8f0ba417SAndroid Build Coastguard Worker 170*8f0ba417SAndroid Build Coastguard Worker // Replace the current aborter and return the old one. 171*8f0ba417SAndroid Build Coastguard Worker AbortFunction SetAborter(AbortFunction&& aborter); 172*8f0ba417SAndroid Build Coastguard Worker 173*8f0ba417SAndroid Build Coastguard Worker // A helper macro that produces an expression that accepts both a qualified name and an 174*8f0ba417SAndroid Build Coastguard Worker // unqualified name for a LogSeverity, and returns a LogSeverity value. 175*8f0ba417SAndroid Build Coastguard Worker // Note: DO NOT USE DIRECTLY. This is an implementation detail. 176*8f0ba417SAndroid Build Coastguard Worker #define SEVERITY_LAMBDA(severity) ([&]() { \ 177*8f0ba417SAndroid Build Coastguard Worker using ::android::base::VERBOSE; \ 178*8f0ba417SAndroid Build Coastguard Worker using ::android::base::DEBUG; \ 179*8f0ba417SAndroid Build Coastguard Worker using ::android::base::INFO; \ 180*8f0ba417SAndroid Build Coastguard Worker using ::android::base::WARNING; \ 181*8f0ba417SAndroid Build Coastguard Worker using ::android::base::ERROR; \ 182*8f0ba417SAndroid Build Coastguard Worker using ::android::base::FATAL_WITHOUT_ABORT; \ 183*8f0ba417SAndroid Build Coastguard Worker using ::android::base::FATAL; \ 184*8f0ba417SAndroid Build Coastguard Worker return (severity); }()) 185*8f0ba417SAndroid Build Coastguard Worker 186*8f0ba417SAndroid Build Coastguard Worker #ifdef __clang_analyzer__ 187*8f0ba417SAndroid Build Coastguard Worker // Clang's static analyzer does not see the conditional statement inside 188*8f0ba417SAndroid Build Coastguard Worker // LogMessage's destructor that will abort on FATAL severity. 189*8f0ba417SAndroid Build Coastguard Worker #define ABORT_AFTER_LOG_FATAL for (;; abort()) 190*8f0ba417SAndroid Build Coastguard Worker 191*8f0ba417SAndroid Build Coastguard Worker struct LogAbortAfterFullExpr { ~LogAbortAfterFullExprLogAbortAfterFullExpr192*8f0ba417SAndroid Build Coastguard Worker ~LogAbortAfterFullExpr() __attribute__((noreturn)) { abort(); } 193*8f0ba417SAndroid Build Coastguard Worker explicit operator bool() const { return false; } 194*8f0ba417SAndroid Build Coastguard Worker }; 195*8f0ba417SAndroid Build Coastguard Worker // Provides an expression that evaluates to the truthiness of `x`, automatically 196*8f0ba417SAndroid Build Coastguard Worker // aborting if `c` is true. 197*8f0ba417SAndroid Build Coastguard Worker #define ABORT_AFTER_LOG_EXPR_IF(c, x) (((c) && ::android::base::LogAbortAfterFullExpr()) || (x)) 198*8f0ba417SAndroid Build Coastguard Worker // Note to the static analyzer that we always execute FATAL logs in practice. 199*8f0ba417SAndroid Build Coastguard Worker #define MUST_LOG_MESSAGE(severity) (SEVERITY_LAMBDA(severity) == ::android::base::FATAL) 200*8f0ba417SAndroid Build Coastguard Worker #else 201*8f0ba417SAndroid Build Coastguard Worker #define ABORT_AFTER_LOG_FATAL 202*8f0ba417SAndroid Build Coastguard Worker #define ABORT_AFTER_LOG_EXPR_IF(c, x) (x) 203*8f0ba417SAndroid Build Coastguard Worker #define MUST_LOG_MESSAGE(severity) false 204*8f0ba417SAndroid Build Coastguard Worker #endif 205*8f0ba417SAndroid Build Coastguard Worker #define ABORT_AFTER_LOG_FATAL_EXPR(x) ABORT_AFTER_LOG_EXPR_IF(true, x) 206*8f0ba417SAndroid Build Coastguard Worker 207*8f0ba417SAndroid Build Coastguard Worker // Defines whether the given severity will be logged or silently swallowed. 208*8f0ba417SAndroid Build Coastguard Worker #define WOULD_LOG(severity) \ 209*8f0ba417SAndroid Build Coastguard Worker (UNLIKELY(::android::base::ShouldLog(SEVERITY_LAMBDA(severity), _LOG_TAG_INTERNAL)) || \ 210*8f0ba417SAndroid Build Coastguard Worker MUST_LOG_MESSAGE(severity)) 211*8f0ba417SAndroid Build Coastguard Worker 212*8f0ba417SAndroid Build Coastguard Worker // Get an ostream that can be used for logging at the given severity and to the default 213*8f0ba417SAndroid Build Coastguard Worker // destination. 214*8f0ba417SAndroid Build Coastguard Worker // 215*8f0ba417SAndroid Build Coastguard Worker // Note that this does not save and restore errno. 216*8f0ba417SAndroid Build Coastguard Worker #define LOG_STREAM(severity) \ 217*8f0ba417SAndroid Build Coastguard Worker ::android::base::LogMessage(__FILE__, __LINE__, SEVERITY_LAMBDA(severity), _LOG_TAG_INTERNAL, \ 218*8f0ba417SAndroid Build Coastguard Worker -1) \ 219*8f0ba417SAndroid Build Coastguard Worker .stream() 220*8f0ba417SAndroid Build Coastguard Worker 221*8f0ba417SAndroid Build Coastguard Worker // Logs a message to logcat on Android otherwise to stderr. If the severity is 222*8f0ba417SAndroid Build Coastguard Worker // FATAL it also causes an abort. For example: 223*8f0ba417SAndroid Build Coastguard Worker // 224*8f0ba417SAndroid Build Coastguard Worker // LOG(FATAL) << "We didn't expect to reach here"; 225*8f0ba417SAndroid Build Coastguard Worker #define LOG(severity) LOGGING_PREAMBLE(severity) && LOG_STREAM(severity) 226*8f0ba417SAndroid Build Coastguard Worker 227*8f0ba417SAndroid Build Coastguard Worker // Conditionally logs a message based on a specified severity level and a boolean condition. 228*8f0ba417SAndroid Build Coastguard Worker // Logs to logcat on Android, or to stderr on host. See also LOG(severity) above. 229*8f0ba417SAndroid Build Coastguard Worker // 230*8f0ba417SAndroid Build Coastguard Worker // The message will only be logged if: 231*8f0ba417SAndroid Build Coastguard Worker // 1. The provided 'cond' evaluates to true. 232*8f0ba417SAndroid Build Coastguard Worker // 2. The 'severity' level is enabled for the current log tag (as determined by the logging 233*8f0ba417SAndroid Build Coastguard Worker // configuration). 234*8f0ba417SAndroid Build Coastguard Worker // 235*8f0ba417SAndroid Build Coastguard Worker // Usage: 236*8f0ba417SAndroid Build Coastguard Worker // 237*8f0ba417SAndroid Build Coastguard Worker // LOG_IF(INFO, some_condition) << "This message will be logged if 'some_condition' is true" << 238*8f0ba417SAndroid Build Coastguard Worker // " and INFO level is enabled."; 239*8f0ba417SAndroid Build Coastguard Worker // 240*8f0ba417SAndroid Build Coastguard Worker // @param severity The severity level of the log message (e.g., VERBOSE, DEBUG, INFO, WARNING, 241*8f0ba417SAndroid Build Coastguard Worker // ERROR, FATAL). 242*8f0ba417SAndroid Build Coastguard Worker // @param cond The boolean condition that determines whether to log the message. 243*8f0ba417SAndroid Build Coastguard Worker #define LOG_IF(severity, cond) \ 244*8f0ba417SAndroid Build Coastguard Worker if (UNLIKELY(cond) && WOULD_LOG(severity)) LOG(severity) 245*8f0ba417SAndroid Build Coastguard Worker 246*8f0ba417SAndroid Build Coastguard Worker // Checks if we want to log something, and sets up appropriate RAII objects if 247*8f0ba417SAndroid Build Coastguard Worker // so. 248*8f0ba417SAndroid Build Coastguard Worker // Note: DO NOT USE DIRECTLY. This is an implementation detail. 249*8f0ba417SAndroid Build Coastguard Worker #define LOGGING_PREAMBLE(severity) \ 250*8f0ba417SAndroid Build Coastguard Worker (WOULD_LOG(severity) && \ 251*8f0ba417SAndroid Build Coastguard Worker ABORT_AFTER_LOG_EXPR_IF((SEVERITY_LAMBDA(severity)) == ::android::base::FATAL, true) && \ 252*8f0ba417SAndroid Build Coastguard Worker ::android::base::ErrnoRestorer()) 253*8f0ba417SAndroid Build Coastguard Worker 254*8f0ba417SAndroid Build Coastguard Worker // A variant of LOG that also logs the current errno value. To be used when 255*8f0ba417SAndroid Build Coastguard Worker // library calls fail. 256*8f0ba417SAndroid Build Coastguard Worker #define PLOG(severity) \ 257*8f0ba417SAndroid Build Coastguard Worker LOGGING_PREAMBLE(severity) && \ 258*8f0ba417SAndroid Build Coastguard Worker ::android::base::LogMessage(__FILE__, __LINE__, SEVERITY_LAMBDA(severity), \ 259*8f0ba417SAndroid Build Coastguard Worker _LOG_TAG_INTERNAL, errno) \ 260*8f0ba417SAndroid Build Coastguard Worker .stream() 261*8f0ba417SAndroid Build Coastguard Worker 262*8f0ba417SAndroid Build Coastguard Worker // Marker that code is yet to be implemented. 263*8f0ba417SAndroid Build Coastguard Worker #define UNIMPLEMENTED(level) \ 264*8f0ba417SAndroid Build Coastguard Worker LOG(level) << __PRETTY_FUNCTION__ << " unimplemented " 265*8f0ba417SAndroid Build Coastguard Worker 266*8f0ba417SAndroid Build Coastguard Worker // Check whether condition x holds and LOG(FATAL) if not. The value of the 267*8f0ba417SAndroid Build Coastguard Worker // expression x is only evaluated once. Extra logging can be appended using << 268*8f0ba417SAndroid Build Coastguard Worker // after. For example: 269*8f0ba417SAndroid Build Coastguard Worker // 270*8f0ba417SAndroid Build Coastguard Worker // CHECK(false == true) results in a log message of 271*8f0ba417SAndroid Build Coastguard Worker // "Check failed: false == true". 272*8f0ba417SAndroid Build Coastguard Worker #define CHECK(x) \ 273*8f0ba417SAndroid Build Coastguard Worker LIKELY((x)) || ABORT_AFTER_LOG_FATAL_EXPR(false) || \ 274*8f0ba417SAndroid Build Coastguard Worker ::android::base::LogMessage(__FILE__, __LINE__, ::android::base::FATAL, _LOG_TAG_INTERNAL, \ 275*8f0ba417SAndroid Build Coastguard Worker -1) \ 276*8f0ba417SAndroid Build Coastguard Worker .stream() \ 277*8f0ba417SAndroid Build Coastguard Worker << "Check failed: " #x << " " 278*8f0ba417SAndroid Build Coastguard Worker 279*8f0ba417SAndroid Build Coastguard Worker // clang-format off 280*8f0ba417SAndroid Build Coastguard Worker // Helper for CHECK_xx(x,y) macros. 281*8f0ba417SAndroid Build Coastguard Worker #define CHECK_OP(LHS, RHS, OP) \ 282*8f0ba417SAndroid Build Coastguard Worker for (auto _values = ::android::base::MakeEagerEvaluator(LHS, RHS); \ 283*8f0ba417SAndroid Build Coastguard Worker UNLIKELY(!(_values.lhs.v OP _values.rhs.v)); \ 284*8f0ba417SAndroid Build Coastguard Worker /* empty */) \ 285*8f0ba417SAndroid Build Coastguard Worker ABORT_AFTER_LOG_FATAL \ 286*8f0ba417SAndroid Build Coastguard Worker ::android::base::LogMessage(__FILE__, __LINE__, ::android::base::FATAL, _LOG_TAG_INTERNAL, -1) \ 287*8f0ba417SAndroid Build Coastguard Worker .stream() \ 288*8f0ba417SAndroid Build Coastguard Worker << "Check failed: " << #LHS << " " << #OP << " " << #RHS << " (" #LHS "=" \ 289*8f0ba417SAndroid Build Coastguard Worker << ::android::base::LogNullGuard<decltype(_values.lhs.v)>::Guard(_values.lhs.v) \ 290*8f0ba417SAndroid Build Coastguard Worker << ", " #RHS "=" \ 291*8f0ba417SAndroid Build Coastguard Worker << ::android::base::LogNullGuard<decltype(_values.rhs.v)>::Guard(_values.rhs.v) \ 292*8f0ba417SAndroid Build Coastguard Worker << ") " 293*8f0ba417SAndroid Build Coastguard Worker // clang-format on 294*8f0ba417SAndroid Build Coastguard Worker 295*8f0ba417SAndroid Build Coastguard Worker // Check whether a condition holds between x and y, LOG(FATAL) if not. The value 296*8f0ba417SAndroid Build Coastguard Worker // of the expressions x and y is evaluated once. Extra logging can be appended 297*8f0ba417SAndroid Build Coastguard Worker // using << after. For example: 298*8f0ba417SAndroid Build Coastguard Worker // 299*8f0ba417SAndroid Build Coastguard Worker // CHECK_NE(0 == 1, false) results in 300*8f0ba417SAndroid Build Coastguard Worker // "Check failed: false != false (0==1=false, false=false) ". 301*8f0ba417SAndroid Build Coastguard Worker #define CHECK_EQ(x, y) CHECK_OP(x, y, == ) 302*8f0ba417SAndroid Build Coastguard Worker #define CHECK_NE(x, y) CHECK_OP(x, y, != ) 303*8f0ba417SAndroid Build Coastguard Worker #define CHECK_LE(x, y) CHECK_OP(x, y, <= ) 304*8f0ba417SAndroid Build Coastguard Worker #define CHECK_LT(x, y) CHECK_OP(x, y, < ) 305*8f0ba417SAndroid Build Coastguard Worker #define CHECK_GE(x, y) CHECK_OP(x, y, >= ) 306*8f0ba417SAndroid Build Coastguard Worker #define CHECK_GT(x, y) CHECK_OP(x, y, > ) 307*8f0ba417SAndroid Build Coastguard Worker 308*8f0ba417SAndroid Build Coastguard Worker // clang-format off 309*8f0ba417SAndroid Build Coastguard Worker // Helper for CHECK_STRxx(s1,s2) macros. 310*8f0ba417SAndroid Build Coastguard Worker #define CHECK_STROP(s1, s2, sense) \ 311*8f0ba417SAndroid Build Coastguard Worker while (UNLIKELY((strcmp(s1, s2) == 0) != (sense))) \ 312*8f0ba417SAndroid Build Coastguard Worker ABORT_AFTER_LOG_FATAL \ 313*8f0ba417SAndroid Build Coastguard Worker ::android::base::LogMessage(__FILE__, __LINE__, ::android::base::FATAL, \ 314*8f0ba417SAndroid Build Coastguard Worker _LOG_TAG_INTERNAL, -1) \ 315*8f0ba417SAndroid Build Coastguard Worker .stream() \ 316*8f0ba417SAndroid Build Coastguard Worker << "Check failed: " << "\"" << (s1) << "\"" \ 317*8f0ba417SAndroid Build Coastguard Worker << ((sense) ? " == " : " != ") << "\"" << (s2) << "\"" 318*8f0ba417SAndroid Build Coastguard Worker // clang-format on 319*8f0ba417SAndroid Build Coastguard Worker 320*8f0ba417SAndroid Build Coastguard Worker // Check for string (const char*) equality between s1 and s2, LOG(FATAL) if not. 321*8f0ba417SAndroid Build Coastguard Worker #define CHECK_STREQ(s1, s2) CHECK_STROP(s1, s2, true) 322*8f0ba417SAndroid Build Coastguard Worker #define CHECK_STRNE(s1, s2) CHECK_STROP(s1, s2, false) 323*8f0ba417SAndroid Build Coastguard Worker 324*8f0ba417SAndroid Build Coastguard Worker // Perform the pthread function call(args), LOG(FATAL) on error. 325*8f0ba417SAndroid Build Coastguard Worker #define CHECK_PTHREAD_CALL(call, args, what) \ 326*8f0ba417SAndroid Build Coastguard Worker do { \ 327*8f0ba417SAndroid Build Coastguard Worker int rc = call args; \ 328*8f0ba417SAndroid Build Coastguard Worker if (rc != 0) { \ 329*8f0ba417SAndroid Build Coastguard Worker errno = rc; \ 330*8f0ba417SAndroid Build Coastguard Worker ABORT_AFTER_LOG_FATAL \ 331*8f0ba417SAndroid Build Coastguard Worker PLOG(FATAL) << #call << " failed for " << (what); \ 332*8f0ba417SAndroid Build Coastguard Worker } \ 333*8f0ba417SAndroid Build Coastguard Worker } while (false) 334*8f0ba417SAndroid Build Coastguard Worker 335*8f0ba417SAndroid Build Coastguard Worker // DCHECKs are debug variants of CHECKs only enabled in debug builds. Generally 336*8f0ba417SAndroid Build Coastguard Worker // CHECK should be used unless profiling identifies a CHECK as being in 337*8f0ba417SAndroid Build Coastguard Worker // performance critical code. 338*8f0ba417SAndroid Build Coastguard Worker #if defined(NDEBUG) && !defined(__clang_analyzer__) 339*8f0ba417SAndroid Build Coastguard Worker static constexpr bool kEnableDChecks = false; 340*8f0ba417SAndroid Build Coastguard Worker #else 341*8f0ba417SAndroid Build Coastguard Worker static constexpr bool kEnableDChecks = true; 342*8f0ba417SAndroid Build Coastguard Worker #endif 343*8f0ba417SAndroid Build Coastguard Worker 344*8f0ba417SAndroid Build Coastguard Worker #define DCHECK(x) \ 345*8f0ba417SAndroid Build Coastguard Worker if (::android::base::kEnableDChecks) CHECK(x) 346*8f0ba417SAndroid Build Coastguard Worker #define DCHECK_EQ(x, y) \ 347*8f0ba417SAndroid Build Coastguard Worker if (::android::base::kEnableDChecks) CHECK_EQ(x, y) 348*8f0ba417SAndroid Build Coastguard Worker #define DCHECK_NE(x, y) \ 349*8f0ba417SAndroid Build Coastguard Worker if (::android::base::kEnableDChecks) CHECK_NE(x, y) 350*8f0ba417SAndroid Build Coastguard Worker #define DCHECK_LE(x, y) \ 351*8f0ba417SAndroid Build Coastguard Worker if (::android::base::kEnableDChecks) CHECK_LE(x, y) 352*8f0ba417SAndroid Build Coastguard Worker #define DCHECK_LT(x, y) \ 353*8f0ba417SAndroid Build Coastguard Worker if (::android::base::kEnableDChecks) CHECK_LT(x, y) 354*8f0ba417SAndroid Build Coastguard Worker #define DCHECK_GE(x, y) \ 355*8f0ba417SAndroid Build Coastguard Worker if (::android::base::kEnableDChecks) CHECK_GE(x, y) 356*8f0ba417SAndroid Build Coastguard Worker #define DCHECK_GT(x, y) \ 357*8f0ba417SAndroid Build Coastguard Worker if (::android::base::kEnableDChecks) CHECK_GT(x, y) 358*8f0ba417SAndroid Build Coastguard Worker #define DCHECK_STREQ(s1, s2) \ 359*8f0ba417SAndroid Build Coastguard Worker if (::android::base::kEnableDChecks) CHECK_STREQ(s1, s2) 360*8f0ba417SAndroid Build Coastguard Worker #define DCHECK_STRNE(s1, s2) \ 361*8f0ba417SAndroid Build Coastguard Worker if (::android::base::kEnableDChecks) CHECK_STRNE(s1, s2) 362*8f0ba417SAndroid Build Coastguard Worker 363*8f0ba417SAndroid Build Coastguard Worker namespace log_detail { 364*8f0ba417SAndroid Build Coastguard Worker 365*8f0ba417SAndroid Build Coastguard Worker // Temporary storage for a single eagerly evaluated check expression operand. 366*8f0ba417SAndroid Build Coastguard Worker template <typename T> struct Storage { StorageStorage367*8f0ba417SAndroid Build Coastguard Worker template <typename U> explicit constexpr Storage(U&& u) : v(std::forward<U>(u)) {} 368*8f0ba417SAndroid Build Coastguard Worker explicit Storage(const Storage& t) = delete; 369*8f0ba417SAndroid Build Coastguard Worker explicit Storage(Storage&& t) = delete; 370*8f0ba417SAndroid Build Coastguard Worker T v; 371*8f0ba417SAndroid Build Coastguard Worker }; 372*8f0ba417SAndroid Build Coastguard Worker 373*8f0ba417SAndroid Build Coastguard Worker // Partial specialization for smart pointers to avoid copying. 374*8f0ba417SAndroid Build Coastguard Worker template <typename T> struct Storage<std::unique_ptr<T>> { 375*8f0ba417SAndroid Build Coastguard Worker explicit constexpr Storage(const std::unique_ptr<T>& ptr) : v(ptr.get()) {} 376*8f0ba417SAndroid Build Coastguard Worker const T* v; 377*8f0ba417SAndroid Build Coastguard Worker }; 378*8f0ba417SAndroid Build Coastguard Worker template <typename T> struct Storage<std::shared_ptr<T>> { 379*8f0ba417SAndroid Build Coastguard Worker explicit constexpr Storage(const std::shared_ptr<T>& ptr) : v(ptr.get()) {} 380*8f0ba417SAndroid Build Coastguard Worker const T* v; 381*8f0ba417SAndroid Build Coastguard Worker }; 382*8f0ba417SAndroid Build Coastguard Worker 383*8f0ba417SAndroid Build Coastguard Worker // Type trait that checks if a type is a (potentially const) char pointer. 384*8f0ba417SAndroid Build Coastguard Worker template <typename T> struct IsCharPointer { 385*8f0ba417SAndroid Build Coastguard Worker using Pointee = std::remove_cv_t<std::remove_pointer_t<T>>; 386*8f0ba417SAndroid Build Coastguard Worker static constexpr bool value = std::is_pointer_v<T> && 387*8f0ba417SAndroid Build Coastguard Worker (std::is_same_v<Pointee, char> || std::is_same_v<Pointee, signed char> || 388*8f0ba417SAndroid Build Coastguard Worker std::is_same_v<Pointee, unsigned char>); 389*8f0ba417SAndroid Build Coastguard Worker }; 390*8f0ba417SAndroid Build Coastguard Worker 391*8f0ba417SAndroid Build Coastguard Worker // Counterpart to Storage that depends on both operands. This is used to prevent 392*8f0ba417SAndroid Build Coastguard Worker // char pointers being treated as strings in the log output - they might point 393*8f0ba417SAndroid Build Coastguard Worker // to buffers of unprintable binary data. 394*8f0ba417SAndroid Build Coastguard Worker template <typename LHS, typename RHS> struct StorageTypes { 395*8f0ba417SAndroid Build Coastguard Worker static constexpr bool voidptr = IsCharPointer<LHS>::value && IsCharPointer<RHS>::value; 396*8f0ba417SAndroid Build Coastguard Worker using LHSType = std::conditional_t<voidptr, const void*, LHS>; 397*8f0ba417SAndroid Build Coastguard Worker using RHSType = std::conditional_t<voidptr, const void*, RHS>; 398*8f0ba417SAndroid Build Coastguard Worker }; 399*8f0ba417SAndroid Build Coastguard Worker 400*8f0ba417SAndroid Build Coastguard Worker // Temporary class created to evaluate the LHS and RHS, used with 401*8f0ba417SAndroid Build Coastguard Worker // MakeEagerEvaluator to infer the types of LHS and RHS. 402*8f0ba417SAndroid Build Coastguard Worker template <typename LHS, typename RHS> 403*8f0ba417SAndroid Build Coastguard Worker struct EagerEvaluator { 404*8f0ba417SAndroid Build Coastguard Worker template <typename A, typename B> constexpr EagerEvaluator(A&& l, B&& r) 405*8f0ba417SAndroid Build Coastguard Worker : lhs(std::forward<A>(l)), rhs(std::forward<B>(r)) {} 406*8f0ba417SAndroid Build Coastguard Worker const Storage<typename StorageTypes<LHS, RHS>::LHSType> lhs; 407*8f0ba417SAndroid Build Coastguard Worker const Storage<typename StorageTypes<LHS, RHS>::RHSType> rhs; 408*8f0ba417SAndroid Build Coastguard Worker }; 409*8f0ba417SAndroid Build Coastguard Worker 410*8f0ba417SAndroid Build Coastguard Worker } // namespace log_detail 411*8f0ba417SAndroid Build Coastguard Worker 412*8f0ba417SAndroid Build Coastguard Worker // Converts std::nullptr_t and null char pointers to the string "null" 413*8f0ba417SAndroid Build Coastguard Worker // when writing the failure message. 414*8f0ba417SAndroid Build Coastguard Worker template <typename T> struct LogNullGuard { 415*8f0ba417SAndroid Build Coastguard Worker static const T& Guard(const T& v) { return v; } 416*8f0ba417SAndroid Build Coastguard Worker }; 417*8f0ba417SAndroid Build Coastguard Worker template <> struct LogNullGuard<std::nullptr_t> { 418*8f0ba417SAndroid Build Coastguard Worker static const char* Guard(const std::nullptr_t&) { return "(null)"; } 419*8f0ba417SAndroid Build Coastguard Worker }; 420*8f0ba417SAndroid Build Coastguard Worker template <> struct LogNullGuard<char*> { 421*8f0ba417SAndroid Build Coastguard Worker static const char* Guard(const char* v) { return v ? v : "(null)"; } 422*8f0ba417SAndroid Build Coastguard Worker }; 423*8f0ba417SAndroid Build Coastguard Worker template <> struct LogNullGuard<const char*> { 424*8f0ba417SAndroid Build Coastguard Worker static const char* Guard(const char* v) { return v ? v : "(null)"; } 425*8f0ba417SAndroid Build Coastguard Worker }; 426*8f0ba417SAndroid Build Coastguard Worker 427*8f0ba417SAndroid Build Coastguard Worker // Helper function for CHECK_xx. 428*8f0ba417SAndroid Build Coastguard Worker template <typename LHS, typename RHS> 429*8f0ba417SAndroid Build Coastguard Worker constexpr auto MakeEagerEvaluator(LHS&& lhs, RHS&& rhs) { 430*8f0ba417SAndroid Build Coastguard Worker return log_detail::EagerEvaluator<std::decay_t<LHS>, std::decay_t<RHS>>( 431*8f0ba417SAndroid Build Coastguard Worker std::forward<LHS>(lhs), std::forward<RHS>(rhs)); 432*8f0ba417SAndroid Build Coastguard Worker } 433*8f0ba417SAndroid Build Coastguard Worker 434*8f0ba417SAndroid Build Coastguard Worker // Data for the log message, not stored in LogMessage to avoid increasing the 435*8f0ba417SAndroid Build Coastguard Worker // stack size. 436*8f0ba417SAndroid Build Coastguard Worker class LogMessageData; 437*8f0ba417SAndroid Build Coastguard Worker 438*8f0ba417SAndroid Build Coastguard Worker // A LogMessage is a temporarily scoped object used by LOG and the unlikely part 439*8f0ba417SAndroid Build Coastguard Worker // of a CHECK. The destructor will abort if the severity is FATAL. 440*8f0ba417SAndroid Build Coastguard Worker class LogMessage { 441*8f0ba417SAndroid Build Coastguard Worker public: 442*8f0ba417SAndroid Build Coastguard Worker // LogId has been deprecated, but this constructor must exist for prebuilts. 443*8f0ba417SAndroid Build Coastguard Worker LogMessage(const char* file, unsigned int line, LogId, LogSeverity severity, const char* tag, 444*8f0ba417SAndroid Build Coastguard Worker int error); 445*8f0ba417SAndroid Build Coastguard Worker LogMessage(const char* file, unsigned int line, LogSeverity severity, const char* tag, int error); 446*8f0ba417SAndroid Build Coastguard Worker 447*8f0ba417SAndroid Build Coastguard Worker ~LogMessage(); 448*8f0ba417SAndroid Build Coastguard Worker 449*8f0ba417SAndroid Build Coastguard Worker // Returns the stream associated with the message, the LogMessage performs 450*8f0ba417SAndroid Build Coastguard Worker // output when it goes out of scope. 451*8f0ba417SAndroid Build Coastguard Worker std::ostream& stream(); 452*8f0ba417SAndroid Build Coastguard Worker 453*8f0ba417SAndroid Build Coastguard Worker // The routine that performs the actual logging. 454*8f0ba417SAndroid Build Coastguard Worker static void LogLine(const char* file, unsigned int line, LogSeverity severity, const char* tag, 455*8f0ba417SAndroid Build Coastguard Worker const char* msg); 456*8f0ba417SAndroid Build Coastguard Worker 457*8f0ba417SAndroid Build Coastguard Worker private: 458*8f0ba417SAndroid Build Coastguard Worker const std::unique_ptr<LogMessageData> data_; 459*8f0ba417SAndroid Build Coastguard Worker 460*8f0ba417SAndroid Build Coastguard Worker DISALLOW_COPY_AND_ASSIGN(LogMessage); 461*8f0ba417SAndroid Build Coastguard Worker }; 462*8f0ba417SAndroid Build Coastguard Worker 463*8f0ba417SAndroid Build Coastguard Worker // Get the minimum severity level for logging. 464*8f0ba417SAndroid Build Coastguard Worker LogSeverity GetMinimumLogSeverity(); 465*8f0ba417SAndroid Build Coastguard Worker 466*8f0ba417SAndroid Build Coastguard Worker // Set the minimum severity level for logging, returning the old severity. 467*8f0ba417SAndroid Build Coastguard Worker LogSeverity SetMinimumLogSeverity(LogSeverity new_severity); 468*8f0ba417SAndroid Build Coastguard Worker 469*8f0ba417SAndroid Build Coastguard Worker // Return whether or not a log message with the associated tag should be logged. 470*8f0ba417SAndroid Build Coastguard Worker bool ShouldLog(LogSeverity severity, const char* tag); 471*8f0ba417SAndroid Build Coastguard Worker 472*8f0ba417SAndroid Build Coastguard Worker // Allows to temporarily change the minimum severity level for logging. 473*8f0ba417SAndroid Build Coastguard Worker class ScopedLogSeverity { 474*8f0ba417SAndroid Build Coastguard Worker public: 475*8f0ba417SAndroid Build Coastguard Worker explicit ScopedLogSeverity(LogSeverity level); 476*8f0ba417SAndroid Build Coastguard Worker ~ScopedLogSeverity(); 477*8f0ba417SAndroid Build Coastguard Worker 478*8f0ba417SAndroid Build Coastguard Worker private: 479*8f0ba417SAndroid Build Coastguard Worker LogSeverity old_; 480*8f0ba417SAndroid Build Coastguard Worker }; 481*8f0ba417SAndroid Build Coastguard Worker 482*8f0ba417SAndroid Build Coastguard Worker } // namespace base 483*8f0ba417SAndroid Build Coastguard Worker } // namespace android 484*8f0ba417SAndroid Build Coastguard Worker 485*8f0ba417SAndroid Build Coastguard Worker namespace std { // NOLINT(cert-dcl58-cpp) 486*8f0ba417SAndroid Build Coastguard Worker 487*8f0ba417SAndroid Build Coastguard Worker // Emit a warning of ostream<< with std::string*. The intention was most likely to print *string. 488*8f0ba417SAndroid Build Coastguard Worker // 489*8f0ba417SAndroid Build Coastguard Worker // Note: for this to work, we need to have this in a namespace. 490*8f0ba417SAndroid Build Coastguard Worker // Note: using a pragma because "-Wgcc-compat" (included in "-Weverything") complains about 491*8f0ba417SAndroid Build Coastguard Worker // diagnose_if. 492*8f0ba417SAndroid Build Coastguard Worker // Note: to print the pointer, use "<< static_cast<const void*>(string_pointer)" instead. 493*8f0ba417SAndroid Build Coastguard Worker // Note: a not-recommended alternative is to let Clang ignore the warning by adding 494*8f0ba417SAndroid Build Coastguard Worker // -Wno-user-defined-warnings to CPPFLAGS. 495*8f0ba417SAndroid Build Coastguard Worker #pragma clang diagnostic push 496*8f0ba417SAndroid Build Coastguard Worker #pragma clang diagnostic ignored "-Wgcc-compat" 497*8f0ba417SAndroid Build Coastguard Worker #define OSTREAM_STRING_POINTER_USAGE_WARNING \ 498*8f0ba417SAndroid Build Coastguard Worker __attribute__((diagnose_if(true, "Unexpected logging of string pointer", "warning"))) 499*8f0ba417SAndroid Build Coastguard Worker inline OSTREAM_STRING_POINTER_USAGE_WARNING 500*8f0ba417SAndroid Build Coastguard Worker std::ostream& operator<<(std::ostream& stream, const std::string* string_pointer) { 501*8f0ba417SAndroid Build Coastguard Worker return stream << static_cast<const void*>(string_pointer); 502*8f0ba417SAndroid Build Coastguard Worker } 503*8f0ba417SAndroid Build Coastguard Worker #pragma clang diagnostic pop 504*8f0ba417SAndroid Build Coastguard Worker 505*8f0ba417SAndroid Build Coastguard Worker } // namespace std 506