1 // Copyright (c) 2014 Marshall A. Greenblatt. Portions copyright (c) 2012
2 // Google Inc. All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 // * Neither the name of Google Inc. nor the name Chromium Embedded
15 // Framework nor the names of its contributors may be used to endorse
16 // or promote products derived from this software without specific prior
17 // written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 //
31 // ---------------------------------------------------------------------------
32 //
33 // The contents of this file are only available to applications that link
34 // against the libcef_dll_wrapper target.
35 //
36 // WARNING: Logging macros should not be used in the main/browser process before
37 // calling CefInitialize or in sub-processes before calling CefExecuteProcess.
38 //
39 // Instructions
40 // ------------
41 //
42 // Make a bunch of macros for logging. The way to log things is to stream
43 // things to LOG(<a particular severity level>). E.g.,
44 //
45 // LOG(INFO) << "Found " << num_cookies << " cookies";
46 //
47 // You can also do conditional logging:
48 //
49 // LOG_IF(INFO, num_cookies > 10) << "Got lots of cookies";
50 //
51 // The CHECK(condition) macro is active in both debug and release builds and
52 // effectively performs a LOG(FATAL) which terminates the process and
53 // generates a crashdump unless a debugger is attached.
54 //
55 // There are also "debug mode" logging macros like the ones above:
56 //
57 // DLOG(INFO) << "Found cookies";
58 //
59 // DLOG_IF(INFO, num_cookies > 10) << "Got lots of cookies";
60 //
61 // All "debug mode" logging is compiled away to nothing for non-debug mode
62 // compiles. LOG_IF and development flags also work well together
63 // because the code can be compiled away sometimes.
64 //
65 // We also have
66 //
67 // LOG_ASSERT(assertion);
68 // DLOG_ASSERT(assertion);
69 //
70 // which is syntactic sugar for {,D}LOG_IF(FATAL, assert fails) << assertion;
71 //
72 // There are "verbose level" logging macros. They look like
73 //
74 // VLOG(1) << "I'm printed when you run the program with --v=1 or more";
75 // VLOG(2) << "I'm printed when you run the program with --v=2 or more";
76 //
77 // These always log at the INFO log level (when they log at all).
78 // The verbose logging can also be turned on module-by-module. For instance,
79 // --vmodule=profile=2,icon_loader=1,browser_*=3,*/chromeos/*=4 --v=0
80 // will cause:
81 // a. VLOG(2) and lower messages to be printed from profile.{h,cc}
82 // b. VLOG(1) and lower messages to be printed from icon_loader.{h,cc}
83 // c. VLOG(3) and lower messages to be printed from files prefixed with
84 // "browser"
85 // d. VLOG(4) and lower messages to be printed from files under a
86 // "chromeos" directory.
87 // e. VLOG(0) and lower messages to be printed from elsewhere
88 //
89 // The wildcarding functionality shown by (c) supports both '*' (match
90 // 0 or more characters) and '?' (match any single character)
91 // wildcards. Any pattern containing a forward or backward slash will
92 // be tested against the whole pathname and not just the module.
93 // E.g., "*/foo/bar/*=2" would change the logging level for all code
94 // in source files under a "foo/bar" directory.
95 //
96 // There's also VLOG_IS_ON(n) "verbose level" condition macro. To be used as
97 //
98 // if (VLOG_IS_ON(2)) {
99 // // do some logging preparation and logging
100 // // that can't be accomplished with just VLOG(2) << ...;
101 // }
102 //
103 // There is also a VLOG_IF "verbose level" condition macro for sample
104 // cases, when some extra computation and preparation for logs is not
105 // needed.
106 //
107 // VLOG_IF(1, (size > 1024))
108 // << "I'm printed when size is more than 1024 and when you run the "
109 // "program with --v=1 or more";
110 //
111 // We also override the standard 'assert' to use 'DLOG_ASSERT'.
112 //
113 // Lastly, there is:
114 //
115 // PLOG(ERROR) << "Couldn't do foo";
116 // DPLOG(ERROR) << "Couldn't do foo";
117 // PLOG_IF(ERROR, cond) << "Couldn't do foo";
118 // DPLOG_IF(ERROR, cond) << "Couldn't do foo";
119 // PCHECK(condition) << "Couldn't do foo";
120 // DPCHECK(condition) << "Couldn't do foo";
121 //
122 // which append the last system error to the message in string form (taken from
123 // GetLastError() on Windows and errno on POSIX).
124 //
125 // The supported severity levels for macros that allow you to specify one
126 // are (in increasing order of severity) INFO, WARNING, ERROR, and FATAL.
127 //
128 // Very important: logging a message at the FATAL severity level causes
129 // the program to terminate (after the message is logged).
130 //
131 // There is the special severity of DFATAL, which logs FATAL in debug mode,
132 // ERROR in normal mode.
133 //
134
135 #ifndef CEF_INCLUDE_BASE_CEF_LOGGING_H_
136 #define CEF_INCLUDE_BASE_CEF_LOGGING_H_
137 #pragma once
138
139 #if defined(USING_CHROMIUM_INCLUDES)
140 // When building CEF include the Chromium header directly.
141 #include "base/logging.h"
142 #include "base/notreached.h"
143 #elif defined(DCHECK)
144 // Do nothing if the macros provided by this header already exist.
145 // This can happen in cases where Chromium code is used directly by the
146 // client application. When using Chromium code directly always include
147 // the Chromium header first to avoid type conflicts.
148
149 // Always define the DCHECK_IS_ON macro which is used from other CEF headers.
150 #if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON)
151 #define DCHECK_IS_ON() false
152 #else
153 #define DCHECK_IS_ON() true
154 #endif
155
156 #else // !defined(DCHECK)
157 // The following is substantially similar to the Chromium implementation.
158 // If the Chromium implementation diverges the below implementation should be
159 // updated to match.
160
161 #include <cassert>
162 #include <cstring>
163 #include <sstream>
164 #include <string>
165
166 #include "include/base/cef_build.h"
167 #include "include/base/cef_macros.h"
168 #include "include/internal/cef_logging_internal.h"
169
170 namespace cef {
171 namespace logging {
172
173 // Gets the current log level.
GetMinLogLevel()174 inline int GetMinLogLevel() {
175 return cef_get_min_log_level();
176 }
177
178 // Gets the current vlog level for the given file (usually taken from
179 // __FILE__). Note that |N| is the size *with* the null terminator.
180 template <size_t N>
GetVlogLevel(const char (& file)[N])181 int GetVlogLevel(const char (&file)[N]) {
182 return cef_get_vlog_level(file, N);
183 }
184
185 typedef int LogSeverity;
186 const LogSeverity LOG_VERBOSE = -1; // This is level 1 verbosity
187 // Note: the log severities are used to index into the array of names,
188 // see log_severity_names.
189 const LogSeverity LOG_INFO = 0;
190 const LogSeverity LOG_WARNING = 1;
191 const LogSeverity LOG_ERROR = 2;
192 const LogSeverity LOG_FATAL = 3;
193 const LogSeverity LOG_NUM_SEVERITIES = 4;
194
195 // LOG_DFATAL is LOG_FATAL in debug mode, ERROR in normal mode
196 #ifdef NDEBUG
197 const LogSeverity LOG_DFATAL = LOG_ERROR;
198 #else
199 const LogSeverity LOG_DFATAL = LOG_FATAL;
200 #endif
201
202 // A few definitions of macros that don't generate much code. These are used
203 // by LOG() and LOG_IF, etc. Since these are used all over our code, it's
204 // better to have compact code for these operations.
205 #define COMPACT_GOOGLE_LOG_EX_INFO(ClassName, ...) \
206 cef::logging::ClassName(__FILE__, __LINE__, cef::logging::LOG_INFO, \
207 ##__VA_ARGS__)
208 #define COMPACT_GOOGLE_LOG_EX_WARNING(ClassName, ...) \
209 cef::logging::ClassName(__FILE__, __LINE__, cef::logging::LOG_WARNING, \
210 ##__VA_ARGS__)
211 #define COMPACT_GOOGLE_LOG_EX_ERROR(ClassName, ...) \
212 cef::logging::ClassName(__FILE__, __LINE__, cef::logging::LOG_ERROR, \
213 ##__VA_ARGS__)
214 #define COMPACT_GOOGLE_LOG_EX_FATAL(ClassName, ...) \
215 cef::logging::ClassName(__FILE__, __LINE__, cef::logging::LOG_FATAL, \
216 ##__VA_ARGS__)
217 #define COMPACT_GOOGLE_LOG_EX_DFATAL(ClassName, ...) \
218 cef::logging::ClassName(__FILE__, __LINE__, cef::logging::LOG_DFATAL, \
219 ##__VA_ARGS__)
220
221 #define COMPACT_GOOGLE_LOG_INFO COMPACT_GOOGLE_LOG_EX_INFO(LogMessage)
222 #define COMPACT_GOOGLE_LOG_WARNING COMPACT_GOOGLE_LOG_EX_WARNING(LogMessage)
223 #define COMPACT_GOOGLE_LOG_ERROR COMPACT_GOOGLE_LOG_EX_ERROR(LogMessage)
224 #define COMPACT_GOOGLE_LOG_FATAL COMPACT_GOOGLE_LOG_EX_FATAL(LogMessage)
225 #define COMPACT_GOOGLE_LOG_DFATAL COMPACT_GOOGLE_LOG_EX_DFATAL(LogMessage)
226
227 #if defined(OS_WIN)
228 // wingdi.h defines ERROR to be 0. When we call LOG(ERROR), it gets
229 // substituted with 0, and it expands to COMPACT_GOOGLE_LOG_0. To allow us
230 // to keep using this syntax, we define this macro to do the same thing
231 // as COMPACT_GOOGLE_LOG_ERROR, and also define ERROR the same way that
232 // the Windows SDK does for consistency.
233 #define ERROR 0
234 #define COMPACT_GOOGLE_LOG_EX_0(ClassName, ...) \
235 COMPACT_GOOGLE_LOG_EX_ERROR(ClassName, ##__VA_ARGS__)
236 #define COMPACT_GOOGLE_LOG_0 COMPACT_GOOGLE_LOG_ERROR
237 // Needed for LOG_IS_ON(ERROR).
238 const LogSeverity LOG_0 = LOG_ERROR;
239 #endif
240
241 // As special cases, we can assume that LOG_IS_ON(FATAL) always holds. Also,
242 // LOG_IS_ON(DFATAL) always holds in debug mode. In particular, CHECK()s will
243 // always fire if they fail.
244 #define LOG_IS_ON(severity) \
245 ((::cef::logging::LOG_##severity) >= ::cef::logging::GetMinLogLevel())
246
247 // We can't do any caching tricks with VLOG_IS_ON() like the
248 // google-glog version since it requires GCC extensions. This means
249 // that using the v-logging functions in conjunction with --vmodule
250 // may be slow.
251 #define VLOG_IS_ON(verboselevel) \
252 ((verboselevel) <= ::cef::logging::GetVlogLevel(__FILE__))
253
254 // Helper macro which avoids evaluating the arguments to a stream if
255 // the condition doesn't hold.
256 #define LAZY_STREAM(stream, condition) \
257 !(condition) ? (void)0 : ::cef::logging::LogMessageVoidify() & (stream)
258
259 // We use the preprocessor's merging operator, "##", so that, e.g.,
260 // LOG(INFO) becomes the token COMPACT_GOOGLE_LOG_INFO. There's some funny
261 // subtle difference between ostream member streaming functions (e.g.,
262 // ostream::operator<<(int) and ostream non-member streaming functions
263 // (e.g., ::operator<<(ostream&, string&): it turns out that it's
264 // impossible to stream something like a string directly to an unnamed
265 // ostream. We employ a neat hack by calling the stream() member
266 // function of LogMessage which seems to avoid the problem.
267 #define LOG_STREAM(severity) COMPACT_GOOGLE_LOG_##severity.stream()
268
269 #define LOG(severity) LAZY_STREAM(LOG_STREAM(severity), LOG_IS_ON(severity))
270 #define LOG_IF(severity, condition) \
271 LAZY_STREAM(LOG_STREAM(severity), LOG_IS_ON(severity) && (condition))
272
273 #define SYSLOG(severity) LOG(severity)
274 #define SYSLOG_IF(severity, condition) LOG_IF(severity, condition)
275
276 // The VLOG macros log with negative verbosities.
277 #define VLOG_STREAM(verbose_level) \
278 cef::logging::LogMessage(__FILE__, __LINE__, -verbose_level).stream()
279
280 #define VLOG(verbose_level) \
281 LAZY_STREAM(VLOG_STREAM(verbose_level), VLOG_IS_ON(verbose_level))
282
283 #define VLOG_IF(verbose_level, condition) \
284 LAZY_STREAM(VLOG_STREAM(verbose_level), \
285 VLOG_IS_ON(verbose_level) && (condition))
286
287 #if defined(OS_WIN)
288 #define VPLOG_STREAM(verbose_level) \
289 cef::logging::Win32ErrorLogMessage(__FILE__, __LINE__, -verbose_level, \
290 ::cef::logging::GetLastSystemErrorCode()) \
291 .stream()
292 #elif defined(OS_POSIX)
293 #define VPLOG_STREAM(verbose_level) \
294 cef::logging::ErrnoLogMessage(__FILE__, __LINE__, -verbose_level, \
295 ::cef::logging::GetLastSystemErrorCode()) \
296 .stream()
297 #endif
298
299 #define VPLOG(verbose_level) \
300 LAZY_STREAM(VPLOG_STREAM(verbose_level), VLOG_IS_ON(verbose_level))
301
302 #define VPLOG_IF(verbose_level, condition) \
303 LAZY_STREAM(VPLOG_STREAM(verbose_level), \
304 VLOG_IS_ON(verbose_level) && (condition))
305
306 // TODO(akalin): Add more VLOG variants, e.g. VPLOG.
307
308 #define LOG_ASSERT(condition) \
309 LOG_IF(FATAL, !(condition)) << "Assert failed: " #condition ". "
310 #define SYSLOG_ASSERT(condition) \
311 SYSLOG_IF(FATAL, !(condition)) << "Assert failed: " #condition ". "
312
313 #if defined(OS_WIN)
314 #define PLOG_STREAM(severity) \
315 COMPACT_GOOGLE_LOG_EX_##severity(Win32ErrorLogMessage, \
316 ::cef::logging::GetLastSystemErrorCode()) \
317 .stream()
318 #elif defined(OS_POSIX)
319 #define PLOG_STREAM(severity) \
320 COMPACT_GOOGLE_LOG_EX_##severity(ErrnoLogMessage, \
321 ::cef::logging::GetLastSystemErrorCode()) \
322 .stream()
323 #endif
324
325 #define PLOG(severity) LAZY_STREAM(PLOG_STREAM(severity), LOG_IS_ON(severity))
326
327 #define PLOG_IF(severity, condition) \
328 LAZY_STREAM(PLOG_STREAM(severity), LOG_IS_ON(severity) && (condition))
329
330 // The actual stream used isn't important.
331 #define EAT_STREAM_PARAMETERS \
332 true ? (void)0 : ::cef::logging::LogMessageVoidify() & LOG_STREAM(FATAL)
333
334 // CHECK dies with a fatal error if condition is not true. It is *not*
335 // controlled by NDEBUG, so the check will be executed regardless of
336 // compilation mode.
337 //
338 // We make sure CHECK et al. always evaluates their arguments, as
339 // doing CHECK(FunctionWithSideEffect()) is a common idiom.
340
341 #define CHECK(condition) \
342 LAZY_STREAM(LOG_STREAM(FATAL), !(condition)) \
343 << "Check failed: " #condition ". "
344
345 #define PCHECK(condition) \
346 LAZY_STREAM(PLOG_STREAM(FATAL), !(condition)) \
347 << "Check failed: " #condition ". "
348
349 // Helper macro for binary operators.
350 // Don't use this macro directly in your code, use CHECK_EQ et al below.
351 //
352 // TODO(akalin): Rewrite this so that constructs like if (...)
353 // CHECK_EQ(...) else { ... } work properly.
354 #define CHECK_OP(name, op, val1, val2) \
355 if (std::string* _result = cef::logging::Check##name##Impl( \
356 (val1), (val2), #val1 " " #op " " #val2)) \
357 cef::logging::LogMessage(__FILE__, __LINE__, _result).stream()
358
359 // Build the error message string. This is separate from the "Impl"
360 // function template because it is not performance critical and so can
361 // be out of line, while the "Impl" code should be inline. Caller
362 // takes ownership of the returned string.
363 template <class t1, class t2>
MakeCheckOpString(const t1 & v1,const t2 & v2,const char * names)364 std::string* MakeCheckOpString(const t1& v1, const t2& v2, const char* names) {
365 std::ostringstream ss;
366 ss << names << " (" << v1 << " vs. " << v2 << ")";
367 std::string* msg = new std::string(ss.str());
368 return msg;
369 }
370
371 // MSVC doesn't like complex extern templates and DLLs.
372 #if !defined(COMPILER_MSVC)
373 // Commonly used instantiations of MakeCheckOpString<>. Explicitly instantiated
374 // in logging.cc.
375 extern template std::string* MakeCheckOpString<int, int>(const int&,
376 const int&,
377 const char* names);
378 extern template std::string* MakeCheckOpString<unsigned long, unsigned long>(
379 const unsigned long&,
380 const unsigned long&,
381 const char* names);
382 extern template std::string* MakeCheckOpString<unsigned long, unsigned int>(
383 const unsigned long&,
384 const unsigned int&,
385 const char* names);
386 extern template std::string* MakeCheckOpString<unsigned int, unsigned long>(
387 const unsigned int&,
388 const unsigned long&,
389 const char* names);
390 extern template std::string* MakeCheckOpString<std::string, std::string>(
391 const std::string&,
392 const std::string&,
393 const char* name);
394 #endif
395
396 // Helper functions for CHECK_OP macro.
397 // The (int, int) specialization works around the issue that the compiler
398 // will not instantiate the template version of the function on values of
399 // unnamed enum type - see comment below.
400 #define DEFINE_CHECK_OP_IMPL(name, op) \
401 template <class t1, class t2> \
402 inline std::string* Check##name##Impl(const t1& v1, const t2& v2, \
403 const char* names) { \
404 if (v1 op v2) \
405 return NULL; \
406 else \
407 return MakeCheckOpString(v1, v2, names); \
408 } \
409 inline std::string* Check##name##Impl(int v1, int v2, const char* names) { \
410 if (v1 op v2) \
411 return NULL; \
412 else \
413 return MakeCheckOpString(v1, v2, names); \
414 }
415 DEFINE_CHECK_OP_IMPL(EQ, ==)
416 DEFINE_CHECK_OP_IMPL(NE, !=)
417 DEFINE_CHECK_OP_IMPL(LE, <=)
418 DEFINE_CHECK_OP_IMPL(LT, <)
419 DEFINE_CHECK_OP_IMPL(GE, >=)
420 DEFINE_CHECK_OP_IMPL(GT, >)
421 #undef DEFINE_CHECK_OP_IMPL
422
423 #define CHECK_EQ(val1, val2) CHECK_OP(EQ, ==, val1, val2)
424 #define CHECK_NE(val1, val2) CHECK_OP(NE, !=, val1, val2)
425 #define CHECK_LE(val1, val2) CHECK_OP(LE, <=, val1, val2)
426 #define CHECK_LT(val1, val2) CHECK_OP(LT, <, val1, val2)
427 #define CHECK_GE(val1, val2) CHECK_OP(GE, >=, val1, val2)
428 #define CHECK_GT(val1, val2) CHECK_OP(GT, >, val1, val2)
429
430 #if defined(NDEBUG)
431 #define ENABLE_DLOG 0
432 #else
433 #define ENABLE_DLOG 1
434 #endif
435
436 #if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON)
437 #define DCHECK_IS_ON() 0
438 #else
439 #define DCHECK_IS_ON() 1
440 #endif
441
442 // Definitions for DLOG et al.
443
444 #if ENABLE_DLOG
445
446 #define DLOG_IS_ON(severity) LOG_IS_ON(severity)
447 #define DLOG_IF(severity, condition) LOG_IF(severity, condition)
448 #define DLOG_ASSERT(condition) LOG_ASSERT(condition)
449 #define DPLOG_IF(severity, condition) PLOG_IF(severity, condition)
450 #define DVLOG_IF(verboselevel, condition) VLOG_IF(verboselevel, condition)
451 #define DVPLOG_IF(verboselevel, condition) VPLOG_IF(verboselevel, condition)
452
453 #else // ENABLE_DLOG
454
455 // If ENABLE_DLOG is off, we want to avoid emitting any references to
456 // |condition| (which may reference a variable defined only if NDEBUG
457 // is not defined). Contrast this with DCHECK et al., which has
458 // different behavior.
459
460 #define DLOG_IS_ON(severity) false
461 #define DLOG_IF(severity, condition) EAT_STREAM_PARAMETERS
462 #define DLOG_ASSERT(condition) EAT_STREAM_PARAMETERS
463 #define DPLOG_IF(severity, condition) EAT_STREAM_PARAMETERS
464 #define DVLOG_IF(verboselevel, condition) EAT_STREAM_PARAMETERS
465 #define DVPLOG_IF(verboselevel, condition) EAT_STREAM_PARAMETERS
466
467 #endif // ENABLE_DLOG
468
469 // DEBUG_MODE is for uses like
470 // if (DEBUG_MODE) foo.CheckThatFoo();
471 // instead of
472 // #ifndef NDEBUG
473 // foo.CheckThatFoo();
474 // #endif
475 //
476 // We tie its state to ENABLE_DLOG.
477 enum { DEBUG_MODE = ENABLE_DLOG };
478
479 #undef ENABLE_DLOG
480
481 #define DLOG(severity) LAZY_STREAM(LOG_STREAM(severity), DLOG_IS_ON(severity))
482
483 #define DPLOG(severity) LAZY_STREAM(PLOG_STREAM(severity), DLOG_IS_ON(severity))
484
485 #define DVLOG(verboselevel) DVLOG_IF(verboselevel, VLOG_IS_ON(verboselevel))
486
487 #define DVPLOG(verboselevel) DVPLOG_IF(verboselevel, VLOG_IS_ON(verboselevel))
488
489 // Definitions for DCHECK et al.
490
491 #if DCHECK_IS_ON()
492
493 #define COMPACT_GOOGLE_LOG_EX_DCHECK(ClassName, ...) \
494 COMPACT_GOOGLE_LOG_EX_FATAL(ClassName, ##__VA_ARGS__)
495 #define COMPACT_GOOGLE_LOG_DCHECK COMPACT_GOOGLE_LOG_FATAL
496 const LogSeverity LOG_DCHECK = LOG_FATAL;
497
498 #else // DCHECK_IS_ON()
499
500 // These are just dummy values.
501 #define COMPACT_GOOGLE_LOG_EX_DCHECK(ClassName, ...) \
502 COMPACT_GOOGLE_LOG_EX_INFO(ClassName, ##__VA_ARGS__)
503 #define COMPACT_GOOGLE_LOG_DCHECK COMPACT_GOOGLE_LOG_INFO
504 const LogSeverity LOG_DCHECK = LOG_INFO;
505
506 #endif // DCHECK_IS_ON()
507
508 // DCHECK et al. make sure to reference |condition| regardless of
509 // whether DCHECKs are enabled; this is so that we don't get unused
510 // variable warnings if the only use of a variable is in a DCHECK.
511 // This behavior is different from DLOG_IF et al.
512
513 #define DCHECK(condition) \
514 LAZY_STREAM(LOG_STREAM(DCHECK), DCHECK_IS_ON() && !(condition)) \
515 << "Check failed: " #condition ". "
516
517 #define DPCHECK(condition) \
518 LAZY_STREAM(PLOG_STREAM(DCHECK), DCHECK_IS_ON() && !(condition)) \
519 << "Check failed: " #condition ". "
520
521 // Helper macro for binary operators.
522 // Don't use this macro directly in your code, use DCHECK_EQ et al below.
523 #define DCHECK_OP(name, op, val1, val2) \
524 if (DCHECK_IS_ON()) \
525 if (std::string* _result = cef::logging::Check##name##Impl( \
526 (val1), (val2), #val1 " " #op " " #val2)) \
527 cef::logging::LogMessage(__FILE__, __LINE__, ::cef::logging::LOG_DCHECK, \
528 _result) \
529 .stream()
530
531 // Equality/Inequality checks - compare two values, and log a
532 // LOG_DCHECK message including the two values when the result is not
533 // as expected. The values must have operator<<(ostream, ...)
534 // defined.
535 //
536 // You may append to the error message like so:
537 // DCHECK_NE(1, 2) << ": The world must be ending!";
538 //
539 // We are very careful to ensure that each argument is evaluated exactly
540 // once, and that anything which is legal to pass as a function argument is
541 // legal here. In particular, the arguments may be temporary expressions
542 // which will end up being destroyed at the end of the apparent statement,
543 // for example:
544 // DCHECK_EQ(string("abc")[1], 'b');
545 //
546 // WARNING: These may not compile correctly if one of the arguments is a pointer
547 // and the other is NULL. To work around this, simply static_cast NULL to the
548 // type of the desired pointer.
549
550 #define DCHECK_EQ(val1, val2) DCHECK_OP(EQ, ==, val1, val2)
551 #define DCHECK_NE(val1, val2) DCHECK_OP(NE, !=, val1, val2)
552 #define DCHECK_LE(val1, val2) DCHECK_OP(LE, <=, val1, val2)
553 #define DCHECK_LT(val1, val2) DCHECK_OP(LT, <, val1, val2)
554 #define DCHECK_GE(val1, val2) DCHECK_OP(GE, >=, val1, val2)
555 #define DCHECK_GT(val1, val2) DCHECK_OP(GT, >, val1, val2)
556
557 #define NOTREACHED() DCHECK(false)
558
559 // Redefine the standard assert to use our nice log files
560 #undef assert
561 #define assert(x) DLOG_ASSERT(x)
562
563 // This class more or less represents a particular log message. You
564 // create an instance of LogMessage and then stream stuff to it.
565 // When you finish streaming to it, ~LogMessage is called and the
566 // full message gets streamed to the appropriate destination.
567 //
568 // You shouldn't actually use LogMessage's constructor to log things,
569 // though. You should use the LOG() macro (and variants thereof)
570 // above.
571 class LogMessage {
572 public:
573 // Used for LOG(severity).
574 LogMessage(const char* file, int line, LogSeverity severity);
575
576 // Used for CHECK_EQ(), etc. Takes ownership of the given string.
577 // Implied severity = LOG_FATAL.
578 LogMessage(const char* file, int line, std::string* result);
579
580 // Used for DCHECK_EQ(), etc. Takes ownership of the given string.
581 LogMessage(const char* file,
582 int line,
583 LogSeverity severity,
584 std::string* result);
585
586 ~LogMessage();
587
stream()588 std::ostream& stream() { return stream_; }
589
590 private:
591 LogSeverity severity_;
592 std::ostringstream stream_;
593
594 // The file and line information passed in to the constructor.
595 const char* file_;
596 const int line_;
597
598 #if defined(OS_WIN)
599 // Stores the current value of GetLastError in the constructor and restores
600 // it in the destructor by calling SetLastError.
601 // This is useful since the LogMessage class uses a lot of Win32 calls
602 // that will lose the value of GLE and the code that called the log function
603 // will have lost the thread error value when the log call returns.
604 class SaveLastError {
605 public:
606 SaveLastError();
607 ~SaveLastError();
608
get_error()609 unsigned long get_error() const { return last_error_; }
610
611 protected:
612 unsigned long last_error_;
613 };
614
615 SaveLastError last_error_;
616 #endif
617
618 DISALLOW_COPY_AND_ASSIGN(LogMessage);
619 };
620
621 // A non-macro interface to the log facility; (useful
622 // when the logging level is not a compile-time constant).
LogAtLevel(int const log_level,std::string const & msg)623 inline void LogAtLevel(int const log_level, std::string const& msg) {
624 LogMessage(__FILE__, __LINE__, log_level).stream() << msg;
625 }
626
627 // This class is used to explicitly ignore values in the conditional
628 // logging macros. This avoids compiler warnings like "value computed
629 // is not used" and "statement has no effect".
630 class LogMessageVoidify {
631 public:
LogMessageVoidify()632 LogMessageVoidify() {}
633 // This has to be an operator with a precedence lower than << but
634 // higher than ?:
635 void operator&(std::ostream&) {}
636 };
637
638 #if defined(OS_WIN)
639 typedef unsigned long SystemErrorCode;
640 #elif defined(OS_POSIX)
641 typedef int SystemErrorCode;
642 #endif
643
644 // Alias for ::GetLastError() on Windows and errno on POSIX. Avoids having to
645 // pull in windows.h just for GetLastError() and DWORD.
646 SystemErrorCode GetLastSystemErrorCode();
647 std::string SystemErrorCodeToString(SystemErrorCode error_code);
648
649 #if defined(OS_WIN)
650 // Appends a formatted system message of the GetLastError() type.
651 class Win32ErrorLogMessage {
652 public:
653 Win32ErrorLogMessage(const char* file,
654 int line,
655 LogSeverity severity,
656 SystemErrorCode err);
657
658 // Appends the error message before destructing the encapsulated class.
659 ~Win32ErrorLogMessage();
660
stream()661 std::ostream& stream() { return log_message_.stream(); }
662
663 private:
664 SystemErrorCode err_;
665 LogMessage log_message_;
666
667 DISALLOW_COPY_AND_ASSIGN(Win32ErrorLogMessage);
668 };
669 #elif defined(OS_POSIX)
670 // Appends a formatted system message of the errno type
671 class ErrnoLogMessage {
672 public:
673 ErrnoLogMessage(const char* file,
674 int line,
675 LogSeverity severity,
676 SystemErrorCode err);
677
678 // Appends the error message before destructing the encapsulated class.
679 ~ErrnoLogMessage();
680
stream()681 std::ostream& stream() { return log_message_.stream(); }
682
683 private:
684 SystemErrorCode err_;
685 LogMessage log_message_;
686
687 DISALLOW_COPY_AND_ASSIGN(ErrnoLogMessage);
688 };
689 #endif // OS_WIN
690
691 } // namespace logging
692 } // namespace cef
693
694 // These functions are provided as a convenience for logging, which is where we
695 // use streams (it is against Google style to use streams in other places). It
696 // is designed to allow you to emit non-ASCII Unicode strings to the log file,
697 // which is normally ASCII. It is relatively slow, so try not to use it for
698 // common cases. Non-ASCII characters will be converted to UTF-8 by these
699 // operators.
700 std::ostream& operator<<(std::ostream& out, const wchar_t* wstr);
701 inline std::ostream& operator<<(std::ostream& out, const std::wstring& wstr) {
702 return out << wstr.c_str();
703 }
704
705 // The NOTIMPLEMENTED() macro annotates codepaths which have
706 // not been implemented yet.
707 //
708 // The implementation of this macro is controlled by NOTIMPLEMENTED_POLICY:
709 // 0 -- Do nothing (stripped by compiler)
710 // 1 -- Warn at compile time
711 // 2 -- Fail at compile time
712 // 3 -- Fail at runtime (DCHECK)
713 // 4 -- [default] LOG(ERROR) at runtime
714 // 5 -- LOG(ERROR) at runtime, only once per call-site
715
716 #ifndef NOTIMPLEMENTED_POLICY
717 #if defined(OS_ANDROID) && defined(OFFICIAL_BUILD)
718 #define NOTIMPLEMENTED_POLICY 0
719 #else
720 // Select default policy: LOG(ERROR)
721 #define NOTIMPLEMENTED_POLICY 4
722 #endif
723 #endif
724
725 #if defined(COMPILER_GCC)
726 // On Linux, with GCC, we can use __PRETTY_FUNCTION__ to get the demangled name
727 // of the current function in the NOTIMPLEMENTED message.
728 #define NOTIMPLEMENTED_MSG "Not implemented reached in " << __PRETTY_FUNCTION__
729 #else
730 #define NOTIMPLEMENTED_MSG "NOT IMPLEMENTED"
731 #endif
732
733 #if NOTIMPLEMENTED_POLICY == 0
734 #define NOTIMPLEMENTED() EAT_STREAM_PARAMETERS
735 #elif NOTIMPLEMENTED_POLICY == 1
736 // TODO, figure out how to generate a warning
737 #define NOTIMPLEMENTED() COMPILE_ASSERT(false, NOT_IMPLEMENTED)
738 #elif NOTIMPLEMENTED_POLICY == 2
739 #define NOTIMPLEMENTED() COMPILE_ASSERT(false, NOT_IMPLEMENTED)
740 #elif NOTIMPLEMENTED_POLICY == 3
741 #define NOTIMPLEMENTED() NOTREACHED()
742 #elif NOTIMPLEMENTED_POLICY == 4
743 #define NOTIMPLEMENTED() LOG(ERROR) << NOTIMPLEMENTED_MSG
744 #elif NOTIMPLEMENTED_POLICY == 5
745 #define NOTIMPLEMENTED() \
746 do { \
747 static bool logged_once = false; \
748 LOG_IF(ERROR, !logged_once) << NOTIMPLEMENTED_MSG; \
749 logged_once = true; \
750 } while (0); \
751 EAT_STREAM_PARAMETERS
752 #endif
753
754 #endif // !USING_CHROMIUM_INCLUDES
755
756 #endif // CEF_INCLUDE_BASE_CEF_LOGGING_H_
757