/src/mozilla-central/security/sandbox/chromium/base/logging.h
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright (c) 2012 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 | | #ifndef BASE_LOGGING_H_ |
6 | | #define BASE_LOGGING_H_ |
7 | | |
8 | | #include <stddef.h> |
9 | | |
10 | | #include <cassert> |
11 | | #include <cstring> |
12 | | #include <sstream> |
13 | | #include <string> |
14 | | #include <type_traits> |
15 | | #include <utility> |
16 | | |
17 | | #include "base/base_export.h" |
18 | | #include "base/callback_forward.h" |
19 | | #include "base/compiler_specific.h" |
20 | | #include "base/debug/debugger.h" |
21 | | #include "base/macros.h" |
22 | | #include "base/strings/string_piece_forward.h" |
23 | | #include "base/template_util.h" |
24 | | #include "build/build_config.h" |
25 | | |
26 | | // |
27 | | // Optional message capabilities |
28 | | // ----------------------------- |
29 | | // Assertion failed messages and fatal errors are displayed in a dialog box |
30 | | // before the application exits. However, running this UI creates a message |
31 | | // loop, which causes application messages to be processed and potentially |
32 | | // dispatched to existing application windows. Since the application is in a |
33 | | // bad state when this assertion dialog is displayed, these messages may not |
34 | | // get processed and hang the dialog, or the application might go crazy. |
35 | | // |
36 | | // Therefore, it can be beneficial to display the error dialog in a separate |
37 | | // process from the main application. When the logging system needs to display |
38 | | // a fatal error dialog box, it will look for a program called |
39 | | // "DebugMessage.exe" in the same directory as the application executable. It |
40 | | // will run this application with the message as the command line, and will |
41 | | // not include the name of the application as is traditional for easier |
42 | | // parsing. |
43 | | // |
44 | | // The code for DebugMessage.exe is only one line. In WinMain, do: |
45 | | // MessageBox(NULL, GetCommandLineW(), L"Fatal Error", 0); |
46 | | // |
47 | | // If DebugMessage.exe is not found, the logging code will use a normal |
48 | | // MessageBox, potentially causing the problems discussed above. |
49 | | |
50 | | |
51 | | // Instructions |
52 | | // ------------ |
53 | | // |
54 | | // Make a bunch of macros for logging. The way to log things is to stream |
55 | | // things to LOG(<a particular severity level>). E.g., |
56 | | // |
57 | | // LOG(INFO) << "Found " << num_cookies << " cookies"; |
58 | | // |
59 | | // You can also do conditional logging: |
60 | | // |
61 | | // LOG_IF(INFO, num_cookies > 10) << "Got lots of cookies"; |
62 | | // |
63 | | // The CHECK(condition) macro is active in both debug and release builds and |
64 | | // effectively performs a LOG(FATAL) which terminates the process and |
65 | | // generates a crashdump unless a debugger is attached. |
66 | | // |
67 | | // There are also "debug mode" logging macros like the ones above: |
68 | | // |
69 | | // DLOG(INFO) << "Found cookies"; |
70 | | // |
71 | | // DLOG_IF(INFO, num_cookies > 10) << "Got lots of cookies"; |
72 | | // |
73 | | // All "debug mode" logging is compiled away to nothing for non-debug mode |
74 | | // compiles. LOG_IF and development flags also work well together |
75 | | // because the code can be compiled away sometimes. |
76 | | // |
77 | | // We also have |
78 | | // |
79 | | // LOG_ASSERT(assertion); |
80 | | // DLOG_ASSERT(assertion); |
81 | | // |
82 | | // which is syntactic sugar for {,D}LOG_IF(FATAL, assert fails) << assertion; |
83 | | // |
84 | | // There are "verbose level" logging macros. They look like |
85 | | // |
86 | | // VLOG(1) << "I'm printed when you run the program with --v=1 or more"; |
87 | | // VLOG(2) << "I'm printed when you run the program with --v=2 or more"; |
88 | | // |
89 | | // These always log at the INFO log level (when they log at all). |
90 | | // The verbose logging can also be turned on module-by-module. For instance, |
91 | | // --vmodule=profile=2,icon_loader=1,browser_*=3,*/chromeos/*=4 --v=0 |
92 | | // will cause: |
93 | | // a. VLOG(2) and lower messages to be printed from profile.{h,cc} |
94 | | // b. VLOG(1) and lower messages to be printed from icon_loader.{h,cc} |
95 | | // c. VLOG(3) and lower messages to be printed from files prefixed with |
96 | | // "browser" |
97 | | // d. VLOG(4) and lower messages to be printed from files under a |
98 | | // "chromeos" directory. |
99 | | // e. VLOG(0) and lower messages to be printed from elsewhere |
100 | | // |
101 | | // The wildcarding functionality shown by (c) supports both '*' (match |
102 | | // 0 or more characters) and '?' (match any single character) |
103 | | // wildcards. Any pattern containing a forward or backward slash will |
104 | | // be tested against the whole pathname and not just the module. |
105 | | // E.g., "*/foo/bar/*=2" would change the logging level for all code |
106 | | // in source files under a "foo/bar" directory. |
107 | | // |
108 | | // There's also VLOG_IS_ON(n) "verbose level" condition macro. To be used as |
109 | | // |
110 | | // if (VLOG_IS_ON(2)) { |
111 | | // // do some logging preparation and logging |
112 | | // // that can't be accomplished with just VLOG(2) << ...; |
113 | | // } |
114 | | // |
115 | | // There is also a VLOG_IF "verbose level" condition macro for sample |
116 | | // cases, when some extra computation and preparation for logs is not |
117 | | // needed. |
118 | | // |
119 | | // VLOG_IF(1, (size > 1024)) |
120 | | // << "I'm printed when size is more than 1024 and when you run the " |
121 | | // "program with --v=1 or more"; |
122 | | // |
123 | | // We also override the standard 'assert' to use 'DLOG_ASSERT'. |
124 | | // |
125 | | // Lastly, there is: |
126 | | // |
127 | | // PLOG(ERROR) << "Couldn't do foo"; |
128 | | // DPLOG(ERROR) << "Couldn't do foo"; |
129 | | // PLOG_IF(ERROR, cond) << "Couldn't do foo"; |
130 | | // DPLOG_IF(ERROR, cond) << "Couldn't do foo"; |
131 | | // PCHECK(condition) << "Couldn't do foo"; |
132 | | // DPCHECK(condition) << "Couldn't do foo"; |
133 | | // |
134 | | // which append the last system error to the message in string form (taken from |
135 | | // GetLastError() on Windows and errno on POSIX). |
136 | | // |
137 | | // The supported severity levels for macros that allow you to specify one |
138 | | // are (in increasing order of severity) INFO, WARNING, ERROR, and FATAL. |
139 | | // |
140 | | // Very important: logging a message at the FATAL severity level causes |
141 | | // the program to terminate (after the message is logged). |
142 | | // |
143 | | // There is the special severity of DFATAL, which logs FATAL in debug mode, |
144 | | // ERROR in normal mode. |
145 | | |
146 | | namespace logging { |
147 | | |
148 | | // TODO(avi): do we want to do a unification of character types here? |
149 | | #if defined(OS_WIN) |
150 | | typedef wchar_t PathChar; |
151 | | #else |
152 | | typedef char PathChar; |
153 | | #endif |
154 | | |
155 | | // Where to record logging output? A flat file and/or system debug log |
156 | | // via OutputDebugString. |
157 | | enum LoggingDestination { |
158 | | LOG_NONE = 0, |
159 | | LOG_TO_FILE = 1 << 0, |
160 | | LOG_TO_SYSTEM_DEBUG_LOG = 1 << 1, |
161 | | |
162 | | LOG_TO_ALL = LOG_TO_FILE | LOG_TO_SYSTEM_DEBUG_LOG, |
163 | | |
164 | | // On Windows, use a file next to the exe; on POSIX platforms, where |
165 | | // it may not even be possible to locate the executable on disk, use |
166 | | // stderr. |
167 | | #if defined(OS_WIN) |
168 | | LOG_DEFAULT = LOG_TO_FILE, |
169 | | #elif defined(OS_POSIX) |
170 | | LOG_DEFAULT = LOG_TO_SYSTEM_DEBUG_LOG, |
171 | | #endif |
172 | | }; |
173 | | |
174 | | // Indicates that the log file should be locked when being written to. |
175 | | // Unless there is only one single-threaded process that is logging to |
176 | | // the log file, the file should be locked during writes to make each |
177 | | // log output atomic. Other writers will block. |
178 | | // |
179 | | // All processes writing to the log file must have their locking set for it to |
180 | | // work properly. Defaults to LOCK_LOG_FILE. |
181 | | enum LogLockingState { LOCK_LOG_FILE, DONT_LOCK_LOG_FILE }; |
182 | | |
183 | | // On startup, should we delete or append to an existing log file (if any)? |
184 | | // Defaults to APPEND_TO_OLD_LOG_FILE. |
185 | | enum OldFileDeletionState { DELETE_OLD_LOG_FILE, APPEND_TO_OLD_LOG_FILE }; |
186 | | |
187 | | struct BASE_EXPORT LoggingSettings { |
188 | | // The defaults values are: |
189 | | // |
190 | | // logging_dest: LOG_DEFAULT |
191 | | // log_file: NULL |
192 | | // lock_log: LOCK_LOG_FILE |
193 | | // delete_old: APPEND_TO_OLD_LOG_FILE |
194 | | LoggingSettings(); |
195 | | |
196 | | LoggingDestination logging_dest; |
197 | | |
198 | | // The three settings below have an effect only when LOG_TO_FILE is |
199 | | // set in |logging_dest|. |
200 | | const PathChar* log_file; |
201 | | LogLockingState lock_log; |
202 | | OldFileDeletionState delete_old; |
203 | | }; |
204 | | |
205 | | // Define different names for the BaseInitLoggingImpl() function depending on |
206 | | // whether NDEBUG is defined or not so that we'll fail to link if someone tries |
207 | | // to compile logging.cc with NDEBUG but includes logging.h without defining it, |
208 | | // or vice versa. |
209 | | #if defined(NDEBUG) |
210 | | #define BaseInitLoggingImpl BaseInitLoggingImpl_built_with_NDEBUG |
211 | | #else |
212 | | #define BaseInitLoggingImpl BaseInitLoggingImpl_built_without_NDEBUG |
213 | | #endif |
214 | | |
215 | | // Implementation of the InitLogging() method declared below. We use a |
216 | | // more-specific name so we can #define it above without affecting other code |
217 | | // that has named stuff "InitLogging". |
218 | | BASE_EXPORT bool BaseInitLoggingImpl(const LoggingSettings& settings); |
219 | | |
220 | | // Sets the log file name and other global logging state. Calling this function |
221 | | // is recommended, and is normally done at the beginning of application init. |
222 | | // If you don't call it, all the flags will be initialized to their default |
223 | | // values, and there is a race condition that may leak a critical section |
224 | | // object if two threads try to do the first log at the same time. |
225 | | // See the definition of the enums above for descriptions and default values. |
226 | | // |
227 | | // The default log file is initialized to "debug.log" in the application |
228 | | // directory. You probably don't want this, especially since the program |
229 | | // directory may not be writable on an enduser's system. |
230 | | // |
231 | | // This function may be called a second time to re-direct logging (e.g after |
232 | | // loging in to a user partition), however it should never be called more than |
233 | | // twice. |
234 | 0 | inline bool InitLogging(const LoggingSettings& settings) { |
235 | 0 | return BaseInitLoggingImpl(settings); |
236 | 0 | } |
237 | | |
238 | | // Sets the log level. Anything at or above this level will be written to the |
239 | | // log file/displayed to the user (if applicable). Anything below this level |
240 | | // will be silently ignored. The log level defaults to 0 (everything is logged |
241 | | // up to level INFO) if this function is not called. |
242 | | // Note that log messages for VLOG(x) are logged at level -x, so setting |
243 | | // the min log level to negative values enables verbose logging. |
244 | | BASE_EXPORT void SetMinLogLevel(int level); |
245 | | |
246 | | // Gets the current log level. |
247 | | BASE_EXPORT int GetMinLogLevel(); |
248 | | |
249 | | // Used by LOG_IS_ON to lazy-evaluate stream arguments. |
250 | | BASE_EXPORT bool ShouldCreateLogMessage(int severity); |
251 | | |
252 | | // Gets the VLOG default verbosity level. |
253 | | BASE_EXPORT int GetVlogVerbosity(); |
254 | | |
255 | | // Note that |N| is the size *with* the null terminator. |
256 | | BASE_EXPORT int GetVlogLevelHelper(const char* file_start, size_t N); |
257 | | |
258 | | // Gets the current vlog level for the given file (usually taken from __FILE__). |
259 | | template <size_t N> |
260 | | int GetVlogLevel(const char (&file)[N]) { |
261 | | return GetVlogLevelHelper(file, N); |
262 | | } |
263 | | |
264 | | // Sets the common items you want to be prepended to each log message. |
265 | | // process and thread IDs default to off, the timestamp defaults to on. |
266 | | // If this function is not called, logging defaults to writing the timestamp |
267 | | // only. |
268 | | BASE_EXPORT void SetLogItems(bool enable_process_id, bool enable_thread_id, |
269 | | bool enable_timestamp, bool enable_tickcount); |
270 | | |
271 | | // Sets whether or not you'd like to see fatal debug messages popped up in |
272 | | // a dialog box or not. |
273 | | // Dialogs are not shown by default. |
274 | | BASE_EXPORT void SetShowErrorDialogs(bool enable_dialogs); |
275 | | |
276 | | // Sets the Log Assert Handler that will be used to notify of check failures. |
277 | | // Resets Log Assert Handler on object destruction. |
278 | | // The default handler shows a dialog box and then terminate the process, |
279 | | // however clients can use this function to override with their own handling |
280 | | // (e.g. a silent one for Unit Tests) |
281 | | using LogAssertHandlerFunction = |
282 | | base::Callback<void(const char* file, |
283 | | int line, |
284 | | const base::StringPiece message, |
285 | | const base::StringPiece stack_trace)>; |
286 | | |
287 | | class BASE_EXPORT ScopedLogAssertHandler { |
288 | | public: |
289 | | explicit ScopedLogAssertHandler(LogAssertHandlerFunction handler); |
290 | | ~ScopedLogAssertHandler(); |
291 | | |
292 | | private: |
293 | | DISALLOW_COPY_AND_ASSIGN(ScopedLogAssertHandler); |
294 | | }; |
295 | | |
296 | | // Sets the Log Message Handler that gets passed every log message before |
297 | | // it's sent to other log destinations (if any). |
298 | | // Returns true to signal that it handled the message and the message |
299 | | // should not be sent to other log destinations. |
300 | | typedef bool (*LogMessageHandlerFunction)(int severity, |
301 | | const char* file, int line, size_t message_start, const std::string& str); |
302 | | BASE_EXPORT void SetLogMessageHandler(LogMessageHandlerFunction handler); |
303 | | BASE_EXPORT LogMessageHandlerFunction GetLogMessageHandler(); |
304 | | |
305 | | // The ANALYZER_ASSUME_TRUE(bool arg) macro adds compiler-specific hints |
306 | | // to Clang which control what code paths are statically analyzed, |
307 | | // and is meant to be used in conjunction with assert & assert-like functions. |
308 | | // The expression is passed straight through if analysis isn't enabled. |
309 | | // |
310 | | // ANALYZER_SKIP_THIS_PATH() suppresses static analysis for the current |
311 | | // codepath and any other branching codepaths that might follow. |
312 | | #if defined(__clang_analyzer__) |
313 | | |
314 | | inline constexpr bool AnalyzerNoReturn() __attribute__((analyzer_noreturn)) { |
315 | | return false; |
316 | | } |
317 | | |
318 | | inline constexpr bool AnalyzerAssumeTrue(bool arg) { |
319 | | // AnalyzerNoReturn() is invoked and analysis is terminated if |arg| is |
320 | | // false. |
321 | | return arg || AnalyzerNoReturn(); |
322 | | } |
323 | | |
324 | | #define ANALYZER_ASSUME_TRUE(arg) logging::AnalyzerAssumeTrue(!!(arg)) |
325 | | #define ANALYZER_SKIP_THIS_PATH() \ |
326 | | static_cast<void>(::logging::AnalyzerNoReturn()) |
327 | | #define ANALYZER_ALLOW_UNUSED(var) static_cast<void>(var); |
328 | | |
329 | | #else // !defined(__clang_analyzer__) |
330 | | |
331 | 0 | #define ANALYZER_ASSUME_TRUE(arg) (arg) |
332 | | #define ANALYZER_SKIP_THIS_PATH() |
333 | | #define ANALYZER_ALLOW_UNUSED(var) static_cast<void>(var); |
334 | | |
335 | | #endif // defined(__clang_analyzer__) |
336 | | |
337 | | typedef int LogSeverity; |
338 | | const LogSeverity LOG_VERBOSE = -1; // This is level 1 verbosity |
339 | | // Note: the log severities are used to index into the array of names, |
340 | | // see log_severity_names. |
341 | | const LogSeverity LOG_INFO = 0; |
342 | | const LogSeverity LOG_WARNING = 1; |
343 | | const LogSeverity LOG_ERROR = 2; |
344 | | const LogSeverity LOG_FATAL = 3; |
345 | | const LogSeverity LOG_NUM_SEVERITIES = 4; |
346 | | |
347 | | // LOG_DFATAL is LOG_FATAL in debug mode, ERROR in normal mode |
348 | | #if defined(NDEBUG) |
349 | | const LogSeverity LOG_DFATAL = LOG_ERROR; |
350 | | #else |
351 | | const LogSeverity LOG_DFATAL = LOG_FATAL; |
352 | | #endif |
353 | | |
354 | | // A few definitions of macros that don't generate much code. These are used |
355 | | // by LOG() and LOG_IF, etc. Since these are used all over our code, it's |
356 | | // better to have compact code for these operations. |
357 | | #define COMPACT_GOOGLE_LOG_EX_INFO(ClassName, ...) \ |
358 | | ::logging::ClassName(__FILE__, __LINE__, ::logging::LOG_INFO, ##__VA_ARGS__) |
359 | | #define COMPACT_GOOGLE_LOG_EX_WARNING(ClassName, ...) \ |
360 | | ::logging::ClassName(__FILE__, __LINE__, ::logging::LOG_WARNING, \ |
361 | | ##__VA_ARGS__) |
362 | | #define COMPACT_GOOGLE_LOG_EX_ERROR(ClassName, ...) \ |
363 | | ::logging::ClassName(__FILE__, __LINE__, ::logging::LOG_ERROR, ##__VA_ARGS__) |
364 | | #define COMPACT_GOOGLE_LOG_EX_FATAL(ClassName, ...) \ |
365 | | ::logging::ClassName(__FILE__, __LINE__, ::logging::LOG_FATAL, ##__VA_ARGS__) |
366 | | #define COMPACT_GOOGLE_LOG_EX_DFATAL(ClassName, ...) \ |
367 | | ::logging::ClassName(__FILE__, __LINE__, ::logging::LOG_DFATAL, ##__VA_ARGS__) |
368 | | #define COMPACT_GOOGLE_LOG_EX_DCHECK(ClassName, ...) \ |
369 | | ::logging::ClassName(__FILE__, __LINE__, ::logging::LOG_DCHECK, ##__VA_ARGS__) |
370 | | |
371 | | #define COMPACT_GOOGLE_LOG_INFO COMPACT_GOOGLE_LOG_EX_INFO(LogMessage) |
372 | | #define COMPACT_GOOGLE_LOG_WARNING COMPACT_GOOGLE_LOG_EX_WARNING(LogMessage) |
373 | | #define COMPACT_GOOGLE_LOG_ERROR COMPACT_GOOGLE_LOG_EX_ERROR(LogMessage) |
374 | | #define COMPACT_GOOGLE_LOG_FATAL COMPACT_GOOGLE_LOG_EX_FATAL(LogMessage) |
375 | | #define COMPACT_GOOGLE_LOG_DFATAL COMPACT_GOOGLE_LOG_EX_DFATAL(LogMessage) |
376 | | #define COMPACT_GOOGLE_LOG_DCHECK COMPACT_GOOGLE_LOG_EX_DCHECK(LogMessage) |
377 | | |
378 | | #if defined(OS_WIN) |
379 | | // wingdi.h defines ERROR to be 0. When we call LOG(ERROR), it gets |
380 | | // substituted with 0, and it expands to COMPACT_GOOGLE_LOG_0. To allow us |
381 | | // to keep using this syntax, we define this macro to do the same thing |
382 | | // as COMPACT_GOOGLE_LOG_ERROR, and also define ERROR the same way that |
383 | | // the Windows SDK does for consistency. |
384 | | #define ERROR 0 |
385 | | #define COMPACT_GOOGLE_LOG_EX_0(ClassName, ...) \ |
386 | | COMPACT_GOOGLE_LOG_EX_ERROR(ClassName , ##__VA_ARGS__) |
387 | | #define COMPACT_GOOGLE_LOG_0 COMPACT_GOOGLE_LOG_ERROR |
388 | | // Needed for LOG_IS_ON(ERROR). |
389 | | const LogSeverity LOG_0 = LOG_ERROR; |
390 | | #endif |
391 | | |
392 | | // As special cases, we can assume that LOG_IS_ON(FATAL) always holds. Also, |
393 | | // LOG_IS_ON(DFATAL) always holds in debug mode. In particular, CHECK()s will |
394 | | // always fire if they fail. |
395 | | #define LOG_IS_ON(severity) \ |
396 | | (::logging::ShouldCreateLogMessage(::logging::LOG_##severity)) |
397 | | |
398 | | // We can't do any caching tricks with VLOG_IS_ON() like the |
399 | | // google-glog version since it requires GCC extensions. This means |
400 | | // that using the v-logging functions in conjunction with --vmodule |
401 | | // may be slow. |
402 | | #define VLOG_IS_ON(verboselevel) \ |
403 | | ((verboselevel) <= ::logging::GetVlogLevel(__FILE__)) |
404 | | |
405 | | // Helper macro which avoids evaluating the arguments to a stream if |
406 | | // the condition doesn't hold. Condition is evaluated once and only once. |
407 | | #define LAZY_STREAM(stream, condition) \ |
408 | 0 | !(condition) ? (void) 0 : ::logging::LogMessageVoidify() & (stream) |
409 | | |
410 | | // We use the preprocessor's merging operator, "##", so that, e.g., |
411 | | // LOG(INFO) becomes the token COMPACT_GOOGLE_LOG_INFO. There's some funny |
412 | | // subtle difference between ostream member streaming functions (e.g., |
413 | | // ostream::operator<<(int) and ostream non-member streaming functions |
414 | | // (e.g., ::operator<<(ostream&, string&): it turns out that it's |
415 | | // impossible to stream something like a string directly to an unnamed |
416 | | // ostream. We employ a neat hack by calling the stream() member |
417 | | // function of LogMessage which seems to avoid the problem. |
418 | | #define LOG_STREAM(severity) COMPACT_GOOGLE_LOG_ ## severity.stream() |
419 | | |
420 | 0 | #define LOG(severity) LAZY_STREAM(LOG_STREAM(severity), LOG_IS_ON(severity)) |
421 | | #define LOG_IF(severity, condition) \ |
422 | | LAZY_STREAM(LOG_STREAM(severity), LOG_IS_ON(severity) && (condition)) |
423 | | |
424 | | // The VLOG macros log with negative verbosities. |
425 | | #define VLOG_STREAM(verbose_level) \ |
426 | | ::logging::LogMessage(__FILE__, __LINE__, -verbose_level).stream() |
427 | | |
428 | | #define VLOG(verbose_level) \ |
429 | | LAZY_STREAM(VLOG_STREAM(verbose_level), VLOG_IS_ON(verbose_level)) |
430 | | |
431 | | #define VLOG_IF(verbose_level, condition) \ |
432 | | LAZY_STREAM(VLOG_STREAM(verbose_level), \ |
433 | | VLOG_IS_ON(verbose_level) && (condition)) |
434 | | |
435 | | #if defined (OS_WIN) |
436 | | #define VPLOG_STREAM(verbose_level) \ |
437 | | ::logging::Win32ErrorLogMessage(__FILE__, __LINE__, -verbose_level, \ |
438 | | ::logging::GetLastSystemErrorCode()).stream() |
439 | | #elif defined(OS_POSIX) |
440 | | #define VPLOG_STREAM(verbose_level) \ |
441 | | ::logging::ErrnoLogMessage(__FILE__, __LINE__, -verbose_level, \ |
442 | | ::logging::GetLastSystemErrorCode()).stream() |
443 | | #endif |
444 | | |
445 | | #define VPLOG(verbose_level) \ |
446 | | LAZY_STREAM(VPLOG_STREAM(verbose_level), VLOG_IS_ON(verbose_level)) |
447 | | |
448 | | #define VPLOG_IF(verbose_level, condition) \ |
449 | | LAZY_STREAM(VPLOG_STREAM(verbose_level), \ |
450 | | VLOG_IS_ON(verbose_level) && (condition)) |
451 | | |
452 | | // TODO(akalin): Add more VLOG variants, e.g. VPLOG. |
453 | | |
454 | | #define LOG_ASSERT(condition) \ |
455 | | LOG_IF(FATAL, !(ANALYZER_ASSUME_TRUE(condition))) \ |
456 | | << "Assert failed: " #condition ". " |
457 | | |
458 | | #if defined(OS_WIN) |
459 | | #define PLOG_STREAM(severity) \ |
460 | | COMPACT_GOOGLE_LOG_EX_ ## severity(Win32ErrorLogMessage, \ |
461 | | ::logging::GetLastSystemErrorCode()).stream() |
462 | | #elif defined(OS_POSIX) |
463 | | #define PLOG_STREAM(severity) \ |
464 | | COMPACT_GOOGLE_LOG_EX_ ## severity(ErrnoLogMessage, \ |
465 | | ::logging::GetLastSystemErrorCode()).stream() |
466 | | #endif |
467 | | |
468 | | #define PLOG(severity) \ |
469 | 0 | LAZY_STREAM(PLOG_STREAM(severity), LOG_IS_ON(severity)) |
470 | | |
471 | | #define PLOG_IF(severity, condition) \ |
472 | | LAZY_STREAM(PLOG_STREAM(severity), LOG_IS_ON(severity) && (condition)) |
473 | | |
474 | | BASE_EXPORT extern std::ostream* g_swallow_stream; |
475 | | |
476 | | // Note that g_swallow_stream is used instead of an arbitrary LOG() stream to |
477 | | // avoid the creation of an object with a non-trivial destructor (LogMessage). |
478 | | // On MSVC x86 (checked on 2015 Update 3), this causes a few additional |
479 | | // pointless instructions to be emitted even at full optimization level, even |
480 | | // though the : arm of the ternary operator is clearly never executed. Using a |
481 | | // simpler object to be &'d with Voidify() avoids these extra instructions. |
482 | | // Using a simpler POD object with a templated operator<< also works to avoid |
483 | | // these instructions. However, this causes warnings on statically defined |
484 | | // implementations of operator<<(std::ostream, ...) in some .cc files, because |
485 | | // they become defined-but-unreferenced functions. A reinterpret_cast of 0 to an |
486 | | // ostream* also is not suitable, because some compilers warn of undefined |
487 | | // behavior. |
488 | | #define EAT_STREAM_PARAMETERS \ |
489 | 0 | true ? (void)0 \ |
490 | 0 | : ::logging::LogMessageVoidify() & (*::logging::g_swallow_stream) |
491 | | |
492 | | // Captures the result of a CHECK_EQ (for example) and facilitates testing as a |
493 | | // boolean. |
494 | | class CheckOpResult { |
495 | | public: |
496 | | // |message| must be non-null if and only if the check failed. |
497 | 0 | CheckOpResult(std::string* message) : message_(message) {} |
498 | | // Returns true if the check succeeded. |
499 | 0 | operator bool() const { return !message_; } |
500 | | // Returns the message. |
501 | 0 | std::string* message() { return message_; } |
502 | | |
503 | | private: |
504 | | std::string* message_; |
505 | | }; |
506 | | |
507 | | // Crashes in the fastest possible way with no attempt at logging. |
508 | | // There are different constraints to satisfy here, see http://crbug.com/664209 |
509 | | // for more context: |
510 | | // - The trap instructions, and hence the PC value at crash time, have to be |
511 | | // distinct and not get folded into the same opcode by the compiler. |
512 | | // On Linux/Android this is tricky because GCC still folds identical |
513 | | // asm volatile blocks. The workaround is generating distinct opcodes for |
514 | | // each CHECK using the __COUNTER__ macro. |
515 | | // - The debug info for the trap instruction has to be attributed to the source |
516 | | // line that has the CHECK(), to make crash reports actionable. This rules |
517 | | // out the ability of using a inline function, at least as long as clang |
518 | | // doesn't support attribute(artificial). |
519 | | // - Failed CHECKs should produce a signal that is distinguishable from an |
520 | | // invalid memory access, to improve the actionability of crash reports. |
521 | | // - The compiler should treat the CHECK as no-return instructions, so that the |
522 | | // trap code can be efficiently packed in the prologue of the function and |
523 | | // doesn't interfere with the main execution flow. |
524 | | // - When debugging, developers shouldn't be able to accidentally step over a |
525 | | // CHECK. This is achieved by putting opcodes that will cause a non |
526 | | // continuable exception after the actual trap instruction. |
527 | | // - Don't cause too much binary bloat. |
528 | | #if defined(COMPILER_GCC) |
529 | | |
530 | | #if defined(ARCH_CPU_X86_FAMILY) && !defined(OS_NACL) |
531 | | // int 3 will generate a SIGTRAP. |
532 | | #define TRAP_SEQUENCE() \ |
533 | | asm volatile( \ |
534 | | "int3; ud2; push %0;" ::"i"(static_cast<unsigned char>(__COUNTER__))) |
535 | | |
536 | | #elif defined(ARCH_CPU_ARMEL) && !defined(OS_NACL) |
537 | | // bkpt will generate a SIGBUS when running on armv7 and a SIGTRAP when running |
538 | | // as a 32 bit userspace app on arm64. There doesn't seem to be any way to |
539 | | // cause a SIGTRAP from userspace without using a syscall (which would be a |
540 | | // problem for sandboxing). |
541 | | #define TRAP_SEQUENCE() \ |
542 | | asm volatile("bkpt #0; udf %0;" ::"i"(__COUNTER__ % 256)) |
543 | | |
544 | | #elif defined(ARCH_CPU_ARM64) && !defined(OS_NACL) |
545 | | // This will always generate a SIGTRAP on arm64. |
546 | | #define TRAP_SEQUENCE() \ |
547 | | asm volatile("brk #0; hlt %0;" ::"i"(__COUNTER__ % 65536)) |
548 | | |
549 | | #else |
550 | | // Crash report accuracy will not be guaranteed on other architectures, but at |
551 | | // least this will crash as expected. |
552 | | #define TRAP_SEQUENCE() __builtin_trap() |
553 | | #endif // ARCH_CPU_* |
554 | | |
555 | | #define IMMEDIATE_CRASH() \ |
556 | | ({ \ |
557 | | TRAP_SEQUENCE(); \ |
558 | | __builtin_unreachable(); \ |
559 | | }) |
560 | | |
561 | | #elif defined(COMPILER_MSVC) |
562 | | |
563 | | // Clang is cleverer about coalescing int3s, so we need to add a unique-ish |
564 | | // instruction following the __debugbreak() to have it emit distinct locations |
565 | | // for CHECKs rather than collapsing them all together. It would be nice to use |
566 | | // a short intrinsic to do this (and perhaps have only one implementation for |
567 | | // both clang and MSVC), however clang-cl currently does not support intrinsics. |
568 | | // On the flip side, MSVC x64 doesn't support inline asm. So, we have to have |
569 | | // two implementations. Normally clang-cl's version will be 5 bytes (1 for |
570 | | // `int3`, 2 for `ud2`, 2 for `push byte imm`, however, TODO(scottmg): |
571 | | // https://crbug.com/694670 clang-cl doesn't currently support %'ing |
572 | | // __COUNTER__, so eventually it will emit the dword form of push. |
573 | | // TODO(scottmg): Reinvestigate a short sequence that will work on both |
574 | | // compilers once clang supports more intrinsics. See https://crbug.com/693713. |
575 | | #if defined(__clang__) |
576 | | #define IMMEDIATE_CRASH() ({__asm int 3 __asm ud2 __asm push __COUNTER__}) |
577 | | #else |
578 | | #define IMMEDIATE_CRASH() __debugbreak() |
579 | | #endif // __clang__ |
580 | | |
581 | | #else |
582 | | #error Port |
583 | | #endif |
584 | | |
585 | | // CHECK dies with a fatal error if condition is not true. It is *not* |
586 | | // controlled by NDEBUG, so the check will be executed regardless of |
587 | | // compilation mode. |
588 | | // |
589 | | // We make sure CHECK et al. always evaluates their arguments, as |
590 | | // doing CHECK(FunctionWithSideEffect()) is a common idiom. |
591 | | |
592 | | #if defined(OFFICIAL_BUILD) && defined(NDEBUG) |
593 | | |
594 | | // Make all CHECK functions discard their log strings to reduce code bloat, and |
595 | | // improve performance, for official release builds. |
596 | | // |
597 | | // This is not calling BreakDebugger since this is called frequently, and |
598 | | // calling an out-of-line function instead of a noreturn inline macro prevents |
599 | | // compiler optimizations. |
600 | | #define CHECK(condition) \ |
601 | | UNLIKELY(!(condition)) ? IMMEDIATE_CRASH() : EAT_STREAM_PARAMETERS |
602 | | |
603 | | // PCHECK includes the system error code, which is useful for determining |
604 | | // why the condition failed. In official builds, preserve only the error code |
605 | | // message so that it is available in crash reports. The stringified |
606 | | // condition and any additional stream parameters are dropped. |
607 | | #define PCHECK(condition) \ |
608 | | LAZY_STREAM(PLOG_STREAM(FATAL), UNLIKELY(!(condition))); \ |
609 | | EAT_STREAM_PARAMETERS |
610 | | |
611 | | #define CHECK_OP(name, op, val1, val2) CHECK((val1) op (val2)) |
612 | | |
613 | | #else // !(OFFICIAL_BUILD && NDEBUG) |
614 | | |
615 | | #if defined(_PREFAST_) && defined(OS_WIN) |
616 | | // Use __analysis_assume to tell the VC++ static analysis engine that |
617 | | // assert conditions are true, to suppress warnings. The LAZY_STREAM |
618 | | // parameter doesn't reference 'condition' in /analyze builds because |
619 | | // this evaluation confuses /analyze. The !! before condition is because |
620 | | // __analysis_assume gets confused on some conditions: |
621 | | // http://randomascii.wordpress.com/2011/09/13/analyze-for-visual-studio-the-ugly-part-5/ |
622 | | |
623 | | #define CHECK(condition) \ |
624 | | __analysis_assume(!!(condition)), \ |
625 | | LAZY_STREAM(LOG_STREAM(FATAL), false) \ |
626 | | << "Check failed: " #condition ". " |
627 | | |
628 | | #define PCHECK(condition) \ |
629 | | __analysis_assume(!!(condition)), \ |
630 | | LAZY_STREAM(PLOG_STREAM(FATAL), false) \ |
631 | | << "Check failed: " #condition ". " |
632 | | |
633 | | #else // _PREFAST_ |
634 | | |
635 | | // Do as much work as possible out of line to reduce inline code size. |
636 | | #define CHECK(condition) \ |
637 | 0 | LAZY_STREAM(::logging::LogMessage(__FILE__, __LINE__, #condition).stream(), \ |
638 | 0 | !ANALYZER_ASSUME_TRUE(condition)) |
639 | | |
640 | | #define PCHECK(condition) \ |
641 | | LAZY_STREAM(PLOG_STREAM(FATAL), !ANALYZER_ASSUME_TRUE(condition)) \ |
642 | | << "Check failed: " #condition ". " |
643 | | |
644 | | #endif // _PREFAST_ |
645 | | |
646 | | // Helper macro for binary operators. |
647 | | // Don't use this macro directly in your code, use CHECK_EQ et al below. |
648 | | // The 'switch' is used to prevent the 'else' from being ambiguous when the |
649 | | // macro is used in an 'if' clause such as: |
650 | | // if (a == 1) |
651 | | // CHECK_EQ(2, a); |
652 | | #define CHECK_OP(name, op, val1, val2) \ |
653 | 0 | switch (0) case 0: default: \ |
654 | 0 | if (::logging::CheckOpResult true_if_passed = \ |
655 | 0 | ::logging::Check##name##Impl((val1), (val2), \ |
656 | 0 | #val1 " " #op " " #val2)) \ |
657 | 0 | ; \ |
658 | 0 | else \ |
659 | 0 | ::logging::LogMessage(__FILE__, __LINE__, true_if_passed.message()).stream() |
660 | | |
661 | | #endif // !(OFFICIAL_BUILD && NDEBUG) |
662 | | |
663 | | // This formats a value for a failing CHECK_XX statement. Ordinarily, |
664 | | // it uses the definition for operator<<, with a few special cases below. |
665 | | template <typename T> |
666 | | inline typename std::enable_if< |
667 | | base::internal::SupportsOstreamOperator<const T&>::value && |
668 | | !std::is_function<typename std::remove_pointer<T>::type>::value, |
669 | | void>::type |
670 | 0 | MakeCheckOpValueString(std::ostream* os, const T& v) { |
671 | 0 | (*os) << v; |
672 | 0 | } Unexecuted instantiation: _ZN7logging22MakeCheckOpValueStringIiEENSt3__19enable_ifIXaasr4base8internal23SupportsOstreamOperatorIRKT_EE5valuentsr3std11is_functionINS1_14remove_pointerIS3_E4typeEEE5valueEvE4typeEPNS1_13basic_ostreamIcNS1_11char_traitsIcEEEES5_ Unexecuted instantiation: _ZN7logging22MakeCheckOpValueStringImEENSt3__19enable_ifIXaasr4base8internal23SupportsOstreamOperatorIRKT_EE5valuentsr3std11is_functionINS1_14remove_pointerIS3_E4typeEEE5valueEvE4typeEPNS1_13basic_ostreamIcNS1_11char_traitsIcEEEES5_ Unexecuted instantiation: _ZN7logging22MakeCheckOpValueStringIjEENSt3__19enable_ifIXaasr4base8internal23SupportsOstreamOperatorIRKT_EE5valuentsr3std11is_functionINS1_14remove_pointerIS3_E4typeEEE5valueEvE4typeEPNS1_13basic_ostreamIcNS1_11char_traitsIcEEEES5_ Unexecuted instantiation: _ZN7logging22MakeCheckOpValueStringINSt3__112basic_stringIcNS1_11char_traitsIcEENS1_9allocatorIcEEEEEENS1_9enable_ifIXaasr4base8internal23SupportsOstreamOperatorIRKT_EE5valuentsr3std11is_functionINS1_14remove_pointerIS9_E4typeEEE5valueEvE4typeEPNS1_13basic_ostreamIcS4_EESB_ Unexecuted instantiation: _ZN7logging22MakeCheckOpValueStringIPN4base13AtExitManagerEEENSt3__19enable_ifIXaasr4base8internal23SupportsOstreamOperatorIRKT_EE5valuentsr3std11is_functionINS4_14remove_pointerIS6_E4typeEEE5valueEvE4typeEPNS4_13basic_ostreamIcNS4_11char_traitsIcEEEES8_ Unexecuted instantiation: _ZN7logging22MakeCheckOpValueStringIlEENSt3__19enable_ifIXaasr4base8internal23SupportsOstreamOperatorIRKT_EE5valuentsr3std11is_functionINS1_14remove_pointerIS3_E4typeEEE5valueEvE4typeEPNS1_13basic_ostreamIcNS1_11char_traitsIcEEEES5_ |
673 | | |
674 | | // Provide an overload for functions and function pointers. Function pointers |
675 | | // don't implicitly convert to void* but do implicitly convert to bool, so |
676 | | // without this function pointers are always printed as 1 or 0. (MSVC isn't |
677 | | // standards-conforming here and converts function pointers to regular |
678 | | // pointers, so this is a no-op for MSVC.) |
679 | | template <typename T> |
680 | | inline typename std::enable_if< |
681 | | std::is_function<typename std::remove_pointer<T>::type>::value, |
682 | | void>::type |
683 | | MakeCheckOpValueString(std::ostream* os, const T& v) { |
684 | | (*os) << reinterpret_cast<const void*>(v); |
685 | | } |
686 | | |
687 | | // We need overloads for enums that don't support operator<<. |
688 | | // (i.e. scoped enums where no operator<< overload was declared). |
689 | | template <typename T> |
690 | | inline typename std::enable_if< |
691 | | !base::internal::SupportsOstreamOperator<const T&>::value && |
692 | | std::is_enum<T>::value, |
693 | | void>::type |
694 | | MakeCheckOpValueString(std::ostream* os, const T& v) { |
695 | | (*os) << static_cast<typename std::underlying_type<T>::type>(v); |
696 | | } |
697 | | |
698 | | // We need an explicit overload for std::nullptr_t. |
699 | | BASE_EXPORT void MakeCheckOpValueString(std::ostream* os, std::nullptr_t p); |
700 | | |
701 | | // Build the error message string. This is separate from the "Impl" |
702 | | // function template because it is not performance critical and so can |
703 | | // be out of line, while the "Impl" code should be inline. Caller |
704 | | // takes ownership of the returned string. |
705 | | template<class t1, class t2> |
706 | 0 | std::string* MakeCheckOpString(const t1& v1, const t2& v2, const char* names) { |
707 | 0 | std::ostringstream ss; |
708 | 0 | ss << names << " ("; |
709 | 0 | MakeCheckOpValueString(&ss, v1); |
710 | 0 | ss << " vs. "; |
711 | 0 | MakeCheckOpValueString(&ss, v2); |
712 | 0 | ss << ")"; |
713 | 0 | std::string* msg = new std::string(ss.str()); |
714 | 0 | return msg; |
715 | 0 | } Unexecuted instantiation: std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >* logging::MakeCheckOpString<int, int>(int const&, int const&, char const*) Unexecuted instantiation: std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >* logging::MakeCheckOpString<unsigned long, unsigned long>(unsigned long const&, unsigned long const&, char const*) Unexecuted instantiation: std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >* logging::MakeCheckOpString<unsigned long, unsigned int>(unsigned long const&, unsigned int const&, char const*) Unexecuted instantiation: std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >* logging::MakeCheckOpString<unsigned int, unsigned long>(unsigned int const&, unsigned long const&, char const*) Unexecuted instantiation: std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >* logging::MakeCheckOpString<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, char const*) Unexecuted instantiation: std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >* logging::MakeCheckOpString<unsigned int, unsigned int>(unsigned int const&, unsigned int const&, char const*) |
716 | | |
717 | | // Commonly used instantiations of MakeCheckOpString<>. Explicitly instantiated |
718 | | // in logging.cc. |
719 | | extern template BASE_EXPORT std::string* MakeCheckOpString<int, int>( |
720 | | const int&, const int&, const char* names); |
721 | | extern template BASE_EXPORT |
722 | | std::string* MakeCheckOpString<unsigned long, unsigned long>( |
723 | | const unsigned long&, const unsigned long&, const char* names); |
724 | | extern template BASE_EXPORT |
725 | | std::string* MakeCheckOpString<unsigned long, unsigned int>( |
726 | | const unsigned long&, const unsigned int&, const char* names); |
727 | | extern template BASE_EXPORT |
728 | | std::string* MakeCheckOpString<unsigned int, unsigned long>( |
729 | | const unsigned int&, const unsigned long&, const char* names); |
730 | | extern template BASE_EXPORT |
731 | | std::string* MakeCheckOpString<std::string, std::string>( |
732 | | const std::string&, const std::string&, const char* name); |
733 | | |
734 | | // Helper functions for CHECK_OP macro. |
735 | | // The (int, int) specialization works around the issue that the compiler |
736 | | // will not instantiate the template version of the function on values of |
737 | | // unnamed enum type - see comment below. |
738 | | // |
739 | | // The checked condition is wrapped with ANALYZER_ASSUME_TRUE, which under |
740 | | // static analysis builds, blocks analysis of the current path if the |
741 | | // condition is false. |
742 | | #define DEFINE_CHECK_OP_IMPL(name, op) \ |
743 | | template <class t1, class t2> \ |
744 | | inline std::string* Check##name##Impl(const t1& v1, const t2& v2, \ |
745 | 0 | const char* names) { \ |
746 | 0 | if (ANALYZER_ASSUME_TRUE(v1 op v2)) \ |
747 | 0 | return NULL; \ |
748 | 0 | else \ |
749 | 0 | return ::logging::MakeCheckOpString(v1, v2, names); \ |
750 | 0 | } \ Unexecuted instantiation: std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >* logging::CheckEQImpl<unsigned long, unsigned long>(unsigned long const&, unsigned long const&, char const*) Unexecuted instantiation: std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >* logging::CheckEQImpl<unsigned int, unsigned long>(unsigned int const&, unsigned long const&, char const*) Unexecuted instantiation: std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >* logging::CheckLEImpl<unsigned long, unsigned long>(unsigned long const&, unsigned long const&, char const*) Unexecuted instantiation: std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >* logging::CheckLTImpl<unsigned long, unsigned long>(unsigned long const&, unsigned long const&, char const*) Unexecuted instantiation: std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >* logging::CheckNEImpl<unsigned int, unsigned long>(unsigned int const&, unsigned long const&, char const*) Unexecuted instantiation: std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >* logging::CheckEQImpl<unsigned int, unsigned int>(unsigned int const&, unsigned int const&, char const*) |
751 | 0 | inline std::string* Check##name##Impl(int v1, int v2, const char* names) { \ |
752 | 0 | if (ANALYZER_ASSUME_TRUE(v1 op v2)) \ |
753 | 0 | return NULL; \ |
754 | 0 | else \ |
755 | 0 | return ::logging::MakeCheckOpString(v1, v2, names); \ |
756 | 0 | } Unexecuted instantiation: logging::CheckEQImpl(int, int, char const*) Unexecuted instantiation: logging::CheckNEImpl(int, int, char const*) Unexecuted instantiation: logging::CheckLEImpl(int, int, char const*) Unexecuted instantiation: logging::CheckLTImpl(int, int, char const*) Unexecuted instantiation: logging::CheckGEImpl(int, int, char const*) Unexecuted instantiation: logging::CheckGTImpl(int, int, char const*) |
757 | | DEFINE_CHECK_OP_IMPL(EQ, ==) |
758 | | DEFINE_CHECK_OP_IMPL(NE, !=) |
759 | | DEFINE_CHECK_OP_IMPL(LE, <=) |
760 | | DEFINE_CHECK_OP_IMPL(LT, < ) |
761 | | DEFINE_CHECK_OP_IMPL(GE, >=) |
762 | | DEFINE_CHECK_OP_IMPL(GT, > ) |
763 | | #undef DEFINE_CHECK_OP_IMPL |
764 | | |
765 | 0 | #define CHECK_EQ(val1, val2) CHECK_OP(EQ, ==, val1, val2) |
766 | 0 | #define CHECK_NE(val1, val2) CHECK_OP(NE, !=, val1, val2) |
767 | 0 | #define CHECK_LE(val1, val2) CHECK_OP(LE, <=, val1, val2) |
768 | 0 | #define CHECK_LT(val1, val2) CHECK_OP(LT, < , val1, val2) |
769 | | #define CHECK_GE(val1, val2) CHECK_OP(GE, >=, val1, val2) |
770 | | #define CHECK_GT(val1, val2) CHECK_OP(GT, > , val1, val2) |
771 | | |
772 | | #if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON) |
773 | | #define DCHECK_IS_ON() 0 |
774 | | #else |
775 | | #define DCHECK_IS_ON() 1 |
776 | | #endif |
777 | | |
778 | | // Definitions for DLOG et al. |
779 | | |
780 | | #if DCHECK_IS_ON() |
781 | | |
782 | | #define DLOG_IS_ON(severity) LOG_IS_ON(severity) |
783 | | #define DLOG_IF(severity, condition) LOG_IF(severity, condition) |
784 | | #define DLOG_ASSERT(condition) LOG_ASSERT(condition) |
785 | | #define DPLOG_IF(severity, condition) PLOG_IF(severity, condition) |
786 | | #define DVLOG_IF(verboselevel, condition) VLOG_IF(verboselevel, condition) |
787 | | #define DVPLOG_IF(verboselevel, condition) VPLOG_IF(verboselevel, condition) |
788 | | |
789 | | #else // DCHECK_IS_ON() |
790 | | |
791 | | // If !DCHECK_IS_ON(), we want to avoid emitting any references to |condition| |
792 | | // (which may reference a variable defined only if DCHECK_IS_ON()). |
793 | | // Contrast this with DCHECK et al., which has different behavior. |
794 | | |
795 | | #define DLOG_IS_ON(severity) false |
796 | | #define DLOG_IF(severity, condition) EAT_STREAM_PARAMETERS |
797 | | #define DLOG_ASSERT(condition) EAT_STREAM_PARAMETERS |
798 | | #define DPLOG_IF(severity, condition) EAT_STREAM_PARAMETERS |
799 | | #define DVLOG_IF(verboselevel, condition) EAT_STREAM_PARAMETERS |
800 | 0 | #define DVPLOG_IF(verboselevel, condition) EAT_STREAM_PARAMETERS |
801 | | |
802 | | #endif // DCHECK_IS_ON() |
803 | | |
804 | | #define DLOG(severity) \ |
805 | 0 | LAZY_STREAM(LOG_STREAM(severity), DLOG_IS_ON(severity)) |
806 | | |
807 | | #define DPLOG(severity) \ |
808 | | LAZY_STREAM(PLOG_STREAM(severity), DLOG_IS_ON(severity)) |
809 | | |
810 | | #define DVLOG(verboselevel) DVLOG_IF(verboselevel, VLOG_IS_ON(verboselevel)) |
811 | | |
812 | 0 | #define DVPLOG(verboselevel) DVPLOG_IF(verboselevel, VLOG_IS_ON(verboselevel)) |
813 | | |
814 | | // Definitions for DCHECK et al. |
815 | | |
816 | | #if DCHECK_IS_ON() |
817 | | |
818 | | #if defined(SYZYASAN) |
819 | | BASE_EXPORT extern LogSeverity LOG_DCHECK; |
820 | | #else |
821 | | const LogSeverity LOG_DCHECK = LOG_FATAL; |
822 | | #endif |
823 | | |
824 | | #else // DCHECK_IS_ON() |
825 | | |
826 | | // This is a dummy value, since the DCHECK implementation is a no-op. |
827 | | const LogSeverity LOG_DCHECK = LOG_INFO; |
828 | | |
829 | | #endif // DCHECK_IS_ON() |
830 | | |
831 | | // DCHECK et al. make sure to reference |condition| regardless of |
832 | | // whether DCHECKs are enabled; this is so that we don't get unused |
833 | | // variable warnings if the only use of a variable is in a DCHECK. |
834 | | // This behavior is different from DLOG_IF et al. |
835 | | // |
836 | | // Note that the definition of the DCHECK macros depends on whether or not |
837 | | // DCHECK_IS_ON() is true. When DCHECK_IS_ON() is false, the macros use |
838 | | // EAT_STREAM_PARAMETERS to avoid expressions that would create temporaries. |
839 | | |
840 | | #if defined(_PREFAST_) && defined(OS_WIN) |
841 | | // See comments on the previous use of __analysis_assume. |
842 | | |
843 | | #define DCHECK(condition) \ |
844 | | __analysis_assume(!!(condition)), \ |
845 | | LAZY_STREAM(LOG_STREAM(DCHECK), false) \ |
846 | | << "Check failed: " #condition ". " |
847 | | |
848 | | #define DPCHECK(condition) \ |
849 | | __analysis_assume(!!(condition)), \ |
850 | | LAZY_STREAM(PLOG_STREAM(DCHECK), false) \ |
851 | | << "Check failed: " #condition ". " |
852 | | |
853 | | #else // !(defined(_PREFAST_) && defined(OS_WIN)) |
854 | | |
855 | | #if DCHECK_IS_ON() |
856 | | |
857 | | #define DCHECK(condition) \ |
858 | | LAZY_STREAM(LOG_STREAM(DCHECK), !ANALYZER_ASSUME_TRUE(condition)) \ |
859 | | << "Check failed: " #condition ". " |
860 | | #define DPCHECK(condition) \ |
861 | | LAZY_STREAM(PLOG_STREAM(DCHECK), !ANALYZER_ASSUME_TRUE(condition)) \ |
862 | | << "Check failed: " #condition ". " |
863 | | |
864 | | #else // DCHECK_IS_ON() |
865 | | |
866 | 0 | #define DCHECK(condition) EAT_STREAM_PARAMETERS << !(condition) |
867 | | #define DPCHECK(condition) EAT_STREAM_PARAMETERS << !(condition) |
868 | | |
869 | | #endif // DCHECK_IS_ON() |
870 | | |
871 | | #endif // defined(_PREFAST_) && defined(OS_WIN) |
872 | | |
873 | | // Helper macro for binary operators. |
874 | | // Don't use this macro directly in your code, use DCHECK_EQ et al below. |
875 | | // The 'switch' is used to prevent the 'else' from being ambiguous when the |
876 | | // macro is used in an 'if' clause such as: |
877 | | // if (a == 1) |
878 | | // DCHECK_EQ(2, a); |
879 | | #if DCHECK_IS_ON() |
880 | | |
881 | | #define DCHECK_OP(name, op, val1, val2) \ |
882 | | switch (0) case 0: default: \ |
883 | | if (::logging::CheckOpResult true_if_passed = \ |
884 | | DCHECK_IS_ON() ? \ |
885 | | ::logging::Check##name##Impl((val1), (val2), \ |
886 | | #val1 " " #op " " #val2) : nullptr) \ |
887 | | ; \ |
888 | | else \ |
889 | | ::logging::LogMessage(__FILE__, __LINE__, ::logging::LOG_DCHECK, \ |
890 | | true_if_passed.message()).stream() |
891 | | |
892 | | #else // DCHECK_IS_ON() |
893 | | |
894 | | // When DCHECKs aren't enabled, DCHECK_OP still needs to reference operator<< |
895 | | // overloads for |val1| and |val2| to avoid potential compiler warnings about |
896 | | // unused functions. For the same reason, it also compares |val1| and |val2| |
897 | | // using |op|. |
898 | | // |
899 | | // Note that the contract of DCHECK_EQ, etc is that arguments are only evaluated |
900 | | // once. Even though |val1| and |val2| appear twice in this version of the macro |
901 | | // expansion, this is OK, since the expression is never actually evaluated. |
902 | | #define DCHECK_OP(name, op, val1, val2) \ |
903 | 0 | EAT_STREAM_PARAMETERS << (::logging::MakeCheckOpValueString( \ |
904 | 0 | ::logging::g_swallow_stream, val1), \ |
905 | 0 | ::logging::MakeCheckOpValueString( \ |
906 | 0 | ::logging::g_swallow_stream, val2), \ |
907 | 0 | (val1)op(val2)) |
908 | | |
909 | | #endif // DCHECK_IS_ON() |
910 | | |
911 | | // Equality/Inequality checks - compare two values, and log a |
912 | | // LOG_DCHECK message including the two values when the result is not |
913 | | // as expected. The values must have operator<<(ostream, ...) |
914 | | // defined. |
915 | | // |
916 | | // You may append to the error message like so: |
917 | | // DCHECK_NE(1, 2) << "The world must be ending!"; |
918 | | // |
919 | | // We are very careful to ensure that each argument is evaluated exactly |
920 | | // once, and that anything which is legal to pass as a function argument is |
921 | | // legal here. In particular, the arguments may be temporary expressions |
922 | | // which will end up being destroyed at the end of the apparent statement, |
923 | | // for example: |
924 | | // DCHECK_EQ(string("abc")[1], 'b'); |
925 | | // |
926 | | // WARNING: These don't compile correctly if one of the arguments is a pointer |
927 | | // and the other is NULL. In new code, prefer nullptr instead. To |
928 | | // work around this for C++98, simply static_cast NULL to the type of the |
929 | | // desired pointer. |
930 | | |
931 | 0 | #define DCHECK_EQ(val1, val2) DCHECK_OP(EQ, ==, val1, val2) |
932 | 0 | #define DCHECK_NE(val1, val2) DCHECK_OP(NE, !=, val1, val2) |
933 | 0 | #define DCHECK_LE(val1, val2) DCHECK_OP(LE, <=, val1, val2) |
934 | 0 | #define DCHECK_LT(val1, val2) DCHECK_OP(LT, < , val1, val2) |
935 | 0 | #define DCHECK_GE(val1, val2) DCHECK_OP(GE, >=, val1, val2) |
936 | 0 | #define DCHECK_GT(val1, val2) DCHECK_OP(GT, > , val1, val2) |
937 | | |
938 | | #if !DCHECK_IS_ON() && defined(OS_CHROMEOS) |
939 | | // Implement logging of NOTREACHED() as a dedicated function to get function |
940 | | // call overhead down to a minimum. |
941 | | void LogErrorNotReached(const char* file, int line); |
942 | | #define NOTREACHED() \ |
943 | | true ? ::logging::LogErrorNotReached(__FILE__, __LINE__) \ |
944 | | : EAT_STREAM_PARAMETERS |
945 | | #else |
946 | 0 | #define NOTREACHED() DCHECK(false) |
947 | | #endif |
948 | | |
949 | | // Redefine the standard assert to use our nice log files |
950 | | #undef assert |
951 | | #define assert(x) DLOG_ASSERT(x) |
952 | | |
953 | | // This class more or less represents a particular log message. You |
954 | | // create an instance of LogMessage and then stream stuff to it. |
955 | | // When you finish streaming to it, ~LogMessage is called and the |
956 | | // full message gets streamed to the appropriate destination. |
957 | | // |
958 | | // You shouldn't actually use LogMessage's constructor to log things, |
959 | | // though. You should use the LOG() macro (and variants thereof) |
960 | | // above. |
961 | | class BASE_EXPORT LogMessage { |
962 | | public: |
963 | | // Used for LOG(severity). |
964 | | LogMessage(const char* file, int line, LogSeverity severity); |
965 | | |
966 | | // Used for CHECK(). Implied severity = LOG_FATAL. |
967 | | LogMessage(const char* file, int line, const char* condition); |
968 | | |
969 | | // Used for CHECK_EQ(), etc. Takes ownership of the given string. |
970 | | // Implied severity = LOG_FATAL. |
971 | | LogMessage(const char* file, int line, std::string* result); |
972 | | |
973 | | // Used for DCHECK_EQ(), etc. Takes ownership of the given string. |
974 | | LogMessage(const char* file, int line, LogSeverity severity, |
975 | | std::string* result); |
976 | | |
977 | | ~LogMessage(); |
978 | | |
979 | 0 | std::ostream& stream() { return stream_; } |
980 | | |
981 | 0 | LogSeverity severity() { return severity_; } |
982 | 0 | std::string str() { return stream_.str(); } |
983 | | |
984 | | private: |
985 | | void Init(const char* file, int line); |
986 | | |
987 | | LogSeverity severity_; |
988 | | std::ostringstream stream_; |
989 | | size_t message_start_; // Offset of the start of the message (past prefix |
990 | | // info). |
991 | | // The file and line information passed in to the constructor. |
992 | | const char* file_; |
993 | | const int line_; |
994 | | |
995 | | #if defined(OS_WIN) |
996 | | // Stores the current value of GetLastError in the constructor and restores |
997 | | // it in the destructor by calling SetLastError. |
998 | | // This is useful since the LogMessage class uses a lot of Win32 calls |
999 | | // that will lose the value of GLE and the code that called the log function |
1000 | | // will have lost the thread error value when the log call returns. |
1001 | | class SaveLastError { |
1002 | | public: |
1003 | | SaveLastError(); |
1004 | | ~SaveLastError(); |
1005 | | |
1006 | | unsigned long get_error() const { return last_error_; } |
1007 | | |
1008 | | protected: |
1009 | | unsigned long last_error_; |
1010 | | }; |
1011 | | |
1012 | | SaveLastError last_error_; |
1013 | | #endif |
1014 | | |
1015 | | DISALLOW_COPY_AND_ASSIGN(LogMessage); |
1016 | | }; |
1017 | | |
1018 | | // This class is used to explicitly ignore values in the conditional |
1019 | | // logging macros. This avoids compiler warnings like "value computed |
1020 | | // is not used" and "statement has no effect". |
1021 | | class LogMessageVoidify { |
1022 | | public: |
1023 | 0 | LogMessageVoidify() { } |
1024 | | // This has to be an operator with a precedence lower than << but |
1025 | | // higher than ?: |
1026 | 0 | void operator&(std::ostream&) { } |
1027 | | }; |
1028 | | |
1029 | | #if defined(OS_WIN) |
1030 | | typedef unsigned long SystemErrorCode; |
1031 | | #elif defined(OS_POSIX) |
1032 | | typedef int SystemErrorCode; |
1033 | | #endif |
1034 | | |
1035 | | // Alias for ::GetLastError() on Windows and errno on POSIX. Avoids having to |
1036 | | // pull in windows.h just for GetLastError() and DWORD. |
1037 | | BASE_EXPORT SystemErrorCode GetLastSystemErrorCode(); |
1038 | | BASE_EXPORT std::string SystemErrorCodeToString(SystemErrorCode error_code); |
1039 | | |
1040 | | #if defined(OS_WIN) |
1041 | | // Appends a formatted system message of the GetLastError() type. |
1042 | | class BASE_EXPORT Win32ErrorLogMessage { |
1043 | | public: |
1044 | | Win32ErrorLogMessage(const char* file, |
1045 | | int line, |
1046 | | LogSeverity severity, |
1047 | | SystemErrorCode err); |
1048 | | |
1049 | | // Appends the error message before destructing the encapsulated class. |
1050 | | ~Win32ErrorLogMessage(); |
1051 | | |
1052 | | std::ostream& stream() { return log_message_.stream(); } |
1053 | | |
1054 | | private: |
1055 | | SystemErrorCode err_; |
1056 | | LogMessage log_message_; |
1057 | | |
1058 | | DISALLOW_COPY_AND_ASSIGN(Win32ErrorLogMessage); |
1059 | | }; |
1060 | | #elif defined(OS_POSIX) |
1061 | | // Appends a formatted system message of the errno type |
1062 | | class BASE_EXPORT ErrnoLogMessage { |
1063 | | public: |
1064 | | ErrnoLogMessage(const char* file, |
1065 | | int line, |
1066 | | LogSeverity severity, |
1067 | | SystemErrorCode err); |
1068 | | |
1069 | | // Appends the error message before destructing the encapsulated class. |
1070 | | ~ErrnoLogMessage(); |
1071 | | |
1072 | 0 | std::ostream& stream() { return log_message_.stream(); } |
1073 | | |
1074 | | private: |
1075 | | SystemErrorCode err_; |
1076 | | LogMessage log_message_; |
1077 | | |
1078 | | DISALLOW_COPY_AND_ASSIGN(ErrnoLogMessage); |
1079 | | }; |
1080 | | #endif // OS_WIN |
1081 | | |
1082 | | // Closes the log file explicitly if open. |
1083 | | // NOTE: Since the log file is opened as necessary by the action of logging |
1084 | | // statements, there's no guarantee that it will stay closed |
1085 | | // after this call. |
1086 | | BASE_EXPORT void CloseLogFile(); |
1087 | | |
1088 | | // Async signal safe logging mechanism. |
1089 | | BASE_EXPORT void RawLog(int level, const char* message); |
1090 | | |
1091 | | #define RAW_LOG(level, message) \ |
1092 | 0 | ::logging::RawLog(::logging::LOG_##level, message) |
1093 | | |
1094 | | #define RAW_CHECK(condition) \ |
1095 | | do { \ |
1096 | | if (!(condition)) \ |
1097 | | ::logging::RawLog(::logging::LOG_FATAL, \ |
1098 | | "Check failed: " #condition "\n"); \ |
1099 | | } while (0) |
1100 | | |
1101 | | #if defined(OS_WIN) |
1102 | | // Returns true if logging to file is enabled. |
1103 | | BASE_EXPORT bool IsLoggingToFileEnabled(); |
1104 | | |
1105 | | // Returns the default log file path. |
1106 | | BASE_EXPORT std::wstring GetLogFileFullPath(); |
1107 | | #endif |
1108 | | |
1109 | | } // namespace logging |
1110 | | |
1111 | | // Note that "The behavior of a C++ program is undefined if it adds declarations |
1112 | | // or definitions to namespace std or to a namespace within namespace std unless |
1113 | | // otherwise specified." --C++11[namespace.std] |
1114 | | // |
1115 | | // We've checked that this particular definition has the intended behavior on |
1116 | | // our implementations, but it's prone to breaking in the future, and please |
1117 | | // don't imitate this in your own definitions without checking with some |
1118 | | // standard library experts. |
1119 | | namespace std { |
1120 | | // These functions are provided as a convenience for logging, which is where we |
1121 | | // use streams (it is against Google style to use streams in other places). It |
1122 | | // is designed to allow you to emit non-ASCII Unicode strings to the log file, |
1123 | | // which is normally ASCII. It is relatively slow, so try not to use it for |
1124 | | // common cases. Non-ASCII characters will be converted to UTF-8 by these |
1125 | | // operators. |
1126 | | BASE_EXPORT std::ostream& operator<<(std::ostream& out, const wchar_t* wstr); |
1127 | 0 | inline std::ostream& operator<<(std::ostream& out, const std::wstring& wstr) { |
1128 | 0 | return out << wstr.c_str(); |
1129 | 0 | } |
1130 | | } // namespace std |
1131 | | |
1132 | | // The NOTIMPLEMENTED() macro annotates codepaths which have |
1133 | | // not been implemented yet. |
1134 | | // |
1135 | | // The implementation of this macro is controlled by NOTIMPLEMENTED_POLICY: |
1136 | | // 0 -- Do nothing (stripped by compiler) |
1137 | | // 1 -- Warn at compile time |
1138 | | // 2 -- Fail at compile time |
1139 | | // 3 -- Fail at runtime (DCHECK) |
1140 | | // 4 -- [default] LOG(ERROR) at runtime |
1141 | | // 5 -- LOG(ERROR) at runtime, only once per call-site |
1142 | | |
1143 | | #ifndef NOTIMPLEMENTED_POLICY |
1144 | | #if defined(OS_ANDROID) && defined(OFFICIAL_BUILD) |
1145 | | #define NOTIMPLEMENTED_POLICY 0 |
1146 | | #else |
1147 | | // Select default policy: LOG(ERROR) |
1148 | | #define NOTIMPLEMENTED_POLICY 4 |
1149 | | #endif |
1150 | | #endif |
1151 | | |
1152 | | #if defined(COMPILER_GCC) |
1153 | | // On Linux, with GCC, we can use __PRETTY_FUNCTION__ to get the demangled name |
1154 | | // of the current function in the NOTIMPLEMENTED message. |
1155 | | #define NOTIMPLEMENTED_MSG "Not implemented reached in " << __PRETTY_FUNCTION__ |
1156 | | #else |
1157 | | #define NOTIMPLEMENTED_MSG "NOT IMPLEMENTED" |
1158 | | #endif |
1159 | | |
1160 | | #if NOTIMPLEMENTED_POLICY == 0 |
1161 | | #define NOTIMPLEMENTED() EAT_STREAM_PARAMETERS |
1162 | | #elif NOTIMPLEMENTED_POLICY == 1 |
1163 | | // TODO, figure out how to generate a warning |
1164 | | #define NOTIMPLEMENTED() static_assert(false, "NOT_IMPLEMENTED") |
1165 | | #elif NOTIMPLEMENTED_POLICY == 2 |
1166 | | #define NOTIMPLEMENTED() static_assert(false, "NOT_IMPLEMENTED") |
1167 | | #elif NOTIMPLEMENTED_POLICY == 3 |
1168 | | #define NOTIMPLEMENTED() NOTREACHED() |
1169 | | #elif NOTIMPLEMENTED_POLICY == 4 |
1170 | | #define NOTIMPLEMENTED() LOG(ERROR) << NOTIMPLEMENTED_MSG |
1171 | | #elif NOTIMPLEMENTED_POLICY == 5 |
1172 | | #define NOTIMPLEMENTED() do {\ |
1173 | | static bool logged_once = false;\ |
1174 | | LOG_IF(ERROR, !logged_once) << NOTIMPLEMENTED_MSG;\ |
1175 | | logged_once = true;\ |
1176 | | } while(0);\ |
1177 | | EAT_STREAM_PARAMETERS |
1178 | | #endif |
1179 | | |
1180 | | #endif // BASE_LOGGING_H_ |