blob: 20bc7f51f23b0fbf947437376be70abb510e2f57 [file] [log] [blame]
[email protected]63e66802012-01-18 21:21:091// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commitd7cae122008-07-26 21:49:384
[email protected]b16ef312008-08-19 18:36:235#include "base/logging.h"
[email protected]f6abeba2008-08-08 13:27:286
[email protected]b16ef312008-08-19 18:36:237#if defined(OS_WIN)
[email protected]e36ddc82009-12-08 04:22:508#include <io.h>
[email protected]f6abeba2008-08-08 13:27:289#include <windows.h>
ananta61762fb2015-09-18 01:00:0910#include "base/files/file_path.h"
11#include "base/files/file_util.h"
[email protected]f6abeba2008-08-08 13:27:2812typedef HANDLE FileHandle;
13typedef HANDLE MutexHandle;
[email protected]e36ddc82009-12-08 04:22:5014// Windows warns on using write(). It prefers _write().
15#define write(fd, buf, count) _write(fd, buf, static_cast<unsigned int>(count))
16// Windows doesn't define STDERR_FILENO. Define it here.
17#define STDERR_FILENO 2
[email protected]052f1b52008-11-06 21:43:0718#elif defined(OS_MACOSX)
mark4c7449c2015-11-10 19:53:4219#include <asl.h>
20#include <CoreFoundation/CoreFoundation.h>
[email protected]f6abeba2008-08-08 13:27:2821#include <mach/mach.h>
22#include <mach/mach_time.h>
23#include <mach-o/dyld.h>
[email protected]e43eddf12009-12-29 00:32:5224#elif defined(OS_POSIX)
[email protected]19ea84ca2010-11-12 08:37:0825#if defined(OS_NACL)
thestig75f87352014-12-03 21:42:2726#include <sys/time.h> // timespec doesn't seem to be in <time.h>
[email protected]19ea84ca2010-11-12 08:37:0827#else
[email protected]052f1b52008-11-06 21:43:0728#include <sys/syscall.h>
[email protected]19ea84ca2010-11-12 08:37:0829#endif
[email protected]052f1b52008-11-06 21:43:0730#include <time.h>
[email protected]614e9fa2008-08-11 22:52:5931#endif
32
33#if defined(OS_POSIX)
[email protected]d8617a62009-10-09 23:52:2034#include <errno.h>
mark4c7449c2015-11-10 19:53:4235#include <paths.h>
[email protected]166326c62010-08-05 15:50:2336#include <pthread.h>
[email protected]f6abeba2008-08-08 13:27:2837#include <stdio.h>
[email protected]eb62f7262013-03-30 14:29:0038#include <stdlib.h>
[email protected]f6abeba2008-08-08 13:27:2839#include <string.h>
mark4c7449c2015-11-10 19:53:4240#include <sys/stat.h>
[email protected]f6abeba2008-08-08 13:27:2841#include <unistd.h>
42#define MAX_PATH PATH_MAX
43typedef FILE* FileHandle;
44typedef pthread_mutex_t* MutexHandle;
45#endif
46
[email protected]1f88b5162011-04-01 00:02:2947#include <algorithm>
48#include <cstring>
initial.commitd7cae122008-07-26 21:49:3849#include <ctime>
50#include <iomanip>
[email protected]1f88b5162011-04-01 00:02:2951#include <ostream>
[email protected]c914d8a2014-04-23 01:11:0152#include <string>
[email protected]b16ef312008-08-19 18:36:2353
initial.commitd7cae122008-07-26 21:49:3854#include "base/base_switches.h"
55#include "base/command_line.h"
[email protected]eb4c4d032012-04-03 18:45:0556#include "base/debug/alias.h"
[email protected]58580352010-10-26 04:07:5057#include "base/debug/debugger.h"
58#include "base/debug/stack_trace.h"
[email protected]2025d002012-11-14 20:54:3559#include "base/posix/eintr_wrapper.h"
[email protected]eb62f7262013-03-30 14:29:0060#include "base/strings/string_piece.h"
[email protected]c914d8a2014-04-23 01:11:0161#include "base/strings/string_util.h"
62#include "base/strings/stringprintf.h"
mark4c7449c2015-11-10 19:53:4263#include "base/strings/sys_string_conversions.h"
[email protected]a4ea1f12013-06-07 18:37:0764#include "base/strings/utf_string_conversions.h"
[email protected]bc581a682011-01-01 23:16:2065#include "base/synchronization/lock_impl.h"
[email protected]63e66802012-01-18 21:21:0966#include "base/threading/platform_thread.h"
[email protected]99b7c57f2010-09-29 19:26:3667#include "base/vlog.h"
[email protected]53c7ce42010-12-14 16:20:0468#if defined(OS_POSIX)
brettw6ee6fd62015-06-09 18:05:2469#include "base/posix/safe_strerror.h"
[email protected]53c7ce42010-12-14 16:20:0470#endif
[email protected]52a261f2009-03-03 15:01:1271
[email protected]3132e35c2011-07-07 20:46:5072#if defined(OS_ANDROID)
73#include <android/log.h>
74#endif
75
initial.commitd7cae122008-07-26 21:49:3876namespace logging {
77
[email protected]064aa162011-12-03 00:30:0878namespace {
79
thestig3e4787d2015-05-19 19:31:5280VlogInfo* g_vlog_info = nullptr;
81VlogInfo* g_vlog_info_prev = nullptr;
initial.commitd7cae122008-07-26 21:49:3882
83const char* const log_severity_names[LOG_NUM_SEVERITIES] = {
[email protected]f2c05492014-06-17 12:04:2384 "INFO", "WARNING", "ERROR", "FATAL" };
initial.commitd7cae122008-07-26 21:49:3885
thestig75f87352014-12-03 21:42:2786const char* log_severity_name(int severity) {
[email protected]80f360a2014-01-23 01:36:1987 if (severity >= 0 && severity < LOG_NUM_SEVERITIES)
88 return log_severity_names[severity];
89 return "UNKNOWN";
90}
91
thestig3e4787d2015-05-19 19:31:5292int g_min_log_level = 0;
[email protected]1d8c2702008-08-19 23:39:3293
thestig3e4787d2015-05-19 19:31:5294LoggingDestination g_logging_destination = LOG_DEFAULT;
initial.commitd7cae122008-07-26 21:49:3895
[email protected]a33c9892008-08-25 20:10:3196// For LOG_ERROR and above, always print to stderr.
97const int kAlwaysPrintErrorLevel = LOG_ERROR;
98
[email protected]614e9fa2008-08-11 22:52:5999// Which log file to use? This is initialized by InitLogging or
initial.commitd7cae122008-07-26 21:49:38100// will be lazily initialized to the default value when it is
101// first needed.
[email protected]f6abeba2008-08-08 13:27:28102#if defined(OS_WIN)
[email protected]614e9fa2008-08-11 22:52:59103typedef std::wstring PathString;
[email protected]f6abeba2008-08-08 13:27:28104#else
[email protected]614e9fa2008-08-11 22:52:59105typedef std::string PathString;
[email protected]f6abeba2008-08-08 13:27:28106#endif
thestig3e4787d2015-05-19 19:31:52107PathString* g_log_file_name = nullptr;
initial.commitd7cae122008-07-26 21:49:38108
thestig3e4787d2015-05-19 19:31:52109// This file is lazily opened and the handle may be nullptr
110FileHandle g_log_file = nullptr;
initial.commitd7cae122008-07-26 21:49:38111
thestig3e4787d2015-05-19 19:31:52112// What should be prepended to each message?
113bool g_log_process_id = false;
114bool g_log_thread_id = false;
115bool g_log_timestamp = true;
116bool g_log_tickcount = false;
initial.commitd7cae122008-07-26 21:49:38117
[email protected]81e0a852010-08-17 00:38:12118// Should we pop up fatal debug messages in a dialog?
119bool show_error_dialogs = false;
120
initial.commitd7cae122008-07-26 21:49:38121// An assert handler override specified by the client to be called instead of
[email protected]fb62a532009-02-12 01:19:05122// the debug message dialog and process termination.
thestig3e4787d2015-05-19 19:31:52123LogAssertHandlerFunction log_assert_handler = nullptr;
[email protected]2b07b8412009-11-25 15:26:34124// A log message handler that gets notified of every log message we process.
thestig3e4787d2015-05-19 19:31:52125LogMessageHandlerFunction log_message_handler = nullptr;
initial.commitd7cae122008-07-26 21:49:38126
[email protected]f6abeba2008-08-08 13:27:28127// Helper functions to wrap platform differences.
128
[email protected]f8588472008-11-05 23:17:24129int32 CurrentProcessId() {
130#if defined(OS_WIN)
131 return GetCurrentProcessId();
132#elif defined(OS_POSIX)
133 return getpid();
134#endif
135}
136
[email protected]f8588472008-11-05 23:17:24137uint64 TickCount() {
138#if defined(OS_WIN)
139 return GetTickCount();
140#elif defined(OS_MACOSX)
141 return mach_absolute_time();
[email protected]19ea84ca2010-11-12 08:37:08142#elif defined(OS_NACL)
143 // NaCl sadly does not have _POSIX_TIMERS enabled in sys/features.h
144 // So we have to use clock() for now.
145 return clock();
[email protected]e43eddf12009-12-29 00:32:52146#elif defined(OS_POSIX)
[email protected]052f1b52008-11-06 21:43:07147 struct timespec ts;
148 clock_gettime(CLOCK_MONOTONIC, &ts);
149
150 uint64 absolute_micro =
151 static_cast<int64>(ts.tv_sec) * 1000000 +
152 static_cast<int64>(ts.tv_nsec) / 1000;
153
154 return absolute_micro;
[email protected]f8588472008-11-05 23:17:24155#endif
156}
157
[email protected]614e9fa2008-08-11 22:52:59158void DeleteFilePath(const PathString& log_name) {
[email protected]f6abeba2008-08-08 13:27:28159#if defined(OS_WIN)
[email protected]614e9fa2008-08-11 22:52:59160 DeleteFile(log_name.c_str());
thestig75f87352014-12-03 21:42:27161#elif defined(OS_NACL)
[email protected]ac07ec52013-04-22 17:32:45162 // Do nothing; unlink() isn't supported on NaCl.
[email protected]f6abeba2008-08-08 13:27:28163#else
[email protected]614e9fa2008-08-11 22:52:59164 unlink(log_name.c_str());
[email protected]f6abeba2008-08-08 13:27:28165#endif
166}
initial.commitd7cae122008-07-26 21:49:38167
[email protected]5f95d532010-10-01 17:16:58168PathString GetDefaultLogFile() {
[email protected]5b84fe32010-09-14 22:24:55169#if defined(OS_WIN)
170 // On Windows we use the same path as the exe.
171 wchar_t module_name[MAX_PATH];
thestig3e4787d2015-05-19 19:31:52172 GetModuleFileName(nullptr, module_name, MAX_PATH);
[email protected]5f95d532010-10-01 17:16:58173
scottmgfc5b7072015-01-27 21:46:28174 PathString log_name = module_name;
175 PathString::size_type last_backslash = log_name.rfind('\\', log_name.size());
[email protected]5f95d532010-10-01 17:16:58176 if (last_backslash != PathString::npos)
scottmgfc5b7072015-01-27 21:46:28177 log_name.erase(last_backslash + 1);
178 log_name += L"debug.log";
179 return log_name;
[email protected]5b84fe32010-09-14 22:24:55180#elif defined(OS_POSIX)
181 // On other platforms we just use the current directory.
[email protected]5f95d532010-10-01 17:16:58182 return PathString("debug.log");
[email protected]5b84fe32010-09-14 22:24:55183#endif
184}
185
ananta61762fb2015-09-18 01:00:09186// We don't need locks on Windows for atomically appending to files. The OS
187// provides this functionality.
188#if !defined(OS_WIN)
[email protected]5b84fe32010-09-14 22:24:55189// This class acts as a wrapper for locking the logging files.
190// LoggingLock::Init() should be called from the main thread before any logging
191// is done. Then whenever logging, be sure to have a local LoggingLock
192// instance on the stack. This will ensure that the lock is unlocked upon
193// exiting the frame.
194// LoggingLocks can not be nested.
195class LoggingLock {
196 public:
197 LoggingLock() {
198 LockLogging();
199 }
200
201 ~LoggingLock() {
202 UnlockLogging();
203 }
204
205 static void Init(LogLockingState lock_log, const PathChar* new_log_file) {
206 if (initialized)
207 return;
208 lock_log_file = lock_log;
[email protected]5f95d532010-10-01 17:16:58209
ananta61762fb2015-09-18 01:00:09210 if (lock_log_file != LOCK_LOG_FILE)
[email protected]bc581a682011-01-01 23:16:20211 log_lock = new base::internal::LockImpl();
ananta61762fb2015-09-18 01:00:09212
[email protected]5b84fe32010-09-14 22:24:55213 initialized = true;
214 }
215
216 private:
217 static void LockLogging() {
218 if (lock_log_file == LOCK_LOG_FILE) {
ananta61762fb2015-09-18 01:00:09219#if defined(OS_POSIX)
[email protected]5b84fe32010-09-14 22:24:55220 pthread_mutex_lock(&log_mutex);
221#endif
222 } else {
223 // use the lock
224 log_lock->Lock();
225 }
226 }
227
228 static void UnlockLogging() {
229 if (lock_log_file == LOCK_LOG_FILE) {
ananta61762fb2015-09-18 01:00:09230#if defined(OS_POSIX)
[email protected]5b84fe32010-09-14 22:24:55231 pthread_mutex_unlock(&log_mutex);
232#endif
233 } else {
234 log_lock->Unlock();
235 }
236 }
237
238 // The lock is used if log file locking is false. It helps us avoid problems
239 // with multiple threads writing to the log file at the same time. Use
240 // LockImpl directly instead of using Lock, because Lock makes logging calls.
[email protected]bc581a682011-01-01 23:16:20241 static base::internal::LockImpl* log_lock;
[email protected]5b84fe32010-09-14 22:24:55242
243 // When we don't use a lock, we are using a global mutex. We need to do this
244 // because LockFileEx is not thread safe.
ananta61762fb2015-09-18 01:00:09245#if defined(OS_POSIX)
[email protected]5b84fe32010-09-14 22:24:55246 static pthread_mutex_t log_mutex;
247#endif
248
249 static bool initialized;
250 static LogLockingState lock_log_file;
251};
252
253// static
254bool LoggingLock::initialized = false;
255// static
thestig3e4787d2015-05-19 19:31:52256base::internal::LockImpl* LoggingLock::log_lock = nullptr;
[email protected]5b84fe32010-09-14 22:24:55257// static
258LogLockingState LoggingLock::lock_log_file = LOCK_LOG_FILE;
259
ananta61762fb2015-09-18 01:00:09260#if defined(OS_POSIX)
[email protected]5b84fe32010-09-14 22:24:55261pthread_mutex_t LoggingLock::log_mutex = PTHREAD_MUTEX_INITIALIZER;
262#endif
263
ananta61762fb2015-09-18 01:00:09264#endif // OS_WIN
265
thestig3e4787d2015-05-19 19:31:52266// Called by logging functions to ensure that |g_log_file| is initialized
initial.commitd7cae122008-07-26 21:49:38267// and can be used for writing. Returns false if the file could not be
thestig3e4787d2015-05-19 19:31:52268// initialized. |g_log_file| will be nullptr in this case.
initial.commitd7cae122008-07-26 21:49:38269bool InitializeLogFileHandle() {
thestig3e4787d2015-05-19 19:31:52270 if (g_log_file)
initial.commitd7cae122008-07-26 21:49:38271 return true;
272
thestig3e4787d2015-05-19 19:31:52273 if (!g_log_file_name) {
[email protected]614e9fa2008-08-11 22:52:59274 // Nobody has called InitLogging to specify a debug log file, so here we
275 // initialize the log file name to a default.
thestig3e4787d2015-05-19 19:31:52276 g_log_file_name = new PathString(GetDefaultLogFile());
initial.commitd7cae122008-07-26 21:49:38277 }
278
thestig3e4787d2015-05-19 19:31:52279 if ((g_logging_destination & LOG_TO_FILE) != 0) {
[email protected]614e9fa2008-08-11 22:52:59280#if defined(OS_WIN)
ananta61762fb2015-09-18 01:00:09281 // The FILE_APPEND_DATA access mask ensures that the file is atomically
282 // appended to across accesses from multiple threads.
283 // https://msdn.microsoft.com/en-us/library/windows/desktop/aa364399(v=vs.85).aspx
284 // https://msdn.microsoft.com/en-us/library/windows/desktop/aa363858(v=vs.85).aspx
285 g_log_file = CreateFile(g_log_file_name->c_str(), FILE_APPEND_DATA,
thestig3e4787d2015-05-19 19:31:52286 FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr,
287 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr);
288 if (g_log_file == INVALID_HANDLE_VALUE || g_log_file == nullptr) {
[email protected]1d8c2702008-08-19 23:39:32289 // try the current directory
ananta61762fb2015-09-18 01:00:09290 base::FilePath file_path;
291 if (!base::GetCurrentDirectory(&file_path))
292 return false;
293
294 *g_log_file_name = file_path.Append(
295 FILE_PATH_LITERAL("debug.log")).value();
296
297 g_log_file = CreateFile(g_log_file_name->c_str(), FILE_APPEND_DATA,
thestig3e4787d2015-05-19 19:31:52298 FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr,
299 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr);
300 if (g_log_file == INVALID_HANDLE_VALUE || g_log_file == nullptr) {
301 g_log_file = nullptr;
[email protected]1d8c2702008-08-19 23:39:32302 return false;
303 }
initial.commitd7cae122008-07-26 21:49:38304 }
[email protected]78c6dd62009-06-08 23:29:11305#elif defined(OS_POSIX)
thestig3e4787d2015-05-19 19:31:52306 g_log_file = fopen(g_log_file_name->c_str(), "a");
307 if (g_log_file == nullptr)
[email protected]78c6dd62009-06-08 23:29:11308 return false;
[email protected]f6abeba2008-08-08 13:27:28309#endif
[email protected]1d8c2702008-08-19 23:39:32310 }
311
initial.commitd7cae122008-07-26 21:49:38312 return true;
313}
314
[email protected]17dcf752013-07-15 21:47:09315void CloseFile(FileHandle log) {
316#if defined(OS_WIN)
317 CloseHandle(log);
318#else
319 fclose(log);
320#endif
321}
322
323void CloseLogFileUnlocked() {
thestig3e4787d2015-05-19 19:31:52324 if (!g_log_file)
[email protected]17dcf752013-07-15 21:47:09325 return;
326
thestig3e4787d2015-05-19 19:31:52327 CloseFile(g_log_file);
328 g_log_file = nullptr;
[email protected]17dcf752013-07-15 21:47:09329}
330
[email protected]064aa162011-12-03 00:30:08331} // namespace
332
[email protected]5e3f7c22013-06-21 21:15:33333LoggingSettings::LoggingSettings()
334 : logging_dest(LOG_DEFAULT),
thestig3e4787d2015-05-19 19:31:52335 log_file(nullptr),
[email protected]5e3f7c22013-06-21 21:15:33336 lock_log(LOCK_LOG_FILE),
[email protected]1a1505512014-03-10 18:23:38337 delete_old(APPEND_TO_OLD_LOG_FILE) {}
[email protected]064aa162011-12-03 00:30:08338
[email protected]5e3f7c22013-06-21 21:15:33339bool BaseInitLoggingImpl(const LoggingSettings& settings) {
[email protected]ac07ec52013-04-22 17:32:45340#if defined(OS_NACL)
[email protected]5e3f7c22013-06-21 21:15:33341 // Can log only to the system debug log.
342 CHECK_EQ(settings.logging_dest & ~LOG_TO_SYSTEM_DEBUG_LOG, 0);
[email protected]ac07ec52013-04-22 17:32:45343#endif
pgal.u-szeged421dddb2014-11-25 12:55:02344 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
thestig3e4787d2015-05-19 19:31:52345 // Don't bother initializing |g_vlog_info| unless we use one of the
[email protected]99b7c57f2010-09-29 19:26:36346 // vlog switches.
347 if (command_line->HasSwitch(switches::kV) ||
348 command_line->HasSwitch(switches::kVModule)) {
thestig3e4787d2015-05-19 19:31:52349 // NOTE: If |g_vlog_info| has already been initialized, it might be in use
[email protected]064aa162011-12-03 00:30:08350 // by another thread. Don't delete the old VLogInfo, just create a second
351 // one. We keep track of both to avoid memory leak warnings.
352 CHECK(!g_vlog_info_prev);
353 g_vlog_info_prev = g_vlog_info;
354
[email protected]99b7c57f2010-09-29 19:26:36355 g_vlog_info =
356 new VlogInfo(command_line->GetSwitchValueASCII(switches::kV),
[email protected]162ac0f2010-11-04 15:50:49357 command_line->GetSwitchValueASCII(switches::kVModule),
thestig3e4787d2015-05-19 19:31:52358 &g_min_log_level);
[email protected]99b7c57f2010-09-29 19:26:36359 }
360
thestig3e4787d2015-05-19 19:31:52361 g_logging_destination = settings.logging_dest;
initial.commitd7cae122008-07-26 21:49:38362
[email protected]5e3f7c22013-06-21 21:15:33363 // ignore file options unless logging to file is set.
thestig3e4787d2015-05-19 19:31:52364 if ((g_logging_destination & LOG_TO_FILE) == 0)
[email protected]c7d5da992010-10-28 00:20:21365 return true;
initial.commitd7cae122008-07-26 21:49:38366
ananta61762fb2015-09-18 01:00:09367#if !defined(OS_WIN)
[email protected]17dcf752013-07-15 21:47:09368 LoggingLock::Init(settings.lock_log, settings.log_file);
369 LoggingLock logging_lock;
ananta61762fb2015-09-18 01:00:09370#endif
[email protected]17dcf752013-07-15 21:47:09371
372 // Calling InitLogging twice or after some log call has already opened the
373 // default log file will re-initialize to the new options.
374 CloseLogFileUnlocked();
375
thestig3e4787d2015-05-19 19:31:52376 if (!g_log_file_name)
377 g_log_file_name = new PathString();
378 *g_log_file_name = settings.log_file;
[email protected]5e3f7c22013-06-21 21:15:33379 if (settings.delete_old == DELETE_OLD_LOG_FILE)
thestig3e4787d2015-05-19 19:31:52380 DeleteFilePath(*g_log_file_name);
initial.commitd7cae122008-07-26 21:49:38381
[email protected]c7d5da992010-10-28 00:20:21382 return InitializeLogFileHandle();
initial.commitd7cae122008-07-26 21:49:38383}
384
385void SetMinLogLevel(int level) {
thestig3e4787d2015-05-19 19:31:52386 g_min_log_level = std::min(LOG_FATAL, level);
initial.commitd7cae122008-07-26 21:49:38387}
388
389int GetMinLogLevel() {
thestig3e4787d2015-05-19 19:31:52390 return g_min_log_level;
initial.commitd7cae122008-07-26 21:49:38391}
392
skobesc78c0ad72015-12-07 20:21:23393bool ShouldCreateLogMessage(int severity) {
394 if (severity < g_min_log_level)
395 return false;
396
397 // Return true here unless we know ~LogMessage won't do anything. Note that
398 // ~LogMessage writes to stderr if severity_ >= kAlwaysPrintErrorLevel, even
399 // when g_logging_destination is LOG_NONE.
400 return g_logging_destination != LOG_NONE || log_message_handler ||
401 severity >= kAlwaysPrintErrorLevel;
402}
403
[email protected]162ac0f2010-11-04 15:50:49404int GetVlogVerbosity() {
405 return std::max(-1, LOG_INFO - GetMinLogLevel());
406}
407
[email protected]99b7c57f2010-09-29 19:26:36408int GetVlogLevelHelper(const char* file, size_t N) {
409 DCHECK_GT(N, 0U);
thestig3e4787d2015-05-19 19:31:52410 // Note: |g_vlog_info| may change on a different thread during startup
411 // (but will always be valid or nullptr).
[email protected]064aa162011-12-03 00:30:08412 VlogInfo* vlog_info = g_vlog_info;
413 return vlog_info ?
414 vlog_info->GetVlogLevel(base::StringPiece(file, N - 1)) :
[email protected]162ac0f2010-11-04 15:50:49415 GetVlogVerbosity();
[email protected]99b7c57f2010-09-29 19:26:36416}
417
initial.commitd7cae122008-07-26 21:49:38418void SetLogItems(bool enable_process_id, bool enable_thread_id,
419 bool enable_timestamp, bool enable_tickcount) {
thestig3e4787d2015-05-19 19:31:52420 g_log_process_id = enable_process_id;
421 g_log_thread_id = enable_thread_id;
422 g_log_timestamp = enable_timestamp;
423 g_log_tickcount = enable_tickcount;
initial.commitd7cae122008-07-26 21:49:38424}
425
[email protected]81e0a852010-08-17 00:38:12426void SetShowErrorDialogs(bool enable_dialogs) {
427 show_error_dialogs = enable_dialogs;
428}
429
initial.commitd7cae122008-07-26 21:49:38430void SetLogAssertHandler(LogAssertHandlerFunction handler) {
431 log_assert_handler = handler;
432}
433
[email protected]2b07b8412009-11-25 15:26:34434void SetLogMessageHandler(LogMessageHandlerFunction handler) {
435 log_message_handler = handler;
436}
437
[email protected]64e5cc02010-11-03 19:20:27438LogMessageHandlerFunction GetLogMessageHandler() {
439 return log_message_handler;
440}
441
[email protected]6d445d32010-09-30 19:10:03442// Explicit instantiations for commonly used comparisons.
443template std::string* MakeCheckOpString<int, int>(
444 const int&, const int&, const char* names);
445template std::string* MakeCheckOpString<unsigned long, unsigned long>(
446 const unsigned long&, const unsigned long&, const char* names);
447template std::string* MakeCheckOpString<unsigned long, unsigned int>(
448 const unsigned long&, const unsigned int&, const char* names);
449template std::string* MakeCheckOpString<unsigned int, unsigned long>(
450 const unsigned int&, const unsigned long&, const char* names);
451template std::string* MakeCheckOpString<std::string, std::string>(
452 const std::string&, const std::string&, const char* name);
[email protected]2b07b8412009-11-25 15:26:34453
[email protected]f2c05492014-06-17 12:04:23454#if !defined(NDEBUG)
[email protected]d81baca42010-03-01 13:10:22455// Displays a message box to the user with the error message in it.
456// Used for fatal messages, where we close the app simultaneously.
[email protected]561513f2010-12-16 23:29:25457// This is for developers only; we don't use this in circumstances
458// (like release builds) where users could see it, since users don't
459// understand these messages anyway.
[email protected]d81baca42010-03-01 13:10:22460void DisplayDebugMessageInDialog(const std::string& str) {
initial.commitd7cae122008-07-26 21:49:38461 if (str.empty())
462 return;
463
[email protected]81e0a852010-08-17 00:38:12464 if (!show_error_dialogs)
[email protected]846ed9c32010-07-29 20:33:44465 return;
466
[email protected]f6abeba2008-08-08 13:27:28467#if defined(OS_WIN)
brettwe33526852015-10-03 00:46:01468 MessageBoxW(nullptr, base::UTF8ToUTF16(str).c_str(), L"Fatal error",
469 MB_OK | MB_ICONHAND | MB_TOPMOST);
[email protected]f6abeba2008-08-08 13:27:28470#else
[email protected]561513f2010-12-16 23:29:25471 // We intentionally don't implement a dialog on other platforms.
472 // You can just look at stderr.
thestig3e4787d2015-05-19 19:31:52473#endif // defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:38474}
[email protected]f2c05492014-06-17 12:04:23475#endif // !defined(NDEBUG)
initial.commitd7cae122008-07-26 21:49:38476
[email protected]3f85caa2009-04-14 16:52:11477#if defined(OS_WIN)
478LogMessage::SaveLastError::SaveLastError() : last_error_(::GetLastError()) {
479}
480
481LogMessage::SaveLastError::~SaveLastError() {
482 ::SetLastError(last_error_);
483}
484#endif // defined(OS_WIN)
485
[email protected]eae9c062011-01-11 00:50:59486LogMessage::LogMessage(const char* file, int line, LogSeverity severity)
487 : severity_(severity), file_(file), line_(line) {
488 Init(file, line);
489}
490
tnagel4a045d3f2015-07-12 14:19:28491LogMessage::LogMessage(const char* file, int line, const char* condition)
492 : severity_(LOG_FATAL), file_(file), line_(line) {
493 Init(file, line);
494 stream_ << "Check failed: " << condition << ". ";
495}
496
[email protected]9c7132e2011-02-08 07:39:08497LogMessage::LogMessage(const char* file, int line, std::string* result)
[email protected]162ac0f2010-11-04 15:50:49498 : severity_(LOG_FATAL), file_(file), line_(line) {
initial.commitd7cae122008-07-26 21:49:38499 Init(file, line);
[email protected]9c7132e2011-02-08 07:39:08500 stream_ << "Check failed: " << *result;
501 delete result;
initial.commitd7cae122008-07-26 21:49:38502}
503
[email protected]fb62a532009-02-12 01:19:05504LogMessage::LogMessage(const char* file, int line, LogSeverity severity,
[email protected]9c7132e2011-02-08 07:39:08505 std::string* result)
[email protected]162ac0f2010-11-04 15:50:49506 : severity_(severity), file_(file), line_(line) {
[email protected]fb62a532009-02-12 01:19:05507 Init(file, line);
[email protected]9c7132e2011-02-08 07:39:08508 stream_ << "Check failed: " << *result;
509 delete result;
[email protected]fb62a532009-02-12 01:19:05510}
511
initial.commitd7cae122008-07-26 21:49:38512LogMessage::~LogMessage() {
jam79dc59a2015-08-17 03:38:16513#if !defined(OFFICIAL_BUILD) && !defined(OS_NACL) && !defined(__UCLIBC__)
brucedawson7c559eb2015-09-05 00:34:42514 if (severity_ == LOG_FATAL && !base::debug::BeingDebugged()) {
515 // Include a stack trace on a fatal, unless a debugger is attached.
[email protected]58580352010-10-26 04:07:50516 base::debug::StackTrace trace;
[email protected]d1ccc35a2010-03-24 05:03:24517 stream_ << std::endl; // Newline to separate from log message.
518 trace.OutputToStream(&stream_);
519 }
[email protected]1d8c2702008-08-19 23:39:32520#endif
[email protected]d1ccc35a2010-03-24 05:03:24521 stream_ << std::endl;
522 std::string str_newline(stream_.str());
523
[email protected]2b07b8412009-11-25 15:26:34524 // Give any log message handler first dibs on the message.
[email protected]5e3f7c22013-06-21 21:15:33525 if (log_message_handler &&
526 log_message_handler(severity_, file_, line_,
527 message_start_, str_newline)) {
[email protected]162ac0f2010-11-04 15:50:49528 // The handler took care of it, no further processing.
[email protected]2b07b8412009-11-25 15:26:34529 return;
[email protected]162ac0f2010-11-04 15:50:49530 }
initial.commitd7cae122008-07-26 21:49:38531
thestig3e4787d2015-05-19 19:31:52532 if ((g_logging_destination & LOG_TO_SYSTEM_DEBUG_LOG) != 0) {
[email protected]f6abeba2008-08-08 13:27:28533#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:38534 OutputDebugStringA(str_newline.c_str());
mark4c7449c2015-11-10 19:53:42535#elif defined(OS_MACOSX)
536 // In LOG_TO_SYSTEM_DEBUG_LOG mode, log messages are always written to
537 // stderr. If stderr is /dev/null, also log via ASL (Apple System Log). If
538 // there's something weird about stderr, assume that log messages are going
539 // nowhere and log via ASL too. Messages logged via ASL show up in
540 // Console.app.
541 //
542 // Programs started by launchd, as UI applications normally are, have had
543 // stderr connected to /dev/null since OS X 10.8. Prior to that, stderr was
544 // a pipe to launchd, which logged what it received (see log_redirect_fd in
545 // 10.7.5 launchd-392.39/launchd/src/launchd_core_logic.c).
546 //
547 // Another alternative would be to determine whether stderr is a pipe to
548 // launchd and avoid logging via ASL only in that case. See 10.7.5
549 // CF-635.21/CFUtilities.c also_do_stderr(). This would result in logging to
550 // both stderr and ASL even in tests, where it's undesirable to log to the
551 // system log at all.
552 //
553 // Note that the ASL client by default discards messages whose levels are
554 // below ASL_LEVEL_NOTICE. It's possible to change that with
555 // asl_set_filter(), but this is pointless because syslogd normally applies
556 // the same filter.
557 const bool log_via_asl = []() {
558 struct stat stderr_stat;
559 if (fstat(fileno(stderr), &stderr_stat) == -1) {
560 return true;
561 }
562 if (!S_ISCHR(stderr_stat.st_mode)) {
563 return false;
564 }
565
566 struct stat dev_null_stat;
567 if (stat(_PATH_DEVNULL, &dev_null_stat) == -1) {
568 return true;
569 }
570
571 return !S_ISCHR(dev_null_stat.st_mode) ||
572 stderr_stat.st_rdev == dev_null_stat.st_rdev;
573 }();
574
575 if (log_via_asl) {
576 // Log roughly the same way that CFLog() and NSLog() would. See 10.10.5
577 // CF-1153.18/CFUtilities.c __CFLogCString().
578 //
579 // The ASL facility is set to the main bundle ID if available. Otherwise,
580 // "com.apple.console" is used.
581 CFBundleRef main_bundle = CFBundleGetMainBundle();
582 CFStringRef main_bundle_id_cf =
583 main_bundle ? CFBundleGetIdentifier(main_bundle) : nullptr;
584 std::string asl_facility =
585 main_bundle_id_cf ? base::SysCFStringRefToUTF8(main_bundle_id_cf)
586 : std::string("com.apple.console");
587
588 class ASLClient {
589 public:
590 explicit ASLClient(const std::string& asl_facility)
591 : client_(asl_open(nullptr,
592 asl_facility.c_str(),
593 ASL_OPT_NO_DELAY)) {}
594 ~ASLClient() { asl_close(client_); }
595
596 aslclient get() const { return client_; }
597
598 private:
599 aslclient client_;
600 DISALLOW_COPY_AND_ASSIGN(ASLClient);
601 } asl_client(asl_facility);
602
603 class ASLMessage {
604 public:
605 ASLMessage() : message_(asl_new(ASL_TYPE_MSG)) {}
606 ~ASLMessage() { asl_free(message_); }
607
608 aslmsg get() const { return message_; }
609
610 private:
611 aslmsg message_;
612 DISALLOW_COPY_AND_ASSIGN(ASLMessage);
613 } asl_message;
614
615 // By default, messages are only readable by the admin group. Explicitly
616 // make them readable by the user generating the messages.
617 char euid_string[12];
618 snprintf(euid_string, arraysize(euid_string), "%d", geteuid());
619 asl_set(asl_message.get(), ASL_KEY_READ_UID, euid_string);
620
621 // Map Chrome log severities to ASL log levels.
622 const char* const asl_level_string = [](LogSeverity severity) {
623 // ASL_LEVEL_* are ints, but ASL needs equivalent strings. This
624 // non-obvious two-step macro trick achieves what's needed.
625 // https://gcc.gnu.org/onlinedocs/cpp/Stringification.html
626#define ASL_LEVEL_STR(level) ASL_LEVEL_STR_X(level)
627#define ASL_LEVEL_STR_X(level) #level
628 switch (severity) {
629 case LOG_INFO:
630 return ASL_LEVEL_STR(ASL_LEVEL_INFO);
631 case LOG_WARNING:
632 return ASL_LEVEL_STR(ASL_LEVEL_WARNING);
633 case LOG_ERROR:
634 return ASL_LEVEL_STR(ASL_LEVEL_ERR);
635 case LOG_FATAL:
636 return ASL_LEVEL_STR(ASL_LEVEL_CRIT);
637 default:
638 return severity < 0 ? ASL_LEVEL_STR(ASL_LEVEL_DEBUG)
639 : ASL_LEVEL_STR(ASL_LEVEL_NOTICE);
640 }
641#undef ASL_LEVEL_STR
642#undef ASL_LEVEL_STR_X
643 }(severity_);
644 asl_set(asl_message.get(), ASL_KEY_LEVEL, asl_level_string);
645
646 asl_set(asl_message.get(), ASL_KEY_MSG, str_newline.c_str());
647
648 asl_send(asl_client.get(), asl_message.get());
649 }
[email protected]3132e35c2011-07-07 20:46:50650#elif defined(OS_ANDROID)
[email protected]efbae7da2013-05-21 22:39:25651 android_LogPriority priority =
652 (severity_ < 0) ? ANDROID_LOG_VERBOSE : ANDROID_LOG_UNKNOWN;
[email protected]3132e35c2011-07-07 20:46:50653 switch (severity_) {
654 case LOG_INFO:
655 priority = ANDROID_LOG_INFO;
656 break;
657 case LOG_WARNING:
658 priority = ANDROID_LOG_WARN;
659 break;
660 case LOG_ERROR:
[email protected]3132e35c2011-07-07 20:46:50661 priority = ANDROID_LOG_ERROR;
662 break;
663 case LOG_FATAL:
664 priority = ANDROID_LOG_FATAL;
665 break;
666 }
667 __android_log_write(priority, "chromium", str_newline.c_str());
[email protected]107bc0f12008-08-26 17:48:18668#endif
[email protected]51105382014-03-14 17:02:15669 ignore_result(fwrite(str_newline.data(), str_newline.size(), 1, stderr));
[email protected]469006c2010-09-24 15:43:06670 fflush(stderr);
[email protected]a33c9892008-08-25 20:10:31671 } else if (severity_ >= kAlwaysPrintErrorLevel) {
672 // When we're only outputting to a log file, above a certain log level, we
673 // should still output to stderr so that we can better detect and diagnose
674 // problems with unit tests, especially on the buildbots.
[email protected]51105382014-03-14 17:02:15675 ignore_result(fwrite(str_newline.data(), str_newline.size(), 1, stderr));
[email protected]1ce41052009-12-02 00:34:02676 fflush(stderr);
[email protected]f6abeba2008-08-08 13:27:28677 }
[email protected]52a261f2009-03-03 15:01:12678
initial.commitd7cae122008-07-26 21:49:38679 // write to log file
thestig3e4787d2015-05-19 19:31:52680 if ((g_logging_destination & LOG_TO_FILE) != 0) {
[email protected]17dcf752013-07-15 21:47:09681 // We can have multiple threads and/or processes, so try to prevent them
682 // from clobbering each other's writes.
683 // If the client app did not call InitLogging, and the lock has not
684 // been created do it now. We do this on demand, but if two threads try
685 // to do this at the same time, there will be a race condition to create
686 // the lock. This is why InitLogging should be called from the main
687 // thread at the beginning of execution.
ananta61762fb2015-09-18 01:00:09688#if !defined(OS_WIN)
thestig3e4787d2015-05-19 19:31:52689 LoggingLock::Init(LOCK_LOG_FILE, nullptr);
[email protected]5b84fe32010-09-14 22:24:55690 LoggingLock logging_lock;
ananta61762fb2015-09-18 01:00:09691#endif
[email protected]5b84fe32010-09-14 22:24:55692 if (InitializeLogFileHandle()) {
[email protected]f6abeba2008-08-08 13:27:28693#if defined(OS_WIN)
[email protected]5b84fe32010-09-14 22:24:55694 DWORD num_written;
thestig3e4787d2015-05-19 19:31:52695 WriteFile(g_log_file,
[email protected]5b84fe32010-09-14 22:24:55696 static_cast<const void*>(str_newline.c_str()),
697 static_cast<DWORD>(str_newline.length()),
698 &num_written,
thestig3e4787d2015-05-19 19:31:52699 nullptr);
[email protected]cba21962010-08-31 22:35:55700#else
[email protected]51105382014-03-14 17:02:15701 ignore_result(fwrite(
thestig3e4787d2015-05-19 19:31:52702 str_newline.data(), str_newline.size(), 1, g_log_file));
703 fflush(g_log_file);
[email protected]cba21962010-08-31 22:35:55704#endif
initial.commitd7cae122008-07-26 21:49:38705 }
706 }
707
708 if (severity_ == LOG_FATAL) {
[email protected]eb4c4d032012-04-03 18:45:05709 // Ensure the first characters of the string are on the stack so they
710 // are contained in minidumps for diagnostic purposes.
711 char str_stack[1024];
712 str_newline.copy(str_stack, arraysize(str_stack));
713 base::debug::Alias(str_stack);
714
[email protected]82d89ab2014-02-28 18:25:34715 if (log_assert_handler) {
716 // Make a copy of the string for the handler out of paranoia.
717 log_assert_handler(std::string(stream_.str()));
[email protected]1ffe08c12008-08-13 11:15:11718 } else {
[email protected]82d89ab2014-02-28 18:25:34719 // Don't use the string with the newline, get a fresh version to send to
720 // the debug message process. We also don't display assertions to the
721 // user in release mode. The enduser can't do anything with this
722 // information, and displaying message boxes when the application is
723 // hosed can cause additional problems.
[email protected]4d5901272008-11-06 00:33:50724#ifndef NDEBUG
brucedawson7c559eb2015-09-05 00:34:42725 if (!base::debug::BeingDebugged()) {
726 // Displaying a dialog is unnecessary when debugging and can complicate
727 // debugging.
728 DisplayDebugMessageInDialog(stream_.str());
729 }
[email protected]4d5901272008-11-06 00:33:50730#endif
[email protected]82d89ab2014-02-28 18:25:34731 // Crash the process to generate a dump.
732 base::debug::BreakDebugger();
initial.commitd7cae122008-07-26 21:49:38733 }
734 }
735}
736
[email protected]eae9c062011-01-11 00:50:59737// writes the common header info to the stream
738void LogMessage::Init(const char* file, int line) {
739 base::StringPiece filename(file);
740 size_t last_slash_pos = filename.find_last_of("\\/");
741 if (last_slash_pos != base::StringPiece::npos)
742 filename.remove_prefix(last_slash_pos + 1);
743
744 // TODO(darin): It might be nice if the columns were fixed width.
745
746 stream_ << '[';
thestig3e4787d2015-05-19 19:31:52747 if (g_log_process_id)
[email protected]eae9c062011-01-11 00:50:59748 stream_ << CurrentProcessId() << ':';
thestig3e4787d2015-05-19 19:31:52749 if (g_log_thread_id)
[email protected]63e66802012-01-18 21:21:09750 stream_ << base::PlatformThread::CurrentId() << ':';
thestig3e4787d2015-05-19 19:31:52751 if (g_log_timestamp) {
752 time_t t = time(nullptr);
[email protected]eae9c062011-01-11 00:50:59753 struct tm local_time = {0};
mostynb7e42a8f2014-12-19 12:47:46754#ifdef _MSC_VER
[email protected]eae9c062011-01-11 00:50:59755 localtime_s(&local_time, &t);
756#else
757 localtime_r(&t, &local_time);
758#endif
759 struct tm* tm_time = &local_time;
760 stream_ << std::setfill('0')
761 << std::setw(2) << 1 + tm_time->tm_mon
762 << std::setw(2) << tm_time->tm_mday
763 << '/'
764 << std::setw(2) << tm_time->tm_hour
765 << std::setw(2) << tm_time->tm_min
766 << std::setw(2) << tm_time->tm_sec
767 << ':';
768 }
thestig3e4787d2015-05-19 19:31:52769 if (g_log_tickcount)
[email protected]eae9c062011-01-11 00:50:59770 stream_ << TickCount() << ':';
771 if (severity_ >= 0)
[email protected]80f360a2014-01-23 01:36:19772 stream_ << log_severity_name(severity_);
[email protected]eae9c062011-01-11 00:50:59773 else
774 stream_ << "VERBOSE" << -severity_;
775
776 stream_ << ":" << filename << "(" << line << ")] ";
777
pkasting9cf9b942014-10-01 22:18:43778 message_start_ = stream_.str().length();
[email protected]eae9c062011-01-11 00:50:59779}
780
[email protected]d8617a62009-10-09 23:52:20781#if defined(OS_WIN)
782// This has already been defined in the header, but defining it again as DWORD
783// ensures that the type used in the header is equivalent to DWORD. If not,
784// the redefinition is a compile error.
785typedef DWORD SystemErrorCode;
786#endif
787
788SystemErrorCode GetLastSystemErrorCode() {
789#if defined(OS_WIN)
790 return ::GetLastError();
791#elif defined(OS_POSIX)
792 return errno;
793#else
794#error Not implemented
795#endif
796}
797
798#if defined(OS_WIN)
[email protected]c914d8a2014-04-23 01:11:01799BASE_EXPORT std::string SystemErrorCodeToString(SystemErrorCode error_code) {
thestig75f87352014-12-03 21:42:27800 const int kErrorMessageBufferSize = 256;
801 char msgbuf[kErrorMessageBufferSize];
[email protected]c914d8a2014-04-23 01:11:01802 DWORD flags = FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS;
thestig3e4787d2015-05-19 19:31:52803 DWORD len = FormatMessageA(flags, nullptr, error_code, 0, msgbuf,
804 arraysize(msgbuf), nullptr);
[email protected]c914d8a2014-04-23 01:11:01805 if (len) {
806 // Messages returned by system end with line breaks.
807 return base::CollapseWhitespaceASCII(msgbuf, true) +
808 base::StringPrintf(" (0x%X)", error_code);
809 }
810 return base::StringPrintf("Error (0x%X) while retrieving error. (0x%X)",
811 GetLastError(), error_code);
[email protected]d8617a62009-10-09 23:52:20812}
[email protected]c914d8a2014-04-23 01:11:01813#elif defined(OS_POSIX)
814BASE_EXPORT std::string SystemErrorCodeToString(SystemErrorCode error_code) {
brettw6ee6fd62015-06-09 18:05:24815 return base::safe_strerror(error_code);
[email protected]c914d8a2014-04-23 01:11:01816}
817#else
818#error Not implemented
thestig3e4787d2015-05-19 19:31:52819#endif // defined(OS_WIN)
[email protected]d8617a62009-10-09 23:52:20820
[email protected]c914d8a2014-04-23 01:11:01821
822#if defined(OS_WIN)
[email protected]d8617a62009-10-09 23:52:20823Win32ErrorLogMessage::Win32ErrorLogMessage(const char* file,
824 int line,
825 LogSeverity severity,
826 SystemErrorCode err)
827 : err_(err),
[email protected]d8617a62009-10-09 23:52:20828 log_message_(file, line, severity) {
829}
830
831Win32ErrorLogMessage::~Win32ErrorLogMessage() {
[email protected]c914d8a2014-04-23 01:11:01832 stream() << ": " << SystemErrorCodeToString(err_);
[email protected]20909e72012-04-05 16:57:06833 // We're about to crash (CHECK). Put |err_| on the stack (by placing it in a
834 // field) and use Alias in hopes that it makes it into crash dumps.
835 DWORD last_error = err_;
836 base::debug::Alias(&last_error);
[email protected]d8617a62009-10-09 23:52:20837}
838#elif defined(OS_POSIX)
839ErrnoLogMessage::ErrnoLogMessage(const char* file,
840 int line,
841 LogSeverity severity,
842 SystemErrorCode err)
843 : err_(err),
844 log_message_(file, line, severity) {
845}
846
847ErrnoLogMessage::~ErrnoLogMessage() {
[email protected]c914d8a2014-04-23 01:11:01848 stream() << ": " << SystemErrorCodeToString(err_);
[email protected]d8617a62009-10-09 23:52:20849}
thestig3e4787d2015-05-19 19:31:52850#endif // defined(OS_WIN)
[email protected]d8617a62009-10-09 23:52:20851
initial.commitd7cae122008-07-26 21:49:38852void CloseLogFile() {
ananta61762fb2015-09-18 01:00:09853#if !defined(OS_WIN)
[email protected]5b84fe32010-09-14 22:24:55854 LoggingLock logging_lock;
ananta61762fb2015-09-18 01:00:09855#endif
[email protected]17dcf752013-07-15 21:47:09856 CloseLogFileUnlocked();
initial.commitd7cae122008-07-26 21:49:38857}
858
[email protected]e36ddc82009-12-08 04:22:50859void RawLog(int level, const char* message) {
thestig3e4787d2015-05-19 19:31:52860 if (level >= g_min_log_level) {
[email protected]e36ddc82009-12-08 04:22:50861 size_t bytes_written = 0;
862 const size_t message_len = strlen(message);
863 int rv;
864 while (bytes_written < message_len) {
865 rv = HANDLE_EINTR(
866 write(STDERR_FILENO, message + bytes_written,
867 message_len - bytes_written));
868 if (rv < 0) {
869 // Give up, nothing we can do now.
870 break;
871 }
872 bytes_written += rv;
873 }
874
875 if (message_len > 0 && message[message_len - 1] != '\n') {
876 do {
877 rv = HANDLE_EINTR(write(STDERR_FILENO, "\n", 1));
878 if (rv < 0) {
879 // Give up, nothing we can do now.
880 break;
881 }
882 } while (rv != 1);
883 }
884 }
885
886 if (level == LOG_FATAL)
[email protected]58580352010-10-26 04:07:50887 base::debug::BreakDebugger();
[email protected]e36ddc82009-12-08 04:22:50888}
889
[email protected]34a907732012-01-20 06:33:27890// This was defined at the beginning of this file.
891#undef write
892
[email protected]f01b88a2013-02-27 22:04:00893#if defined(OS_WIN)
ananta61762fb2015-09-18 01:00:09894bool IsLoggingToFileEnabled() {
895 return g_logging_destination & LOG_TO_FILE;
896}
897
[email protected]f01b88a2013-02-27 22:04:00898std::wstring GetLogFileFullPath() {
thestig3e4787d2015-05-19 19:31:52899 if (g_log_file_name)
900 return *g_log_file_name;
[email protected]f01b88a2013-02-27 22:04:00901 return std::wstring();
902}
903#endif
904
tnagel80388e682015-05-26 13:27:56905BASE_EXPORT void LogErrorNotReached(const char* file, int line) {
tnagelff3f34a2015-05-24 12:59:14906 LogMessage(file, line, LOG_ERROR).stream()
907 << "NOTREACHED() hit.";
908}
909
[email protected]96fd0032009-04-24 00:13:08910} // namespace logging
initial.commitd7cae122008-07-26 21:49:38911
[email protected]81411c62014-07-08 23:03:06912std::ostream& std::operator<<(std::ostream& out, const wchar_t* wstr) {
thestig75f87352014-12-03 21:42:27913 return out << base::WideToUTF8(wstr);
initial.commitd7cae122008-07-26 21:49:38914}