158310b49fc8a7a713b922319a849a419858db79eDan Albert/*
258310b49fc8a7a713b922319a849a419858db79eDan Albert * Copyright (C) 2015 The Android Open Source Project
358310b49fc8a7a713b922319a849a419858db79eDan Albert *
458310b49fc8a7a713b922319a849a419858db79eDan Albert * Licensed under the Apache License, Version 2.0 (the "License");
558310b49fc8a7a713b922319a849a419858db79eDan Albert * you may not use this file except in compliance with the License.
658310b49fc8a7a713b922319a849a419858db79eDan Albert * You may obtain a copy of the License at
758310b49fc8a7a713b922319a849a419858db79eDan Albert *
858310b49fc8a7a713b922319a849a419858db79eDan Albert *      http://www.apache.org/licenses/LICENSE-2.0
958310b49fc8a7a713b922319a849a419858db79eDan Albert *
1058310b49fc8a7a713b922319a849a419858db79eDan Albert * Unless required by applicable law or agreed to in writing, software
1158310b49fc8a7a713b922319a849a419858db79eDan Albert * distributed under the License is distributed on an "AS IS" BASIS,
1258310b49fc8a7a713b922319a849a419858db79eDan Albert * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1358310b49fc8a7a713b922319a849a419858db79eDan Albert * See the License for the specific language governing permissions and
1458310b49fc8a7a713b922319a849a419858db79eDan Albert * limitations under the License.
1558310b49fc8a7a713b922319a849a419858db79eDan Albert */
1654c72aaccc45edf4832cfdc5053d9ae6acc9bcdfElliott Hughes
1754c72aaccc45edf4832cfdc5053d9ae6acc9bcdfElliott Hughes#ifndef ANDROID_BASE_LOGGING_H
1854c72aaccc45edf4832cfdc5053d9ae6acc9bcdfElliott Hughes#define ANDROID_BASE_LOGGING_H
1958310b49fc8a7a713b922319a849a419858db79eDan Albert
209f4c8f753bdff01c99613fca5d5a089c677517dfElliott Hughes//
219f4c8f753bdff01c99613fca5d5a089c677517dfElliott Hughes// Google-style C++ logging.
229f4c8f753bdff01c99613fca5d5a089c677517dfElliott Hughes//
239f4c8f753bdff01c99613fca5d5a089c677517dfElliott Hughes
249f4c8f753bdff01c99613fca5d5a089c677517dfElliott Hughes// This header provides a C++ stream interface to logging.
259f4c8f753bdff01c99613fca5d5a089c677517dfElliott Hughes//
269f4c8f753bdff01c99613fca5d5a089c677517dfElliott Hughes// To log:
279f4c8f753bdff01c99613fca5d5a089c677517dfElliott Hughes//
289f4c8f753bdff01c99613fca5d5a089c677517dfElliott Hughes//   LOG(INFO) << "Some text; " << some_value;
299f4c8f753bdff01c99613fca5d5a089c677517dfElliott Hughes//
309f4c8f753bdff01c99613fca5d5a089c677517dfElliott Hughes// Replace `INFO` with any severity from `enum LogSeverity`.
319f4c8f753bdff01c99613fca5d5a089c677517dfElliott Hughes//
329f4c8f753bdff01c99613fca5d5a089c677517dfElliott Hughes// To log the result of a failed function and include the string
339f4c8f753bdff01c99613fca5d5a089c677517dfElliott Hughes// representation of `errno` at the end:
349f4c8f753bdff01c99613fca5d5a089c677517dfElliott Hughes//
359f4c8f753bdff01c99613fca5d5a089c677517dfElliott Hughes//   PLOG(ERROR) << "Write failed";
369f4c8f753bdff01c99613fca5d5a089c677517dfElliott Hughes//
379f4c8f753bdff01c99613fca5d5a089c677517dfElliott Hughes// The output will be something like `Write failed: I/O error`.
389f4c8f753bdff01c99613fca5d5a089c677517dfElliott Hughes// Remember this as 'P' as in perror(3).
399f4c8f753bdff01c99613fca5d5a089c677517dfElliott Hughes//
409f4c8f753bdff01c99613fca5d5a089c677517dfElliott Hughes// To output your own types, simply implement operator<< as normal.
419f4c8f753bdff01c99613fca5d5a089c677517dfElliott Hughes//
429f4c8f753bdff01c99613fca5d5a089c677517dfElliott Hughes// By default, output goes to logcat on Android and stderr on the host.
439f4c8f753bdff01c99613fca5d5a089c677517dfElliott Hughes// A process can use `SetLogger` to decide where all logging goes.
449f4c8f753bdff01c99613fca5d5a089c677517dfElliott Hughes// Implementations are provided for logcat, stderr, and dmesg.
459f4c8f753bdff01c99613fca5d5a089c677517dfElliott Hughes
469f4c8f753bdff01c99613fca5d5a089c677517dfElliott Hughes// This header also provides assertions:
479f4c8f753bdff01c99613fca5d5a089c677517dfElliott Hughes//
489f4c8f753bdff01c99613fca5d5a089c677517dfElliott Hughes//   CHECK(must_be_true);
499f4c8f753bdff01c99613fca5d5a089c677517dfElliott Hughes//   CHECK_EQ(a, b) << z_is_interesting_too;
509f4c8f753bdff01c99613fca5d5a089c677517dfElliott Hughes
51ac3f7d9a78f9024563cc47f62206c6e3475778ddSpencer Low// NOTE: For Windows, you must include logging.h after windows.h to allow the
52ac3f7d9a78f9024563cc47f62206c6e3475778ddSpencer Low// following code to suppress the evil ERROR macro:
53ea41c213bea5dcc04dea39a2903dc7f301bd5184Dan Albert#ifdef _WIN32
54ac3f7d9a78f9024563cc47f62206c6e3475778ddSpencer Low// windows.h includes wingdi.h which defines an evil macro ERROR.
55ac3f7d9a78f9024563cc47f62206c6e3475778ddSpencer Low#ifdef ERROR
56ac3f7d9a78f9024563cc47f62206c6e3475778ddSpencer Low#undef ERROR
57ea41c213bea5dcc04dea39a2903dc7f301bd5184Dan Albert#endif
58142ec75cf882bf023853f0321cd559377daccc60Spencer Low#endif
59ea41c213bea5dcc04dea39a2903dc7f301bd5184Dan Albert
60b547c85b5b9d7fc565e47a5d0c734c3a66af242aDan Albert#include <functional>
6158310b49fc8a7a713b922319a849a419858db79eDan Albert#include <memory>
6258310b49fc8a7a713b922319a849a419858db79eDan Albert#include <ostream>
6358310b49fc8a7a713b922319a849a419858db79eDan Albert
644f71319df011d796a60a43fc1bc68e16fbf7d321Elliott Hughes#include "android-base/macros.h"
6558310b49fc8a7a713b922319a849a419858db79eDan Albert
6658310b49fc8a7a713b922319a849a419858db79eDan Albertnamespace android {
6758310b49fc8a7a713b922319a849a419858db79eDan Albertnamespace base {
6858310b49fc8a7a713b922319a849a419858db79eDan Albert
6958310b49fc8a7a713b922319a849a419858db79eDan Albertenum LogSeverity {
7058310b49fc8a7a713b922319a849a419858db79eDan Albert  VERBOSE,
7158310b49fc8a7a713b922319a849a419858db79eDan Albert  DEBUG,
7258310b49fc8a7a713b922319a849a419858db79eDan Albert  INFO,
7358310b49fc8a7a713b922319a849a419858db79eDan Albert  WARNING,
7458310b49fc8a7a713b922319a849a419858db79eDan Albert  ERROR,
75550829d84bd403c9d29c003614962ee72a9a052dAndreas Gampe  FATAL_WITHOUT_ABORT,
7658310b49fc8a7a713b922319a849a419858db79eDan Albert  FATAL,
7758310b49fc8a7a713b922319a849a419858db79eDan Albert};
7858310b49fc8a7a713b922319a849a419858db79eDan Albert
790c055863ebb57c3446897e89c2c39a67442c1be4Dan Albertenum LogId {
80b547c85b5b9d7fc565e47a5d0c734c3a66af242aDan Albert  DEFAULT,
810c055863ebb57c3446897e89c2c39a67442c1be4Dan Albert  MAIN,
820c055863ebb57c3446897e89c2c39a67442c1be4Dan Albert  SYSTEM,
830c055863ebb57c3446897e89c2c39a67442c1be4Dan Albert};
840c055863ebb57c3446897e89c2c39a67442c1be4Dan Albert
852691e335feec4806439054b5c6fe3e7a06cdd695Andreas Gampeusing LogFunction = std::function<void(LogId, LogSeverity, const char*, const char*,
862691e335feec4806439054b5c6fe3e7a06cdd695Andreas Gampe                                       unsigned int, const char*)>;
872691e335feec4806439054b5c6fe3e7a06cdd695Andreas Gampeusing AbortFunction = std::function<void(const char*)>;
88b547c85b5b9d7fc565e47a5d0c734c3a66af242aDan Albert
897bc87a5a780361928bd1aeed9d2f22233fe05407Elliott Hughesvoid KernelLogger(LogId, LogSeverity, const char*, const char*, unsigned int, const char*);
907bc87a5a780361928bd1aeed9d2f22233fe05407Elliott Hughesvoid StderrLogger(LogId, LogSeverity, const char*, const char*, unsigned int, const char*);
91b547c85b5b9d7fc565e47a5d0c734c3a66af242aDan Albert
922691e335feec4806439054b5c6fe3e7a06cdd695Andreas Gampevoid DefaultAborter(const char* abort_message);
932691e335feec4806439054b5c6fe3e7a06cdd695Andreas Gampe
94b547c85b5b9d7fc565e47a5d0c734c3a66af242aDan Albert#ifdef __ANDROID__
95b547c85b5b9d7fc565e47a5d0c734c3a66af242aDan Albert// We expose this even though it is the default because a user that wants to
96b547c85b5b9d7fc565e47a5d0c734c3a66af242aDan Albert// override the default log buffer will have to construct this themselves.
97b547c85b5b9d7fc565e47a5d0c734c3a66af242aDan Albertclass LogdLogger {
98b547c85b5b9d7fc565e47a5d0c734c3a66af242aDan Albert public:
99b547c85b5b9d7fc565e47a5d0c734c3a66af242aDan Albert  explicit LogdLogger(LogId default_log_id = android::base::MAIN);
100b547c85b5b9d7fc565e47a5d0c734c3a66af242aDan Albert
101b547c85b5b9d7fc565e47a5d0c734c3a66af242aDan Albert  void operator()(LogId, LogSeverity, const char* tag, const char* file,
102b547c85b5b9d7fc565e47a5d0c734c3a66af242aDan Albert                  unsigned int line, const char* message);
103b547c85b5b9d7fc565e47a5d0c734c3a66af242aDan Albert
104b547c85b5b9d7fc565e47a5d0c734c3a66af242aDan Albert private:
105b547c85b5b9d7fc565e47a5d0c734c3a66af242aDan Albert  LogId default_log_id_;
106b547c85b5b9d7fc565e47a5d0c734c3a66af242aDan Albert};
107b547c85b5b9d7fc565e47a5d0c734c3a66af242aDan Albert#endif
108b547c85b5b9d7fc565e47a5d0c734c3a66af242aDan Albert
10958310b49fc8a7a713b922319a849a419858db79eDan Albert// Configure logging based on ANDROID_LOG_TAGS environment variable.
11058310b49fc8a7a713b922319a849a419858db79eDan Albert// We need to parse a string that looks like
11158310b49fc8a7a713b922319a849a419858db79eDan Albert//
11258310b49fc8a7a713b922319a849a419858db79eDan Albert//      *:v jdwp:d dalvikvm:d dalvikvm-gc:i dalvikvmi:i
11358310b49fc8a7a713b922319a849a419858db79eDan Albert//
11458310b49fc8a7a713b922319a849a419858db79eDan Albert// The tag (or '*' for the global level) comes first, followed by a colon and a
11558310b49fc8a7a713b922319a849a419858db79eDan Albert// letter indicating the minimum priority level we're expected to log.  This can
11658310b49fc8a7a713b922319a849a419858db79eDan Albert// be used to reveal or conceal logs with specific tags.
1172691e335feec4806439054b5c6fe3e7a06cdd695Andreas Gampe#ifdef __ANDROID__
1182691e335feec4806439054b5c6fe3e7a06cdd695Andreas Gampe#define INIT_LOGGING_DEFAULT_LOGGER LogdLogger()
1192691e335feec4806439054b5c6fe3e7a06cdd695Andreas Gampe#else
1202691e335feec4806439054b5c6fe3e7a06cdd695Andreas Gampe#define INIT_LOGGING_DEFAULT_LOGGER StderrLogger
1212691e335feec4806439054b5c6fe3e7a06cdd695Andreas Gampe#endif
1222691e335feec4806439054b5c6fe3e7a06cdd695Andreas Gampevoid InitLogging(char* argv[],
1232691e335feec4806439054b5c6fe3e7a06cdd695Andreas Gampe                 LogFunction&& logger = INIT_LOGGING_DEFAULT_LOGGER,
1242691e335feec4806439054b5c6fe3e7a06cdd695Andreas Gampe                 AbortFunction&& aborter = DefaultAborter);
1252691e335feec4806439054b5c6fe3e7a06cdd695Andreas Gampe#undef INIT_LOGGING_DEFAULT_LOGGER
12658310b49fc8a7a713b922319a849a419858db79eDan Albert
127b547c85b5b9d7fc565e47a5d0c734c3a66af242aDan Albert// Replace the current logger.
1287bc87a5a780361928bd1aeed9d2f22233fe05407Elliott Hughesvoid SetLogger(LogFunction&& logger);
129765ae6bcfc62148de629f68315fdf8cd1bc35342Spencer Low
1302691e335feec4806439054b5c6fe3e7a06cdd695Andreas Gampe// Replace the current aborter.
1312691e335feec4806439054b5c6fe3e7a06cdd695Andreas Gampevoid SetAborter(AbortFunction&& aborter);
1322691e335feec4806439054b5c6fe3e7a06cdd695Andreas Gampe
133765ae6bcfc62148de629f68315fdf8cd1bc35342Spencer Lowclass ErrnoRestorer {
134765ae6bcfc62148de629f68315fdf8cd1bc35342Spencer Low public:
135765ae6bcfc62148de629f68315fdf8cd1bc35342Spencer Low  ErrnoRestorer()
136765ae6bcfc62148de629f68315fdf8cd1bc35342Spencer Low      : saved_errno_(errno) {
137765ae6bcfc62148de629f68315fdf8cd1bc35342Spencer Low  }
138765ae6bcfc62148de629f68315fdf8cd1bc35342Spencer Low
139765ae6bcfc62148de629f68315fdf8cd1bc35342Spencer Low  ~ErrnoRestorer() {
140765ae6bcfc62148de629f68315fdf8cd1bc35342Spencer Low    errno = saved_errno_;
141765ae6bcfc62148de629f68315fdf8cd1bc35342Spencer Low  }
142765ae6bcfc62148de629f68315fdf8cd1bc35342Spencer Low
1432527628edae5651cb28e72b2c98f24e72dcdb384Yabin Cui  // Allow this object to be used as part of && operation.
144765ae6bcfc62148de629f68315fdf8cd1bc35342Spencer Low  operator bool() const {
1452527628edae5651cb28e72b2c98f24e72dcdb384Yabin Cui    return true;
146765ae6bcfc62148de629f68315fdf8cd1bc35342Spencer Low  }
147765ae6bcfc62148de629f68315fdf8cd1bc35342Spencer Low
148765ae6bcfc62148de629f68315fdf8cd1bc35342Spencer Low private:
149765ae6bcfc62148de629f68315fdf8cd1bc35342Spencer Low  const int saved_errno_;
150765ae6bcfc62148de629f68315fdf8cd1bc35342Spencer Low
151765ae6bcfc62148de629f68315fdf8cd1bc35342Spencer Low  DISALLOW_COPY_AND_ASSIGN(ErrnoRestorer);
152765ae6bcfc62148de629f68315fdf8cd1bc35342Spencer Low};
153765ae6bcfc62148de629f68315fdf8cd1bc35342Spencer Low
1541f5fb430471beb1a6d458a6a31d7990a6158c199Andreas Gampe// A helper macro that produces an expression that accepts both a qualified name and an
1551f5fb430471beb1a6d458a6a31d7990a6158c199Andreas Gampe// unqualified name for a LogSeverity, and returns a LogSeverity value.
1561f5fb430471beb1a6d458a6a31d7990a6158c199Andreas Gampe// Note: DO NOT USE DIRECTLY. This is an implementation detail.
1571f5fb430471beb1a6d458a6a31d7990a6158c199Andreas Gampe#define SEVERITY_LAMBDA(severity) ([&]() {    \
1581f5fb430471beb1a6d458a6a31d7990a6158c199Andreas Gampe  using ::android::base::VERBOSE;             \
1591f5fb430471beb1a6d458a6a31d7990a6158c199Andreas Gampe  using ::android::base::DEBUG;               \
1601f5fb430471beb1a6d458a6a31d7990a6158c199Andreas Gampe  using ::android::base::INFO;                \
1611f5fb430471beb1a6d458a6a31d7990a6158c199Andreas Gampe  using ::android::base::WARNING;             \
1621f5fb430471beb1a6d458a6a31d7990a6158c199Andreas Gampe  using ::android::base::ERROR;               \
1631f5fb430471beb1a6d458a6a31d7990a6158c199Andreas Gampe  using ::android::base::FATAL_WITHOUT_ABORT; \
1641f5fb430471beb1a6d458a6a31d7990a6158c199Andreas Gampe  using ::android::base::FATAL;               \
1651f5fb430471beb1a6d458a6a31d7990a6158c199Andreas Gampe  return (severity); }())
1661f5fb430471beb1a6d458a6a31d7990a6158c199Andreas Gampe
167436f5a031f3dfa195919be36873300a221376b53Andreas Gampe// Defines whether the given severity will be logged or silently swallowed.
1681f5fb430471beb1a6d458a6a31d7990a6158c199Andreas Gampe#define WOULD_LOG(severity) \
1691f5fb430471beb1a6d458a6a31d7990a6158c199Andreas Gampe  UNLIKELY((SEVERITY_LAMBDA(severity)) >= ::android::base::GetMinimumLogSeverity())
170436f5a031f3dfa195919be36873300a221376b53Andreas Gampe
171436f5a031f3dfa195919be36873300a221376b53Andreas Gampe// Get an ostream that can be used for logging at the given severity and to the default
172436f5a031f3dfa195919be36873300a221376b53Andreas Gampe// destination.
173436f5a031f3dfa195919be36873300a221376b53Andreas Gampe//
174436f5a031f3dfa195919be36873300a221376b53Andreas Gampe// Notes:
175436f5a031f3dfa195919be36873300a221376b53Andreas Gampe// 1) This will not check whether the severity is high enough. One should use WOULD_LOG to filter
176436f5a031f3dfa195919be36873300a221376b53Andreas Gampe//    usage manually.
177436f5a031f3dfa195919be36873300a221376b53Andreas Gampe// 2) This does not save and restore errno.
1781f5fb430471beb1a6d458a6a31d7990a6158c199Andreas Gampe#define LOG_STREAM(severity) LOG_STREAM_TO(DEFAULT, severity)
179436f5a031f3dfa195919be36873300a221376b53Andreas Gampe
180436f5a031f3dfa195919be36873300a221376b53Andreas Gampe// Get an ostream that can be used for logging at the given severity and to the
181436f5a031f3dfa195919be36873300a221376b53Andreas Gampe// given destination. The same notes as for LOG_STREAM apply.
1821f5fb430471beb1a6d458a6a31d7990a6158c199Andreas Gampe#define LOG_STREAM_TO(dest, severity)                                   \
183436f5a031f3dfa195919be36873300a221376b53Andreas Gampe  ::android::base::LogMessage(__FILE__, __LINE__,                       \
184436f5a031f3dfa195919be36873300a221376b53Andreas Gampe                              ::android::base::dest,                    \
1851f5fb430471beb1a6d458a6a31d7990a6158c199Andreas Gampe                              SEVERITY_LAMBDA(severity), -1).stream()
186436f5a031f3dfa195919be36873300a221376b53Andreas Gampe
18758310b49fc8a7a713b922319a849a419858db79eDan Albert// Logs a message to logcat on Android otherwise to stderr. If the severity is
18858310b49fc8a7a713b922319a849a419858db79eDan Albert// FATAL it also causes an abort. For example:
18958310b49fc8a7a713b922319a849a419858db79eDan Albert//
19058310b49fc8a7a713b922319a849a419858db79eDan Albert//     LOG(FATAL) << "We didn't expect to reach here";
1911f5fb430471beb1a6d458a6a31d7990a6158c199Andreas Gampe#define LOG(severity) LOG_TO(DEFAULT, severity)
1920c055863ebb57c3446897e89c2c39a67442c1be4Dan Albert
1930c055863ebb57c3446897e89c2c39a67442c1be4Dan Albert// Logs a message to logcat with the specified log ID on Android otherwise to
1940c055863ebb57c3446897e89c2c39a67442c1be4Dan Albert// stderr. If the severity is FATAL it also causes an abort.
195765ae6bcfc62148de629f68315fdf8cd1bc35342Spencer Low// Use an if-else statement instead of just an if statement here. So if there is a
196765ae6bcfc62148de629f68315fdf8cd1bc35342Spencer Low// else statement after LOG() macro, it won't bind to the if statement in the macro.
197765ae6bcfc62148de629f68315fdf8cd1bc35342Spencer Low// do-while(0) statement doesn't work here. Because we need to support << operator
198765ae6bcfc62148de629f68315fdf8cd1bc35342Spencer Low// following the macro, like "LOG(DEBUG) << xxx;".
1991f5fb430471beb1a6d458a6a31d7990a6158c199Andreas Gampe
2001f5fb430471beb1a6d458a6a31d7990a6158c199Andreas Gampe#define LOG_TO(dest, severity) \
2011f5fb430471beb1a6d458a6a31d7990a6158c199Andreas Gampe  WOULD_LOG(severity) &&                   \
202436f5a031f3dfa195919be36873300a221376b53Andreas Gampe    ::android::base::ErrnoRestorer() &&    \
2031f5fb430471beb1a6d458a6a31d7990a6158c199Andreas Gampe      LOG_STREAM_TO(dest, severity)
20458310b49fc8a7a713b922319a849a419858db79eDan Albert
20558310b49fc8a7a713b922319a849a419858db79eDan Albert// A variant of LOG that also logs the current errno value. To be used when
20658310b49fc8a7a713b922319a849a419858db79eDan Albert// library calls fail.
2071f5fb430471beb1a6d458a6a31d7990a6158c199Andreas Gampe#define PLOG(severity) PLOG_TO(DEFAULT, severity)
2080c055863ebb57c3446897e89c2c39a67442c1be4Dan Albert
2090c055863ebb57c3446897e89c2c39a67442c1be4Dan Albert// Behaves like PLOG, but logs to the specified log ID.
2101f5fb430471beb1a6d458a6a31d7990a6158c199Andreas Gampe#define PLOG_TO(dest, severity)                         \
2111f5fb430471beb1a6d458a6a31d7990a6158c199Andreas Gampe  WOULD_LOG(SEVERITY_LAMBDA(severity)) &&               \
212436f5a031f3dfa195919be36873300a221376b53Andreas Gampe    ::android::base::ErrnoRestorer() &&                 \
213436f5a031f3dfa195919be36873300a221376b53Andreas Gampe      ::android::base::LogMessage(__FILE__, __LINE__,   \
214436f5a031f3dfa195919be36873300a221376b53Andreas Gampe          ::android::base::dest,                        \
2151f5fb430471beb1a6d458a6a31d7990a6158c199Andreas Gampe          SEVERITY_LAMBDA(severity), errno).stream()
21658310b49fc8a7a713b922319a849a419858db79eDan Albert
21758310b49fc8a7a713b922319a849a419858db79eDan Albert// Marker that code is yet to be implemented.
21858310b49fc8a7a713b922319a849a419858db79eDan Albert#define UNIMPLEMENTED(level) \
21958310b49fc8a7a713b922319a849a419858db79eDan Albert  LOG(level) << __PRETTY_FUNCTION__ << " unimplemented "
22058310b49fc8a7a713b922319a849a419858db79eDan Albert
2217e0e14ca84892771c08377479d509db749cd304dChih-Hung Hsieh#ifdef __clang_analyzer__
2227e0e14ca84892771c08377479d509db749cd304dChih-Hung Hsieh// ClangL static analyzer does not see the conditional statement inside
2237e0e14ca84892771c08377479d509db749cd304dChih-Hung Hsieh// LogMessage's destructor that will abort on FATAL severity.
2247e0e14ca84892771c08377479d509db749cd304dChih-Hung Hsieh#define ABORT_AFTER_LOG_FATAL for (;;abort())
2257e0e14ca84892771c08377479d509db749cd304dChih-Hung Hsieh#else
2267e0e14ca84892771c08377479d509db749cd304dChih-Hung Hsieh#define ABORT_AFTER_LOG_FATAL
2277e0e14ca84892771c08377479d509db749cd304dChih-Hung Hsieh#endif
2287e0e14ca84892771c08377479d509db749cd304dChih-Hung Hsieh
22958310b49fc8a7a713b922319a849a419858db79eDan Albert// Check whether condition x holds and LOG(FATAL) if not. The value of the
23058310b49fc8a7a713b922319a849a419858db79eDan Albert// expression x is only evaluated once. Extra logging can be appended using <<
23158310b49fc8a7a713b922319a849a419858db79eDan Albert// after. For example:
23258310b49fc8a7a713b922319a849a419858db79eDan Albert//
23358310b49fc8a7a713b922319a849a419858db79eDan Albert//     CHECK(false == true) results in a log message of
23458310b49fc8a7a713b922319a849a419858db79eDan Albert//       "Check failed: false == true".
235765ae6bcfc62148de629f68315fdf8cd1bc35342Spencer Low#define CHECK(x)                                                              \
2362527628edae5651cb28e72b2c98f24e72dcdb384Yabin Cui  LIKELY((x)) ||                                                              \
2377e0e14ca84892771c08377479d509db749cd304dChih-Hung Hsieh    ABORT_AFTER_LOG_FATAL                                                     \
238765ae6bcfc62148de629f68315fdf8cd1bc35342Spencer Low    ::android::base::LogMessage(__FILE__, __LINE__, ::android::base::DEFAULT, \
239765ae6bcfc62148de629f68315fdf8cd1bc35342Spencer Low                                ::android::base::FATAL, -1).stream()          \
240765ae6bcfc62148de629f68315fdf8cd1bc35342Spencer Low        << "Check failed: " #x << " "
24158310b49fc8a7a713b922319a849a419858db79eDan Albert
24258310b49fc8a7a713b922319a849a419858db79eDan Albert// Helper for CHECK_xx(x,y) macros.
243b547c85b5b9d7fc565e47a5d0c734c3a66af242aDan Albert#define CHECK_OP(LHS, RHS, OP)                                              \
244b547c85b5b9d7fc565e47a5d0c734c3a66af242aDan Albert  for (auto _values = ::android::base::MakeEagerEvaluator(LHS, RHS);        \
245b547c85b5b9d7fc565e47a5d0c734c3a66af242aDan Albert       UNLIKELY(!(_values.lhs OP _values.rhs));                             \
246b547c85b5b9d7fc565e47a5d0c734c3a66af242aDan Albert       /* empty */)                                                         \
2477e0e14ca84892771c08377479d509db749cd304dChih-Hung Hsieh  ABORT_AFTER_LOG_FATAL                                                     \
248b547c85b5b9d7fc565e47a5d0c734c3a66af242aDan Albert  ::android::base::LogMessage(__FILE__, __LINE__, ::android::base::DEFAULT, \
249b547c85b5b9d7fc565e47a5d0c734c3a66af242aDan Albert                              ::android::base::FATAL, -1).stream()          \
250b547c85b5b9d7fc565e47a5d0c734c3a66af242aDan Albert      << "Check failed: " << #LHS << " " << #OP << " " << #RHS              \
25158310b49fc8a7a713b922319a849a419858db79eDan Albert      << " (" #LHS "=" << _values.lhs << ", " #RHS "=" << _values.rhs << ") "
25258310b49fc8a7a713b922319a849a419858db79eDan Albert
25358310b49fc8a7a713b922319a849a419858db79eDan Albert// Check whether a condition holds between x and y, LOG(FATAL) if not. The value
25458310b49fc8a7a713b922319a849a419858db79eDan Albert// of the expressions x and y is evaluated once. Extra logging can be appended
25558310b49fc8a7a713b922319a849a419858db79eDan Albert// using << after. For example:
25658310b49fc8a7a713b922319a849a419858db79eDan Albert//
25758310b49fc8a7a713b922319a849a419858db79eDan Albert//     CHECK_NE(0 == 1, false) results in
25858310b49fc8a7a713b922319a849a419858db79eDan Albert//       "Check failed: false != false (0==1=false, false=false) ".
25958310b49fc8a7a713b922319a849a419858db79eDan Albert#define CHECK_EQ(x, y) CHECK_OP(x, y, == )
26058310b49fc8a7a713b922319a849a419858db79eDan Albert#define CHECK_NE(x, y) CHECK_OP(x, y, != )
26158310b49fc8a7a713b922319a849a419858db79eDan Albert#define CHECK_LE(x, y) CHECK_OP(x, y, <= )
26258310b49fc8a7a713b922319a849a419858db79eDan Albert#define CHECK_LT(x, y) CHECK_OP(x, y, < )
26358310b49fc8a7a713b922319a849a419858db79eDan Albert#define CHECK_GE(x, y) CHECK_OP(x, y, >= )
26458310b49fc8a7a713b922319a849a419858db79eDan Albert#define CHECK_GT(x, y) CHECK_OP(x, y, > )
26558310b49fc8a7a713b922319a849a419858db79eDan Albert
26658310b49fc8a7a713b922319a849a419858db79eDan Albert// Helper for CHECK_STRxx(s1,s2) macros.
267d8f26e2ac94fe30def727b37e55547c3aa77bbe3Andreas Gampe#define CHECK_STROP(s1, s2, sense)                                             \
268d8f26e2ac94fe30def727b37e55547c3aa77bbe3Andreas Gampe  while (UNLIKELY((strcmp(s1, s2) == 0) != (sense)))                           \
269d8f26e2ac94fe30def727b37e55547c3aa77bbe3Andreas Gampe    ABORT_AFTER_LOG_FATAL                                                      \
270d8f26e2ac94fe30def727b37e55547c3aa77bbe3Andreas Gampe    ::android::base::LogMessage(__FILE__, __LINE__, ::android::base::DEFAULT,  \
271d8f26e2ac94fe30def727b37e55547c3aa77bbe3Andreas Gampe                                ::android::base::FATAL, -1).stream()           \
272d8f26e2ac94fe30def727b37e55547c3aa77bbe3Andreas Gampe        << "Check failed: " << "\"" << (s1) << "\""                            \
273d8f26e2ac94fe30def727b37e55547c3aa77bbe3Andreas Gampe        << ((sense) ? " == " : " != ") << "\"" << (s2) << "\""
27458310b49fc8a7a713b922319a849a419858db79eDan Albert
27558310b49fc8a7a713b922319a849a419858db79eDan Albert// Check for string (const char*) equality between s1 and s2, LOG(FATAL) if not.
27658310b49fc8a7a713b922319a849a419858db79eDan Albert#define CHECK_STREQ(s1, s2) CHECK_STROP(s1, s2, true)
27758310b49fc8a7a713b922319a849a419858db79eDan Albert#define CHECK_STRNE(s1, s2) CHECK_STROP(s1, s2, false)
27858310b49fc8a7a713b922319a849a419858db79eDan Albert
27958310b49fc8a7a713b922319a849a419858db79eDan Albert// Perform the pthread function call(args), LOG(FATAL) on error.
28058310b49fc8a7a713b922319a849a419858db79eDan Albert#define CHECK_PTHREAD_CALL(call, args, what)                           \
28158310b49fc8a7a713b922319a849a419858db79eDan Albert  do {                                                                 \
28258310b49fc8a7a713b922319a849a419858db79eDan Albert    int rc = call args;                                                \
28358310b49fc8a7a713b922319a849a419858db79eDan Albert    if (rc != 0) {                                                     \
28458310b49fc8a7a713b922319a849a419858db79eDan Albert      errno = rc;                                                      \
2857e0e14ca84892771c08377479d509db749cd304dChih-Hung Hsieh      ABORT_AFTER_LOG_FATAL                                            \
286c713bce90183fa4ade82bc6f4b08280d32f1fb58Chih-Hung Hsieh      PLOG(FATAL) << #call << " failed for " << (what);                \
28758310b49fc8a7a713b922319a849a419858db79eDan Albert    }                                                                  \
28858310b49fc8a7a713b922319a849a419858db79eDan Albert  } while (false)
28958310b49fc8a7a713b922319a849a419858db79eDan Albert
29058310b49fc8a7a713b922319a849a419858db79eDan Albert// CHECK that can be used in a constexpr function. For example:
29158310b49fc8a7a713b922319a849a419858db79eDan Albert//
29258310b49fc8a7a713b922319a849a419858db79eDan Albert//    constexpr int half(int n) {
29358310b49fc8a7a713b922319a849a419858db79eDan Albert//      return
29458310b49fc8a7a713b922319a849a419858db79eDan Albert//          DCHECK_CONSTEXPR(n >= 0, , 0)
29558310b49fc8a7a713b922319a849a419858db79eDan Albert//          CHECK_CONSTEXPR((n & 1) == 0),
29658310b49fc8a7a713b922319a849a419858db79eDan Albert//              << "Extra debugging output: n = " << n, 0)
29758310b49fc8a7a713b922319a849a419858db79eDan Albert//          n / 2;
29858310b49fc8a7a713b922319a849a419858db79eDan Albert//    }
29958310b49fc8a7a713b922319a849a419858db79eDan Albert#define CHECK_CONSTEXPR(x, out, dummy)                                     \
30058310b49fc8a7a713b922319a849a419858db79eDan Albert  (UNLIKELY(!(x)))                                                         \
30158310b49fc8a7a713b922319a849a419858db79eDan Albert      ? (LOG(FATAL) << "Check failed: " << #x out, dummy) \
30258310b49fc8a7a713b922319a849a419858db79eDan Albert      :
30358310b49fc8a7a713b922319a849a419858db79eDan Albert
30458310b49fc8a7a713b922319a849a419858db79eDan Albert// DCHECKs are debug variants of CHECKs only enabled in debug builds. Generally
30558310b49fc8a7a713b922319a849a419858db79eDan Albert// CHECK should be used unless profiling identifies a CHECK as being in
30658310b49fc8a7a713b922319a849a419858db79eDan Albert// performance critical code.
30758310b49fc8a7a713b922319a849a419858db79eDan Albert#if defined(NDEBUG)
30858310b49fc8a7a713b922319a849a419858db79eDan Albertstatic constexpr bool kEnableDChecks = false;
30958310b49fc8a7a713b922319a849a419858db79eDan Albert#else
31058310b49fc8a7a713b922319a849a419858db79eDan Albertstatic constexpr bool kEnableDChecks = true;
31158310b49fc8a7a713b922319a849a419858db79eDan Albert#endif
31258310b49fc8a7a713b922319a849a419858db79eDan Albert
31358310b49fc8a7a713b922319a849a419858db79eDan Albert#define DCHECK(x) \
31458310b49fc8a7a713b922319a849a419858db79eDan Albert  if (::android::base::kEnableDChecks) CHECK(x)
31558310b49fc8a7a713b922319a849a419858db79eDan Albert#define DCHECK_EQ(x, y) \
31658310b49fc8a7a713b922319a849a419858db79eDan Albert  if (::android::base::kEnableDChecks) CHECK_EQ(x, y)
31758310b49fc8a7a713b922319a849a419858db79eDan Albert#define DCHECK_NE(x, y) \
31858310b49fc8a7a713b922319a849a419858db79eDan Albert  if (::android::base::kEnableDChecks) CHECK_NE(x, y)
31958310b49fc8a7a713b922319a849a419858db79eDan Albert#define DCHECK_LE(x, y) \
32058310b49fc8a7a713b922319a849a419858db79eDan Albert  if (::android::base::kEnableDChecks) CHECK_LE(x, y)
32158310b49fc8a7a713b922319a849a419858db79eDan Albert#define DCHECK_LT(x, y) \
32258310b49fc8a7a713b922319a849a419858db79eDan Albert  if (::android::base::kEnableDChecks) CHECK_LT(x, y)
32358310b49fc8a7a713b922319a849a419858db79eDan Albert#define DCHECK_GE(x, y) \
32458310b49fc8a7a713b922319a849a419858db79eDan Albert  if (::android::base::kEnableDChecks) CHECK_GE(x, y)
32558310b49fc8a7a713b922319a849a419858db79eDan Albert#define DCHECK_GT(x, y) \
32658310b49fc8a7a713b922319a849a419858db79eDan Albert  if (::android::base::kEnableDChecks) CHECK_GT(x, y)
32758310b49fc8a7a713b922319a849a419858db79eDan Albert#define DCHECK_STREQ(s1, s2) \
32858310b49fc8a7a713b922319a849a419858db79eDan Albert  if (::android::base::kEnableDChecks) CHECK_STREQ(s1, s2)
32958310b49fc8a7a713b922319a849a419858db79eDan Albert#define DCHECK_STRNE(s1, s2) \
33058310b49fc8a7a713b922319a849a419858db79eDan Albert  if (::android::base::kEnableDChecks) CHECK_STRNE(s1, s2)
33158310b49fc8a7a713b922319a849a419858db79eDan Albert#if defined(NDEBUG)
33258310b49fc8a7a713b922319a849a419858db79eDan Albert#define DCHECK_CONSTEXPR(x, out, dummy)
33358310b49fc8a7a713b922319a849a419858db79eDan Albert#else
33458310b49fc8a7a713b922319a849a419858db79eDan Albert#define DCHECK_CONSTEXPR(x, out, dummy) CHECK_CONSTEXPR(x, out, dummy)
33558310b49fc8a7a713b922319a849a419858db79eDan Albert#endif
33658310b49fc8a7a713b922319a849a419858db79eDan Albert
33758310b49fc8a7a713b922319a849a419858db79eDan Albert// Temporary class created to evaluate the LHS and RHS, used with
33858310b49fc8a7a713b922319a849a419858db79eDan Albert// MakeEagerEvaluator to infer the types of LHS and RHS.
33958310b49fc8a7a713b922319a849a419858db79eDan Alberttemplate <typename LHS, typename RHS>
34058310b49fc8a7a713b922319a849a419858db79eDan Albertstruct EagerEvaluator {
34122847131da5606126c68bb1650d87f7823f5f560Andreas Gampe  constexpr EagerEvaluator(LHS l, RHS r) : lhs(l), rhs(r) {
34258310b49fc8a7a713b922319a849a419858db79eDan Albert  }
34358310b49fc8a7a713b922319a849a419858db79eDan Albert  LHS lhs;
34458310b49fc8a7a713b922319a849a419858db79eDan Albert  RHS rhs;
34558310b49fc8a7a713b922319a849a419858db79eDan Albert};
34658310b49fc8a7a713b922319a849a419858db79eDan Albert
34758310b49fc8a7a713b922319a849a419858db79eDan Albert// Helper function for CHECK_xx.
34858310b49fc8a7a713b922319a849a419858db79eDan Alberttemplate <typename LHS, typename RHS>
34922847131da5606126c68bb1650d87f7823f5f560Andreas Gampeconstexpr EagerEvaluator<LHS, RHS> MakeEagerEvaluator(LHS lhs, RHS rhs) {
35058310b49fc8a7a713b922319a849a419858db79eDan Albert  return EagerEvaluator<LHS, RHS>(lhs, rhs);
35158310b49fc8a7a713b922319a849a419858db79eDan Albert}
35258310b49fc8a7a713b922319a849a419858db79eDan Albert
35358310b49fc8a7a713b922319a849a419858db79eDan Albert// Explicitly instantiate EagerEvalue for pointers so that char*s aren't treated
35458310b49fc8a7a713b922319a849a419858db79eDan Albert// as strings. To compare strings use CHECK_STREQ and CHECK_STRNE. We rely on
35558310b49fc8a7a713b922319a849a419858db79eDan Albert// signed/unsigned warnings to protect you against combinations not explicitly
35658310b49fc8a7a713b922319a849a419858db79eDan Albert// listed below.
35758310b49fc8a7a713b922319a849a419858db79eDan Albert#define EAGER_PTR_EVALUATOR(T1, T2)               \
35858310b49fc8a7a713b922319a849a419858db79eDan Albert  template <>                                     \
35958310b49fc8a7a713b922319a849a419858db79eDan Albert  struct EagerEvaluator<T1, T2> {                 \
36058310b49fc8a7a713b922319a849a419858db79eDan Albert    EagerEvaluator(T1 l, T2 r)                    \
36158310b49fc8a7a713b922319a849a419858db79eDan Albert        : lhs(reinterpret_cast<const void*>(l)),  \
36258310b49fc8a7a713b922319a849a419858db79eDan Albert          rhs(reinterpret_cast<const void*>(r)) { \
36358310b49fc8a7a713b922319a849a419858db79eDan Albert    }                                             \
36458310b49fc8a7a713b922319a849a419858db79eDan Albert    const void* lhs;                              \
36558310b49fc8a7a713b922319a849a419858db79eDan Albert    const void* rhs;                              \
36658310b49fc8a7a713b922319a849a419858db79eDan Albert  }
36758310b49fc8a7a713b922319a849a419858db79eDan AlbertEAGER_PTR_EVALUATOR(const char*, const char*);
36858310b49fc8a7a713b922319a849a419858db79eDan AlbertEAGER_PTR_EVALUATOR(const char*, char*);
36958310b49fc8a7a713b922319a849a419858db79eDan AlbertEAGER_PTR_EVALUATOR(char*, const char*);
37058310b49fc8a7a713b922319a849a419858db79eDan AlbertEAGER_PTR_EVALUATOR(char*, char*);
37158310b49fc8a7a713b922319a849a419858db79eDan AlbertEAGER_PTR_EVALUATOR(const unsigned char*, const unsigned char*);
37258310b49fc8a7a713b922319a849a419858db79eDan AlbertEAGER_PTR_EVALUATOR(const unsigned char*, unsigned char*);
37358310b49fc8a7a713b922319a849a419858db79eDan AlbertEAGER_PTR_EVALUATOR(unsigned char*, const unsigned char*);
37458310b49fc8a7a713b922319a849a419858db79eDan AlbertEAGER_PTR_EVALUATOR(unsigned char*, unsigned char*);
37558310b49fc8a7a713b922319a849a419858db79eDan AlbertEAGER_PTR_EVALUATOR(const signed char*, const signed char*);
37658310b49fc8a7a713b922319a849a419858db79eDan AlbertEAGER_PTR_EVALUATOR(const signed char*, signed char*);
37758310b49fc8a7a713b922319a849a419858db79eDan AlbertEAGER_PTR_EVALUATOR(signed char*, const signed char*);
37858310b49fc8a7a713b922319a849a419858db79eDan AlbertEAGER_PTR_EVALUATOR(signed char*, signed char*);
37958310b49fc8a7a713b922319a849a419858db79eDan Albert
38058310b49fc8a7a713b922319a849a419858db79eDan Albert// Data for the log message, not stored in LogMessage to avoid increasing the
38158310b49fc8a7a713b922319a849a419858db79eDan Albert// stack size.
38258310b49fc8a7a713b922319a849a419858db79eDan Albertclass LogMessageData;
38358310b49fc8a7a713b922319a849a419858db79eDan Albert
38458310b49fc8a7a713b922319a849a419858db79eDan Albert// A LogMessage is a temporarily scoped object used by LOG and the unlikely part
38558310b49fc8a7a713b922319a849a419858db79eDan Albert// of a CHECK. The destructor will abort if the severity is FATAL.
38658310b49fc8a7a713b922319a849a419858db79eDan Albertclass LogMessage {
38758310b49fc8a7a713b922319a849a419858db79eDan Albert public:
3880c055863ebb57c3446897e89c2c39a67442c1be4Dan Albert  LogMessage(const char* file, unsigned int line, LogId id,
3890c055863ebb57c3446897e89c2c39a67442c1be4Dan Albert             LogSeverity severity, int error);
39058310b49fc8a7a713b922319a849a419858db79eDan Albert
39158310b49fc8a7a713b922319a849a419858db79eDan Albert  ~LogMessage();
39258310b49fc8a7a713b922319a849a419858db79eDan Albert
39358310b49fc8a7a713b922319a849a419858db79eDan Albert  // Returns the stream associated with the message, the LogMessage performs
39458310b49fc8a7a713b922319a849a419858db79eDan Albert  // output when it goes out of scope.
39558310b49fc8a7a713b922319a849a419858db79eDan Albert  std::ostream& stream();
39658310b49fc8a7a713b922319a849a419858db79eDan Albert
39758310b49fc8a7a713b922319a849a419858db79eDan Albert  // The routine that performs the actual logging.
3980c055863ebb57c3446897e89c2c39a67442c1be4Dan Albert  static void LogLine(const char* file, unsigned int line, LogId id,
3990c055863ebb57c3446897e89c2c39a67442c1be4Dan Albert                      LogSeverity severity, const char* msg);
40058310b49fc8a7a713b922319a849a419858db79eDan Albert
40158310b49fc8a7a713b922319a849a419858db79eDan Albert private:
40258310b49fc8a7a713b922319a849a419858db79eDan Albert  const std::unique_ptr<LogMessageData> data_;
40358310b49fc8a7a713b922319a849a419858db79eDan Albert
40458310b49fc8a7a713b922319a849a419858db79eDan Albert  DISALLOW_COPY_AND_ASSIGN(LogMessage);
40558310b49fc8a7a713b922319a849a419858db79eDan Albert};
40658310b49fc8a7a713b922319a849a419858db79eDan Albert
4077bc87a5a780361928bd1aeed9d2f22233fe05407Elliott Hughes// Get the minimum severity level for logging.
4087bc87a5a780361928bd1aeed9d2f22233fe05407Elliott HughesLogSeverity GetMinimumLogSeverity();
4097bc87a5a780361928bd1aeed9d2f22233fe05407Elliott Hughes
4107bc87a5a780361928bd1aeed9d2f22233fe05407Elliott Hughes// Set the minimum severity level for logging, returning the old severity.
4117bc87a5a780361928bd1aeed9d2f22233fe05407Elliott HughesLogSeverity SetMinimumLogSeverity(LogSeverity new_severity);
4127bc87a5a780361928bd1aeed9d2f22233fe05407Elliott Hughes
41358310b49fc8a7a713b922319a849a419858db79eDan Albert// Allows to temporarily change the minimum severity level for logging.
41458310b49fc8a7a713b922319a849a419858db79eDan Albertclass ScopedLogSeverity {
41558310b49fc8a7a713b922319a849a419858db79eDan Albert public:
41658310b49fc8a7a713b922319a849a419858db79eDan Albert  explicit ScopedLogSeverity(LogSeverity level);
41758310b49fc8a7a713b922319a849a419858db79eDan Albert  ~ScopedLogSeverity();
41858310b49fc8a7a713b922319a849a419858db79eDan Albert
41958310b49fc8a7a713b922319a849a419858db79eDan Albert private:
42058310b49fc8a7a713b922319a849a419858db79eDan Albert  LogSeverity old_;
42158310b49fc8a7a713b922319a849a419858db79eDan Albert};
42258310b49fc8a7a713b922319a849a419858db79eDan Albert
42358310b49fc8a7a713b922319a849a419858db79eDan Albert}  // namespace base
42458310b49fc8a7a713b922319a849a419858db79eDan Albert}  // namespace android
42558310b49fc8a7a713b922319a849a419858db79eDan Albert
42654c72aaccc45edf4832cfdc5053d9ae6acc9bcdfElliott Hughes#endif  // ANDROID_BASE_LOGGING_H
427