btolsch | 9d6900c | 2018-05-30 18:22:53 -0700 | [diff] [blame] | 1 | // Copyright 2018 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef PLATFORM_API_LOGGING_H_ |
| 6 | #define PLATFORM_API_LOGGING_H_ |
| 7 | |
| 8 | #include <sstream> |
| 9 | |
| 10 | namespace openscreen { |
| 11 | namespace platform { |
| 12 | |
| 13 | enum class LogLevel { |
| 14 | kVerbose = 0, |
| 15 | kInfo, |
| 16 | kWarning, |
| 17 | kError, |
| 18 | kFatal, |
| 19 | }; |
| 20 | |
| 21 | std::string LogLevelToString(LogLevel level); |
| 22 | |
| 23 | // |
| 24 | // PLATFORM IMPLEMENTATION |
| 25 | // The follow functions must be implemented by the platform. |
| 26 | void SetLogLevel(LogLevel level, int verbose_level = 0); |
| 27 | void LogWithLevel(LogLevel level, |
| 28 | int verbose_level, |
| 29 | const char* file, |
| 30 | int line, |
| 31 | const char* msg); |
| 32 | void Break(); |
| 33 | // |
| 34 | // END PLATFORM IMPLEMENTATION |
| 35 | // |
| 36 | |
| 37 | // The stream-based logging macros below are adapted from Chromium's |
| 38 | // base/logging.h. |
| 39 | class LogMessage { |
| 40 | public: |
| 41 | LogMessage(LogLevel level, int verbose_level, const char* file, int line); |
| 42 | ~LogMessage(); |
| 43 | |
| 44 | std::ostream& stream() { return stream_; } |
| 45 | |
| 46 | private: |
| 47 | const LogLevel level_; |
| 48 | const int verbose_level_; |
| 49 | const char* const file_; |
| 50 | const int line_; |
| 51 | std::ostringstream stream_; |
| 52 | }; |
| 53 | |
| 54 | #define VLOG(l) \ |
| 55 | ::openscreen::platform::LogMessage( \ |
| 56 | ::openscreen::platform::LogLevel::kVerbose, l, __FILE__, __LINE__) \ |
| 57 | .stream() |
| 58 | #define LOG_INFO \ |
| 59 | ::openscreen::platform::LogMessage(::openscreen::platform::LogLevel::kInfo, \ |
| 60 | 0, __FILE__, __LINE__) \ |
| 61 | .stream() |
| 62 | #define LOG_WARN \ |
| 63 | ::openscreen::platform::LogMessage( \ |
| 64 | ::openscreen::platform::LogLevel::kWarning, 0, __FILE__, __LINE__) \ |
| 65 | .stream() |
| 66 | #define LOG_ERROR \ |
| 67 | ::openscreen::platform::LogMessage(::openscreen::platform::LogLevel::kError, \ |
| 68 | 0, __FILE__, __LINE__) \ |
| 69 | .stream() |
| 70 | #define LOG_FATAL \ |
| 71 | ::openscreen::platform::LogMessage(::openscreen::platform::LogLevel::kFatal, \ |
| 72 | 0, __FILE__, __LINE__) \ |
| 73 | .stream() |
| 74 | |
| 75 | namespace detail { |
| 76 | |
| 77 | class Voidify { |
| 78 | public: |
| 79 | Voidify() = default; |
| 80 | void operator&(std::ostream&) {} |
| 81 | }; |
| 82 | |
| 83 | } // namespace detail |
| 84 | |
| 85 | #define LAZY_STREAM(stream, condition) \ |
| 86 | !(condition) ? (void)0 : ::openscreen::platform::detail::Voidify() & (stream) |
| 87 | #define EAT_STREAM LAZY_STREAM(LOG_INFO, false) |
| 88 | #define LOG_IF(level, condition) LAZY_STREAM(LOG_##level, (condition)) |
| 89 | #define VLOG_IF(level, condition) LAZY_STREAM(VLOG(level), (condition)) |
| 90 | |
btolsch | 834accf | 2018-06-13 23:10:28 -0700 | [diff] [blame] | 91 | // TODO(btolsch): Add tests for (D)CHECK and possibly logging. |
btolsch | 9d6900c | 2018-05-30 18:22:53 -0700 | [diff] [blame] | 92 | #define CHECK(condition) \ |
| 93 | LAZY_STREAM(LOG_FATAL, !(condition)) << "CHECK(" << #condition << ") failed: " |
| 94 | |
btolsch | 834accf | 2018-06-13 23:10:28 -0700 | [diff] [blame] | 95 | #define CHECK_EQ(a, b) CHECK((a) == (b)) << a << " vs. " << b << ": " |
| 96 | #define CHECK_NE(a, b) CHECK((a) != (b)) << a << " vs. " << b << ": " |
| 97 | #define CHECK_LT(a, b) CHECK((a) < (b)) << a << " vs. " << b << ": " |
| 98 | #define CHECK_LE(a, b) CHECK((a) <= (b)) << a << " vs. " << b << ": " |
| 99 | #define CHECK_GT(a, b) CHECK((a) > (b)) << a << " vs. " << b << ": " |
| 100 | #define CHECK_GE(a, b) CHECK((a) >= (b)) << a << " vs. " << b << ": " |
| 101 | |
btolsch | 9d6900c | 2018-05-30 18:22:53 -0700 | [diff] [blame] | 102 | #if defined(_DEBUG) || defined(DCHECK_ALWAYS_ON) |
| 103 | #define DCHECK_IS_ON() 1 |
| 104 | #define DCHECK(condition) CHECK(condition) |
btolsch | 834accf | 2018-06-13 23:10:28 -0700 | [diff] [blame] | 105 | #define DCHECK_EQ(a, b) CHECK_EQ(a, b) |
| 106 | #define DCHECK_NE(a, b) CHECK_NE(a, b) |
| 107 | #define DCHECK_LT(a, b) CHECK_LT(a, b) |
| 108 | #define DCHECK_LE(a, b) CHECK_LE(a, b) |
| 109 | #define DCHECK_GT(a, b) CHECK_GT(a, b) |
| 110 | #define DCHECK_GE(a, b) CHECK_GE(a, b) |
btolsch | 9d6900c | 2018-05-30 18:22:53 -0700 | [diff] [blame] | 111 | #else |
| 112 | #define DCHECK_IS_ON() 0 |
| 113 | #define DCHECK(condition) EAT_STREAM |
btolsch | 834accf | 2018-06-13 23:10:28 -0700 | [diff] [blame] | 114 | #define DCHECK_EQ(a, b) EAT_STREAM |
| 115 | #define DCHECK_NE(a, b) EAT_STREAM |
| 116 | #define DCHECK_LT(a, b) EAT_STREAM |
| 117 | #define DCHECK_LE(a, b) EAT_STREAM |
| 118 | #define DCHECK_GT(a, b) EAT_STREAM |
| 119 | #define DCHECK_GE(a, b) EAT_STREAM |
btolsch | 9d6900c | 2018-05-30 18:22:53 -0700 | [diff] [blame] | 120 | #endif |
| 121 | |
btolsch | 834accf | 2018-06-13 23:10:28 -0700 | [diff] [blame] | 122 | #define DLOG_INFO LOG_IF(INFO, DCHECK_IS_ON()) |
| 123 | #define DLOG_WARN LOG_IF(WARN, DCHECK_IS_ON()) |
| 124 | #define DLOG_ERROR LOG_IF(ERROR, DCHECK_IS_ON()) |
| 125 | #define DLOG_FATAL LOG_IF(FATAL, DCHECK_IS_ON()) |
| 126 | #define DLOG_IF(level, condition) LOG_IF(level, DCHECK_IS_ON() && (condition)) |
| 127 | #define DVLOG(l) VLOG_IF(l, DCHECK_IS_ON()) |
| 128 | #define DVLOG_IF(l, condition) VLOG_IF(l, DCHECK_IS_ON() && (condition)) |
| 129 | |
btolsch | 4b68dc9 | 2018-07-26 00:26:54 -0700 | [diff] [blame] | 130 | #define UNIMPLEMENTED() LOG_ERROR << __func__ << ": unimplemented" |
| 131 | |
btolsch | 9d6900c | 2018-05-30 18:22:53 -0700 | [diff] [blame] | 132 | } // namespace platform |
| 133 | } // namespace openscreen |
| 134 | |
| 135 | #endif |