• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/internal/cef_logging_internal.h"
168 
169 namespace cef {
170 namespace logging {
171 
172 // Gets the current log level.
GetMinLogLevel()173 inline int GetMinLogLevel() {
174   return cef_get_min_log_level();
175 }
176 
177 // Gets the current vlog level for the given file (usually taken from
178 // __FILE__). Note that |N| is the size *with* the null terminator.
179 template <size_t N>
GetVlogLevel(const char (& file)[N])180 int GetVlogLevel(const char (&file)[N]) {
181   return cef_get_vlog_level(file, N);
182 }
183 
184 typedef int LogSeverity;
185 const LogSeverity LOG_VERBOSE = -1;  // This is level 1 verbosity
186 // Note: the log severities are used to index into the array of names,
187 // see log_severity_names.
188 const LogSeverity LOG_INFO = 0;
189 const LogSeverity LOG_WARNING = 1;
190 const LogSeverity LOG_ERROR = 2;
191 const LogSeverity LOG_FATAL = 3;
192 const LogSeverity LOG_NUM_SEVERITIES = 4;
193 
194 // LOG_DFATAL is LOG_FATAL in debug mode, ERROR in normal mode
195 #ifdef NDEBUG
196 const LogSeverity LOG_DFATAL = LOG_ERROR;
197 #else
198 const LogSeverity LOG_DFATAL = LOG_FATAL;
199 #endif
200 
201 // A few definitions of macros that don't generate much code. These are used
202 // by LOG() and LOG_IF, etc. Since these are used all over our code, it's
203 // better to have compact code for these operations.
204 #define COMPACT_GOOGLE_LOG_EX_INFO(ClassName, ...)                        \
205   ::cef::logging::ClassName(__FILE__, __LINE__, ::cef::logging::LOG_INFO, \
206                             ##__VA_ARGS__)
207 #define COMPACT_GOOGLE_LOG_EX_WARNING(ClassName, ...)                        \
208   ::cef::logging::ClassName(__FILE__, __LINE__, ::cef::logging::LOG_WARNING, \
209                             ##__VA_ARGS__)
210 #define COMPACT_GOOGLE_LOG_EX_ERROR(ClassName, ...)                        \
211   ::cef::logging::ClassName(__FILE__, __LINE__, ::cef::logging::LOG_ERROR, \
212                             ##__VA_ARGS__)
213 #define COMPACT_GOOGLE_LOG_EX_FATAL(ClassName, ...)                        \
214   ::cef::logging::ClassName(__FILE__, __LINE__, ::cef::logging::LOG_FATAL, \
215                             ##__VA_ARGS__)
216 #define COMPACT_GOOGLE_LOG_EX_DFATAL(ClassName, ...)                        \
217   ::cef::logging::ClassName(__FILE__, __LINE__, ::cef::logging::LOG_DFATAL, \
218                             ##__VA_ARGS__)
219 
220 #define COMPACT_GOOGLE_LOG_INFO COMPACT_GOOGLE_LOG_EX_INFO(LogMessage)
221 #define COMPACT_GOOGLE_LOG_WARNING COMPACT_GOOGLE_LOG_EX_WARNING(LogMessage)
222 #define COMPACT_GOOGLE_LOG_ERROR COMPACT_GOOGLE_LOG_EX_ERROR(LogMessage)
223 #define COMPACT_GOOGLE_LOG_FATAL COMPACT_GOOGLE_LOG_EX_FATAL(LogMessage)
224 #define COMPACT_GOOGLE_LOG_DFATAL COMPACT_GOOGLE_LOG_EX_DFATAL(LogMessage)
225 
226 #if defined(OS_WIN)
227 // wingdi.h defines ERROR to be 0. When we call LOG(ERROR), it gets
228 // substituted with 0, and it expands to COMPACT_GOOGLE_LOG_0. To allow us
229 // to keep using this syntax, we define this macro to do the same thing
230 // as COMPACT_GOOGLE_LOG_ERROR, and also define ERROR the same way that
231 // the Windows SDK does for consistency.
232 #define ERROR 0
233 #define COMPACT_GOOGLE_LOG_EX_0(ClassName, ...) \
234   COMPACT_GOOGLE_LOG_EX_ERROR(ClassName, ##__VA_ARGS__)
235 #define COMPACT_GOOGLE_LOG_0 COMPACT_GOOGLE_LOG_ERROR
236 // Needed for LOG_IS_ON(ERROR).
237 const LogSeverity LOG_0 = LOG_ERROR;
238 #endif
239 
240 // As special cases, we can assume that LOG_IS_ON(FATAL) always holds. Also,
241 // LOG_IS_ON(DFATAL) always holds in debug mode. In particular, CHECK()s will
242 // always fire if they fail.
243 #define LOG_IS_ON(severity) \
244   ((::cef::logging::LOG_##severity) >= ::cef::logging::GetMinLogLevel())
245 
246 // We can't do any caching tricks with VLOG_IS_ON() like the
247 // google-glog version since it requires GCC extensions.  This means
248 // that using the v-logging functions in conjunction with --vmodule
249 // may be slow.
250 #define VLOG_IS_ON(verboselevel) \
251   ((verboselevel) <= ::cef::logging::GetVlogLevel(__FILE__))
252 
253 // Helper macro which avoids evaluating the arguments to a stream if
254 // the condition doesn't hold.
255 #define LAZY_STREAM(stream, condition) \
256   !(condition) ? (void)0 : ::cef::logging::LogMessageVoidify() & (stream)
257 
258 // We use the preprocessor's merging operator, "##", so that, e.g.,
259 // LOG(INFO) becomes the token COMPACT_GOOGLE_LOG_INFO.  There's some funny
260 // subtle difference between ostream member streaming functions (e.g.,
261 // ostream::operator<<(int) and ostream non-member streaming functions
262 // (e.g., ::operator<<(ostream&, string&): it turns out that it's
263 // impossible to stream something like a string directly to an unnamed
264 // ostream. We employ a neat hack by calling the stream() member
265 // function of LogMessage which seems to avoid the problem.
266 #define LOG_STREAM(severity) COMPACT_GOOGLE_LOG_##severity.stream()
267 
268 #define LOG(severity) LAZY_STREAM(LOG_STREAM(severity), LOG_IS_ON(severity))
269 #define LOG_IF(severity, condition) \
270   LAZY_STREAM(LOG_STREAM(severity), LOG_IS_ON(severity) && (condition))
271 
272 #define SYSLOG(severity) LOG(severity)
273 #define SYSLOG_IF(severity, condition) LOG_IF(severity, condition)
274 
275 // The VLOG macros log with negative verbosities.
276 #define VLOG_STREAM(verbose_level) \
277   cef::logging::LogMessage(__FILE__, __LINE__, -verbose_level).stream()
278 
279 #define VLOG(verbose_level) \
280   LAZY_STREAM(VLOG_STREAM(verbose_level), VLOG_IS_ON(verbose_level))
281 
282 #define VLOG_IF(verbose_level, condition) \
283   LAZY_STREAM(VLOG_STREAM(verbose_level), \
284               VLOG_IS_ON(verbose_level) && (condition))
285 
286 #if defined(OS_WIN)
287 #define VPLOG_STREAM(verbose_level)                                            \
288   cef::logging::Win32ErrorLogMessage(__FILE__, __LINE__, -verbose_level,       \
289                                      ::cef::logging::GetLastSystemErrorCode()) \
290       .stream()
291 #elif defined(OS_POSIX)
292 #define VPLOG_STREAM(verbose_level)                                       \
293   cef::logging::ErrnoLogMessage(__FILE__, __LINE__, -verbose_level,       \
294                                 ::cef::logging::GetLastSystemErrorCode()) \
295       .stream()
296 #endif
297 
298 #define VPLOG(verbose_level) \
299   LAZY_STREAM(VPLOG_STREAM(verbose_level), VLOG_IS_ON(verbose_level))
300 
301 #define VPLOG_IF(verbose_level, condition) \
302   LAZY_STREAM(VPLOG_STREAM(verbose_level), \
303               VLOG_IS_ON(verbose_level) && (condition))
304 
305 // TODO(akalin): Add more VLOG variants, e.g. VPLOG.
306 
307 #define LOG_ASSERT(condition) \
308   LOG_IF(FATAL, !(condition)) << "Assert failed: " #condition ". "
309 #define SYSLOG_ASSERT(condition) \
310   SYSLOG_IF(FATAL, !(condition)) << "Assert failed: " #condition ". "
311 
312 #if defined(OS_WIN)
313 #define PLOG_STREAM(severity)                                                \
314   COMPACT_GOOGLE_LOG_EX_##severity(Win32ErrorLogMessage,                     \
315                                    ::cef::logging::GetLastSystemErrorCode()) \
316       .stream()
317 #elif defined(OS_POSIX)
318 #define PLOG_STREAM(severity)                                                \
319   COMPACT_GOOGLE_LOG_EX_##severity(ErrnoLogMessage,                          \
320                                    ::cef::logging::GetLastSystemErrorCode()) \
321       .stream()
322 #endif
323 
324 #define PLOG(severity) LAZY_STREAM(PLOG_STREAM(severity), LOG_IS_ON(severity))
325 
326 #define PLOG_IF(severity, condition) \
327   LAZY_STREAM(PLOG_STREAM(severity), LOG_IS_ON(severity) && (condition))
328 
329 // The actual stream used isn't important.
330 #define EAT_STREAM_PARAMETERS \
331   true ? (void)0 : ::cef::logging::LogMessageVoidify() & LOG_STREAM(FATAL)
332 
333 // CHECK dies with a fatal error if condition is not true.  It is *not*
334 // controlled by NDEBUG, so the check will be executed regardless of
335 // compilation mode.
336 //
337 // We make sure CHECK et al. always evaluates their arguments, as
338 // doing CHECK(FunctionWithSideEffect()) is a common idiom.
339 
340 #define CHECK(condition)                       \
341   LAZY_STREAM(LOG_STREAM(FATAL), !(condition)) \
342       << "Check failed: " #condition ". "
343 
344 #define PCHECK(condition)                       \
345   LAZY_STREAM(PLOG_STREAM(FATAL), !(condition)) \
346       << "Check failed: " #condition ". "
347 
348 // Helper macro for binary operators.
349 // Don't use this macro directly in your code, use CHECK_EQ et al below.
350 //
351 // TODO(akalin): Rewrite this so that constructs like if (...)
352 // CHECK_EQ(...) else { ... } work properly.
353 #define CHECK_OP(name, op, val1, val2)                        \
354   if (std::string* _result = cef::logging::Check##name##Impl( \
355           (val1), (val2), #val1 " " #op " " #val2))           \
356   cef::logging::LogMessage(__FILE__, __LINE__, _result).stream()
357 
358 // Build the error message string.  This is separate from the "Impl"
359 // function template because it is not performance critical and so can
360 // be out of line, while the "Impl" code should be inline.  Caller
361 // takes ownership of the returned string.
362 template <class t1, class t2>
MakeCheckOpString(const t1 & v1,const t2 & v2,const char * names)363 std::string* MakeCheckOpString(const t1& v1, const t2& v2, const char* names) {
364   std::ostringstream ss;
365   ss << names << " (" << v1 << " vs. " << v2 << ")";
366   std::string* msg = new std::string(ss.str());
367   return msg;
368 }
369 
370 // MSVC doesn't like complex extern templates and DLLs.
371 #if !defined(COMPILER_MSVC)
372 // Commonly used instantiations of MakeCheckOpString<>. Explicitly instantiated
373 // in logging.cc.
374 extern template std::string* MakeCheckOpString<int, int>(const int&,
375                                                          const int&,
376                                                          const char* names);
377 extern template std::string* MakeCheckOpString<unsigned long, unsigned long>(
378     const unsigned long&,
379     const unsigned long&,
380     const char* names);
381 extern template std::string* MakeCheckOpString<unsigned long, unsigned int>(
382     const unsigned long&,
383     const unsigned int&,
384     const char* names);
385 extern template std::string* MakeCheckOpString<unsigned int, unsigned long>(
386     const unsigned int&,
387     const unsigned long&,
388     const char* names);
389 extern template std::string* MakeCheckOpString<std::string, std::string>(
390     const std::string&,
391     const std::string&,
392     const char* name);
393 #endif
394 
395 // Helper functions for CHECK_OP macro.
396 // The (int, int) specialization works around the issue that the compiler
397 // will not instantiate the template version of the function on values of
398 // unnamed enum type - see comment below.
399 #define DEFINE_CHECK_OP_IMPL(name, op)                                       \
400   template <class t1, class t2>                                              \
401   inline std::string* Check##name##Impl(const t1& v1, const t2& v2,          \
402                                         const char* names) {                 \
403     if (v1 op v2)                                                            \
404       return NULL;                                                           \
405     else                                                                     \
406       return MakeCheckOpString(v1, v2, names);                               \
407   }                                                                          \
408   inline std::string* Check##name##Impl(int v1, int v2, const char* names) { \
409     if (v1 op v2)                                                            \
410       return NULL;                                                           \
411     else                                                                     \
412       return MakeCheckOpString(v1, v2, names);                               \
413   }
414 DEFINE_CHECK_OP_IMPL(EQ, ==)
415 DEFINE_CHECK_OP_IMPL(NE, !=)
416 DEFINE_CHECK_OP_IMPL(LE, <=)
417 DEFINE_CHECK_OP_IMPL(LT, <)
418 DEFINE_CHECK_OP_IMPL(GE, >=)
419 DEFINE_CHECK_OP_IMPL(GT, >)
420 #undef DEFINE_CHECK_OP_IMPL
421 
422 #define CHECK_EQ(val1, val2) CHECK_OP(EQ, ==, val1, val2)
423 #define CHECK_NE(val1, val2) CHECK_OP(NE, !=, val1, val2)
424 #define CHECK_LE(val1, val2) CHECK_OP(LE, <=, val1, val2)
425 #define CHECK_LT(val1, val2) CHECK_OP(LT, <, val1, val2)
426 #define CHECK_GE(val1, val2) CHECK_OP(GE, >=, val1, val2)
427 #define CHECK_GT(val1, val2) CHECK_OP(GT, >, val1, val2)
428 
429 #if defined(NDEBUG)
430 #define ENABLE_DLOG 0
431 #else
432 #define ENABLE_DLOG 1
433 #endif
434 
435 #if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON)
436 #define DCHECK_IS_ON() 0
437 #else
438 #define DCHECK_IS_ON() 1
439 #endif
440 
441 // Definitions for DLOG et al.
442 
443 #if ENABLE_DLOG
444 
445 #define DLOG_IS_ON(severity) LOG_IS_ON(severity)
446 #define DLOG_IF(severity, condition) LOG_IF(severity, condition)
447 #define DLOG_ASSERT(condition) LOG_ASSERT(condition)
448 #define DPLOG_IF(severity, condition) PLOG_IF(severity, condition)
449 #define DVLOG_IF(verboselevel, condition) VLOG_IF(verboselevel, condition)
450 #define DVPLOG_IF(verboselevel, condition) VPLOG_IF(verboselevel, condition)
451 
452 #else  // ENABLE_DLOG
453 
454 // If ENABLE_DLOG is off, we want to avoid emitting any references to
455 // |condition| (which may reference a variable defined only if NDEBUG
456 // is not defined).  Contrast this with DCHECK et al., which has
457 // different behavior.
458 
459 #define DLOG_IS_ON(severity) false
460 #define DLOG_IF(severity, condition) EAT_STREAM_PARAMETERS
461 #define DLOG_ASSERT(condition) EAT_STREAM_PARAMETERS
462 #define DPLOG_IF(severity, condition) EAT_STREAM_PARAMETERS
463 #define DVLOG_IF(verboselevel, condition) EAT_STREAM_PARAMETERS
464 #define DVPLOG_IF(verboselevel, condition) EAT_STREAM_PARAMETERS
465 
466 #endif  // ENABLE_DLOG
467 
468 // DEBUG_MODE is for uses like
469 //   if (DEBUG_MODE) foo.CheckThatFoo();
470 // instead of
471 //   #ifndef NDEBUG
472 //     foo.CheckThatFoo();
473 //   #endif
474 //
475 // We tie its state to ENABLE_DLOG.
476 enum { DEBUG_MODE = ENABLE_DLOG };
477 
478 #undef ENABLE_DLOG
479 
480 #define DLOG(severity) LAZY_STREAM(LOG_STREAM(severity), DLOG_IS_ON(severity))
481 
482 #define DPLOG(severity) LAZY_STREAM(PLOG_STREAM(severity), DLOG_IS_ON(severity))
483 
484 #define DVLOG(verboselevel) DVLOG_IF(verboselevel, VLOG_IS_ON(verboselevel))
485 
486 #define DVPLOG(verboselevel) DVPLOG_IF(verboselevel, VLOG_IS_ON(verboselevel))
487 
488 // Definitions for DCHECK et al.
489 
490 #if DCHECK_IS_ON()
491 
492 #define COMPACT_GOOGLE_LOG_EX_DCHECK(ClassName, ...) \
493   COMPACT_GOOGLE_LOG_EX_FATAL(ClassName, ##__VA_ARGS__)
494 #define COMPACT_GOOGLE_LOG_DCHECK COMPACT_GOOGLE_LOG_FATAL
495 const LogSeverity LOG_DCHECK = LOG_FATAL;
496 
497 #else  // DCHECK_IS_ON()
498 
499 // These are just dummy values.
500 #define COMPACT_GOOGLE_LOG_EX_DCHECK(ClassName, ...) \
501   COMPACT_GOOGLE_LOG_EX_INFO(ClassName, ##__VA_ARGS__)
502 #define COMPACT_GOOGLE_LOG_DCHECK COMPACT_GOOGLE_LOG_INFO
503 const LogSeverity LOG_DCHECK = LOG_INFO;
504 
505 #endif  // DCHECK_IS_ON()
506 
507 // DCHECK et al. make sure to reference |condition| regardless of
508 // whether DCHECKs are enabled; this is so that we don't get unused
509 // variable warnings if the only use of a variable is in a DCHECK.
510 // This behavior is different from DLOG_IF et al.
511 
512 #define DCHECK(condition)                                         \
513   LAZY_STREAM(LOG_STREAM(DCHECK), DCHECK_IS_ON() && !(condition)) \
514       << "Check failed: " #condition ". "
515 
516 #define DPCHECK(condition)                                         \
517   LAZY_STREAM(PLOG_STREAM(DCHECK), DCHECK_IS_ON() && !(condition)) \
518       << "Check failed: " #condition ". "
519 
520 // Helper macro for binary operators.
521 // Don't use this macro directly in your code, use DCHECK_EQ et al below.
522 #define DCHECK_OP(name, op, val1, val2)                                    \
523   if (DCHECK_IS_ON())                                                      \
524     if (std::string* _result = cef::logging::Check##name##Impl(            \
525             (val1), (val2), #val1 " " #op " " #val2))                      \
526   cef::logging::LogMessage(__FILE__, __LINE__, ::cef::logging::LOG_DCHECK, \
527                            _result)                                        \
528       .stream()
529 
530 // Equality/Inequality checks - compare two values, and log a
531 // LOG_DCHECK message including the two values when the result is not
532 // as expected.  The values must have operator<<(ostream, ...)
533 // defined.
534 //
535 // You may append to the error message like so:
536 //   DCHECK_NE(1, 2) << ": The world must be ending!";
537 //
538 // We are very careful to ensure that each argument is evaluated exactly
539 // once, and that anything which is legal to pass as a function argument is
540 // legal here.  In particular, the arguments may be temporary expressions
541 // which will end up being destroyed at the end of the apparent statement,
542 // for example:
543 //   DCHECK_EQ(string("abc")[1], 'b');
544 //
545 // WARNING: These may not compile correctly if one of the arguments is a pointer
546 // and the other is NULL. To work around this, simply static_cast NULL to the
547 // type of the desired pointer.
548 
549 #define DCHECK_EQ(val1, val2) DCHECK_OP(EQ, ==, val1, val2)
550 #define DCHECK_NE(val1, val2) DCHECK_OP(NE, !=, val1, val2)
551 #define DCHECK_LE(val1, val2) DCHECK_OP(LE, <=, val1, val2)
552 #define DCHECK_LT(val1, val2) DCHECK_OP(LT, <, val1, val2)
553 #define DCHECK_GE(val1, val2) DCHECK_OP(GE, >=, val1, val2)
554 #define DCHECK_GT(val1, val2) DCHECK_OP(GT, >, val1, val2)
555 
556 #define NOTREACHED() DCHECK(false)
557 
558 // Redefine the standard assert to use our nice log files
559 #undef assert
560 #define assert(x) DLOG_ASSERT(x)
561 
562 // This class more or less represents a particular log message.  You
563 // create an instance of LogMessage and then stream stuff to it.
564 // When you finish streaming to it, ~LogMessage is called and the
565 // full message gets streamed to the appropriate destination.
566 //
567 // You shouldn't actually use LogMessage's constructor to log things,
568 // though.  You should use the LOG() macro (and variants thereof)
569 // above.
570 class LogMessage {
571  public:
572   // Used for LOG(severity).
573   LogMessage(const char* file, int line, LogSeverity severity);
574 
575   // Used for CHECK_EQ(), etc. Takes ownership of the given string.
576   // Implied severity = LOG_FATAL.
577   LogMessage(const char* file, int line, std::string* result);
578 
579   // Used for DCHECK_EQ(), etc. Takes ownership of the given string.
580   LogMessage(const char* file,
581              int line,
582              LogSeverity severity,
583              std::string* result);
584 
585   LogMessage(const LogMessage&) = delete;
586   LogMessage& operator=(const LogMessage&) = delete;
587 
588   ~LogMessage();
589 
stream()590   std::ostream& stream() { return stream_; }
591 
592  private:
593   LogSeverity severity_;
594   std::ostringstream stream_;
595 
596   // The file and line information passed in to the constructor.
597   const char* file_;
598   const int line_;
599 
600 #if defined(OS_WIN)
601   // Stores the current value of GetLastError in the constructor and restores
602   // it in the destructor by calling SetLastError.
603   // This is useful since the LogMessage class uses a lot of Win32 calls
604   // that will lose the value of GLE and the code that called the log function
605   // will have lost the thread error value when the log call returns.
606   class SaveLastError {
607    public:
608     SaveLastError();
609     ~SaveLastError();
610 
get_error()611     unsigned long get_error() const { return last_error_; }
612 
613    protected:
614     unsigned long last_error_;
615   };
616 
617   SaveLastError last_error_;
618 #endif
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   Win32ErrorLogMessage(const Win32ErrorLogMessage&) = delete;
659   Win32ErrorLogMessage& operator=(const Win32ErrorLogMessage&) = delete;
660 
661   // Appends the error message before destructing the encapsulated class.
662   ~Win32ErrorLogMessage();
663 
stream()664   std::ostream& stream() { return log_message_.stream(); }
665 
666  private:
667   SystemErrorCode err_;
668   LogMessage log_message_;
669 };
670 #elif defined(OS_POSIX)
671 // Appends a formatted system message of the errno type
672 class ErrnoLogMessage {
673  public:
674   ErrnoLogMessage(const char* file,
675                   int line,
676                   LogSeverity severity,
677                   SystemErrorCode err);
678 
679   ErrnoLogMessage(const ErrnoLogMessage&) = delete;
680   ErrnoLogMessage& operator=(const ErrnoLogMessage&) = delete;
681 
682   // Appends the error message before destructing the encapsulated class.
683   ~ErrnoLogMessage();
684 
stream()685   std::ostream& stream() { return log_message_.stream(); }
686 
687  private:
688   SystemErrorCode err_;
689   LogMessage log_message_;
690 };
691 #endif  // OS_WIN
692 
693 }  // namespace logging
694 }  // namespace cef
695 
696 // These functions are provided as a convenience for logging, which is where we
697 // use streams (it is against Google style to use streams in other places). It
698 // is designed to allow you to emit non-ASCII Unicode strings to the log file,
699 // which is normally ASCII. It is relatively slow, so try not to use it for
700 // common cases. Non-ASCII characters will be converted to UTF-8 by these
701 // operators.
702 std::ostream& operator<<(std::ostream& out, const wchar_t* wstr);
703 inline std::ostream& operator<<(std::ostream& out, const std::wstring& wstr) {
704   return out << wstr.c_str();
705 }
706 
707 // The NOTIMPLEMENTED() macro annotates codepaths which have
708 // not been implemented yet.
709 //
710 // The implementation of this macro is controlled by NOTIMPLEMENTED_POLICY:
711 //   0 -- Do nothing (stripped by compiler)
712 //   1 -- Warn at compile time
713 //   2 -- Fail at compile time
714 //   3 -- Fail at runtime (DCHECK)
715 //   4 -- [default] LOG(ERROR) at runtime
716 //   5 -- LOG(ERROR) at runtime, only once per call-site
717 
718 #ifndef NOTIMPLEMENTED_POLICY
719 #if defined(OS_ANDROID) && defined(OFFICIAL_BUILD)
720 #define NOTIMPLEMENTED_POLICY 0
721 #else
722 // Select default policy: LOG(ERROR)
723 #define NOTIMPLEMENTED_POLICY 4
724 #endif
725 #endif
726 
727 #if defined(COMPILER_GCC)
728 // On Linux, with GCC, we can use __PRETTY_FUNCTION__ to get the demangled name
729 // of the current function in the NOTIMPLEMENTED message.
730 #define NOTIMPLEMENTED_MSG "Not implemented reached in " << __PRETTY_FUNCTION__
731 #else
732 #define NOTIMPLEMENTED_MSG "NOT IMPLEMENTED"
733 #endif
734 
735 #if NOTIMPLEMENTED_POLICY == 0
736 #define NOTIMPLEMENTED() EAT_STREAM_PARAMETERS
737 #elif NOTIMPLEMENTED_POLICY == 1
738 // TODO, figure out how to generate a warning
739 #define NOTIMPLEMENTED() COMPILE_ASSERT(false, NOT_IMPLEMENTED)
740 #elif NOTIMPLEMENTED_POLICY == 2
741 #define NOTIMPLEMENTED() COMPILE_ASSERT(false, NOT_IMPLEMENTED)
742 #elif NOTIMPLEMENTED_POLICY == 3
743 #define NOTIMPLEMENTED() NOTREACHED()
744 #elif NOTIMPLEMENTED_POLICY == 4
745 #define NOTIMPLEMENTED() LOG(ERROR) << NOTIMPLEMENTED_MSG
746 #elif NOTIMPLEMENTED_POLICY == 5
747 #define NOTIMPLEMENTED()                               \
748   do {                                                 \
749     static bool logged_once = false;                   \
750     LOG_IF(ERROR, !logged_once) << NOTIMPLEMENTED_MSG; \
751     logged_once = true;                                \
752   } while (0);                                         \
753   EAT_STREAM_PARAMETERS
754 #endif
755 
756 #endif  // !USING_CHROMIUM_INCLUDES
757 
758 #endif  // CEF_INCLUDE_BASE_CEF_LOGGING_H_
759