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