Blink roll 25b6bd3a7a131ffe68d809546ad1a20707915cdc:3a503f41ae42e5b79cfcd2ff10e65afde...
[chromium-blink-merge.git] / mojo / public / cpp / environment / logging.h
blob5df18fb542272dc8d44e5c8aa5c961e52314e81c
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.
5 // Logging macros, similar to Chromium's base/logging.h, except with |MOJO_|
6 // prefixes and missing some features (notably |CHECK_EQ()|, etc.).
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.
12 #ifndef MOJO_PUBLIC_CPP_ENVIRONMENT_LOGGING_H_
13 #define MOJO_PUBLIC_CPP_ENVIRONMENT_LOGGING_H_
15 #include <sstream>
17 #include "mojo/public/c/environment/logger.h"
18 #include "mojo/public/cpp/environment/environment.h"
19 #include "mojo/public/cpp/system/macros.h"
21 #define MOJO_LOG_STREAM(level) \
22 ::mojo::internal::LogMessage(__FILE__, __LINE__, MOJO_LOG_LEVEL_##level) \
23 .stream()
25 #define MOJO_LAZY_LOG_STREAM(level, condition) \
26 !(condition) ? (void)0 \
27 : ::mojo::internal::VoidifyOstream() & MOJO_LOG_STREAM(level)
29 #define MOJO_SHOULD_LOG(level) \
30 (MOJO_LOG_LEVEL_##level >= \
31 ::mojo::Environment::GetDefaultLogger()->GetMinimumLogLevel())
33 #define MOJO_LOG(level) MOJO_LAZY_LOG_STREAM(level, MOJO_SHOULD_LOG(level))
35 #define MOJO_LOG_IF(level, condition) \
36 MOJO_LAZY_LOG_STREAM(level, MOJO_SHOULD_LOG(level) && (condition))
38 #define MOJO_CHECK(condition) \
39 MOJO_LAZY_LOG_STREAM(FATAL, !(condition)) << "Check failed: " #condition "." \
40 " "
42 // Note: For non-debug builds, |MOJO_DLOG_IF()| *eliminates* (i.e., doesn't
43 // compile) the condition, whereas |MOJO_DCHECK()| "neuters" the condition
44 // (i.e., compiles, but doesn't evaluate).
45 #ifdef NDEBUG
47 #define MOJO_DLOG(level) MOJO_LAZY_LOG_STREAM(level, false)
48 #define MOJO_DLOG_IF(level, condition) MOJO_LAZY_LOG_STREAM(level, false)
49 #define MOJO_DCHECK(condition) \
50 MOJO_LAZY_LOG_STREAM(FATAL, false ? !(condition) : false)
52 #else
54 #define MOJO_DLOG(level) MOJO_LOG(level)
55 #define MOJO_DLOG_IF(level, condition) MOJO_LOG_IF(level, condition)
56 #define MOJO_DCHECK(condition) MOJO_CHECK(condition)
58 #endif // NDEBUG
60 namespace mojo {
61 namespace internal {
63 class LogMessage {
64 public:
65 LogMessage(const char* file, int line, MojoLogLevel log_level);
66 ~LogMessage();
68 std::ostream& stream() { return stream_; }
70 private:
71 const MojoLogLevel log_level_;
72 std::ostringstream stream_;
74 MOJO_DISALLOW_COPY_AND_ASSIGN(LogMessage);
77 // Used to ignore a stream.
78 struct VoidifyOstream {
79 // Use & since it has precedence lower than << but higher than ?:.
80 void operator&(std::ostream&) {}
83 } // namespace internal
84 } // namespace mojo
86 #endif // MOJO_PUBLIC_CPP_ENVIRONMENT_LOGGING_H_