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 <mojo/environment/logger.h> | |
16 | |
17 #include <sstream> | |
18 | |
19 #include "mojo/public/cpp/environment/environment.h" | |
20 #include "mojo/public/cpp/system/macros.h" | |
21 | |
22 #define MOJO_LOG_STREAM(level) \ | |
23 ::mojo::internal::LogMessage(MOJO_LOG_LEVEL_##level, __FILE__, __LINE__) \ | |
24 .stream() | |
25 | |
26 #define MOJO_LAZY_LOG_STREAM(level, condition) \ | |
27 !(condition) ? (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) MOJO_LAZY_LOG_STREAM(level, MOJO_SHOULD_LOG(level)) | |
35 | |
36 #define MOJO_LOG_IF(level, condition) \ | |
37 MOJO_LAZY_LOG_STREAM(level, MOJO_SHOULD_LOG(level) && (condition)) | |
38 | |
39 #define MOJO_CHECK(condition) \ | |
40 MOJO_LAZY_LOG_STREAM(FATAL, !(condition)) << "Check failed: " #condition "." \ | |
41 " " | |
42 | |
43 // Note: For non-debug builds, |MOJO_DLOG_IF()| *eliminates* (i.e., doesn't | |
44 // compile) the condition, whereas |MOJO_DCHECK()| "neuters" the condition | |
45 // (i.e., compiles, but doesn't evaluate). | |
46 #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 #else | |
50 #define MOJO_DLOG(level) MOJO_LOG(level) | |
51 #define MOJO_DLOG_IF(level, condition) MOJO_LOG_IF(level, condition) | |
52 #endif // NDEBUG | |
53 | |
54 #if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON) | |
55 #define MOJO_DCHECK(condition) \ | |
56 MOJO_LAZY_LOG_STREAM(FATAL, false ? !(condition) : false) | |
57 #else | |
58 #define MOJO_DCHECK(condition) MOJO_CHECK(condition) | |
59 #endif // NDEBUG && !defined(DCHECK_ALWAYS_ON) | |
60 | |
61 namespace mojo { | |
62 namespace internal { | |
63 | |
64 class LogMessage { | |
65 public: | |
66 LogMessage(MojoLogLevel log_level, const char* file, int line); | |
67 ~LogMessage(); | |
68 | |
69 std::ostream& stream() { return stream_; } | |
70 | |
71 private: | |
72 const MojoLogLevel log_level_; | |
73 const char* const file_; | |
74 const int line_; | |
75 std::ostringstream stream_; | |
76 | |
77 MOJO_DISALLOW_COPY_AND_ASSIGN(LogMessage); | |
78 }; | |
79 | |
80 // Used to ignore a stream. | |
81 struct VoidifyOstream { | |
82 // Use & since it has precedence lower than << but higher than ?:. | |
83 void operator&(std::ostream&) {} | |
84 }; | |
85 | |
86 } // namespace internal | |
87 } // namespace mojo | |
88 | |
89 #endif // MOJO_PUBLIC_CPP_ENVIRONMENT_LOGGING_H_ | |
OLD | NEW |