OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 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 // Logging macros, similar to Chromium's base/logging.h, except with |MOJO_| |
| 6 // prefixes and missing some features (notably |CHECK_EQ()|, etc.). |
| 7 |
| 8 // TODO(vtl): It's weird that this is in the environment directory, since its |
| 9 // implementation (in environment/lib) is meant to be used by any implementation |
| 10 // of the environment. |
| 11 |
| 12 #ifndef MOJO_PUBLIC_CPP_ENVIRONMENT_LOGGING_H_ |
| 13 #define MOJO_PUBLIC_CPP_ENVIRONMENT_LOGGING_H_ |
| 14 |
| 15 #include <sstream> |
| 16 |
| 17 #include "mojo/public/c/environment/logger.h" |
| 18 #include "mojo/public/cpp/environment/environment.h" |
| 19 #include "mojo/public/cpp/system/macros.h" |
| 20 |
| 21 #define MOJO_LOG_STREAM(level) \ |
| 22 ::mojo::internal::LogMessage(__FILE__, __LINE__, \ |
| 23 MOJO_LOG_LEVEL_ ## level).stream() |
| 24 |
| 25 #define MOJO_LAZY_LOG_STREAM(level, condition) \ |
| 26 !(condition) ? \ |
| 27 (void) 0 : \ |
| 28 ::mojo::internal::VoidifyOstream() & MOJO_LOG_STREAM(level) |
| 29 |
| 30 #define MOJO_SHOULD_LOG(level) \ |
| 31 (MOJO_LOG_LEVEL_ ## level >= \ |
| 32 ::mojo::Environment::GetDefaultLogger()->GetMinimumLogLevel()) |
| 33 |
| 34 #define MOJO_LOG(level) \ |
| 35 MOJO_LAZY_LOG_STREAM(level, MOJO_SHOULD_LOG(level)) |
| 36 |
| 37 #define MOJO_LOG_IF(level, condition) \ |
| 38 MOJO_LAZY_LOG_STREAM(level, MOJO_SHOULD_LOG(level) && (condition)) |
| 39 |
| 40 #define MOJO_CHECK(condition) \ |
| 41 MOJO_LAZY_LOG_STREAM(FATAL, !(condition)) \ |
| 42 << "Check failed: " #condition ". " |
| 43 |
| 44 // Note: For non-debug builds, |MOJO_DLOG_IF()| *eliminates* (i.e., doesn't |
| 45 // compile) the condition, whereas |MOJO_DCHECK()| "neuters" the condition |
| 46 // (i.e., compiles, but doesn't evaluate). |
| 47 #ifdef NDEBUG |
| 48 |
| 49 #define MOJO_DLOG(level) MOJO_LAZY_LOG_STREAM(level, false) |
| 50 #define MOJO_DLOG_IF(level, condition) MOJO_LAZY_LOG_STREAM(level, false) |
| 51 #define MOJO_DCHECK(condition) MOJO_LAZY_LOG_STREAM(FATAL, false && (condition)) |
| 52 |
| 53 #else |
| 54 |
| 55 #define MOJO_DLOG(level) MOJO_LOG(level) |
| 56 #define MOJO_DLOG_IF(level, condition) MOJO_LOG_IF(level, condition) |
| 57 #define MOJO_DCHECK(condition) MOJO_CHECK(condition) |
| 58 |
| 59 #endif // NDEBUG |
| 60 |
| 61 namespace mojo { |
| 62 namespace internal { |
| 63 |
| 64 class LogMessage { |
| 65 public: |
| 66 LogMessage(const char* file, int line, MojoLogLevel log_level); |
| 67 ~LogMessage(); |
| 68 |
| 69 std::ostream& stream() { return stream_; } |
| 70 |
| 71 private: |
| 72 const MojoLogLevel log_level_; |
| 73 std::ostringstream stream_; |
| 74 |
| 75 MOJO_DISALLOW_COPY_AND_ASSIGN(LogMessage); |
| 76 }; |
| 77 |
| 78 // Used to ignore a stream. |
| 79 struct VoidifyOstream { |
| 80 // Use & since it has precedence lower than << but higher than ?:. |
| 81 void operator&(std::ostream&) {} |
| 82 }; |
| 83 |
| 84 } // namespace internal |
| 85 } // namespace mojo |
| 86 |
| 87 #endif // MOJO_PUBLIC_CPP_ENVIRONMENT_LOGGING_H_ |
OLD | NEW |