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 "base/macros.h" | |
18 #include "mojo/public/c/environment/logger.h" | |
19 #include "mojo/public/cpp/environment/environment.h" | |
20 #include "mojo/public/cpp/system/macros.h" | |
21 | |
22 #if defined(OS_WIN) | |
23 // To avoid a compile failure on Windows because it defines ERROR, which is also | |
24 // used by the logs. Similar to change in base/logging.h. | |
25 #undef ERROR | |
26 #endif | |
27 | |
28 #define MOJO_LOG_STREAM(level) \ | |
29 ::mojo::internal::LogMessage(MOJO_LOG_LEVEL_##level, __FILE__, __LINE__) \ | |
30 .stream() | |
31 | |
32 #define MOJO_LAZY_LOG_STREAM(level, condition) \ | |
33 !(condition) ? (void)0 \ | |
34 : ::mojo::internal::VoidifyOstream() & MOJO_LOG_STREAM(level) | |
35 | |
36 #define MOJO_SHOULD_LOG(level) \ | |
37 (MOJO_LOG_LEVEL_##level >= \ | |
38 ::mojo::Environment::GetDefaultLogger()->GetMinimumLogLevel()) | |
39 | |
40 #define MOJO_LOG(level) MOJO_LAZY_LOG_STREAM(level, MOJO_SHOULD_LOG(level)) | |
41 | |
42 #define MOJO_LOG_IF(level, condition) \ | |
43 MOJO_LAZY_LOG_STREAM(level, MOJO_SHOULD_LOG(level) && (condition)) | |
44 | |
45 #define MOJO_CHECK(condition) \ | |
46 MOJO_LAZY_LOG_STREAM(FATAL, !(condition)) << "Check failed: " #condition "." \ | |
47 " " | |
48 | |
49 // Note: For non-debug builds, |MOJO_DLOG_IF()| *eliminates* (i.e., doesn't | |
50 // compile) the condition, whereas |MOJO_DCHECK()| "neuters" the condition | |
51 // (i.e., compiles, but doesn't evaluate). | |
52 #ifdef NDEBUG | |
53 #define MOJO_DLOG(level) MOJO_LAZY_LOG_STREAM(level, false) | |
54 #define MOJO_DLOG_IF(level, condition) MOJO_LAZY_LOG_STREAM(level, false) | |
55 #else | |
56 #define MOJO_DLOG(level) MOJO_LOG(level) | |
57 #define MOJO_DLOG_IF(level, condition) MOJO_LOG_IF(level, condition) | |
58 #endif // NDEBUG | |
59 | |
60 #if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON) | |
61 #define MOJO_DCHECK(condition) \ | |
62 MOJO_LAZY_LOG_STREAM(FATAL, false ? !(condition) : false) | |
63 #else | |
64 #define MOJO_DCHECK(condition) MOJO_CHECK(condition) | |
65 #endif // NDEBUG && !defined(DCHECK_ALWAYS_ON) | |
66 | |
67 #define MOJO_NOTREACHED() MOJO_DCHECK(false) | |
68 | |
69 namespace mojo { | |
70 namespace internal { | |
71 | |
72 class LogMessage { | |
73 public: | |
74 LogMessage(MojoLogLevel log_level, const char* file, int line); | |
75 ~LogMessage(); | |
76 | |
77 std::ostream& stream() { return stream_; } | |
78 | |
79 private: | |
80 const MojoLogLevel log_level_; | |
81 const char* const file_; | |
82 const int line_; | |
83 std::ostringstream stream_; | |
84 | |
85 DISALLOW_COPY_AND_ASSIGN(LogMessage); | |
86 }; | |
87 | |
88 // Used to ignore a stream. | |
89 struct VoidifyOstream { | |
90 // Use & since it has precedence lower than << but higher than ?:. | |
91 void operator&(std::ostream&) {} | |
92 }; | |
93 | |
94 } // namespace internal | |
95 } // namespace mojo | |
96 | |
97 #endif // MOJO_PUBLIC_CPP_ENVIRONMENT_LOGGING_H_ | |
OLD | NEW |