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 | |
| 91 | #define CHECK(condition) \ |
| 92 | LAZY_STREAM(LOG_FATAL, !(condition)) << "CHECK(" << #condition << ") failed: " |
| 93 | |
| 94 | #if defined(_DEBUG) || defined(DCHECK_ALWAYS_ON) |
| 95 | #define DCHECK_IS_ON() 1 |
| 96 | #define DCHECK(condition) CHECK(condition) |
| 97 | #else |
| 98 | #define DCHECK_IS_ON() 0 |
| 99 | #define DCHECK(condition) EAT_STREAM |
| 100 | #endif |
| 101 | |
| 102 | } // namespace platform |
| 103 | } // namespace openscreen |
| 104 | |
| 105 | #endif |