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__, MOJO_LOG_LEVEL_##level) \ | |
23 .stream() | |
24 | |
25 #define MOJO_LAZY_LOG_STREAM(level, condition) \ | |
26 !(condition) ? (void)0 \ | |
27 : ::mojo::internal::VoidifyOstream() & MOJO_LOG_STREAM(level) | |
28 | |
29 #define MOJO_SHOULD_LOG(level) \ | |
30 (MOJO_LOG_LEVEL_##level >= \ | |
31 ::mojo::Environment::GetDefaultLogger()->GetMinimumLogLevel()) | |
32 | |
33 #define MOJO_LOG(level) MOJO_LAZY_LOG_STREAM(level, MOJO_SHOULD_LOG(level)) | |
34 | |
35 #define MOJO_LOG_IF(level, condition) \ | |
36 MOJO_LAZY_LOG_STREAM(level, MOJO_SHOULD_LOG(level) && (condition)) | |
37 | |
38 #define MOJO_CHECK(condition) \ | |
39 MOJO_LAZY_LOG_STREAM(FATAL, !(condition)) << "Check failed: " #condition "." \ | |
40 " " | |
41 | |
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 | |
46 | |
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) | |
51 | |
52 #else | |
53 | |
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) | |
57 | |
58 #endif // NDEBUG | |
59 | |
60 namespace mojo { | |
61 namespace internal { | |
62 | |
63 class LogMessage { | |
64 public: | |
65 LogMessage(const char* file, int line, MojoLogLevel log_level); | |
66 ~LogMessage(); | |
67 | |
68 std::ostream& stream() { return stream_; } | |
69 | |
70 private: | |
71 const MojoLogLevel log_level_; | |
72 std::ostringstream stream_; | |
73 | |
74 MOJO_DISALLOW_COPY_AND_ASSIGN(LogMessage); | |
75 }; | |
76 | |
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&) {} | |
81 }; | |
82 | |
83 } // namespace internal | |
84 } // namespace mojo | |
85 | |
86 #endif // MOJO_PUBLIC_CPP_ENVIRONMENT_LOGGING_H_ | |
OLD | NEW |