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