• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2012 The Chromium Authors
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_ALLOCATOR_PARTITION_ALLOCATOR_SRC_PARTITION_ALLOC_PARTITION_ALLOC_BASE_LOGGING_H_
6 #define BASE_ALLOCATOR_PARTITION_ALLOCATOR_SRC_PARTITION_ALLOC_PARTITION_ALLOC_BASE_LOGGING_H_
7 
8 #include <stddef.h>
9 
10 #include <cassert>
11 #include <cstdint>
12 
13 #include "build/build_config.h"
14 #include "partition_alloc/partition_alloc_base/compiler_specific.h"
15 #include "partition_alloc/partition_alloc_base/component_export.h"
16 #include "partition_alloc/partition_alloc_base/debug/debugging_buildflags.h"
17 #include "partition_alloc/partition_alloc_base/log_message.h"
18 
19 // TODO(1151236): Need to update the description, because logging for PA
20 // standalone library was minimized.
21 //
22 // Optional message capabilities
23 // -----------------------------
24 // Assertion failed messages and fatal errors are displayed in a dialog box
25 // before the application exits. However, running this UI creates a message
26 // loop, which causes application messages to be processed and potentially
27 // dispatched to existing application windows. Since the application is in a
28 // bad state when this assertion dialog is displayed, these messages may not
29 // get processed and hang the dialog, or the application might go crazy.
30 //
31 // Therefore, it can be beneficial to display the error dialog in a separate
32 // process from the main application. When the logging system needs to display
33 // a fatal error dialog box, it will look for a program called
34 // "DebugMessage.exe" in the same directory as the application executable. It
35 // will run this application with the message as the command line, and will
36 // not include the name of the application as is traditional for easier
37 // parsing.
38 //
39 // The code for DebugMessage.exe is only one line. In WinMain, do:
40 //   MessageBox(NULL, GetCommandLineW(), L"Fatal Error", 0);
41 //
42 // If DebugMessage.exe is not found, the logging code will use a normal
43 // MessageBox, potentially causing the problems discussed above.
44 
45 // Instructions
46 // ------------
47 //
48 // Make a bunch of macros for logging.  The way to log things is to stream
49 // things to PA_LOG(<a particular severity level>).  E.g.,
50 //
51 //   PA_LOG(INFO) << "Found " << num_cookies << " cookies";
52 //
53 // You can also do conditional logging:
54 //
55 //   PA_LOG_IF(INFO, num_cookies > 10) << "Got lots of cookies";
56 //
57 // The CHECK(condition) macro is active in both debug and release builds and
58 // effectively performs a PA_LOG(FATAL) which terminates the process and
59 // generates a crashdump unless a debugger is attached.
60 //
61 // There are also "debug mode" logging macros like the ones above:
62 //
63 //   PA_DLOG(INFO) << "Found cookies";
64 //
65 //   PA_DLOG_IF(INFO, num_cookies > 10) << "Got lots of cookies";
66 //
67 // All "debug mode" logging is compiled away to nothing for non-debug mode
68 // compiles.  PA_LOG_IF and development flags also work well together
69 // because the code can be compiled away sometimes.
70 //
71 // We also have
72 //
73 //   PA_LOG_ASSERT(assertion);
74 //   PA_DLOG_ASSERT(assertion);
75 //
76 // which is syntactic sugar for PA_{,D}LOG_IF(FATAL, assert fails) << assertion;
77 //
78 // There are "verbose level" logging macros.  They look like
79 //
80 //   PA_VLOG(1) << "I'm printed when you run the program with --v=1 or more";
81 //   PA_VLOG(2) << "I'm printed when you run the program with --v=2 or more";
82 //
83 // These always log at the INFO log level (when they log at all).
84 //
85 // There's also PA_VLOG_IS_ON(n) "verbose level" condition macro. To be used as
86 //
87 //   if (PA_VLOG_IS_ON(2)) {
88 //     // do some logging preparation and logging
89 //     // that can't be accomplished with just PA_VLOG(2) << ...;
90 //   }
91 //
92 // There is also a PA_VLOG_IF "verbose level" condition macro for sample
93 // cases, when some extra computation and preparation for logs is not
94 // needed.
95 //
96 //   PA_VLOG_IF(1, (size > 1024))
97 //      << "I'm printed when size is more than 1024 and when you run the "
98 //         "program with --v=1 or more";
99 //
100 // We also override the standard 'assert' to use 'PA_DLOG_ASSERT'.
101 //
102 // Lastly, there is:
103 //
104 //   PA_PLOG(ERROR) << "Couldn't do foo";
105 //   PA_DPLOG(ERROR) << "Couldn't do foo";
106 //   PA_PLOG_IF(ERROR, cond) << "Couldn't do foo";
107 //   PA_DPLOG_IF(ERROR, cond) << "Couldn't do foo";
108 //   PA_PCHECK(condition) << "Couldn't do foo";
109 //   PA_DPCHECK(condition) << "Couldn't do foo";
110 //
111 // which append the last system error to the message in string form (taken from
112 // GetLastError() on Windows and errno on POSIX).
113 //
114 // The supported severity levels for macros that allow you to specify one
115 // are (in increasing order of severity) INFO, WARNING, ERROR, and FATAL.
116 //
117 // Very important: logging a message at the FATAL severity level causes
118 // the program to terminate (after the message is logged).
119 //
120 // There is the special severity of DFATAL, which logs FATAL in DCHECK-enabled
121 // builds, ERROR in normal mode.
122 //
123 // Output is formatted as per the following example:
124 // [VERBOSE1:drm_device_handle.cc(90)] Succeeded
125 // authenticating /dev/dri/card0 in 0 ms with 1 attempt(s)
126 //
127 // The colon separated fields inside the brackets are as follows:
128 // 1. The log level
129 // 2. The filename and line number where the log was instantiated
130 //
131 // Additional logging-related information can be found here:
132 // https://chromium.googlesource.com/chromium/src/+/main/docs/linux/debugging.md#Logging
133 
134 namespace partition_alloc::internal::logging {
135 
136 // Sets the log level. Anything at or above this level will be written to the
137 // log file/displayed to the user (if applicable). Anything below this level
138 // will be silently ignored. The log level defaults to 0 (everything is logged
139 // up to level INFO) if this function is not called.
140 // Note that log messages for VLOG(x) are logged at level -x, so setting
141 // the min log level to negative values enables verbose logging.
142 PA_COMPONENT_EXPORT(PARTITION_ALLOC_BASE) void SetMinLogLevel(int level);
143 
144 // Gets the current log level.
145 PA_COMPONENT_EXPORT(PARTITION_ALLOC_BASE) int GetMinLogLevel();
146 
147 // Used by PA_LOG_IS_ON to lazy-evaluate stream arguments.
148 PA_COMPONENT_EXPORT(PARTITION_ALLOC_BASE)
149 bool ShouldCreateLogMessage(int severity);
150 
151 // Gets the PA_VLOG default verbosity level.
152 PA_COMPONENT_EXPORT(PARTITION_ALLOC_BASE) int GetVlogVerbosity();
153 
154 // A few definitions of macros that don't generate much code. These are used
155 // by PA_LOG() and LOG_IF, etc. Since these are used all over our code, it's
156 // better to have compact code for these operations.
157 #define PA_COMPACT_GOOGLE_LOG_EX_INFO(ClassName)   \
158   ::partition_alloc::internal::logging::ClassName( \
159       __FILE__, __LINE__, ::partition_alloc::internal::logging::LOGGING_INFO)
160 #define PA_COMPACT_GOOGLE_PLOG_EX_INFO(ClassName, error_code)                 \
161   ::partition_alloc::internal::logging::ClassName(                            \
162       __FILE__, __LINE__, ::partition_alloc::internal::logging::LOGGING_INFO, \
163       error_code)
164 #define PA_COMPACT_GOOGLE_LOG_EX_WARNING(ClassName) \
165   ::partition_alloc::internal::logging::ClassName(  \
166       __FILE__, __LINE__,                           \
167       ::partition_alloc::internal::logging::LOGGING_WARNING)
168 #define PA_COMPACT_GOOGLE_PLOG_EX_WARNING(ClassName, error_code) \
169   ::partition_alloc::internal::logging::ClassName(               \
170       __FILE__, __LINE__,                                        \
171       ::partition_alloc::internal::logging::LOGGING_WARNING)
172 #define PA_COMPACT_GOOGLE_LOG_EX_ERROR(ClassName)  \
173   ::partition_alloc::internal::logging::ClassName( \
174       __FILE__, __LINE__, ::partition_alloc::internal::logging::LOGGING_ERROR)
175 #define PA_COMPACT_GOOGLE_PLOG_EX_ERROR(ClassName, error_code)                 \
176   ::partition_alloc::internal::logging::ClassName(                             \
177       __FILE__, __LINE__, ::partition_alloc::internal::logging::LOGGING_ERROR, \
178       error_code)
179 #define PA_COMPACT_GOOGLE_LOG_EX_FATAL(ClassName)  \
180   ::partition_alloc::internal::logging::ClassName( \
181       __FILE__, __LINE__, ::partition_alloc::internal::logging::LOGGING_FATAL)
182 #define PA_COMPACT_GOOGLE_PLOG_EX_FATAL(ClassName, error_code)                 \
183   ::partition_alloc::internal::logging::ClassName(                             \
184       __FILE__, __LINE__, ::partition_alloc::internal::logging::LOGGING_FATAL, \
185       error_code)
186 #define PA_COMPACT_GOOGLE_LOG_EX_DFATAL(ClassName) \
187   ::partition_alloc::internal::logging::ClassName( \
188       __FILE__, __LINE__,                          \
189       ::partition_alloc::internal::logging::LOGGING_DFATAL)
190 #define PA_COMPACT_GOOGLE_PLOG_EX_DFATAL(ClassName, error_code) \
191   ::partition_alloc::internal::logging::ClassName(              \
192       __FILE__, __LINE__,                                       \
193       ::partition_alloc::internal::logging::LOGGING_DFATAL, error_code)
194 #define PA_COMPACT_GOOGLE_LOG_EX_DCHECK(ClassName) \
195   ::partition_alloc::internal::logging::ClassName( \
196       __FILE__, __LINE__,                          \
197       ::partition_alloc::internal::logging::LOGGING_DCHECK)
198 #define PA_COMPACT_GOOGLE_PLOG_EX_DCHECK(ClassName, error_code) \
199   ::partition_alloc::internal::logging::ClassName(              \
200       __FILE__, __LINE__,                                       \
201       ::partition_alloc::internal::logging::LOGGING_DCHECK, error_code)
202 
203 #define PA_COMPACT_GOOGLE_LOG_INFO PA_COMPACT_GOOGLE_LOG_EX_INFO(LogMessage)
204 #define PA_COMPACT_GOOGLE_LOG_WARNING \
205   PA_COMPACT_GOOGLE_LOG_EX_WARNING(LogMessage)
206 #define PA_COMPACT_GOOGLE_LOG_ERROR PA_COMPACT_GOOGLE_LOG_EX_ERROR(LogMessage)
207 #define PA_COMPACT_GOOGLE_LOG_FATAL PA_COMPACT_GOOGLE_LOG_EX_FATAL(LogMessage)
208 #define PA_COMPACT_GOOGLE_LOG_DFATAL PA_COMPACT_GOOGLE_LOG_EX_DFATAL(LogMessage)
209 #define PA_COMPACT_GOOGLE_LOG_DCHECK PA_COMPACT_GOOGLE_LOG_EX_DCHECK(LogMessage)
210 
211 #if BUILDFLAG(IS_WIN)
212 // wingdi.h defines ERROR to be 0. When we call PA_LOG(ERROR), it gets
213 // substituted with 0, and it expands to PA_COMPACT_GOOGLE_LOG_0. To allow us
214 // to keep using this syntax, we define this macro to do the same thing
215 // as PA_COMPACT_GOOGLE_LOG_ERROR, and also define ERROR the same way that
216 // the Windows SDK does for consistency.
217 #define PA_ERROR 0
218 #define PA_COMPACT_GOOGLE_LOG_EX_0(ClassName) \
219   PA_COMPACT_GOOGLE_LOG_EX_ERROR(ClassName)
220 #define PA_COMPACT_GOOGLE_LOG_0 PA_COMPACT_GOOGLE_LOG_ERROR
221 // Needed for LOG_IS_ON(ERROR).
222 constexpr LogSeverity LOGGING_0 = LOGGING_ERROR;
223 #endif
224 
225 // As special cases, we can assume that LOG_IS_ON(FATAL) always holds. Also,
226 // LOG_IS_ON(DFATAL) always holds in debug mode. In particular, CHECK()s will
227 // always fire if they fail.
228 #define PA_LOG_IS_ON(severity)                                   \
229   (::partition_alloc::internal::logging::ShouldCreateLogMessage( \
230       ::partition_alloc::internal::logging::LOGGING_##severity))
231 
232 // We don't do any caching tricks with VLOG_IS_ON() like the
233 // google-glog version since it increases binary size.  This means
234 // that using the v-logging functions in conjunction with --vmodule
235 // may be slow.
236 #define PA_VLOG_IS_ON(verboselevel) \
237   ((verboselevel) <= ::partition_alloc::internal::logging::GetVlogVerbosity())
238 
239 // Helper macro which avoids evaluating the arguments to a stream if
240 // the condition doesn't hold. Condition is evaluated once and only once.
241 #define PA_LAZY_STREAM(stream, condition) \
242   !(condition)                            \
243       ? (void)0                           \
244       : ::partition_alloc::internal::logging::LogMessageVoidify() & (stream)
245 
246 // We use the preprocessor's merging operator, "##", so that, e.g.,
247 // PA_LOG(INFO) becomes the token PA_COMPACT_GOOGLE_LOG_INFO.  There's some
248 // funny subtle difference between ostream member streaming functions (e.g.,
249 // ostream::operator<<(int) and ostream non-member streaming functions
250 // (e.g., ::operator<<(ostream&, string&): it turns out that it's
251 // impossible to stream something like a string directly to an unnamed
252 // ostream. We employ a neat hack by calling the stream() member
253 // function of LogMessage which seems to avoid the problem.
254 #define PA_LOG_STREAM(severity) PA_COMPACT_GOOGLE_LOG_##severity.stream()
255 
256 #define PA_LOG(severity) \
257   PA_LAZY_STREAM(PA_LOG_STREAM(severity), PA_LOG_IS_ON(severity))
258 #define PA_LOG_IF(severity, condition) \
259   PA_LAZY_STREAM(PA_LOG_STREAM(severity), PA_LOG_IS_ON(severity) && (condition))
260 
261 // The VLOG macros log with negative verbosities.
262 #define PA_VLOG_STREAM(verbose_level)                                  \
263   ::partition_alloc::internal::logging::LogMessage(__FILE__, __LINE__, \
264                                                    -(verbose_level))   \
265       .stream()
266 
267 #define PA_VLOG(verbose_level) \
268   PA_LAZY_STREAM(PA_VLOG_STREAM(verbose_level), PA_VLOG_IS_ON(verbose_level))
269 
270 #define PA_VLOG_IF(verbose_level, condition)    \
271   PA_LAZY_STREAM(PA_VLOG_STREAM(verbose_level), \
272                  PA_VLOG_IS_ON(verbose_level) && (condition))
273 
274 #if BUILDFLAG(IS_WIN)
275 #define PA_VPLOG_STREAM(verbose_level)                                \
276   ::partition_alloc::internal::logging::Win32ErrorLogMessage(         \
277       __FILE__, __LINE__, -(verbose_level),                           \
278       ::partition_alloc::internal::logging::GetLastSystemErrorCode()) \
279       .stream()
280 #elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
281 #define PA_VPLOG_STREAM(verbose_level)                                \
282   ::partition_alloc::internal::logging::ErrnoLogMessage(              \
283       __FILE__, __LINE__, -(verbose_level),                           \
284       ::partition_alloc::internal::logging::GetLastSystemErrorCode()) \
285       .stream()
286 #endif
287 
288 #define PA_VPLOG(verbose_level) \
289   PA_LAZY_STREAM(PA_VPLOG_STREAM(verbose_level), PA_VLOG_IS_ON(verbose_level))
290 
291 #define PA_VPLOG_IF(verbose_level, condition)    \
292   PA_LAZY_STREAM(PA_VPLOG_STREAM(verbose_level), \
293                  PA_VLOG_IS_ON(verbose_level) && (condition))
294 
295 // TODO(akalin): Add more VLOG variants, e.g. VPLOG.
296 
297 #define PA_LOG_ASSERT(condition)                          \
298   PA_LOG_IF(FATAL, !(PA_ANALYZER_ASSUME_TRUE(condition))) \
299       << "Assert failed: " #condition ". "
300 
301 #if BUILDFLAG(IS_WIN)
302 #define PA_PLOG_STREAM(severity)                                      \
303   PA_COMPACT_GOOGLE_PLOG_EX_##severity(                               \
304       Win32ErrorLogMessage,                                           \
305       ::partition_alloc::internal::logging::GetLastSystemErrorCode()) \
306       .stream()
307 #elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
308 #define PA_PLOG_STREAM(severity)                                      \
309   PA_COMPACT_GOOGLE_PLOG_EX_##severity(                               \
310       ErrnoLogMessage,                                                \
311       ::partition_alloc::internal::logging::GetLastSystemErrorCode()) \
312       .stream()
313 #endif
314 
315 #define PA_PLOG(severity) \
316   PA_LAZY_STREAM(PA_PLOG_STREAM(severity), PA_LOG_IS_ON(severity))
317 
318 #define PA_PLOG_IF(severity, condition)    \
319   PA_LAZY_STREAM(PA_PLOG_STREAM(severity), \
320                  PA_LOG_IS_ON(severity) && (condition))
321 
322 // Note that g_swallow_stream is used instead of an arbitrary PA_LOG() stream to
323 // avoid the creation of an object with a non-trivial destructor (LogMessage).
324 // On MSVC x86 (checked on 2015 Update 3), this causes a few additional
325 // pointless instructions to be emitted even at full optimization level, even
326 // though the : arm of the ternary operator is clearly never executed. Using a
327 // simpler object to be &'d with Voidify() avoids these extra instructions.
328 // Using a simpler POD object with a templated operator<< also works to avoid
329 // these instructions. However, this causes warnings on statically defined
330 // implementations of operator<<(std::ostream, ...) in some .cc files, because
331 // they become defined-but-unreferenced functions. A reinterpret_cast of 0 to an
332 // ostream* also is not suitable, because some compilers warn of undefined
333 // behavior.
334 #define PA_EAT_STREAM_PARAMETERS                                     \
335   true ? (void)0                                                     \
336        : ::partition_alloc::internal::logging::LogMessageVoidify() & \
337              (*::partition_alloc::internal::logging::g_swallow_stream)
338 
339 // Definitions for DLOG et al.
340 
341 #if BUILDFLAG(PA_DCHECK_IS_ON)
342 
343 #define PA_DLOG_IS_ON(severity) PA_LOG_IS_ON(severity)
344 #define PA_DLOG_IF(severity, condition) PA_LOG_IF(severity, condition)
345 #define PA_DLOG_ASSERT(condition) PA_LOG_ASSERT(condition)
346 #define PA_DPLOG_IF(severity, condition) PA_PLOG_IF(severity, condition)
347 #define PA_DVLOG_IF(verboselevel, condition) PA_VLOG_IF(verboselevel, condition)
348 #define PA_DVPLOG_IF(verboselevel, condition) \
349   PA_VPLOG_IF(verboselevel, condition)
350 
351 #else  // BUILDFLAG(PA_DCHECK_IS_ON)
352 
353 // If !BUILDFLAG(PA_DCHECK_IS_ON), we want to avoid emitting any references to
354 // |condition| (which may reference a variable defined only if
355 // BUILDFLAG(PA_DCHECK_IS_ON)). Contrast this with DCHECK et al., which has
356 // different behavior.
357 
358 #define PA_DLOG_IS_ON(severity) false
359 #define PA_DLOG_IF(severity, condition) PA_EAT_STREAM_PARAMETERS
360 #define PA_DLOG_ASSERT(condition) PA_EAT_STREAM_PARAMETERS
361 #define PA_DPLOG_IF(severity, condition) PA_EAT_STREAM_PARAMETERS
362 #define PA_DVLOG_IF(verboselevel, condition) PA_EAT_STREAM_PARAMETERS
363 #define PA_DVPLOG_IF(verboselevel, condition) PA_EAT_STREAM_PARAMETERS
364 
365 #endif  // BUILDFLAG(PA_DCHECK_IS_ON)
366 
367 #define PA_DLOG(severity) \
368   PA_LAZY_STREAM(PA_LOG_STREAM(severity), PA_DLOG_IS_ON(severity))
369 
370 #define PA_DPLOG(severity) \
371   PA_LAZY_STREAM(PA_PLOG_STREAM(severity), PA_DLOG_IS_ON(severity))
372 
373 #define PA_DVLOG(verboselevel) PA_DVLOG_IF(verboselevel, true)
374 
375 #define PA_DVPLOG(verboselevel) PA_DVPLOG_IF(verboselevel, true)
376 
377 // Definitions for DCHECK et al.
378 
379 #if BUILDFLAG(PA_DCHECK_IS_CONFIGURABLE)
380 PA_COMPONENT_EXPORT(PARTITION_ALLOC_BASE) extern LogSeverity LOGGING_DCHECK;
381 #else
382 constexpr LogSeverity LOGGING_DCHECK = LOGGING_FATAL;
383 #endif  // BUILDFLAG(PA_DCHECK_IS_CONFIGURABLE)
384 
385 // Redefine the standard assert to use our nice log files
386 #undef assert
387 #define assert(x) PA_DLOG_ASSERT(x)
388 
389 // Async signal safe logging mechanism.
390 PA_COMPONENT_EXPORT(PARTITION_ALLOC_BASE)
391 void RawLog(int level, const char* message);
392 
393 #define PA_RAW_LOG(level, message)              \
394   ::partition_alloc::internal::logging::RawLog( \
395       ::partition_alloc::internal::logging::LOGGING_##level, message)
396 
397 }  // namespace partition_alloc::internal::logging
398 
399 #endif  // BASE_ALLOCATOR_PARTITION_ALLOCATOR_SRC_PARTITION_ALLOC_PARTITION_ALLOC_BASE_LOGGING_H_
400