blob: 9ad50e8d9934d8f941aafae0083174bd53f9ebba [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
avi51ba3e692015-12-26 17:30:507#include <limits.h>
avi9b6f42932015-12-26 22:15:148#include <stdint.h>
9
10#include "base/macros.h"
11#include "build/build_config.h"
avi51ba3e692015-12-26 17:30:5012
[email protected]b16ef312008-08-19 18:36:2313#if defined(OS_WIN)
[email protected]e36ddc82009-12-08 04:22:5014#include <io.h>
[email protected]f6abeba2008-08-08 13:27:2815#include <windows.h>
ananta61762fb2015-09-18 01:00:0916#include "base/files/file_path.h"
17#include "base/files/file_util.h"
[email protected]f6abeba2008-08-08 13:27:2818typedef HANDLE FileHandle;
19typedef HANDLE MutexHandle;
[email protected]e36ddc82009-12-08 04:22:5020// Windows warns on using write(). It prefers _write().
21#define write(fd, buf, count) _write(fd, buf, static_cast<unsigned int>(count))
22// Windows doesn't define STDERR_FILENO. Define it here.
23#define STDERR_FILENO 2
[email protected]052f1b52008-11-06 21:43:0724#elif defined(OS_MACOSX)
mark4c7449c2015-11-10 19:53:4225#include <asl.h>
26#include <CoreFoundation/CoreFoundation.h>
[email protected]f6abeba2008-08-08 13:27:2827#include <mach/mach.h>
28#include <mach/mach_time.h>
29#include <mach-o/dyld.h>
[email protected]e43eddf12009-12-29 00:32:5230#elif defined(OS_POSIX)
[email protected]19ea84ca2010-11-12 08:37:0831#if defined(OS_NACL)
thestig75f87352014-12-03 21:42:2732#include <sys/time.h> // timespec doesn't seem to be in <time.h>
[email protected]19ea84ca2010-11-12 08:37:0833#else
[email protected]052f1b52008-11-06 21:43:0734#include <sys/syscall.h>
[email protected]19ea84ca2010-11-12 08:37:0835#endif
[email protected]052f1b52008-11-06 21:43:0736#include <time.h>
[email protected]614e9fa2008-08-11 22:52:5937#endif
38
39#if defined(OS_POSIX)
[email protected]d8617a62009-10-09 23:52:2040#include <errno.h>
mark4c7449c2015-11-10 19:53:4241#include <paths.h>
[email protected]166326c62010-08-05 15:50:2342#include <pthread.h>
[email protected]f6abeba2008-08-08 13:27:2843#include <stdio.h>
[email protected]eb62f7262013-03-30 14:29:0044#include <stdlib.h>
[email protected]f6abeba2008-08-08 13:27:2845#include <string.h>
mark4c7449c2015-11-10 19:53:4246#include <sys/stat.h>
[email protected]f6abeba2008-08-08 13:27:2847#include <unistd.h>
48#define MAX_PATH PATH_MAX
49typedef FILE* FileHandle;
50typedef pthread_mutex_t* MutexHandle;
51#endif
52
[email protected]1f88b5162011-04-01 00:02:2953#include <algorithm>
54#include <cstring>
initial.commitd7cae122008-07-26 21:49:3855#include <ctime>
56#include <iomanip>
[email protected]1f88b5162011-04-01 00:02:2957#include <ostream>
[email protected]c914d8a2014-04-23 01:11:0158#include <string>
[email protected]b16ef312008-08-19 18:36:2359
initial.commitd7cae122008-07-26 21:49:3860#include "base/base_switches.h"
61#include "base/command_line.h"
[email protected]eb4c4d032012-04-03 18:45:0562#include "base/debug/alias.h"
[email protected]58580352010-10-26 04:07:5063#include "base/debug/debugger.h"
64#include "base/debug/stack_trace.h"
[email protected]2025d002012-11-14 20:54:3565#include "base/posix/eintr_wrapper.h"
[email protected]eb62f7262013-03-30 14:29:0066#include "base/strings/string_piece.h"
[email protected]c914d8a2014-04-23 01:11:0167#include "base/strings/string_util.h"
68#include "base/strings/stringprintf.h"
mark4c7449c2015-11-10 19:53:4269#include "base/strings/sys_string_conversions.h"
[email protected]a4ea1f12013-06-07 18:37:0770#include "base/strings/utf_string_conversions.h"
[email protected]bc581a682011-01-01 23:16:2071#include "base/synchronization/lock_impl.h"
[email protected]63e66802012-01-18 21:21:0972#include "base/threading/platform_thread.h"
[email protected]99b7c57f2010-09-29 19:26:3673#include "base/vlog.h"
[email protected]53c7ce42010-12-14 16:20:0474#if defined(OS_POSIX)
brettw6ee6fd62015-06-09 18:05:2475#include "base/posix/safe_strerror.h"
[email protected]53c7ce42010-12-14 16:20:0476#endif
[email protected]52a261f2009-03-03 15:01:1277
[email protected]3132e35c2011-07-07 20:46:5078#if defined(OS_ANDROID)
79#include <android/log.h>
80#endif
81
initial.commitd7cae122008-07-26 21:49:3882namespace logging {
83
[email protected]064aa162011-12-03 00:30:0884namespace {
85
thestig3e4787d2015-05-19 19:31:5286VlogInfo* g_vlog_info = nullptr;
87VlogInfo* g_vlog_info_prev = nullptr;
initial.commitd7cae122008-07-26 21:49:3888
89const char* const log_severity_names[LOG_NUM_SEVERITIES] = {
[email protected]f2c05492014-06-17 12:04:2390 "INFO", "WARNING", "ERROR", "FATAL" };
initial.commitd7cae122008-07-26 21:49:3891
thestig75f87352014-12-03 21:42:2792const char* log_severity_name(int severity) {
[email protected]80f360a2014-01-23 01:36:1993 if (severity >= 0 && severity < LOG_NUM_SEVERITIES)
94 return log_severity_names[severity];
95 return "UNKNOWN";
96}
97
thestig3e4787d2015-05-19 19:31:5298int g_min_log_level = 0;
[email protected]1d8c2702008-08-19 23:39:3299
thestig3e4787d2015-05-19 19:31:52100LoggingDestination g_logging_destination = LOG_DEFAULT;
initial.commitd7cae122008-07-26 21:49:38101
[email protected]a33c9892008-08-25 20:10:31102// For LOG_ERROR and above, always print to stderr.
103const int kAlwaysPrintErrorLevel = LOG_ERROR;
104
[email protected]614e9fa2008-08-11 22:52:59105// Which log file to use? This is initialized by InitLogging or
initial.commitd7cae122008-07-26 21:49:38106// will be lazily initialized to the default value when it is
107// first needed.
[email protected]f6abeba2008-08-08 13:27:28108#if defined(OS_WIN)
[email protected]614e9fa2008-08-11 22:52:59109typedef std::wstring PathString;
[email protected]f6abeba2008-08-08 13:27:28110#else
[email protected]614e9fa2008-08-11 22:52:59111typedef std::string PathString;
[email protected]f6abeba2008-08-08 13:27:28112#endif
thestig3e4787d2015-05-19 19:31:52113PathString* g_log_file_name = nullptr;
initial.commitd7cae122008-07-26 21:49:38114
thestig3e4787d2015-05-19 19:31:52115// This file is lazily opened and the handle may be nullptr
116FileHandle g_log_file = nullptr;
initial.commitd7cae122008-07-26 21:49:38117
thestig3e4787d2015-05-19 19:31:52118// What should be prepended to each message?
119bool g_log_process_id = false;
120bool g_log_thread_id = false;
121bool g_log_timestamp = true;
122bool g_log_tickcount = false;
initial.commitd7cae122008-07-26 21:49:38123
[email protected]81e0a852010-08-17 00:38:12124// Should we pop up fatal debug messages in a dialog?
125bool show_error_dialogs = false;
126
initial.commitd7cae122008-07-26 21:49:38127// An assert handler override specified by the client to be called instead of
[email protected]fb62a532009-02-12 01:19:05128// the debug message dialog and process termination.
thestig3e4787d2015-05-19 19:31:52129LogAssertHandlerFunction log_assert_handler = nullptr;
[email protected]2b07b8412009-11-25 15:26:34130// A log message handler that gets notified of every log message we process.
thestig3e4787d2015-05-19 19:31:52131LogMessageHandlerFunction log_message_handler = nullptr;
initial.commitd7cae122008-07-26 21:49:38132
[email protected]f6abeba2008-08-08 13:27:28133// Helper functions to wrap platform differences.
134
avi9b6f42932015-12-26 22:15:14135int32_t CurrentProcessId() {
[email protected]f8588472008-11-05 23:17:24136#if defined(OS_WIN)
137 return GetCurrentProcessId();
138#elif defined(OS_POSIX)
139 return getpid();
140#endif
141}
142
avi9b6f42932015-12-26 22:15:14143uint64_t TickCount() {
[email protected]f8588472008-11-05 23:17:24144#if defined(OS_WIN)
145 return GetTickCount();
146#elif defined(OS_MACOSX)
147 return mach_absolute_time();
[email protected]19ea84ca2010-11-12 08:37:08148#elif defined(OS_NACL)
149 // NaCl sadly does not have _POSIX_TIMERS enabled in sys/features.h
150 // So we have to use clock() for now.
151 return clock();
[email protected]e43eddf12009-12-29 00:32:52152#elif defined(OS_POSIX)
[email protected]052f1b52008-11-06 21:43:07153 struct timespec ts;
154 clock_gettime(CLOCK_MONOTONIC, &ts);
155
avi9b6f42932015-12-26 22:15:14156 uint64_t absolute_micro = static_cast<int64_t>(ts.tv_sec) * 1000000 +
157 static_cast<int64_t>(ts.tv_nsec) / 1000;
[email protected]052f1b52008-11-06 21:43:07158
159 return absolute_micro;
[email protected]f8588472008-11-05 23:17:24160#endif
161}
162
[email protected]614e9fa2008-08-11 22:52:59163void DeleteFilePath(const PathString& log_name) {
[email protected]f6abeba2008-08-08 13:27:28164#if defined(OS_WIN)
[email protected]614e9fa2008-08-11 22:52:59165 DeleteFile(log_name.c_str());
thestig75f87352014-12-03 21:42:27166#elif defined(OS_NACL)
[email protected]ac07ec52013-04-22 17:32:45167 // Do nothing; unlink() isn't supported on NaCl.
[email protected]f6abeba2008-08-08 13:27:28168#else
[email protected]614e9fa2008-08-11 22:52:59169 unlink(log_name.c_str());
[email protected]f6abeba2008-08-08 13:27:28170#endif
171}
initial.commitd7cae122008-07-26 21:49:38172
[email protected]5f95d532010-10-01 17:16:58173PathString GetDefaultLogFile() {
[email protected]5b84fe32010-09-14 22:24:55174#if defined(OS_WIN)
175 // On Windows we use the same path as the exe.
176 wchar_t module_name[MAX_PATH];
thestig3e4787d2015-05-19 19:31:52177 GetModuleFileName(nullptr, module_name, MAX_PATH);
[email protected]5f95d532010-10-01 17:16:58178
scottmgfc5b7072015-01-27 21:46:28179 PathString log_name = module_name;
180 PathString::size_type last_backslash = log_name.rfind('\\', log_name.size());
[email protected]5f95d532010-10-01 17:16:58181 if (last_backslash != PathString::npos)
scottmgfc5b7072015-01-27 21:46:28182 log_name.erase(last_backslash + 1);
183 log_name += L"debug.log";
184 return log_name;
[email protected]5b84fe32010-09-14 22:24:55185#elif defined(OS_POSIX)
186 // On other platforms we just use the current directory.
[email protected]5f95d532010-10-01 17:16:58187 return PathString("debug.log");
[email protected]5b84fe32010-09-14 22:24:55188#endif
189}
190
ananta61762fb2015-09-18 01:00:09191// We don't need locks on Windows for atomically appending to files. The OS
192// provides this functionality.
193#if !defined(OS_WIN)
[email protected]5b84fe32010-09-14 22:24:55194// This class acts as a wrapper for locking the logging files.
195// LoggingLock::Init() should be called from the main thread before any logging
196// is done. Then whenever logging, be sure to have a local LoggingLock
197// instance on the stack. This will ensure that the lock is unlocked upon
198// exiting the frame.
199// LoggingLocks can not be nested.
200class LoggingLock {
201 public:
202 LoggingLock() {
203 LockLogging();
204 }
205
206 ~LoggingLock() {
207 UnlockLogging();
208 }
209
210 static void Init(LogLockingState lock_log, const PathChar* new_log_file) {
211 if (initialized)
212 return;
213 lock_log_file = lock_log;
[email protected]5f95d532010-10-01 17:16:58214
ananta61762fb2015-09-18 01:00:09215 if (lock_log_file != LOCK_LOG_FILE)
[email protected]bc581a682011-01-01 23:16:20216 log_lock = new base::internal::LockImpl();
ananta61762fb2015-09-18 01:00:09217
[email protected]5b84fe32010-09-14 22:24:55218 initialized = true;
219 }
220
221 private:
222 static void LockLogging() {
223 if (lock_log_file == LOCK_LOG_FILE) {
ananta61762fb2015-09-18 01:00:09224#if defined(OS_POSIX)
[email protected]5b84fe32010-09-14 22:24:55225 pthread_mutex_lock(&log_mutex);
226#endif
227 } else {
228 // use the lock
229 log_lock->Lock();
230 }
231 }
232
233 static void UnlockLogging() {
234 if (lock_log_file == LOCK_LOG_FILE) {
ananta61762fb2015-09-18 01:00:09235#if defined(OS_POSIX)
[email protected]5b84fe32010-09-14 22:24:55236 pthread_mutex_unlock(&log_mutex);
237#endif
238 } else {
239 log_lock->Unlock();
240 }
241 }
242
243 // The lock is used if log file locking is false. It helps us avoid problems
244 // with multiple threads writing to the log file at the same time. Use
245 // LockImpl directly instead of using Lock, because Lock makes logging calls.
[email protected]bc581a682011-01-01 23:16:20246 static base::internal::LockImpl* log_lock;
[email protected]5b84fe32010-09-14 22:24:55247
248 // When we don't use a lock, we are using a global mutex. We need to do this
249 // because LockFileEx is not thread safe.
ananta61762fb2015-09-18 01:00:09250#if defined(OS_POSIX)
[email protected]5b84fe32010-09-14 22:24:55251 static pthread_mutex_t log_mutex;
252#endif
253
254 static bool initialized;
255 static LogLockingState lock_log_file;
256};
257
258// static
259bool LoggingLock::initialized = false;
260// static
thestig3e4787d2015-05-19 19:31:52261base::internal::LockImpl* LoggingLock::log_lock = nullptr;
[email protected]5b84fe32010-09-14 22:24:55262// static
263LogLockingState LoggingLock::lock_log_file = LOCK_LOG_FILE;
264
ananta61762fb2015-09-18 01:00:09265#if defined(OS_POSIX)
[email protected]5b84fe32010-09-14 22:24:55266pthread_mutex_t LoggingLock::log_mutex = PTHREAD_MUTEX_INITIALIZER;
267#endif
268
ananta61762fb2015-09-18 01:00:09269#endif // OS_WIN
270
thestig3e4787d2015-05-19 19:31:52271// Called by logging functions to ensure that |g_log_file| is initialized
initial.commitd7cae122008-07-26 21:49:38272// and can be used for writing. Returns false if the file could not be
thestig3e4787d2015-05-19 19:31:52273// initialized. |g_log_file| will be nullptr in this case.
initial.commitd7cae122008-07-26 21:49:38274bool InitializeLogFileHandle() {
thestig3e4787d2015-05-19 19:31:52275 if (g_log_file)
initial.commitd7cae122008-07-26 21:49:38276 return true;
277
thestig3e4787d2015-05-19 19:31:52278 if (!g_log_file_name) {
[email protected]614e9fa2008-08-11 22:52:59279 // Nobody has called InitLogging to specify a debug log file, so here we
280 // initialize the log file name to a default.
thestig3e4787d2015-05-19 19:31:52281 g_log_file_name = new PathString(GetDefaultLogFile());
initial.commitd7cae122008-07-26 21:49:38282 }
283
thestig3e4787d2015-05-19 19:31:52284 if ((g_logging_destination & LOG_TO_FILE) != 0) {
[email protected]614e9fa2008-08-11 22:52:59285#if defined(OS_WIN)
ananta61762fb2015-09-18 01:00:09286 // The FILE_APPEND_DATA access mask ensures that the file is atomically
287 // appended to across accesses from multiple threads.
288 // https://msdn.microsoft.com/en-us/library/windows/desktop/aa364399(v=vs.85).aspx
289 // https://msdn.microsoft.com/en-us/library/windows/desktop/aa363858(v=vs.85).aspx
290 g_log_file = CreateFile(g_log_file_name->c_str(), FILE_APPEND_DATA,
thestig3e4787d2015-05-19 19:31:52291 FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr,
292 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr);
293 if (g_log_file == INVALID_HANDLE_VALUE || g_log_file == nullptr) {
[email protected]1d8c2702008-08-19 23:39:32294 // try the current directory
ananta61762fb2015-09-18 01:00:09295 base::FilePath file_path;
296 if (!base::GetCurrentDirectory(&file_path))
297 return false;
298
299 *g_log_file_name = file_path.Append(
300 FILE_PATH_LITERAL("debug.log")).value();
301
302 g_log_file = CreateFile(g_log_file_name->c_str(), FILE_APPEND_DATA,
thestig3e4787d2015-05-19 19:31:52303 FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr,
304 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr);
305 if (g_log_file == INVALID_HANDLE_VALUE || g_log_file == nullptr) {
306 g_log_file = nullptr;
[email protected]1d8c2702008-08-19 23:39:32307 return false;
308 }
initial.commitd7cae122008-07-26 21:49:38309 }
[email protected]78c6dd62009-06-08 23:29:11310#elif defined(OS_POSIX)
thestig3e4787d2015-05-19 19:31:52311 g_log_file = fopen(g_log_file_name->c_str(), "a");
312 if (g_log_file == nullptr)
[email protected]78c6dd62009-06-08 23:29:11313 return false;
[email protected]f6abeba2008-08-08 13:27:28314#endif
[email protected]1d8c2702008-08-19 23:39:32315 }
316
initial.commitd7cae122008-07-26 21:49:38317 return true;
318}
319
[email protected]17dcf752013-07-15 21:47:09320void CloseFile(FileHandle log) {
321#if defined(OS_WIN)
322 CloseHandle(log);
323#else
324 fclose(log);
325#endif
326}
327
328void CloseLogFileUnlocked() {
thestig3e4787d2015-05-19 19:31:52329 if (!g_log_file)
[email protected]17dcf752013-07-15 21:47:09330 return;
331
thestig3e4787d2015-05-19 19:31:52332 CloseFile(g_log_file);
333 g_log_file = nullptr;
[email protected]17dcf752013-07-15 21:47:09334}
335
[email protected]064aa162011-12-03 00:30:08336} // namespace
337
[email protected]5e3f7c22013-06-21 21:15:33338LoggingSettings::LoggingSettings()
339 : logging_dest(LOG_DEFAULT),
thestig3e4787d2015-05-19 19:31:52340 log_file(nullptr),
[email protected]5e3f7c22013-06-21 21:15:33341 lock_log(LOCK_LOG_FILE),
[email protected]1a1505512014-03-10 18:23:38342 delete_old(APPEND_TO_OLD_LOG_FILE) {}
[email protected]064aa162011-12-03 00:30:08343
[email protected]5e3f7c22013-06-21 21:15:33344bool BaseInitLoggingImpl(const LoggingSettings& settings) {
[email protected]ac07ec52013-04-22 17:32:45345#if defined(OS_NACL)
[email protected]5e3f7c22013-06-21 21:15:33346 // Can log only to the system debug log.
347 CHECK_EQ(settings.logging_dest & ~LOG_TO_SYSTEM_DEBUG_LOG, 0);
[email protected]ac07ec52013-04-22 17:32:45348#endif
pgal.u-szeged421dddb2014-11-25 12:55:02349 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
thestig3e4787d2015-05-19 19:31:52350 // Don't bother initializing |g_vlog_info| unless we use one of the
[email protected]99b7c57f2010-09-29 19:26:36351 // vlog switches.
352 if (command_line->HasSwitch(switches::kV) ||
353 command_line->HasSwitch(switches::kVModule)) {
thestig3e4787d2015-05-19 19:31:52354 // NOTE: If |g_vlog_info| has already been initialized, it might be in use
[email protected]064aa162011-12-03 00:30:08355 // by another thread. Don't delete the old VLogInfo, just create a second
356 // one. We keep track of both to avoid memory leak warnings.
357 CHECK(!g_vlog_info_prev);
358 g_vlog_info_prev = g_vlog_info;
359
[email protected]99b7c57f2010-09-29 19:26:36360 g_vlog_info =
361 new VlogInfo(command_line->GetSwitchValueASCII(switches::kV),
[email protected]162ac0f2010-11-04 15:50:49362 command_line->GetSwitchValueASCII(switches::kVModule),
thestig3e4787d2015-05-19 19:31:52363 &g_min_log_level);
[email protected]99b7c57f2010-09-29 19:26:36364 }
365
thestig3e4787d2015-05-19 19:31:52366 g_logging_destination = settings.logging_dest;
initial.commitd7cae122008-07-26 21:49:38367
[email protected]5e3f7c22013-06-21 21:15:33368 // ignore file options unless logging to file is set.
thestig3e4787d2015-05-19 19:31:52369 if ((g_logging_destination & LOG_TO_FILE) == 0)
[email protected]c7d5da992010-10-28 00:20:21370 return true;
initial.commitd7cae122008-07-26 21:49:38371
ananta61762fb2015-09-18 01:00:09372#if !defined(OS_WIN)
[email protected]17dcf752013-07-15 21:47:09373 LoggingLock::Init(settings.lock_log, settings.log_file);
374 LoggingLock logging_lock;
ananta61762fb2015-09-18 01:00:09375#endif
[email protected]17dcf752013-07-15 21:47:09376
377 // Calling InitLogging twice or after some log call has already opened the
378 // default log file will re-initialize to the new options.
379 CloseLogFileUnlocked();
380
thestig3e4787d2015-05-19 19:31:52381 if (!g_log_file_name)
382 g_log_file_name = new PathString();
383 *g_log_file_name = settings.log_file;
[email protected]5e3f7c22013-06-21 21:15:33384 if (settings.delete_old == DELETE_OLD_LOG_FILE)
thestig3e4787d2015-05-19 19:31:52385 DeleteFilePath(*g_log_file_name);
initial.commitd7cae122008-07-26 21:49:38386
[email protected]c7d5da992010-10-28 00:20:21387 return InitializeLogFileHandle();
initial.commitd7cae122008-07-26 21:49:38388}
389
390void SetMinLogLevel(int level) {
thestig3e4787d2015-05-19 19:31:52391 g_min_log_level = std::min(LOG_FATAL, level);
initial.commitd7cae122008-07-26 21:49:38392}
393
394int GetMinLogLevel() {
thestig3e4787d2015-05-19 19:31:52395 return g_min_log_level;
initial.commitd7cae122008-07-26 21:49:38396}
397
skobesc78c0ad72015-12-07 20:21:23398bool ShouldCreateLogMessage(int severity) {
399 if (severity < g_min_log_level)
400 return false;
401
402 // Return true here unless we know ~LogMessage won't do anything. Note that
403 // ~LogMessage writes to stderr if severity_ >= kAlwaysPrintErrorLevel, even
404 // when g_logging_destination is LOG_NONE.
405 return g_logging_destination != LOG_NONE || log_message_handler ||
406 severity >= kAlwaysPrintErrorLevel;
407}
408
[email protected]162ac0f2010-11-04 15:50:49409int GetVlogVerbosity() {
410 return std::max(-1, LOG_INFO - GetMinLogLevel());
411}
412
[email protected]99b7c57f2010-09-29 19:26:36413int GetVlogLevelHelper(const char* file, size_t N) {
414 DCHECK_GT(N, 0U);
thestig3e4787d2015-05-19 19:31:52415 // Note: |g_vlog_info| may change on a different thread during startup
416 // (but will always be valid or nullptr).
[email protected]064aa162011-12-03 00:30:08417 VlogInfo* vlog_info = g_vlog_info;
418 return vlog_info ?
419 vlog_info->GetVlogLevel(base::StringPiece(file, N - 1)) :
[email protected]162ac0f2010-11-04 15:50:49420 GetVlogVerbosity();
[email protected]99b7c57f2010-09-29 19:26:36421}
422
initial.commitd7cae122008-07-26 21:49:38423void SetLogItems(bool enable_process_id, bool enable_thread_id,
424 bool enable_timestamp, bool enable_tickcount) {
thestig3e4787d2015-05-19 19:31:52425 g_log_process_id = enable_process_id;
426 g_log_thread_id = enable_thread_id;
427 g_log_timestamp = enable_timestamp;
428 g_log_tickcount = enable_tickcount;
initial.commitd7cae122008-07-26 21:49:38429}
430
[email protected]81e0a852010-08-17 00:38:12431void SetShowErrorDialogs(bool enable_dialogs) {
432 show_error_dialogs = enable_dialogs;
433}
434
initial.commitd7cae122008-07-26 21:49:38435void SetLogAssertHandler(LogAssertHandlerFunction handler) {
436 log_assert_handler = handler;
437}
438
[email protected]2b07b8412009-11-25 15:26:34439void SetLogMessageHandler(LogMessageHandlerFunction handler) {
440 log_message_handler = handler;
441}
442
[email protected]64e5cc02010-11-03 19:20:27443LogMessageHandlerFunction GetLogMessageHandler() {
444 return log_message_handler;
445}
446
[email protected]6d445d32010-09-30 19:10:03447// Explicit instantiations for commonly used comparisons.
448template std::string* MakeCheckOpString<int, int>(
449 const int&, const int&, const char* names);
450template std::string* MakeCheckOpString<unsigned long, unsigned long>(
451 const unsigned long&, const unsigned long&, const char* names);
452template std::string* MakeCheckOpString<unsigned long, unsigned int>(
453 const unsigned long&, const unsigned int&, const char* names);
454template std::string* MakeCheckOpString<unsigned int, unsigned long>(
455 const unsigned int&, const unsigned long&, const char* names);
456template std::string* MakeCheckOpString<std::string, std::string>(
457 const std::string&, const std::string&, const char* name);
[email protected]2b07b8412009-11-25 15:26:34458
[email protected]f2c05492014-06-17 12:04:23459#if !defined(NDEBUG)
[email protected]d81baca42010-03-01 13:10:22460// Displays a message box to the user with the error message in it.
461// Used for fatal messages, where we close the app simultaneously.
[email protected]561513f2010-12-16 23:29:25462// This is for developers only; we don't use this in circumstances
463// (like release builds) where users could see it, since users don't
464// understand these messages anyway.
[email protected]d81baca42010-03-01 13:10:22465void DisplayDebugMessageInDialog(const std::string& str) {
initial.commitd7cae122008-07-26 21:49:38466 if (str.empty())
467 return;
468
[email protected]81e0a852010-08-17 00:38:12469 if (!show_error_dialogs)
[email protected]846ed9c32010-07-29 20:33:44470 return;
471
[email protected]f6abeba2008-08-08 13:27:28472#if defined(OS_WIN)
brettwe33526852015-10-03 00:46:01473 MessageBoxW(nullptr, base::UTF8ToUTF16(str).c_str(), L"Fatal error",
474 MB_OK | MB_ICONHAND | MB_TOPMOST);
[email protected]f6abeba2008-08-08 13:27:28475#else
[email protected]561513f2010-12-16 23:29:25476 // We intentionally don't implement a dialog on other platforms.
477 // You can just look at stderr.
thestig3e4787d2015-05-19 19:31:52478#endif // defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:38479}
[email protected]f2c05492014-06-17 12:04:23480#endif // !defined(NDEBUG)
initial.commitd7cae122008-07-26 21:49:38481
[email protected]3f85caa2009-04-14 16:52:11482#if defined(OS_WIN)
483LogMessage::SaveLastError::SaveLastError() : last_error_(::GetLastError()) {
484}
485
486LogMessage::SaveLastError::~SaveLastError() {
487 ::SetLastError(last_error_);
488}
489#endif // defined(OS_WIN)
490
[email protected]eae9c062011-01-11 00:50:59491LogMessage::LogMessage(const char* file, int line, LogSeverity severity)
492 : severity_(severity), file_(file), line_(line) {
493 Init(file, line);
494}
495
tnagel4a045d3f2015-07-12 14:19:28496LogMessage::LogMessage(const char* file, int line, const char* condition)
497 : severity_(LOG_FATAL), file_(file), line_(line) {
498 Init(file, line);
499 stream_ << "Check failed: " << condition << ". ";
500}
501
[email protected]9c7132e2011-02-08 07:39:08502LogMessage::LogMessage(const char* file, int line, std::string* result)
[email protected]162ac0f2010-11-04 15:50:49503 : severity_(LOG_FATAL), file_(file), line_(line) {
initial.commitd7cae122008-07-26 21:49:38504 Init(file, line);
[email protected]9c7132e2011-02-08 07:39:08505 stream_ << "Check failed: " << *result;
506 delete result;
initial.commitd7cae122008-07-26 21:49:38507}
508
[email protected]fb62a532009-02-12 01:19:05509LogMessage::LogMessage(const char* file, int line, LogSeverity severity,
[email protected]9c7132e2011-02-08 07:39:08510 std::string* result)
[email protected]162ac0f2010-11-04 15:50:49511 : severity_(severity), file_(file), line_(line) {
[email protected]fb62a532009-02-12 01:19:05512 Init(file, line);
[email protected]9c7132e2011-02-08 07:39:08513 stream_ << "Check failed: " << *result;
514 delete result;
[email protected]fb62a532009-02-12 01:19:05515}
516
initial.commitd7cae122008-07-26 21:49:38517LogMessage::~LogMessage() {
jam79dc59a2015-08-17 03:38:16518#if !defined(OFFICIAL_BUILD) && !defined(OS_NACL) && !defined(__UCLIBC__)
brucedawson7c559eb2015-09-05 00:34:42519 if (severity_ == LOG_FATAL && !base::debug::BeingDebugged()) {
520 // Include a stack trace on a fatal, unless a debugger is attached.
[email protected]58580352010-10-26 04:07:50521 base::debug::StackTrace trace;
[email protected]d1ccc35a2010-03-24 05:03:24522 stream_ << std::endl; // Newline to separate from log message.
523 trace.OutputToStream(&stream_);
524 }
[email protected]1d8c2702008-08-19 23:39:32525#endif
[email protected]d1ccc35a2010-03-24 05:03:24526 stream_ << std::endl;
527 std::string str_newline(stream_.str());
528
[email protected]2b07b8412009-11-25 15:26:34529 // Give any log message handler first dibs on the message.
[email protected]5e3f7c22013-06-21 21:15:33530 if (log_message_handler &&
531 log_message_handler(severity_, file_, line_,
532 message_start_, str_newline)) {
[email protected]162ac0f2010-11-04 15:50:49533 // The handler took care of it, no further processing.
[email protected]2b07b8412009-11-25 15:26:34534 return;
[email protected]162ac0f2010-11-04 15:50:49535 }
initial.commitd7cae122008-07-26 21:49:38536
thestig3e4787d2015-05-19 19:31:52537 if ((g_logging_destination & LOG_TO_SYSTEM_DEBUG_LOG) != 0) {
[email protected]f6abeba2008-08-08 13:27:28538#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:38539 OutputDebugStringA(str_newline.c_str());
mark4c7449c2015-11-10 19:53:42540#elif defined(OS_MACOSX)
541 // In LOG_TO_SYSTEM_DEBUG_LOG mode, log messages are always written to
542 // stderr. If stderr is /dev/null, also log via ASL (Apple System Log). If
543 // there's something weird about stderr, assume that log messages are going
544 // nowhere and log via ASL too. Messages logged via ASL show up in
545 // Console.app.
546 //
547 // Programs started by launchd, as UI applications normally are, have had
548 // stderr connected to /dev/null since OS X 10.8. Prior to that, stderr was
549 // a pipe to launchd, which logged what it received (see log_redirect_fd in
550 // 10.7.5 launchd-392.39/launchd/src/launchd_core_logic.c).
551 //
552 // Another alternative would be to determine whether stderr is a pipe to
553 // launchd and avoid logging via ASL only in that case. See 10.7.5
554 // CF-635.21/CFUtilities.c also_do_stderr(). This would result in logging to
555 // both stderr and ASL even in tests, where it's undesirable to log to the
556 // system log at all.
557 //
558 // Note that the ASL client by default discards messages whose levels are
559 // below ASL_LEVEL_NOTICE. It's possible to change that with
560 // asl_set_filter(), but this is pointless because syslogd normally applies
561 // the same filter.
562 const bool log_via_asl = []() {
563 struct stat stderr_stat;
564 if (fstat(fileno(stderr), &stderr_stat) == -1) {
565 return true;
566 }
567 if (!S_ISCHR(stderr_stat.st_mode)) {
568 return false;
569 }
570
571 struct stat dev_null_stat;
572 if (stat(_PATH_DEVNULL, &dev_null_stat) == -1) {
573 return true;
574 }
575
576 return !S_ISCHR(dev_null_stat.st_mode) ||
577 stderr_stat.st_rdev == dev_null_stat.st_rdev;
578 }();
579
580 if (log_via_asl) {
581 // Log roughly the same way that CFLog() and NSLog() would. See 10.10.5
582 // CF-1153.18/CFUtilities.c __CFLogCString().
583 //
584 // The ASL facility is set to the main bundle ID if available. Otherwise,
585 // "com.apple.console" is used.
586 CFBundleRef main_bundle = CFBundleGetMainBundle();
587 CFStringRef main_bundle_id_cf =
588 main_bundle ? CFBundleGetIdentifier(main_bundle) : nullptr;
589 std::string asl_facility =
590 main_bundle_id_cf ? base::SysCFStringRefToUTF8(main_bundle_id_cf)
591 : std::string("com.apple.console");
592
593 class ASLClient {
594 public:
595 explicit ASLClient(const std::string& asl_facility)
596 : client_(asl_open(nullptr,
597 asl_facility.c_str(),
598 ASL_OPT_NO_DELAY)) {}
599 ~ASLClient() { asl_close(client_); }
600
601 aslclient get() const { return client_; }
602
603 private:
604 aslclient client_;
605 DISALLOW_COPY_AND_ASSIGN(ASLClient);
606 } asl_client(asl_facility);
607
608 class ASLMessage {
609 public:
610 ASLMessage() : message_(asl_new(ASL_TYPE_MSG)) {}
611 ~ASLMessage() { asl_free(message_); }
612
613 aslmsg get() const { return message_; }
614
615 private:
616 aslmsg message_;
617 DISALLOW_COPY_AND_ASSIGN(ASLMessage);
618 } asl_message;
619
620 // By default, messages are only readable by the admin group. Explicitly
621 // make them readable by the user generating the messages.
622 char euid_string[12];
623 snprintf(euid_string, arraysize(euid_string), "%d", geteuid());
624 asl_set(asl_message.get(), ASL_KEY_READ_UID, euid_string);
625
626 // Map Chrome log severities to ASL log levels.
627 const char* const asl_level_string = [](LogSeverity severity) {
628 // ASL_LEVEL_* are ints, but ASL needs equivalent strings. This
629 // non-obvious two-step macro trick achieves what's needed.
630 // https://gcc.gnu.org/onlinedocs/cpp/Stringification.html
631#define ASL_LEVEL_STR(level) ASL_LEVEL_STR_X(level)
632#define ASL_LEVEL_STR_X(level) #level
633 switch (severity) {
634 case LOG_INFO:
635 return ASL_LEVEL_STR(ASL_LEVEL_INFO);
636 case LOG_WARNING:
637 return ASL_LEVEL_STR(ASL_LEVEL_WARNING);
638 case LOG_ERROR:
639 return ASL_LEVEL_STR(ASL_LEVEL_ERR);
640 case LOG_FATAL:
641 return ASL_LEVEL_STR(ASL_LEVEL_CRIT);
642 default:
643 return severity < 0 ? ASL_LEVEL_STR(ASL_LEVEL_DEBUG)
644 : ASL_LEVEL_STR(ASL_LEVEL_NOTICE);
645 }
646#undef ASL_LEVEL_STR
647#undef ASL_LEVEL_STR_X
648 }(severity_);
649 asl_set(asl_message.get(), ASL_KEY_LEVEL, asl_level_string);
650
651 asl_set(asl_message.get(), ASL_KEY_MSG, str_newline.c_str());
652
653 asl_send(asl_client.get(), asl_message.get());
654 }
[email protected]3132e35c2011-07-07 20:46:50655#elif defined(OS_ANDROID)
[email protected]efbae7da2013-05-21 22:39:25656 android_LogPriority priority =
657 (severity_ < 0) ? ANDROID_LOG_VERBOSE : ANDROID_LOG_UNKNOWN;
[email protected]3132e35c2011-07-07 20:46:50658 switch (severity_) {
659 case LOG_INFO:
660 priority = ANDROID_LOG_INFO;
661 break;
662 case LOG_WARNING:
663 priority = ANDROID_LOG_WARN;
664 break;
665 case LOG_ERROR:
[email protected]3132e35c2011-07-07 20:46:50666 priority = ANDROID_LOG_ERROR;
667 break;
668 case LOG_FATAL:
669 priority = ANDROID_LOG_FATAL;
670 break;
671 }
672 __android_log_write(priority, "chromium", str_newline.c_str());
[email protected]107bc0f12008-08-26 17:48:18673#endif
[email protected]51105382014-03-14 17:02:15674 ignore_result(fwrite(str_newline.data(), str_newline.size(), 1, stderr));
[email protected]469006c2010-09-24 15:43:06675 fflush(stderr);
[email protected]a33c9892008-08-25 20:10:31676 } else if (severity_ >= kAlwaysPrintErrorLevel) {
677 // When we're only outputting to a log file, above a certain log level, we
678 // should still output to stderr so that we can better detect and diagnose
679 // problems with unit tests, especially on the buildbots.
[email protected]51105382014-03-14 17:02:15680 ignore_result(fwrite(str_newline.data(), str_newline.size(), 1, stderr));
[email protected]1ce41052009-12-02 00:34:02681 fflush(stderr);
[email protected]f6abeba2008-08-08 13:27:28682 }
[email protected]52a261f2009-03-03 15:01:12683
initial.commitd7cae122008-07-26 21:49:38684 // write to log file
thestig3e4787d2015-05-19 19:31:52685 if ((g_logging_destination & LOG_TO_FILE) != 0) {
[email protected]17dcf752013-07-15 21:47:09686 // We can have multiple threads and/or processes, so try to prevent them
687 // from clobbering each other's writes.
688 // If the client app did not call InitLogging, and the lock has not
689 // been created do it now. We do this on demand, but if two threads try
690 // to do this at the same time, there will be a race condition to create
691 // the lock. This is why InitLogging should be called from the main
692 // thread at the beginning of execution.
ananta61762fb2015-09-18 01:00:09693#if !defined(OS_WIN)
thestig3e4787d2015-05-19 19:31:52694 LoggingLock::Init(LOCK_LOG_FILE, nullptr);
[email protected]5b84fe32010-09-14 22:24:55695 LoggingLock logging_lock;
ananta61762fb2015-09-18 01:00:09696#endif
[email protected]5b84fe32010-09-14 22:24:55697 if (InitializeLogFileHandle()) {
[email protected]f6abeba2008-08-08 13:27:28698#if defined(OS_WIN)
[email protected]5b84fe32010-09-14 22:24:55699 DWORD num_written;
thestig3e4787d2015-05-19 19:31:52700 WriteFile(g_log_file,
[email protected]5b84fe32010-09-14 22:24:55701 static_cast<const void*>(str_newline.c_str()),
702 static_cast<DWORD>(str_newline.length()),
703 &num_written,
thestig3e4787d2015-05-19 19:31:52704 nullptr);
[email protected]cba21962010-08-31 22:35:55705#else
[email protected]51105382014-03-14 17:02:15706 ignore_result(fwrite(
thestig3e4787d2015-05-19 19:31:52707 str_newline.data(), str_newline.size(), 1, g_log_file));
708 fflush(g_log_file);
[email protected]cba21962010-08-31 22:35:55709#endif
initial.commitd7cae122008-07-26 21:49:38710 }
711 }
712
713 if (severity_ == LOG_FATAL) {
[email protected]eb4c4d032012-04-03 18:45:05714 // Ensure the first characters of the string are on the stack so they
715 // are contained in minidumps for diagnostic purposes.
716 char str_stack[1024];
717 str_newline.copy(str_stack, arraysize(str_stack));
718 base::debug::Alias(str_stack);
719
[email protected]82d89ab2014-02-28 18:25:34720 if (log_assert_handler) {
721 // Make a copy of the string for the handler out of paranoia.
722 log_assert_handler(std::string(stream_.str()));
[email protected]1ffe08c12008-08-13 11:15:11723 } else {
[email protected]82d89ab2014-02-28 18:25:34724 // Don't use the string with the newline, get a fresh version to send to
725 // the debug message process. We also don't display assertions to the
726 // user in release mode. The enduser can't do anything with this
727 // information, and displaying message boxes when the application is
728 // hosed can cause additional problems.
[email protected]4d5901272008-11-06 00:33:50729#ifndef NDEBUG
brucedawson7c559eb2015-09-05 00:34:42730 if (!base::debug::BeingDebugged()) {
731 // Displaying a dialog is unnecessary when debugging and can complicate
732 // debugging.
733 DisplayDebugMessageInDialog(stream_.str());
734 }
[email protected]4d5901272008-11-06 00:33:50735#endif
[email protected]82d89ab2014-02-28 18:25:34736 // Crash the process to generate a dump.
737 base::debug::BreakDebugger();
initial.commitd7cae122008-07-26 21:49:38738 }
739 }
740}
741
[email protected]eae9c062011-01-11 00:50:59742// writes the common header info to the stream
743void LogMessage::Init(const char* file, int line) {
744 base::StringPiece filename(file);
745 size_t last_slash_pos = filename.find_last_of("\\/");
746 if (last_slash_pos != base::StringPiece::npos)
747 filename.remove_prefix(last_slash_pos + 1);
748
749 // TODO(darin): It might be nice if the columns were fixed width.
750
751 stream_ << '[';
thestig3e4787d2015-05-19 19:31:52752 if (g_log_process_id)
[email protected]eae9c062011-01-11 00:50:59753 stream_ << CurrentProcessId() << ':';
thestig3e4787d2015-05-19 19:31:52754 if (g_log_thread_id)
[email protected]63e66802012-01-18 21:21:09755 stream_ << base::PlatformThread::CurrentId() << ':';
thestig3e4787d2015-05-19 19:31:52756 if (g_log_timestamp) {
757 time_t t = time(nullptr);
[email protected]eae9c062011-01-11 00:50:59758 struct tm local_time = {0};
mostynb7e42a8f2014-12-19 12:47:46759#ifdef _MSC_VER
[email protected]eae9c062011-01-11 00:50:59760 localtime_s(&local_time, &t);
761#else
762 localtime_r(&t, &local_time);
763#endif
764 struct tm* tm_time = &local_time;
765 stream_ << std::setfill('0')
766 << std::setw(2) << 1 + tm_time->tm_mon
767 << std::setw(2) << tm_time->tm_mday
768 << '/'
769 << std::setw(2) << tm_time->tm_hour
770 << std::setw(2) << tm_time->tm_min
771 << std::setw(2) << tm_time->tm_sec
772 << ':';
773 }
thestig3e4787d2015-05-19 19:31:52774 if (g_log_tickcount)
[email protected]eae9c062011-01-11 00:50:59775 stream_ << TickCount() << ':';
776 if (severity_ >= 0)
[email protected]80f360a2014-01-23 01:36:19777 stream_ << log_severity_name(severity_);
[email protected]eae9c062011-01-11 00:50:59778 else
779 stream_ << "VERBOSE" << -severity_;
780
781 stream_ << ":" << filename << "(" << line << ")] ";
782
pkasting9cf9b942014-10-01 22:18:43783 message_start_ = stream_.str().length();
[email protected]eae9c062011-01-11 00:50:59784}
785
[email protected]d8617a62009-10-09 23:52:20786#if defined(OS_WIN)
787// This has already been defined in the header, but defining it again as DWORD
788// ensures that the type used in the header is equivalent to DWORD. If not,
789// the redefinition is a compile error.
790typedef DWORD SystemErrorCode;
791#endif
792
793SystemErrorCode GetLastSystemErrorCode() {
794#if defined(OS_WIN)
795 return ::GetLastError();
796#elif defined(OS_POSIX)
797 return errno;
798#else
799#error Not implemented
800#endif
801}
802
803#if defined(OS_WIN)
[email protected]c914d8a2014-04-23 01:11:01804BASE_EXPORT std::string SystemErrorCodeToString(SystemErrorCode error_code) {
thestig75f87352014-12-03 21:42:27805 const int kErrorMessageBufferSize = 256;
806 char msgbuf[kErrorMessageBufferSize];
[email protected]c914d8a2014-04-23 01:11:01807 DWORD flags = FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS;
thestig3e4787d2015-05-19 19:31:52808 DWORD len = FormatMessageA(flags, nullptr, error_code, 0, msgbuf,
809 arraysize(msgbuf), nullptr);
[email protected]c914d8a2014-04-23 01:11:01810 if (len) {
811 // Messages returned by system end with line breaks.
812 return base::CollapseWhitespaceASCII(msgbuf, true) +
813 base::StringPrintf(" (0x%X)", error_code);
814 }
815 return base::StringPrintf("Error (0x%X) while retrieving error. (0x%X)",
816 GetLastError(), error_code);
[email protected]d8617a62009-10-09 23:52:20817}
[email protected]c914d8a2014-04-23 01:11:01818#elif defined(OS_POSIX)
819BASE_EXPORT std::string SystemErrorCodeToString(SystemErrorCode error_code) {
brettw6ee6fd62015-06-09 18:05:24820 return base::safe_strerror(error_code);
[email protected]c914d8a2014-04-23 01:11:01821}
822#else
823#error Not implemented
thestig3e4787d2015-05-19 19:31:52824#endif // defined(OS_WIN)
[email protected]d8617a62009-10-09 23:52:20825
[email protected]c914d8a2014-04-23 01:11:01826
827#if defined(OS_WIN)
[email protected]d8617a62009-10-09 23:52:20828Win32ErrorLogMessage::Win32ErrorLogMessage(const char* file,
829 int line,
830 LogSeverity severity,
831 SystemErrorCode err)
832 : err_(err),
[email protected]d8617a62009-10-09 23:52:20833 log_message_(file, line, severity) {
834}
835
836Win32ErrorLogMessage::~Win32ErrorLogMessage() {
[email protected]c914d8a2014-04-23 01:11:01837 stream() << ": " << SystemErrorCodeToString(err_);
[email protected]20909e72012-04-05 16:57:06838 // We're about to crash (CHECK). Put |err_| on the stack (by placing it in a
839 // field) and use Alias in hopes that it makes it into crash dumps.
840 DWORD last_error = err_;
841 base::debug::Alias(&last_error);
[email protected]d8617a62009-10-09 23:52:20842}
843#elif defined(OS_POSIX)
844ErrnoLogMessage::ErrnoLogMessage(const char* file,
845 int line,
846 LogSeverity severity,
847 SystemErrorCode err)
848 : err_(err),
849 log_message_(file, line, severity) {
850}
851
852ErrnoLogMessage::~ErrnoLogMessage() {
[email protected]c914d8a2014-04-23 01:11:01853 stream() << ": " << SystemErrorCodeToString(err_);
[email protected]d8617a62009-10-09 23:52:20854}
thestig3e4787d2015-05-19 19:31:52855#endif // defined(OS_WIN)
[email protected]d8617a62009-10-09 23:52:20856
initial.commitd7cae122008-07-26 21:49:38857void CloseLogFile() {
ananta61762fb2015-09-18 01:00:09858#if !defined(OS_WIN)
[email protected]5b84fe32010-09-14 22:24:55859 LoggingLock logging_lock;
ananta61762fb2015-09-18 01:00:09860#endif
[email protected]17dcf752013-07-15 21:47:09861 CloseLogFileUnlocked();
initial.commitd7cae122008-07-26 21:49:38862}
863
[email protected]e36ddc82009-12-08 04:22:50864void RawLog(int level, const char* message) {
thestig3e4787d2015-05-19 19:31:52865 if (level >= g_min_log_level) {
[email protected]e36ddc82009-12-08 04:22:50866 size_t bytes_written = 0;
867 const size_t message_len = strlen(message);
868 int rv;
869 while (bytes_written < message_len) {
870 rv = HANDLE_EINTR(
871 write(STDERR_FILENO, message + bytes_written,
872 message_len - bytes_written));
873 if (rv < 0) {
874 // Give up, nothing we can do now.
875 break;
876 }
877 bytes_written += rv;
878 }
879
880 if (message_len > 0 && message[message_len - 1] != '\n') {
881 do {
882 rv = HANDLE_EINTR(write(STDERR_FILENO, "\n", 1));
883 if (rv < 0) {
884 // Give up, nothing we can do now.
885 break;
886 }
887 } while (rv != 1);
888 }
889 }
890
891 if (level == LOG_FATAL)
[email protected]58580352010-10-26 04:07:50892 base::debug::BreakDebugger();
[email protected]e36ddc82009-12-08 04:22:50893}
894
[email protected]34a907732012-01-20 06:33:27895// This was defined at the beginning of this file.
896#undef write
897
[email protected]f01b88a2013-02-27 22:04:00898#if defined(OS_WIN)
ananta61762fb2015-09-18 01:00:09899bool IsLoggingToFileEnabled() {
900 return g_logging_destination & LOG_TO_FILE;
901}
902
[email protected]f01b88a2013-02-27 22:04:00903std::wstring GetLogFileFullPath() {
thestig3e4787d2015-05-19 19:31:52904 if (g_log_file_name)
905 return *g_log_file_name;
[email protected]f01b88a2013-02-27 22:04:00906 return std::wstring();
907}
908#endif
909
tnagel80388e682015-05-26 13:27:56910BASE_EXPORT void LogErrorNotReached(const char* file, int line) {
tnagelff3f34a2015-05-24 12:59:14911 LogMessage(file, line, LOG_ERROR).stream()
912 << "NOTREACHED() hit.";
913}
914
[email protected]96fd0032009-04-24 00:13:08915} // namespace logging
initial.commitd7cae122008-07-26 21:49:38916
[email protected]81411c62014-07-08 23:03:06917std::ostream& std::operator<<(std::ostream& out, const wchar_t* wstr) {
thestig75f87352014-12-03 21:42:27918 return out << base::WideToUTF8(wstr);
initial.commitd7cae122008-07-26 21:49:38919}