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 |
btolsch | 6d3d0bc | 2018-12-10 18:59:28 -0800 | [diff] [blame] | 25 | // The following functions must be implemented by the platform. |
| 26 | void LogInit(const char* filename); |
btolsch | 9d6900c | 2018-05-30 18:22:53 -0700 | [diff] [blame] | 27 | void SetLogLevel(LogLevel level, int verbose_level = 0); |
| 28 | void LogWithLevel(LogLevel level, |
| 29 | int verbose_level, |
| 30 | const char* file, |
| 31 | int line, |
| 32 | const char* msg); |
| 33 | void Break(); |
| 34 | // |
| 35 | // END PLATFORM IMPLEMENTATION |
| 36 | // |
| 37 | |
| 38 | // The stream-based logging macros below are adapted from Chromium's |
| 39 | // base/logging.h. |
| 40 | class LogMessage { |
| 41 | public: |
| 42 | LogMessage(LogLevel level, int verbose_level, const char* file, int line); |
| 43 | ~LogMessage(); |
| 44 | |
| 45 | std::ostream& stream() { return stream_; } |
| 46 | |
| 47 | private: |
| 48 | const LogLevel level_; |
| 49 | const int verbose_level_; |
| 50 | const char* const file_; |
| 51 | const int line_; |
| 52 | std::ostringstream stream_; |
| 53 | }; |
| 54 | |
btolsch | fc64768 | 2018-11-13 23:43:33 -0800 | [diff] [blame] | 55 | #define OSP_VLOG(l) \ |
btolsch | 9d6900c | 2018-05-30 18:22:53 -0700 | [diff] [blame] | 56 | ::openscreen::platform::LogMessage( \ |
| 57 | ::openscreen::platform::LogLevel::kVerbose, l, __FILE__, __LINE__) \ |
| 58 | .stream() |
btolsch | fc64768 | 2018-11-13 23:43:33 -0800 | [diff] [blame] | 59 | #define OSP_LOG_INFO \ |
btolsch | 9d6900c | 2018-05-30 18:22:53 -0700 | [diff] [blame] | 60 | ::openscreen::platform::LogMessage(::openscreen::platform::LogLevel::kInfo, \ |
| 61 | 0, __FILE__, __LINE__) \ |
| 62 | .stream() |
btolsch | fc64768 | 2018-11-13 23:43:33 -0800 | [diff] [blame] | 63 | #define OSP_LOG_WARN \ |
btolsch | 9d6900c | 2018-05-30 18:22:53 -0700 | [diff] [blame] | 64 | ::openscreen::platform::LogMessage( \ |
| 65 | ::openscreen::platform::LogLevel::kWarning, 0, __FILE__, __LINE__) \ |
| 66 | .stream() |
btolsch | fc64768 | 2018-11-13 23:43:33 -0800 | [diff] [blame] | 67 | #define OSP_LOG_ERROR \ |
btolsch | 9d6900c | 2018-05-30 18:22:53 -0700 | [diff] [blame] | 68 | ::openscreen::platform::LogMessage(::openscreen::platform::LogLevel::kError, \ |
| 69 | 0, __FILE__, __LINE__) \ |
| 70 | .stream() |
btolsch | fc64768 | 2018-11-13 23:43:33 -0800 | [diff] [blame] | 71 | #define OSP_LOG_FATAL \ |
btolsch | 9d6900c | 2018-05-30 18:22:53 -0700 | [diff] [blame] | 72 | ::openscreen::platform::LogMessage(::openscreen::platform::LogLevel::kFatal, \ |
| 73 | 0, __FILE__, __LINE__) \ |
| 74 | .stream() |
| 75 | |
| 76 | namespace detail { |
| 77 | |
| 78 | class Voidify { |
| 79 | public: |
| 80 | Voidify() = default; |
| 81 | void operator&(std::ostream&) {} |
| 82 | }; |
| 83 | |
| 84 | } // namespace detail |
| 85 | |
btolsch | fc64768 | 2018-11-13 23:43:33 -0800 | [diff] [blame] | 86 | #define OSP_LAZY_STREAM(stream, condition) \ |
btolsch | 9d6900c | 2018-05-30 18:22:53 -0700 | [diff] [blame] | 87 | !(condition) ? (void)0 : ::openscreen::platform::detail::Voidify() & (stream) |
btolsch | fc64768 | 2018-11-13 23:43:33 -0800 | [diff] [blame] | 88 | #define OSP_EAT_STREAM OSP_LAZY_STREAM(OSP_LOG_INFO, false) |
| 89 | #define OSP_LOG_IF(level, condition) \ |
| 90 | OSP_LAZY_STREAM(OSP_LOG_##level, (condition)) |
| 91 | #define OSP_VLOG_IF(level, condition) \ |
| 92 | OSP_LAZY_STREAM(OSP_VLOG(level), (condition)) |
btolsch | 9d6900c | 2018-05-30 18:22:53 -0700 | [diff] [blame] | 93 | |
btolsch | fc64768 | 2018-11-13 23:43:33 -0800 | [diff] [blame] | 94 | // TODO(btolsch): Add tests for (D)OSP_CHECK and possibly logging. |
| 95 | #define OSP_CHECK(condition) \ |
| 96 | OSP_LAZY_STREAM(OSP_LOG_FATAL, !(condition)) \ |
| 97 | << "OSP_CHECK(" << #condition << ") failed: " |
btolsch | 9d6900c | 2018-05-30 18:22:53 -0700 | [diff] [blame] | 98 | |
btolsch | fc64768 | 2018-11-13 23:43:33 -0800 | [diff] [blame] | 99 | #define OSP_CHECK_EQ(a, b) OSP_CHECK((a) == (b)) << a << " vs. " << b << ": " |
| 100 | #define OSP_CHECK_NE(a, b) OSP_CHECK((a) != (b)) << a << " vs. " << b << ": " |
| 101 | #define OSP_CHECK_LT(a, b) OSP_CHECK((a) < (b)) << a << " vs. " << b << ": " |
| 102 | #define OSP_CHECK_LE(a, b) OSP_CHECK((a) <= (b)) << a << " vs. " << b << ": " |
| 103 | #define OSP_CHECK_GT(a, b) OSP_CHECK((a) > (b)) << a << " vs. " << b << ": " |
| 104 | #define OSP_CHECK_GE(a, b) OSP_CHECK((a) >= (b)) << a << " vs. " << b << ": " |
btolsch | 834accf | 2018-06-13 23:10:28 -0700 | [diff] [blame] | 105 | |
btolsch | fc64768 | 2018-11-13 23:43:33 -0800 | [diff] [blame] | 106 | #if defined(_DEBUG) || defined(OSP_DCHECK_ALWAYS_ON) |
| 107 | #define OSP_DCHECK_IS_ON() 1 |
| 108 | #define OSP_DCHECK(condition) OSP_CHECK(condition) |
| 109 | #define OSP_DCHECK_EQ(a, b) OSP_CHECK_EQ(a, b) |
| 110 | #define OSP_DCHECK_NE(a, b) OSP_CHECK_NE(a, b) |
| 111 | #define OSP_DCHECK_LT(a, b) OSP_CHECK_LT(a, b) |
| 112 | #define OSP_DCHECK_LE(a, b) OSP_CHECK_LE(a, b) |
| 113 | #define OSP_DCHECK_GT(a, b) OSP_CHECK_GT(a, b) |
| 114 | #define OSP_DCHECK_GE(a, b) OSP_CHECK_GE(a, b) |
btolsch | 9d6900c | 2018-05-30 18:22:53 -0700 | [diff] [blame] | 115 | #else |
btolsch | fc64768 | 2018-11-13 23:43:33 -0800 | [diff] [blame] | 116 | #define OSP_DCHECK_IS_ON() 0 |
| 117 | #define OSP_DCHECK(condition) OSP_EAT_STREAM |
| 118 | #define OSP_DCHECK_EQ(a, b) OSP_EAT_STREAM |
| 119 | #define OSP_DCHECK_NE(a, b) OSP_EAT_STREAM |
| 120 | #define OSP_DCHECK_LT(a, b) OSP_EAT_STREAM |
| 121 | #define OSP_DCHECK_LE(a, b) OSP_EAT_STREAM |
| 122 | #define OSP_DCHECK_GT(a, b) OSP_EAT_STREAM |
| 123 | #define OSP_DCHECK_GE(a, b) OSP_EAT_STREAM |
btolsch | 9d6900c | 2018-05-30 18:22:53 -0700 | [diff] [blame] | 124 | #endif |
| 125 | |
btolsch | fc64768 | 2018-11-13 23:43:33 -0800 | [diff] [blame] | 126 | #define OSP_DLOG_INFO OSP_LOG_IF(INFO, OSP_DCHECK_IS_ON()) |
| 127 | #define OSP_DLOG_WARN OSP_LOG_IF(WARN, OSP_DCHECK_IS_ON()) |
| 128 | #define OSP_DLOG_ERROR OSP_LOG_IF(ERROR, OSP_DCHECK_IS_ON()) |
| 129 | #define OSP_DLOG_FATAL OSP_LOG_IF(FATAL, OSP_DCHECK_IS_ON()) |
| 130 | #define OSP_DLOG_IF(level, condition) \ |
| 131 | OSP_LOG_IF(level, OSP_DCHECK_IS_ON() && (condition)) |
| 132 | #define OSP_DVLOG(l) OSP_VLOG_IF(l, OSP_DCHECK_IS_ON()) |
| 133 | #define OSP_DVLOG_IF(l, condition) \ |
| 134 | OSP_VLOG_IF(l, OSP_DCHECK_IS_ON() && (condition)) |
btolsch | 834accf | 2018-06-13 23:10:28 -0700 | [diff] [blame] | 135 | |
btolsch | fc64768 | 2018-11-13 23:43:33 -0800 | [diff] [blame] | 136 | #define OSP_UNIMPLEMENTED() OSP_LOG_ERROR << __func__ << ": unimplemented" |
btolsch | 4b68dc9 | 2018-07-26 00:26:54 -0700 | [diff] [blame] | 137 | |
btolsch | 9d6900c | 2018-05-30 18:22:53 -0700 | [diff] [blame] | 138 | } // namespace platform |
| 139 | } // namespace openscreen |
| 140 | |
btolsch | a21e8ed | 2018-08-30 15:13:48 -0700 | [diff] [blame] | 141 | #endif // PLATFORM_API_LOGGING_H_ |