blob: 98cecd094fd84e7312c9a3e9998dfb44cfd05925 [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#endif
[email protected]052f1b52008-11-06 21:43:0734#include <time.h>
[email protected]614e9fa2008-08-11 22:52:5935#endif
36
37#if defined(OS_POSIX)
[email protected]d8617a62009-10-09 23:52:2038#include <errno.h>
mark4c7449c2015-11-10 19:53:4239#include <paths.h>
[email protected]166326c62010-08-05 15:50:2340#include <pthread.h>
[email protected]f6abeba2008-08-08 13:27:2841#include <stdio.h>
[email protected]eb62f7262013-03-30 14:29:0042#include <stdlib.h>
[email protected]f6abeba2008-08-08 13:27:2843#include <string.h>
mark4c7449c2015-11-10 19:53:4244#include <sys/stat.h>
[email protected]f6abeba2008-08-08 13:27:2845#include <unistd.h>
46#define MAX_PATH PATH_MAX
47typedef FILE* FileHandle;
48typedef pthread_mutex_t* MutexHandle;
49#endif
50
[email protected]1f88b5162011-04-01 00:02:2951#include <algorithm>
52#include <cstring>
initial.commitd7cae122008-07-26 21:49:3853#include <ctime>
54#include <iomanip>
[email protected]1f88b5162011-04-01 00:02:2955#include <ostream>
[email protected]c914d8a2014-04-23 01:11:0156#include <string>
[email protected]b16ef312008-08-19 18:36:2357
initial.commitd7cae122008-07-26 21:49:3858#include "base/base_switches.h"
59#include "base/command_line.h"
[email protected]eb4c4d032012-04-03 18:45:0560#include "base/debug/alias.h"
[email protected]58580352010-10-26 04:07:5061#include "base/debug/debugger.h"
62#include "base/debug/stack_trace.h"
[email protected]2025d002012-11-14 20:54:3563#include "base/posix/eintr_wrapper.h"
[email protected]eb62f7262013-03-30 14:29:0064#include "base/strings/string_piece.h"
[email protected]c914d8a2014-04-23 01:11:0165#include "base/strings/string_util.h"
66#include "base/strings/stringprintf.h"
mark4c7449c2015-11-10 19:53:4267#include "base/strings/sys_string_conversions.h"
[email protected]a4ea1f12013-06-07 18:37:0768#include "base/strings/utf_string_conversions.h"
[email protected]bc581a682011-01-01 23:16:2069#include "base/synchronization/lock_impl.h"
[email protected]63e66802012-01-18 21:21:0970#include "base/threading/platform_thread.h"
[email protected]99b7c57f2010-09-29 19:26:3671#include "base/vlog.h"
[email protected]53c7ce42010-12-14 16:20:0472#if defined(OS_POSIX)
brettw6ee6fd62015-06-09 18:05:2473#include "base/posix/safe_strerror.h"
[email protected]53c7ce42010-12-14 16:20:0474#endif
[email protected]52a261f2009-03-03 15:01:1275
[email protected]3132e35c2011-07-07 20:46:5076#if defined(OS_ANDROID)
77#include <android/log.h>
78#endif
79
initial.commitd7cae122008-07-26 21:49:3880namespace logging {
81
[email protected]064aa162011-12-03 00:30:0882namespace {
83
thestig3e4787d2015-05-19 19:31:5284VlogInfo* g_vlog_info = nullptr;
85VlogInfo* g_vlog_info_prev = nullptr;
initial.commitd7cae122008-07-26 21:49:3886
87const char* const log_severity_names[LOG_NUM_SEVERITIES] = {
[email protected]f2c05492014-06-17 12:04:2388 "INFO", "WARNING", "ERROR", "FATAL" };
initial.commitd7cae122008-07-26 21:49:3889
thestig75f87352014-12-03 21:42:2790const char* log_severity_name(int severity) {
[email protected]80f360a2014-01-23 01:36:1991 if (severity >= 0 && severity < LOG_NUM_SEVERITIES)
92 return log_severity_names[severity];
93 return "UNKNOWN";
94}
95
thestig3e4787d2015-05-19 19:31:5296int g_min_log_level = 0;
[email protected]1d8c2702008-08-19 23:39:3297
thestig3e4787d2015-05-19 19:31:5298LoggingDestination g_logging_destination = LOG_DEFAULT;
initial.commitd7cae122008-07-26 21:49:3899
[email protected]a33c9892008-08-25 20:10:31100// For LOG_ERROR and above, always print to stderr.
101const int kAlwaysPrintErrorLevel = LOG_ERROR;
102
[email protected]614e9fa2008-08-11 22:52:59103// Which log file to use? This is initialized by InitLogging or
initial.commitd7cae122008-07-26 21:49:38104// will be lazily initialized to the default value when it is
105// first needed.
[email protected]f6abeba2008-08-08 13:27:28106#if defined(OS_WIN)
[email protected]614e9fa2008-08-11 22:52:59107typedef std::wstring PathString;
[email protected]f6abeba2008-08-08 13:27:28108#else
[email protected]614e9fa2008-08-11 22:52:59109typedef std::string PathString;
[email protected]f6abeba2008-08-08 13:27:28110#endif
thestig3e4787d2015-05-19 19:31:52111PathString* g_log_file_name = nullptr;
initial.commitd7cae122008-07-26 21:49:38112
thestig3e4787d2015-05-19 19:31:52113// This file is lazily opened and the handle may be nullptr
114FileHandle g_log_file = nullptr;
initial.commitd7cae122008-07-26 21:49:38115
thestig3e4787d2015-05-19 19:31:52116// What should be prepended to each message?
117bool g_log_process_id = false;
118bool g_log_thread_id = false;
119bool g_log_timestamp = true;
120bool g_log_tickcount = false;
initial.commitd7cae122008-07-26 21:49:38121
[email protected]81e0a852010-08-17 00:38:12122// Should we pop up fatal debug messages in a dialog?
123bool show_error_dialogs = false;
124
initial.commitd7cae122008-07-26 21:49:38125// An assert handler override specified by the client to be called instead of
[email protected]fb62a532009-02-12 01:19:05126// the debug message dialog and process termination.
thestig3e4787d2015-05-19 19:31:52127LogAssertHandlerFunction log_assert_handler = nullptr;
[email protected]2b07b8412009-11-25 15:26:34128// A log message handler that gets notified of every log message we process.
thestig3e4787d2015-05-19 19:31:52129LogMessageHandlerFunction log_message_handler = nullptr;
initial.commitd7cae122008-07-26 21:49:38130
[email protected]f6abeba2008-08-08 13:27:28131// Helper functions to wrap platform differences.
132
avi9b6f42932015-12-26 22:15:14133int32_t CurrentProcessId() {
[email protected]f8588472008-11-05 23:17:24134#if defined(OS_WIN)
135 return GetCurrentProcessId();
136#elif defined(OS_POSIX)
137 return getpid();
138#endif
139}
140
avi9b6f42932015-12-26 22:15:14141uint64_t TickCount() {
[email protected]f8588472008-11-05 23:17:24142#if defined(OS_WIN)
143 return GetTickCount();
144#elif defined(OS_MACOSX)
145 return mach_absolute_time();
[email protected]19ea84ca2010-11-12 08:37:08146#elif defined(OS_NACL)
147 // NaCl sadly does not have _POSIX_TIMERS enabled in sys/features.h
148 // So we have to use clock() for now.
149 return clock();
[email protected]e43eddf12009-12-29 00:32:52150#elif defined(OS_POSIX)
[email protected]052f1b52008-11-06 21:43:07151 struct timespec ts;
152 clock_gettime(CLOCK_MONOTONIC, &ts);
153
avi9b6f42932015-12-26 22:15:14154 uint64_t absolute_micro = static_cast<int64_t>(ts.tv_sec) * 1000000 +
155 static_cast<int64_t>(ts.tv_nsec) / 1000;
[email protected]052f1b52008-11-06 21:43:07156
157 return absolute_micro;
[email protected]f8588472008-11-05 23:17:24158#endif
159}
160
[email protected]614e9fa2008-08-11 22:52:59161void DeleteFilePath(const PathString& log_name) {
[email protected]f6abeba2008-08-08 13:27:28162#if defined(OS_WIN)
[email protected]614e9fa2008-08-11 22:52:59163 DeleteFile(log_name.c_str());
thestig75f87352014-12-03 21:42:27164#elif defined(OS_NACL)
[email protected]ac07ec52013-04-22 17:32:45165 // Do nothing; unlink() isn't supported on NaCl.
[email protected]f6abeba2008-08-08 13:27:28166#else
[email protected]614e9fa2008-08-11 22:52:59167 unlink(log_name.c_str());
[email protected]f6abeba2008-08-08 13:27:28168#endif
169}
initial.commitd7cae122008-07-26 21:49:38170
[email protected]5f95d532010-10-01 17:16:58171PathString GetDefaultLogFile() {
[email protected]5b84fe32010-09-14 22:24:55172#if defined(OS_WIN)
173 // On Windows we use the same path as the exe.
174 wchar_t module_name[MAX_PATH];
thestig3e4787d2015-05-19 19:31:52175 GetModuleFileName(nullptr, module_name, MAX_PATH);
[email protected]5f95d532010-10-01 17:16:58176
scottmgfc5b7072015-01-27 21:46:28177 PathString log_name = module_name;
178 PathString::size_type last_backslash = log_name.rfind('\\', log_name.size());
[email protected]5f95d532010-10-01 17:16:58179 if (last_backslash != PathString::npos)
scottmgfc5b7072015-01-27 21:46:28180 log_name.erase(last_backslash + 1);
181 log_name += L"debug.log";
182 return log_name;
[email protected]5b84fe32010-09-14 22:24:55183#elif defined(OS_POSIX)
184 // On other platforms we just use the current directory.
[email protected]5f95d532010-10-01 17:16:58185 return PathString("debug.log");
[email protected]5b84fe32010-09-14 22:24:55186#endif
187}
188
ananta61762fb2015-09-18 01:00:09189// We don't need locks on Windows for atomically appending to files. The OS
190// provides this functionality.
191#if !defined(OS_WIN)
[email protected]5b84fe32010-09-14 22:24:55192// This class acts as a wrapper for locking the logging files.
193// LoggingLock::Init() should be called from the main thread before any logging
194// is done. Then whenever logging, be sure to have a local LoggingLock
195// instance on the stack. This will ensure that the lock is unlocked upon
196// exiting the frame.
197// LoggingLocks can not be nested.
198class LoggingLock {
199 public:
200 LoggingLock() {
201 LockLogging();
202 }
203
204 ~LoggingLock() {
205 UnlockLogging();
206 }
207
208 static void Init(LogLockingState lock_log, const PathChar* new_log_file) {
209 if (initialized)
210 return;
211 lock_log_file = lock_log;
[email protected]5f95d532010-10-01 17:16:58212
ananta61762fb2015-09-18 01:00:09213 if (lock_log_file != LOCK_LOG_FILE)
[email protected]bc581a682011-01-01 23:16:20214 log_lock = new base::internal::LockImpl();
ananta61762fb2015-09-18 01:00:09215
[email protected]5b84fe32010-09-14 22:24:55216 initialized = true;
217 }
218
219 private:
220 static void LockLogging() {
221 if (lock_log_file == LOCK_LOG_FILE) {
ananta61762fb2015-09-18 01:00:09222#if defined(OS_POSIX)
[email protected]5b84fe32010-09-14 22:24:55223 pthread_mutex_lock(&log_mutex);
224#endif
225 } else {
226 // use the lock
227 log_lock->Lock();
228 }
229 }
230
231 static void UnlockLogging() {
232 if (lock_log_file == LOCK_LOG_FILE) {
ananta61762fb2015-09-18 01:00:09233#if defined(OS_POSIX)
[email protected]5b84fe32010-09-14 22:24:55234 pthread_mutex_unlock(&log_mutex);
235#endif
236 } else {
237 log_lock->Unlock();
238 }
239 }
240
241 // The lock is used if log file locking is false. It helps us avoid problems
242 // with multiple threads writing to the log file at the same time. Use
243 // LockImpl directly instead of using Lock, because Lock makes logging calls.
[email protected]bc581a682011-01-01 23:16:20244 static base::internal::LockImpl* log_lock;
[email protected]5b84fe32010-09-14 22:24:55245
246 // When we don't use a lock, we are using a global mutex. We need to do this
247 // because LockFileEx is not thread safe.
ananta61762fb2015-09-18 01:00:09248#if defined(OS_POSIX)
[email protected]5b84fe32010-09-14 22:24:55249 static pthread_mutex_t log_mutex;
250#endif
251
252 static bool initialized;
253 static LogLockingState lock_log_file;
254};
255
256// static
257bool LoggingLock::initialized = false;
258// static
thestig3e4787d2015-05-19 19:31:52259base::internal::LockImpl* LoggingLock::log_lock = nullptr;
[email protected]5b84fe32010-09-14 22:24:55260// static
261LogLockingState LoggingLock::lock_log_file = LOCK_LOG_FILE;
262
ananta61762fb2015-09-18 01:00:09263#if defined(OS_POSIX)
[email protected]5b84fe32010-09-14 22:24:55264pthread_mutex_t LoggingLock::log_mutex = PTHREAD_MUTEX_INITIALIZER;
265#endif
266
ananta61762fb2015-09-18 01:00:09267#endif // OS_WIN
268
thestig3e4787d2015-05-19 19:31:52269// Called by logging functions to ensure that |g_log_file| is initialized
initial.commitd7cae122008-07-26 21:49:38270// and can be used for writing. Returns false if the file could not be
thestig3e4787d2015-05-19 19:31:52271// initialized. |g_log_file| will be nullptr in this case.
initial.commitd7cae122008-07-26 21:49:38272bool InitializeLogFileHandle() {
thestig3e4787d2015-05-19 19:31:52273 if (g_log_file)
initial.commitd7cae122008-07-26 21:49:38274 return true;
275
thestig3e4787d2015-05-19 19:31:52276 if (!g_log_file_name) {
[email protected]614e9fa2008-08-11 22:52:59277 // Nobody has called InitLogging to specify a debug log file, so here we
278 // initialize the log file name to a default.
thestig3e4787d2015-05-19 19:31:52279 g_log_file_name = new PathString(GetDefaultLogFile());
initial.commitd7cae122008-07-26 21:49:38280 }
281
thestig3e4787d2015-05-19 19:31:52282 if ((g_logging_destination & LOG_TO_FILE) != 0) {
[email protected]614e9fa2008-08-11 22:52:59283#if defined(OS_WIN)
ananta61762fb2015-09-18 01:00:09284 // The FILE_APPEND_DATA access mask ensures that the file is atomically
285 // appended to across accesses from multiple threads.
286 // https://msdn.microsoft.com/en-us/library/windows/desktop/aa364399(v=vs.85).aspx
287 // https://msdn.microsoft.com/en-us/library/windows/desktop/aa363858(v=vs.85).aspx
288 g_log_file = CreateFile(g_log_file_name->c_str(), FILE_APPEND_DATA,
thestig3e4787d2015-05-19 19:31:52289 FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr,
290 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr);
291 if (g_log_file == INVALID_HANDLE_VALUE || g_log_file == nullptr) {
[email protected]1d8c2702008-08-19 23:39:32292 // try the current directory
ananta61762fb2015-09-18 01:00:09293 base::FilePath file_path;
294 if (!base::GetCurrentDirectory(&file_path))
295 return false;
296
297 *g_log_file_name = file_path.Append(
298 FILE_PATH_LITERAL("debug.log")).value();
299
300 g_log_file = CreateFile(g_log_file_name->c_str(), FILE_APPEND_DATA,
thestig3e4787d2015-05-19 19:31:52301 FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr,
302 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr);
303 if (g_log_file == INVALID_HANDLE_VALUE || g_log_file == nullptr) {
304 g_log_file = nullptr;
[email protected]1d8c2702008-08-19 23:39:32305 return false;
306 }
initial.commitd7cae122008-07-26 21:49:38307 }
[email protected]78c6dd62009-06-08 23:29:11308#elif defined(OS_POSIX)
thestig3e4787d2015-05-19 19:31:52309 g_log_file = fopen(g_log_file_name->c_str(), "a");
310 if (g_log_file == nullptr)
[email protected]78c6dd62009-06-08 23:29:11311 return false;
[email protected]f6abeba2008-08-08 13:27:28312#endif
[email protected]1d8c2702008-08-19 23:39:32313 }
314
initial.commitd7cae122008-07-26 21:49:38315 return true;
316}
317
[email protected]17dcf752013-07-15 21:47:09318void CloseFile(FileHandle log) {
319#if defined(OS_WIN)
320 CloseHandle(log);
321#else
322 fclose(log);
323#endif
324}
325
326void CloseLogFileUnlocked() {
thestig3e4787d2015-05-19 19:31:52327 if (!g_log_file)
[email protected]17dcf752013-07-15 21:47:09328 return;
329
thestig3e4787d2015-05-19 19:31:52330 CloseFile(g_log_file);
331 g_log_file = nullptr;
[email protected]17dcf752013-07-15 21:47:09332}
333
[email protected]064aa162011-12-03 00:30:08334} // namespace
335
[email protected]5e3f7c22013-06-21 21:15:33336LoggingSettings::LoggingSettings()
337 : logging_dest(LOG_DEFAULT),
thestig3e4787d2015-05-19 19:31:52338 log_file(nullptr),
[email protected]5e3f7c22013-06-21 21:15:33339 lock_log(LOCK_LOG_FILE),
[email protected]1a1505512014-03-10 18:23:38340 delete_old(APPEND_TO_OLD_LOG_FILE) {}
[email protected]064aa162011-12-03 00:30:08341
[email protected]5e3f7c22013-06-21 21:15:33342bool BaseInitLoggingImpl(const LoggingSettings& settings) {
[email protected]ac07ec52013-04-22 17:32:45343#if defined(OS_NACL)
[email protected]5e3f7c22013-06-21 21:15:33344 // Can log only to the system debug log.
345 CHECK_EQ(settings.logging_dest & ~LOG_TO_SYSTEM_DEBUG_LOG, 0);
[email protected]ac07ec52013-04-22 17:32:45346#endif
pgal.u-szeged421dddb2014-11-25 12:55:02347 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
thestig3e4787d2015-05-19 19:31:52348 // Don't bother initializing |g_vlog_info| unless we use one of the
[email protected]99b7c57f2010-09-29 19:26:36349 // vlog switches.
350 if (command_line->HasSwitch(switches::kV) ||
351 command_line->HasSwitch(switches::kVModule)) {
thestig3e4787d2015-05-19 19:31:52352 // NOTE: If |g_vlog_info| has already been initialized, it might be in use
[email protected]064aa162011-12-03 00:30:08353 // by another thread. Don't delete the old VLogInfo, just create a second
354 // one. We keep track of both to avoid memory leak warnings.
355 CHECK(!g_vlog_info_prev);
356 g_vlog_info_prev = g_vlog_info;
357
[email protected]99b7c57f2010-09-29 19:26:36358 g_vlog_info =
359 new VlogInfo(command_line->GetSwitchValueASCII(switches::kV),
[email protected]162ac0f2010-11-04 15:50:49360 command_line->GetSwitchValueASCII(switches::kVModule),
thestig3e4787d2015-05-19 19:31:52361 &g_min_log_level);
[email protected]99b7c57f2010-09-29 19:26:36362 }
363
thestig3e4787d2015-05-19 19:31:52364 g_logging_destination = settings.logging_dest;
initial.commitd7cae122008-07-26 21:49:38365
[email protected]5e3f7c22013-06-21 21:15:33366 // ignore file options unless logging to file is set.
thestig3e4787d2015-05-19 19:31:52367 if ((g_logging_destination & LOG_TO_FILE) == 0)
[email protected]c7d5da992010-10-28 00:20:21368 return true;
initial.commitd7cae122008-07-26 21:49:38369
ananta61762fb2015-09-18 01:00:09370#if !defined(OS_WIN)
[email protected]17dcf752013-07-15 21:47:09371 LoggingLock::Init(settings.lock_log, settings.log_file);
372 LoggingLock logging_lock;
ananta61762fb2015-09-18 01:00:09373#endif
[email protected]17dcf752013-07-15 21:47:09374
375 // Calling InitLogging twice or after some log call has already opened the
376 // default log file will re-initialize to the new options.
377 CloseLogFileUnlocked();
378
thestig3e4787d2015-05-19 19:31:52379 if (!g_log_file_name)
380 g_log_file_name = new PathString();
381 *g_log_file_name = settings.log_file;
[email protected]5e3f7c22013-06-21 21:15:33382 if (settings.delete_old == DELETE_OLD_LOG_FILE)
thestig3e4787d2015-05-19 19:31:52383 DeleteFilePath(*g_log_file_name);
initial.commitd7cae122008-07-26 21:49:38384
[email protected]c7d5da992010-10-28 00:20:21385 return InitializeLogFileHandle();
initial.commitd7cae122008-07-26 21:49:38386}
387
388void SetMinLogLevel(int level) {
thestig3e4787d2015-05-19 19:31:52389 g_min_log_level = std::min(LOG_FATAL, level);
initial.commitd7cae122008-07-26 21:49:38390}
391
392int GetMinLogLevel() {
thestig3e4787d2015-05-19 19:31:52393 return g_min_log_level;
initial.commitd7cae122008-07-26 21:49:38394}
395
skobesc78c0ad72015-12-07 20:21:23396bool ShouldCreateLogMessage(int severity) {
397 if (severity < g_min_log_level)
398 return false;
399
400 // Return true here unless we know ~LogMessage won't do anything. Note that
401 // ~LogMessage writes to stderr if severity_ >= kAlwaysPrintErrorLevel, even
402 // when g_logging_destination is LOG_NONE.
403 return g_logging_destination != LOG_NONE || log_message_handler ||
404 severity >= kAlwaysPrintErrorLevel;
405}
406
[email protected]162ac0f2010-11-04 15:50:49407int GetVlogVerbosity() {
408 return std::max(-1, LOG_INFO - GetMinLogLevel());
409}
410
[email protected]99b7c57f2010-09-29 19:26:36411int GetVlogLevelHelper(const char* file, size_t N) {
412 DCHECK_GT(N, 0U);
thestig3e4787d2015-05-19 19:31:52413 // Note: |g_vlog_info| may change on a different thread during startup
414 // (but will always be valid or nullptr).
[email protected]064aa162011-12-03 00:30:08415 VlogInfo* vlog_info = g_vlog_info;
416 return vlog_info ?
417 vlog_info->GetVlogLevel(base::StringPiece(file, N - 1)) :
[email protected]162ac0f2010-11-04 15:50:49418 GetVlogVerbosity();
[email protected]99b7c57f2010-09-29 19:26:36419}
420
initial.commitd7cae122008-07-26 21:49:38421void SetLogItems(bool enable_process_id, bool enable_thread_id,
422 bool enable_timestamp, bool enable_tickcount) {
thestig3e4787d2015-05-19 19:31:52423 g_log_process_id = enable_process_id;
424 g_log_thread_id = enable_thread_id;
425 g_log_timestamp = enable_timestamp;
426 g_log_tickcount = enable_tickcount;
initial.commitd7cae122008-07-26 21:49:38427}
428
[email protected]81e0a852010-08-17 00:38:12429void SetShowErrorDialogs(bool enable_dialogs) {
430 show_error_dialogs = enable_dialogs;
431}
432
initial.commitd7cae122008-07-26 21:49:38433void SetLogAssertHandler(LogAssertHandlerFunction handler) {
434 log_assert_handler = handler;
435}
436
[email protected]2b07b8412009-11-25 15:26:34437void SetLogMessageHandler(LogMessageHandlerFunction handler) {
438 log_message_handler = handler;
439}
440
[email protected]64e5cc02010-11-03 19:20:27441LogMessageHandlerFunction GetLogMessageHandler() {
442 return log_message_handler;
443}
444
[email protected]6d445d32010-09-30 19:10:03445// Explicit instantiations for commonly used comparisons.
446template std::string* MakeCheckOpString<int, int>(
447 const int&, const int&, const char* names);
448template std::string* MakeCheckOpString<unsigned long, unsigned long>(
449 const unsigned long&, const unsigned long&, const char* names);
450template std::string* MakeCheckOpString<unsigned long, unsigned int>(
451 const unsigned long&, const unsigned int&, const char* names);
452template std::string* MakeCheckOpString<unsigned int, unsigned long>(
453 const unsigned int&, const unsigned long&, const char* names);
454template std::string* MakeCheckOpString<std::string, std::string>(
455 const std::string&, const std::string&, const char* name);
[email protected]2b07b8412009-11-25 15:26:34456
jbroman6bcfec422016-05-26 00:28:46457void MakeCheckOpValueString(std::ostream* os, std::nullptr_t p) {
brucedawson93a60b8c2016-04-28 20:46:16458 (*os) << "nullptr";
459}
460
[email protected]f2c05492014-06-17 12:04:23461#if !defined(NDEBUG)
[email protected]d81baca42010-03-01 13:10:22462// Displays a message box to the user with the error message in it.
463// Used for fatal messages, where we close the app simultaneously.
[email protected]561513f2010-12-16 23:29:25464// This is for developers only; we don't use this in circumstances
465// (like release builds) where users could see it, since users don't
466// understand these messages anyway.
[email protected]d81baca42010-03-01 13:10:22467void DisplayDebugMessageInDialog(const std::string& str) {
initial.commitd7cae122008-07-26 21:49:38468 if (str.empty())
469 return;
470
[email protected]81e0a852010-08-17 00:38:12471 if (!show_error_dialogs)
[email protected]846ed9c32010-07-29 20:33:44472 return;
473
[email protected]f6abeba2008-08-08 13:27:28474#if defined(OS_WIN)
brettwe33526852015-10-03 00:46:01475 MessageBoxW(nullptr, base::UTF8ToUTF16(str).c_str(), L"Fatal error",
476 MB_OK | MB_ICONHAND | MB_TOPMOST);
[email protected]f6abeba2008-08-08 13:27:28477#else
[email protected]561513f2010-12-16 23:29:25478 // We intentionally don't implement a dialog on other platforms.
479 // You can just look at stderr.
thestig3e4787d2015-05-19 19:31:52480#endif // defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:38481}
[email protected]f2c05492014-06-17 12:04:23482#endif // !defined(NDEBUG)
initial.commitd7cae122008-07-26 21:49:38483
[email protected]3f85caa2009-04-14 16:52:11484#if defined(OS_WIN)
485LogMessage::SaveLastError::SaveLastError() : last_error_(::GetLastError()) {
486}
487
488LogMessage::SaveLastError::~SaveLastError() {
489 ::SetLastError(last_error_);
490}
491#endif // defined(OS_WIN)
492
[email protected]eae9c062011-01-11 00:50:59493LogMessage::LogMessage(const char* file, int line, LogSeverity severity)
494 : severity_(severity), file_(file), line_(line) {
495 Init(file, line);
496}
497
tnagel4a045d3f2015-07-12 14:19:28498LogMessage::LogMessage(const char* file, int line, const char* condition)
499 : severity_(LOG_FATAL), file_(file), line_(line) {
500 Init(file, line);
501 stream_ << "Check failed: " << condition << ". ";
502}
503
[email protected]9c7132e2011-02-08 07:39:08504LogMessage::LogMessage(const char* file, int line, std::string* result)
[email protected]162ac0f2010-11-04 15:50:49505 : severity_(LOG_FATAL), file_(file), line_(line) {
initial.commitd7cae122008-07-26 21:49:38506 Init(file, line);
[email protected]9c7132e2011-02-08 07:39:08507 stream_ << "Check failed: " << *result;
508 delete result;
initial.commitd7cae122008-07-26 21:49:38509}
510
[email protected]fb62a532009-02-12 01:19:05511LogMessage::LogMessage(const char* file, int line, LogSeverity severity,
[email protected]9c7132e2011-02-08 07:39:08512 std::string* result)
[email protected]162ac0f2010-11-04 15:50:49513 : severity_(severity), file_(file), line_(line) {
[email protected]fb62a532009-02-12 01:19:05514 Init(file, line);
[email protected]9c7132e2011-02-08 07:39:08515 stream_ << "Check failed: " << *result;
516 delete result;
[email protected]fb62a532009-02-12 01:19:05517}
518
initial.commitd7cae122008-07-26 21:49:38519LogMessage::~LogMessage() {
jam79dc59a2015-08-17 03:38:16520#if !defined(OFFICIAL_BUILD) && !defined(OS_NACL) && !defined(__UCLIBC__)
brucedawson7c559eb2015-09-05 00:34:42521 if (severity_ == LOG_FATAL && !base::debug::BeingDebugged()) {
522 // Include a stack trace on a fatal, unless a debugger is attached.
[email protected]58580352010-10-26 04:07:50523 base::debug::StackTrace trace;
[email protected]d1ccc35a2010-03-24 05:03:24524 stream_ << std::endl; // Newline to separate from log message.
525 trace.OutputToStream(&stream_);
526 }
[email protected]1d8c2702008-08-19 23:39:32527#endif
[email protected]d1ccc35a2010-03-24 05:03:24528 stream_ << std::endl;
529 std::string str_newline(stream_.str());
530
[email protected]2b07b8412009-11-25 15:26:34531 // Give any log message handler first dibs on the message.
[email protected]5e3f7c22013-06-21 21:15:33532 if (log_message_handler &&
533 log_message_handler(severity_, file_, line_,
534 message_start_, str_newline)) {
[email protected]162ac0f2010-11-04 15:50:49535 // The handler took care of it, no further processing.
[email protected]2b07b8412009-11-25 15:26:34536 return;
[email protected]162ac0f2010-11-04 15:50:49537 }
initial.commitd7cae122008-07-26 21:49:38538
thestig3e4787d2015-05-19 19:31:52539 if ((g_logging_destination & LOG_TO_SYSTEM_DEBUG_LOG) != 0) {
[email protected]f6abeba2008-08-08 13:27:28540#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:38541 OutputDebugStringA(str_newline.c_str());
mark4c7449c2015-11-10 19:53:42542#elif defined(OS_MACOSX)
543 // In LOG_TO_SYSTEM_DEBUG_LOG mode, log messages are always written to
544 // stderr. If stderr is /dev/null, also log via ASL (Apple System Log). If
545 // there's something weird about stderr, assume that log messages are going
546 // nowhere and log via ASL too. Messages logged via ASL show up in
547 // Console.app.
548 //
549 // Programs started by launchd, as UI applications normally are, have had
550 // stderr connected to /dev/null since OS X 10.8. Prior to that, stderr was
551 // a pipe to launchd, which logged what it received (see log_redirect_fd in
552 // 10.7.5 launchd-392.39/launchd/src/launchd_core_logic.c).
553 //
554 // Another alternative would be to determine whether stderr is a pipe to
555 // launchd and avoid logging via ASL only in that case. See 10.7.5
556 // CF-635.21/CFUtilities.c also_do_stderr(). This would result in logging to
557 // both stderr and ASL even in tests, where it's undesirable to log to the
558 // system log at all.
559 //
560 // Note that the ASL client by default discards messages whose levels are
561 // below ASL_LEVEL_NOTICE. It's possible to change that with
562 // asl_set_filter(), but this is pointless because syslogd normally applies
563 // the same filter.
564 const bool log_via_asl = []() {
565 struct stat stderr_stat;
566 if (fstat(fileno(stderr), &stderr_stat) == -1) {
567 return true;
568 }
569 if (!S_ISCHR(stderr_stat.st_mode)) {
570 return false;
571 }
572
573 struct stat dev_null_stat;
574 if (stat(_PATH_DEVNULL, &dev_null_stat) == -1) {
575 return true;
576 }
577
578 return !S_ISCHR(dev_null_stat.st_mode) ||
579 stderr_stat.st_rdev == dev_null_stat.st_rdev;
580 }();
581
582 if (log_via_asl) {
583 // Log roughly the same way that CFLog() and NSLog() would. See 10.10.5
584 // CF-1153.18/CFUtilities.c __CFLogCString().
585 //
586 // The ASL facility is set to the main bundle ID if available. Otherwise,
587 // "com.apple.console" is used.
588 CFBundleRef main_bundle = CFBundleGetMainBundle();
589 CFStringRef main_bundle_id_cf =
590 main_bundle ? CFBundleGetIdentifier(main_bundle) : nullptr;
591 std::string asl_facility =
592 main_bundle_id_cf ? base::SysCFStringRefToUTF8(main_bundle_id_cf)
593 : std::string("com.apple.console");
594
595 class ASLClient {
596 public:
597 explicit ASLClient(const std::string& asl_facility)
598 : client_(asl_open(nullptr,
599 asl_facility.c_str(),
600 ASL_OPT_NO_DELAY)) {}
601 ~ASLClient() { asl_close(client_); }
602
603 aslclient get() const { return client_; }
604
605 private:
606 aslclient client_;
607 DISALLOW_COPY_AND_ASSIGN(ASLClient);
608 } asl_client(asl_facility);
609
610 class ASLMessage {
611 public:
612 ASLMessage() : message_(asl_new(ASL_TYPE_MSG)) {}
613 ~ASLMessage() { asl_free(message_); }
614
615 aslmsg get() const { return message_; }
616
617 private:
618 aslmsg message_;
619 DISALLOW_COPY_AND_ASSIGN(ASLMessage);
620 } asl_message;
621
622 // By default, messages are only readable by the admin group. Explicitly
623 // make them readable by the user generating the messages.
624 char euid_string[12];
625 snprintf(euid_string, arraysize(euid_string), "%d", geteuid());
626 asl_set(asl_message.get(), ASL_KEY_READ_UID, euid_string);
627
628 // Map Chrome log severities to ASL log levels.
629 const char* const asl_level_string = [](LogSeverity severity) {
630 // ASL_LEVEL_* are ints, but ASL needs equivalent strings. This
631 // non-obvious two-step macro trick achieves what's needed.
632 // https://gcc.gnu.org/onlinedocs/cpp/Stringification.html
633#define ASL_LEVEL_STR(level) ASL_LEVEL_STR_X(level)
634#define ASL_LEVEL_STR_X(level) #level
635 switch (severity) {
636 case LOG_INFO:
637 return ASL_LEVEL_STR(ASL_LEVEL_INFO);
638 case LOG_WARNING:
639 return ASL_LEVEL_STR(ASL_LEVEL_WARNING);
640 case LOG_ERROR:
641 return ASL_LEVEL_STR(ASL_LEVEL_ERR);
642 case LOG_FATAL:
643 return ASL_LEVEL_STR(ASL_LEVEL_CRIT);
644 default:
645 return severity < 0 ? ASL_LEVEL_STR(ASL_LEVEL_DEBUG)
646 : ASL_LEVEL_STR(ASL_LEVEL_NOTICE);
647 }
648#undef ASL_LEVEL_STR
649#undef ASL_LEVEL_STR_X
650 }(severity_);
651 asl_set(asl_message.get(), ASL_KEY_LEVEL, asl_level_string);
652
653 asl_set(asl_message.get(), ASL_KEY_MSG, str_newline.c_str());
654
655 asl_send(asl_client.get(), asl_message.get());
656 }
[email protected]3132e35c2011-07-07 20:46:50657#elif defined(OS_ANDROID)
[email protected]efbae7da2013-05-21 22:39:25658 android_LogPriority priority =
659 (severity_ < 0) ? ANDROID_LOG_VERBOSE : ANDROID_LOG_UNKNOWN;
[email protected]3132e35c2011-07-07 20:46:50660 switch (severity_) {
661 case LOG_INFO:
662 priority = ANDROID_LOG_INFO;
663 break;
664 case LOG_WARNING:
665 priority = ANDROID_LOG_WARN;
666 break;
667 case LOG_ERROR:
[email protected]3132e35c2011-07-07 20:46:50668 priority = ANDROID_LOG_ERROR;
669 break;
670 case LOG_FATAL:
671 priority = ANDROID_LOG_FATAL;
672 break;
673 }
674 __android_log_write(priority, "chromium", str_newline.c_str());
[email protected]107bc0f12008-08-26 17:48:18675#endif
[email protected]51105382014-03-14 17:02:15676 ignore_result(fwrite(str_newline.data(), str_newline.size(), 1, stderr));
[email protected]469006c2010-09-24 15:43:06677 fflush(stderr);
[email protected]a33c9892008-08-25 20:10:31678 } else if (severity_ >= kAlwaysPrintErrorLevel) {
679 // When we're only outputting to a log file, above a certain log level, we
680 // should still output to stderr so that we can better detect and diagnose
681 // problems with unit tests, especially on the buildbots.
[email protected]51105382014-03-14 17:02:15682 ignore_result(fwrite(str_newline.data(), str_newline.size(), 1, stderr));
[email protected]1ce41052009-12-02 00:34:02683 fflush(stderr);
[email protected]f6abeba2008-08-08 13:27:28684 }
[email protected]52a261f2009-03-03 15:01:12685
initial.commitd7cae122008-07-26 21:49:38686 // write to log file
thestig3e4787d2015-05-19 19:31:52687 if ((g_logging_destination & LOG_TO_FILE) != 0) {
[email protected]17dcf752013-07-15 21:47:09688 // We can have multiple threads and/or processes, so try to prevent them
689 // from clobbering each other's writes.
690 // If the client app did not call InitLogging, and the lock has not
691 // been created do it now. We do this on demand, but if two threads try
692 // to do this at the same time, there will be a race condition to create
693 // the lock. This is why InitLogging should be called from the main
694 // thread at the beginning of execution.
ananta61762fb2015-09-18 01:00:09695#if !defined(OS_WIN)
thestig3e4787d2015-05-19 19:31:52696 LoggingLock::Init(LOCK_LOG_FILE, nullptr);
[email protected]5b84fe32010-09-14 22:24:55697 LoggingLock logging_lock;
ananta61762fb2015-09-18 01:00:09698#endif
[email protected]5b84fe32010-09-14 22:24:55699 if (InitializeLogFileHandle()) {
[email protected]f6abeba2008-08-08 13:27:28700#if defined(OS_WIN)
[email protected]5b84fe32010-09-14 22:24:55701 DWORD num_written;
thestig3e4787d2015-05-19 19:31:52702 WriteFile(g_log_file,
[email protected]5b84fe32010-09-14 22:24:55703 static_cast<const void*>(str_newline.c_str()),
704 static_cast<DWORD>(str_newline.length()),
705 &num_written,
thestig3e4787d2015-05-19 19:31:52706 nullptr);
[email protected]cba21962010-08-31 22:35:55707#else
[email protected]51105382014-03-14 17:02:15708 ignore_result(fwrite(
thestig3e4787d2015-05-19 19:31:52709 str_newline.data(), str_newline.size(), 1, g_log_file));
710 fflush(g_log_file);
[email protected]cba21962010-08-31 22:35:55711#endif
initial.commitd7cae122008-07-26 21:49:38712 }
713 }
714
715 if (severity_ == LOG_FATAL) {
[email protected]eb4c4d032012-04-03 18:45:05716 // Ensure the first characters of the string are on the stack so they
717 // are contained in minidumps for diagnostic purposes.
718 char str_stack[1024];
719 str_newline.copy(str_stack, arraysize(str_stack));
720 base::debug::Alias(str_stack);
721
[email protected]82d89ab2014-02-28 18:25:34722 if (log_assert_handler) {
723 // Make a copy of the string for the handler out of paranoia.
724 log_assert_handler(std::string(stream_.str()));
[email protected]1ffe08c12008-08-13 11:15:11725 } else {
[email protected]82d89ab2014-02-28 18:25:34726 // Don't use the string with the newline, get a fresh version to send to
727 // the debug message process. We also don't display assertions to the
728 // user in release mode. The enduser can't do anything with this
729 // information, and displaying message boxes when the application is
730 // hosed can cause additional problems.
[email protected]4d5901272008-11-06 00:33:50731#ifndef NDEBUG
brucedawson7c559eb2015-09-05 00:34:42732 if (!base::debug::BeingDebugged()) {
733 // Displaying a dialog is unnecessary when debugging and can complicate
734 // debugging.
735 DisplayDebugMessageInDialog(stream_.str());
736 }
[email protected]4d5901272008-11-06 00:33:50737#endif
[email protected]82d89ab2014-02-28 18:25:34738 // Crash the process to generate a dump.
739 base::debug::BreakDebugger();
initial.commitd7cae122008-07-26 21:49:38740 }
741 }
742}
743
[email protected]eae9c062011-01-11 00:50:59744// writes the common header info to the stream
745void LogMessage::Init(const char* file, int line) {
746 base::StringPiece filename(file);
747 size_t last_slash_pos = filename.find_last_of("\\/");
748 if (last_slash_pos != base::StringPiece::npos)
749 filename.remove_prefix(last_slash_pos + 1);
750
751 // TODO(darin): It might be nice if the columns were fixed width.
752
753 stream_ << '[';
thestig3e4787d2015-05-19 19:31:52754 if (g_log_process_id)
[email protected]eae9c062011-01-11 00:50:59755 stream_ << CurrentProcessId() << ':';
thestig3e4787d2015-05-19 19:31:52756 if (g_log_thread_id)
[email protected]63e66802012-01-18 21:21:09757 stream_ << base::PlatformThread::CurrentId() << ':';
thestig3e4787d2015-05-19 19:31:52758 if (g_log_timestamp) {
759 time_t t = time(nullptr);
[email protected]eae9c062011-01-11 00:50:59760 struct tm local_time = {0};
mostynb7e42a8f2014-12-19 12:47:46761#ifdef _MSC_VER
[email protected]eae9c062011-01-11 00:50:59762 localtime_s(&local_time, &t);
763#else
764 localtime_r(&t, &local_time);
765#endif
766 struct tm* tm_time = &local_time;
767 stream_ << std::setfill('0')
768 << std::setw(2) << 1 + tm_time->tm_mon
769 << std::setw(2) << tm_time->tm_mday
770 << '/'
771 << std::setw(2) << tm_time->tm_hour
772 << std::setw(2) << tm_time->tm_min
773 << std::setw(2) << tm_time->tm_sec
774 << ':';
775 }
thestig3e4787d2015-05-19 19:31:52776 if (g_log_tickcount)
[email protected]eae9c062011-01-11 00:50:59777 stream_ << TickCount() << ':';
778 if (severity_ >= 0)
[email protected]80f360a2014-01-23 01:36:19779 stream_ << log_severity_name(severity_);
[email protected]eae9c062011-01-11 00:50:59780 else
781 stream_ << "VERBOSE" << -severity_;
782
783 stream_ << ":" << filename << "(" << line << ")] ";
784
pkasting9cf9b942014-10-01 22:18:43785 message_start_ = stream_.str().length();
[email protected]eae9c062011-01-11 00:50:59786}
787
[email protected]d8617a62009-10-09 23:52:20788#if defined(OS_WIN)
789// This has already been defined in the header, but defining it again as DWORD
790// ensures that the type used in the header is equivalent to DWORD. If not,
791// the redefinition is a compile error.
792typedef DWORD SystemErrorCode;
793#endif
794
795SystemErrorCode GetLastSystemErrorCode() {
796#if defined(OS_WIN)
797 return ::GetLastError();
798#elif defined(OS_POSIX)
799 return errno;
800#else
801#error Not implemented
802#endif
803}
804
805#if defined(OS_WIN)
[email protected]c914d8a2014-04-23 01:11:01806BASE_EXPORT std::string SystemErrorCodeToString(SystemErrorCode error_code) {
thestig75f87352014-12-03 21:42:27807 const int kErrorMessageBufferSize = 256;
808 char msgbuf[kErrorMessageBufferSize];
[email protected]c914d8a2014-04-23 01:11:01809 DWORD flags = FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS;
thestig3e4787d2015-05-19 19:31:52810 DWORD len = FormatMessageA(flags, nullptr, error_code, 0, msgbuf,
811 arraysize(msgbuf), nullptr);
[email protected]c914d8a2014-04-23 01:11:01812 if (len) {
813 // Messages returned by system end with line breaks.
814 return base::CollapseWhitespaceASCII(msgbuf, true) +
815 base::StringPrintf(" (0x%X)", error_code);
816 }
817 return base::StringPrintf("Error (0x%X) while retrieving error. (0x%X)",
818 GetLastError(), error_code);
[email protected]d8617a62009-10-09 23:52:20819}
[email protected]c914d8a2014-04-23 01:11:01820#elif defined(OS_POSIX)
821BASE_EXPORT std::string SystemErrorCodeToString(SystemErrorCode error_code) {
brettw6ee6fd62015-06-09 18:05:24822 return base::safe_strerror(error_code);
[email protected]c914d8a2014-04-23 01:11:01823}
824#else
825#error Not implemented
thestig3e4787d2015-05-19 19:31:52826#endif // defined(OS_WIN)
[email protected]d8617a62009-10-09 23:52:20827
[email protected]c914d8a2014-04-23 01:11:01828
829#if defined(OS_WIN)
[email protected]d8617a62009-10-09 23:52:20830Win32ErrorLogMessage::Win32ErrorLogMessage(const char* file,
831 int line,
832 LogSeverity severity,
833 SystemErrorCode err)
834 : err_(err),
[email protected]d8617a62009-10-09 23:52:20835 log_message_(file, line, severity) {
836}
837
838Win32ErrorLogMessage::~Win32ErrorLogMessage() {
[email protected]c914d8a2014-04-23 01:11:01839 stream() << ": " << SystemErrorCodeToString(err_);
[email protected]20909e72012-04-05 16:57:06840 // We're about to crash (CHECK). Put |err_| on the stack (by placing it in a
841 // field) and use Alias in hopes that it makes it into crash dumps.
842 DWORD last_error = err_;
843 base::debug::Alias(&last_error);
[email protected]d8617a62009-10-09 23:52:20844}
845#elif defined(OS_POSIX)
846ErrnoLogMessage::ErrnoLogMessage(const char* file,
847 int line,
848 LogSeverity severity,
849 SystemErrorCode err)
850 : err_(err),
851 log_message_(file, line, severity) {
852}
853
854ErrnoLogMessage::~ErrnoLogMessage() {
[email protected]c914d8a2014-04-23 01:11:01855 stream() << ": " << SystemErrorCodeToString(err_);
[email protected]d8617a62009-10-09 23:52:20856}
thestig3e4787d2015-05-19 19:31:52857#endif // defined(OS_WIN)
[email protected]d8617a62009-10-09 23:52:20858
initial.commitd7cae122008-07-26 21:49:38859void CloseLogFile() {
ananta61762fb2015-09-18 01:00:09860#if !defined(OS_WIN)
[email protected]5b84fe32010-09-14 22:24:55861 LoggingLock logging_lock;
ananta61762fb2015-09-18 01:00:09862#endif
[email protected]17dcf752013-07-15 21:47:09863 CloseLogFileUnlocked();
initial.commitd7cae122008-07-26 21:49:38864}
865
[email protected]e36ddc82009-12-08 04:22:50866void RawLog(int level, const char* message) {
erikchen0c9fe712016-03-11 22:07:49867 if (level >= g_min_log_level && message) {
[email protected]e36ddc82009-12-08 04:22:50868 size_t bytes_written = 0;
869 const size_t message_len = strlen(message);
870 int rv;
871 while (bytes_written < message_len) {
872 rv = HANDLE_EINTR(
873 write(STDERR_FILENO, message + bytes_written,
874 message_len - bytes_written));
875 if (rv < 0) {
876 // Give up, nothing we can do now.
877 break;
878 }
879 bytes_written += rv;
880 }
881
882 if (message_len > 0 && message[message_len - 1] != '\n') {
883 do {
884 rv = HANDLE_EINTR(write(STDERR_FILENO, "\n", 1));
885 if (rv < 0) {
886 // Give up, nothing we can do now.
887 break;
888 }
889 } while (rv != 1);
890 }
891 }
892
893 if (level == LOG_FATAL)
[email protected]58580352010-10-26 04:07:50894 base::debug::BreakDebugger();
[email protected]e36ddc82009-12-08 04:22:50895}
896
[email protected]34a907732012-01-20 06:33:27897// This was defined at the beginning of this file.
898#undef write
899
[email protected]f01b88a2013-02-27 22:04:00900#if defined(OS_WIN)
ananta61762fb2015-09-18 01:00:09901bool IsLoggingToFileEnabled() {
902 return g_logging_destination & LOG_TO_FILE;
903}
904
[email protected]f01b88a2013-02-27 22:04:00905std::wstring GetLogFileFullPath() {
thestig3e4787d2015-05-19 19:31:52906 if (g_log_file_name)
907 return *g_log_file_name;
[email protected]f01b88a2013-02-27 22:04:00908 return std::wstring();
909}
910#endif
911
tnagel80388e682015-05-26 13:27:56912BASE_EXPORT void LogErrorNotReached(const char* file, int line) {
tnagelff3f34a2015-05-24 12:59:14913 LogMessage(file, line, LOG_ERROR).stream()
914 << "NOTREACHED() hit.";
915}
916
[email protected]96fd0032009-04-24 00:13:08917} // namespace logging
initial.commitd7cae122008-07-26 21:49:38918
[email protected]81411c62014-07-08 23:03:06919std::ostream& std::operator<<(std::ostream& out, const wchar_t* wstr) {
erikchen0c9fe712016-03-11 22:07:49920 return out << (wstr ? base::WideToUTF8(wstr) : std::string());
initial.commitd7cae122008-07-26 21:49:38921}