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 | |
Jordan Bayles | d08f8d2 | 2019-01-17 15:06:02 -0800 | [diff] [blame] | 10 | #include "third_party/abseil/src/absl/strings/string_view.h" |
| 11 | |
btolsch | 9d6900c | 2018-05-30 18:22:53 -0700 | [diff] [blame] | 12 | namespace openscreen { |
| 13 | namespace platform { |
| 14 | |
| 15 | enum class LogLevel { |
| 16 | kVerbose = 0, |
| 17 | kInfo, |
| 18 | kWarning, |
| 19 | kError, |
| 20 | kFatal, |
| 21 | }; |
| 22 | |
| 23 | std::string LogLevelToString(LogLevel level); |
| 24 | |
| 25 | // |
| 26 | // PLATFORM IMPLEMENTATION |
btolsch | 6d3d0bc | 2018-12-10 18:59:28 -0800 | [diff] [blame] | 27 | // The following functions must be implemented by the platform. |
| 28 | void LogInit(const char* filename); |
btolsch | 9d6900c | 2018-05-30 18:22:53 -0700 | [diff] [blame] | 29 | void SetLogLevel(LogLevel level, int verbose_level = 0); |
| 30 | void LogWithLevel(LogLevel level, |
| 31 | int verbose_level, |
Jordan Bayles | d08f8d2 | 2019-01-17 15:06:02 -0800 | [diff] [blame] | 32 | absl::string_view file, |
btolsch | 9d6900c | 2018-05-30 18:22:53 -0700 | [diff] [blame] | 33 | int line, |
Jordan Bayles | d08f8d2 | 2019-01-17 15:06:02 -0800 | [diff] [blame] | 34 | absl::string_view msg); |
btolsch | 9d6900c | 2018-05-30 18:22:53 -0700 | [diff] [blame] | 35 | void Break(); |
| 36 | // |
| 37 | // END PLATFORM IMPLEMENTATION |
| 38 | // |
| 39 | |
| 40 | // The stream-based logging macros below are adapted from Chromium's |
| 41 | // base/logging.h. |
| 42 | class LogMessage { |
| 43 | public: |
Jordan Bayles | d08f8d2 | 2019-01-17 15:06:02 -0800 | [diff] [blame] | 44 | LogMessage(LogLevel level, |
| 45 | int verbose_level, |
| 46 | absl::string_view file, |
| 47 | int line); |
btolsch | 9d6900c | 2018-05-30 18:22:53 -0700 | [diff] [blame] | 48 | ~LogMessage(); |
| 49 | |
| 50 | std::ostream& stream() { return stream_; } |
| 51 | |
| 52 | private: |
| 53 | const LogLevel level_; |
| 54 | const int verbose_level_; |
Jordan Bayles | d08f8d2 | 2019-01-17 15:06:02 -0800 | [diff] [blame] | 55 | |
| 56 | // The file here comes from the __FILE__ macro, which should persist while |
| 57 | // we are doing the logging. Hence, keeping it unmanaged here and not |
| 58 | // creating a copy should be safe. |
| 59 | absl::string_view const file_; |
btolsch | 9d6900c | 2018-05-30 18:22:53 -0700 | [diff] [blame] | 60 | const int line_; |
| 61 | std::ostringstream stream_; |
| 62 | }; |
| 63 | |
Jordan Bayles | 8ccb485 | 2019-01-16 18:51:27 -0800 | [diff] [blame^] | 64 | // TODO(jophba): Remove magic number logging. We almost always use 1, |
| 65 | // demarcation of different levels is non-obvious. |
btolsch | fc64768 | 2018-11-13 23:43:33 -0800 | [diff] [blame] | 66 | #define OSP_VLOG(l) \ |
btolsch | 9d6900c | 2018-05-30 18:22:53 -0700 | [diff] [blame] | 67 | ::openscreen::platform::LogMessage( \ |
| 68 | ::openscreen::platform::LogLevel::kVerbose, l, __FILE__, __LINE__) \ |
| 69 | .stream() |
btolsch | fc64768 | 2018-11-13 23:43:33 -0800 | [diff] [blame] | 70 | #define OSP_LOG_INFO \ |
btolsch | 9d6900c | 2018-05-30 18:22:53 -0700 | [diff] [blame] | 71 | ::openscreen::platform::LogMessage(::openscreen::platform::LogLevel::kInfo, \ |
| 72 | 0, __FILE__, __LINE__) \ |
| 73 | .stream() |
btolsch | fc64768 | 2018-11-13 23:43:33 -0800 | [diff] [blame] | 74 | #define OSP_LOG_WARN \ |
btolsch | 9d6900c | 2018-05-30 18:22:53 -0700 | [diff] [blame] | 75 | ::openscreen::platform::LogMessage( \ |
| 76 | ::openscreen::platform::LogLevel::kWarning, 0, __FILE__, __LINE__) \ |
| 77 | .stream() |
btolsch | fc64768 | 2018-11-13 23:43:33 -0800 | [diff] [blame] | 78 | #define OSP_LOG_ERROR \ |
btolsch | 9d6900c | 2018-05-30 18:22:53 -0700 | [diff] [blame] | 79 | ::openscreen::platform::LogMessage(::openscreen::platform::LogLevel::kError, \ |
| 80 | 0, __FILE__, __LINE__) \ |
| 81 | .stream() |
btolsch | fc64768 | 2018-11-13 23:43:33 -0800 | [diff] [blame] | 82 | #define OSP_LOG_FATAL \ |
btolsch | 9d6900c | 2018-05-30 18:22:53 -0700 | [diff] [blame] | 83 | ::openscreen::platform::LogMessage(::openscreen::platform::LogLevel::kFatal, \ |
| 84 | 0, __FILE__, __LINE__) \ |
| 85 | .stream() |
| 86 | |
| 87 | namespace detail { |
| 88 | |
| 89 | class Voidify { |
| 90 | public: |
| 91 | Voidify() = default; |
| 92 | void operator&(std::ostream&) {} |
| 93 | }; |
| 94 | |
| 95 | } // namespace detail |
| 96 | |
btolsch | fc64768 | 2018-11-13 23:43:33 -0800 | [diff] [blame] | 97 | #define OSP_LAZY_STREAM(stream, condition) \ |
btolsch | 9d6900c | 2018-05-30 18:22:53 -0700 | [diff] [blame] | 98 | !(condition) ? (void)0 : ::openscreen::platform::detail::Voidify() & (stream) |
btolsch | fc64768 | 2018-11-13 23:43:33 -0800 | [diff] [blame] | 99 | #define OSP_EAT_STREAM OSP_LAZY_STREAM(OSP_LOG_INFO, false) |
| 100 | #define OSP_LOG_IF(level, condition) \ |
| 101 | OSP_LAZY_STREAM(OSP_LOG_##level, (condition)) |
| 102 | #define OSP_VLOG_IF(level, condition) \ |
| 103 | OSP_LAZY_STREAM(OSP_VLOG(level), (condition)) |
btolsch | 9d6900c | 2018-05-30 18:22:53 -0700 | [diff] [blame] | 104 | |
btolsch | fc64768 | 2018-11-13 23:43:33 -0800 | [diff] [blame] | 105 | // TODO(btolsch): Add tests for (D)OSP_CHECK and possibly logging. |
| 106 | #define OSP_CHECK(condition) \ |
| 107 | OSP_LAZY_STREAM(OSP_LOG_FATAL, !(condition)) \ |
| 108 | << "OSP_CHECK(" << #condition << ") failed: " |
btolsch | 9d6900c | 2018-05-30 18:22:53 -0700 | [diff] [blame] | 109 | |
btolsch | fc64768 | 2018-11-13 23:43:33 -0800 | [diff] [blame] | 110 | #define OSP_CHECK_EQ(a, b) OSP_CHECK((a) == (b)) << a << " vs. " << b << ": " |
| 111 | #define OSP_CHECK_NE(a, b) OSP_CHECK((a) != (b)) << a << " vs. " << b << ": " |
| 112 | #define OSP_CHECK_LT(a, b) OSP_CHECK((a) < (b)) << a << " vs. " << b << ": " |
| 113 | #define OSP_CHECK_LE(a, b) OSP_CHECK((a) <= (b)) << a << " vs. " << b << ": " |
| 114 | #define OSP_CHECK_GT(a, b) OSP_CHECK((a) > (b)) << a << " vs. " << b << ": " |
| 115 | #define OSP_CHECK_GE(a, b) OSP_CHECK((a) >= (b)) << a << " vs. " << b << ": " |
btolsch | 834accf | 2018-06-13 23:10:28 -0700 | [diff] [blame] | 116 | |
Yuri Wiitala | 0233cdd | 2019-02-04 17:57:25 -0800 | [diff] [blame] | 117 | #if defined(_DEBUG) || defined(DCHECK_ALWAYS_ON) |
btolsch | fc64768 | 2018-11-13 23:43:33 -0800 | [diff] [blame] | 118 | #define OSP_DCHECK_IS_ON() 1 |
| 119 | #define OSP_DCHECK(condition) OSP_CHECK(condition) |
| 120 | #define OSP_DCHECK_EQ(a, b) OSP_CHECK_EQ(a, b) |
| 121 | #define OSP_DCHECK_NE(a, b) OSP_CHECK_NE(a, b) |
| 122 | #define OSP_DCHECK_LT(a, b) OSP_CHECK_LT(a, b) |
| 123 | #define OSP_DCHECK_LE(a, b) OSP_CHECK_LE(a, b) |
| 124 | #define OSP_DCHECK_GT(a, b) OSP_CHECK_GT(a, b) |
| 125 | #define OSP_DCHECK_GE(a, b) OSP_CHECK_GE(a, b) |
btolsch | 9d6900c | 2018-05-30 18:22:53 -0700 | [diff] [blame] | 126 | #else |
btolsch | fc64768 | 2018-11-13 23:43:33 -0800 | [diff] [blame] | 127 | #define OSP_DCHECK_IS_ON() 0 |
Yuri Wiitala | 0233cdd | 2019-02-04 17:57:25 -0800 | [diff] [blame] | 128 | // When DCHECKs are off, nothing will be logged. Use that fact to make |
| 129 | // references to the |condition| expression (or |a| and |b|) so the compiler |
| 130 | // won't emit unused variable warnings/errors when DCHECKs are turned off. |
| 131 | #define OSP_DCHECK(condition) OSP_EAT_STREAM << !(condition) |
| 132 | #define OSP_DCHECK_EQ(a, b) OSP_EAT_STREAM << !((a) == (b)) |
| 133 | #define OSP_DCHECK_NE(a, b) OSP_EAT_STREAM << !((a) != (b)) |
| 134 | #define OSP_DCHECK_LT(a, b) OSP_EAT_STREAM << !((a) < (b)) |
| 135 | #define OSP_DCHECK_LE(a, b) OSP_EAT_STREAM << !((a) <= (b)) |
| 136 | #define OSP_DCHECK_GT(a, b) OSP_EAT_STREAM << !((a) > (b)) |
| 137 | #define OSP_DCHECK_GE(a, b) OSP_EAT_STREAM << !((a) >= (b)) |
btolsch | 9d6900c | 2018-05-30 18:22:53 -0700 | [diff] [blame] | 138 | #endif |
| 139 | |
btolsch | fc64768 | 2018-11-13 23:43:33 -0800 | [diff] [blame] | 140 | #define OSP_DLOG_INFO OSP_LOG_IF(INFO, OSP_DCHECK_IS_ON()) |
| 141 | #define OSP_DLOG_WARN OSP_LOG_IF(WARN, OSP_DCHECK_IS_ON()) |
| 142 | #define OSP_DLOG_ERROR OSP_LOG_IF(ERROR, OSP_DCHECK_IS_ON()) |
| 143 | #define OSP_DLOG_FATAL OSP_LOG_IF(FATAL, OSP_DCHECK_IS_ON()) |
| 144 | #define OSP_DLOG_IF(level, condition) \ |
| 145 | OSP_LOG_IF(level, OSP_DCHECK_IS_ON() && (condition)) |
Jordan Bayles | 8ccb485 | 2019-01-16 18:51:27 -0800 | [diff] [blame^] | 146 | |
| 147 | // Verbose logging methods |
| 148 | // Conventions on log level are: |
| 149 | // 1 = verbose |
| 150 | // 2 = very verbose |
| 151 | // 3 = debugging |
| 152 | #define OSP_DVLOG OSP_VLOG_IF(1, OSP_DCHECK_IS_ON()) |
| 153 | #define OSP_DVVLOG OSP_VLOG_IF(2, OSP_DCHECK_IS_ON()) |
| 154 | #define OSP_DVLOG_DEBUG OSP_VLOG_IF(3, OSP_DCHECK_IS_ON()) |
| 155 | |
btolsch | fc64768 | 2018-11-13 23:43:33 -0800 | [diff] [blame] | 156 | #define OSP_DVLOG_IF(l, condition) \ |
| 157 | OSP_VLOG_IF(l, OSP_DCHECK_IS_ON() && (condition)) |
btolsch | 834accf | 2018-06-13 23:10:28 -0700 | [diff] [blame] | 158 | |
btolsch | fc64768 | 2018-11-13 23:43:33 -0800 | [diff] [blame] | 159 | #define OSP_UNIMPLEMENTED() OSP_LOG_ERROR << __func__ << ": unimplemented" |
btolsch | 4b68dc9 | 2018-07-26 00:26:54 -0700 | [diff] [blame] | 160 | |
Yuri Wiitala | 10dea9f | 2019-02-04 20:19:17 -0800 | [diff] [blame] | 161 | #define OSP_NOTREACHED() OSP_CHECK(false) << __func__ << ": NOTREACHED() hit." |
| 162 | |
btolsch | 9d6900c | 2018-05-30 18:22:53 -0700 | [diff] [blame] | 163 | } // namespace platform |
| 164 | } // namespace openscreen |
| 165 | |
btolsch | a21e8ed | 2018-08-30 15:13:48 -0700 | [diff] [blame] | 166 | #endif // PLATFORM_API_LOGGING_H_ |