blob: 22e0aa0c6371fea45122739a9d90203dc000e12e [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>
8
[email protected]b16ef312008-08-19 18:36:239#if defined(OS_WIN)
[email protected]e36ddc82009-12-08 04:22:5010#include <io.h>
[email protected]f6abeba2008-08-08 13:27:2811#include <windows.h>
ananta61762fb2015-09-18 01:00:0912#include "base/files/file_path.h"
13#include "base/files/file_util.h"
[email protected]f6abeba2008-08-08 13:27:2814typedef HANDLE FileHandle;
15typedef HANDLE MutexHandle;
[email protected]e36ddc82009-12-08 04:22:5016// Windows warns on using write(). It prefers _write().
17#define write(fd, buf, count) _write(fd, buf, static_cast<unsigned int>(count))
18// Windows doesn't define STDERR_FILENO. Define it here.
19#define STDERR_FILENO 2
[email protected]052f1b52008-11-06 21:43:0720#elif defined(OS_MACOSX)
mark4c7449c2015-11-10 19:53:4221#include <asl.h>
22#include <CoreFoundation/CoreFoundation.h>
[email protected]f6abeba2008-08-08 13:27:2823#include <mach/mach.h>
24#include <mach/mach_time.h>
25#include <mach-o/dyld.h>
[email protected]e43eddf12009-12-29 00:32:5226#elif defined(OS_POSIX)
[email protected]19ea84ca2010-11-12 08:37:0827#if defined(OS_NACL)
thestig75f87352014-12-03 21:42:2728#include <sys/time.h> // timespec doesn't seem to be in <time.h>
[email protected]19ea84ca2010-11-12 08:37:0829#else
[email protected]052f1b52008-11-06 21:43:0730#include <sys/syscall.h>
[email protected]19ea84ca2010-11-12 08:37:0831#endif
[email protected]052f1b52008-11-06 21:43:0732#include <time.h>
[email protected]614e9fa2008-08-11 22:52:5933#endif
34
35#if defined(OS_POSIX)
[email protected]d8617a62009-10-09 23:52:2036#include <errno.h>
mark4c7449c2015-11-10 19:53:4237#include <paths.h>
[email protected]166326c62010-08-05 15:50:2338#include <pthread.h>
[email protected]f6abeba2008-08-08 13:27:2839#include <stdio.h>
[email protected]eb62f7262013-03-30 14:29:0040#include <stdlib.h>
[email protected]f6abeba2008-08-08 13:27:2841#include <string.h>
mark4c7449c2015-11-10 19:53:4242#include <sys/stat.h>
[email protected]f6abeba2008-08-08 13:27:2843#include <unistd.h>
44#define MAX_PATH PATH_MAX
45typedef FILE* FileHandle;
46typedef pthread_mutex_t* MutexHandle;
47#endif
48
[email protected]1f88b5162011-04-01 00:02:2949#include <algorithm>
50#include <cstring>
initial.commitd7cae122008-07-26 21:49:3851#include <ctime>
52#include <iomanip>
[email protected]1f88b5162011-04-01 00:02:2953#include <ostream>
[email protected]c914d8a2014-04-23 01:11:0154#include <string>
[email protected]b16ef312008-08-19 18:36:2355
initial.commitd7cae122008-07-26 21:49:3856#include "base/base_switches.h"
57#include "base/command_line.h"
[email protected]eb4c4d032012-04-03 18:45:0558#include "base/debug/alias.h"
[email protected]58580352010-10-26 04:07:5059#include "base/debug/debugger.h"
60#include "base/debug/stack_trace.h"
[email protected]2025d002012-11-14 20:54:3561#include "base/posix/eintr_wrapper.h"
[email protected]eb62f7262013-03-30 14:29:0062#include "base/strings/string_piece.h"
[email protected]c914d8a2014-04-23 01:11:0163#include "base/strings/string_util.h"
64#include "base/strings/stringprintf.h"
mark4c7449c2015-11-10 19:53:4265#include "base/strings/sys_string_conversions.h"
[email protected]a4ea1f12013-06-07 18:37:0766#include "base/strings/utf_string_conversions.h"
[email protected]bc581a682011-01-01 23:16:2067#include "base/synchronization/lock_impl.h"
[email protected]63e66802012-01-18 21:21:0968#include "base/threading/platform_thread.h"
[email protected]99b7c57f2010-09-29 19:26:3669#include "base/vlog.h"
[email protected]53c7ce42010-12-14 16:20:0470#if defined(OS_POSIX)
brettw6ee6fd62015-06-09 18:05:2471#include "base/posix/safe_strerror.h"
[email protected]53c7ce42010-12-14 16:20:0472#endif
[email protected]52a261f2009-03-03 15:01:1273
[email protected]3132e35c2011-07-07 20:46:5074#if defined(OS_ANDROID)
75#include <android/log.h>
76#endif
77
initial.commitd7cae122008-07-26 21:49:3878namespace logging {
79
[email protected]064aa162011-12-03 00:30:0880namespace {
81
thestig3e4787d2015-05-19 19:31:5282VlogInfo* g_vlog_info = nullptr;
83VlogInfo* g_vlog_info_prev = nullptr;
initial.commitd7cae122008-07-26 21:49:3884
85const char* const log_severity_names[LOG_NUM_SEVERITIES] = {
[email protected]f2c05492014-06-17 12:04:2386 "INFO", "WARNING", "ERROR", "FATAL" };
initial.commitd7cae122008-07-26 21:49:3887
thestig75f87352014-12-03 21:42:2788const char* log_severity_name(int severity) {
[email protected]80f360a2014-01-23 01:36:1989 if (severity >= 0 && severity < LOG_NUM_SEVERITIES)
90 return log_severity_names[severity];
91 return "UNKNOWN";
92}
93
thestig3e4787d2015-05-19 19:31:5294int g_min_log_level = 0;
[email protected]1d8c2702008-08-19 23:39:3295
thestig3e4787d2015-05-19 19:31:5296LoggingDestination g_logging_destination = LOG_DEFAULT;
initial.commitd7cae122008-07-26 21:49:3897
[email protected]a33c9892008-08-25 20:10:3198// For LOG_ERROR and above, always print to stderr.
99const int kAlwaysPrintErrorLevel = LOG_ERROR;
100
[email protected]614e9fa2008-08-11 22:52:59101// Which log file to use? This is initialized by InitLogging or
initial.commitd7cae122008-07-26 21:49:38102// will be lazily initialized to the default value when it is
103// first needed.
[email protected]f6abeba2008-08-08 13:27:28104#if defined(OS_WIN)
[email protected]614e9fa2008-08-11 22:52:59105typedef std::wstring PathString;
[email protected]f6abeba2008-08-08 13:27:28106#else
[email protected]614e9fa2008-08-11 22:52:59107typedef std::string PathString;
[email protected]f6abeba2008-08-08 13:27:28108#endif
thestig3e4787d2015-05-19 19:31:52109PathString* g_log_file_name = nullptr;
initial.commitd7cae122008-07-26 21:49:38110
thestig3e4787d2015-05-19 19:31:52111// This file is lazily opened and the handle may be nullptr
112FileHandle g_log_file = nullptr;
initial.commitd7cae122008-07-26 21:49:38113
thestig3e4787d2015-05-19 19:31:52114// What should be prepended to each message?
115bool g_log_process_id = false;
116bool g_log_thread_id = false;
117bool g_log_timestamp = true;
118bool g_log_tickcount = false;
initial.commitd7cae122008-07-26 21:49:38119
[email protected]81e0a852010-08-17 00:38:12120// Should we pop up fatal debug messages in a dialog?
121bool show_error_dialogs = false;
122
initial.commitd7cae122008-07-26 21:49:38123// An assert handler override specified by the client to be called instead of
[email protected]fb62a532009-02-12 01:19:05124// the debug message dialog and process termination.
thestig3e4787d2015-05-19 19:31:52125LogAssertHandlerFunction log_assert_handler = nullptr;
[email protected]2b07b8412009-11-25 15:26:34126// A log message handler that gets notified of every log message we process.
thestig3e4787d2015-05-19 19:31:52127LogMessageHandlerFunction log_message_handler = nullptr;
initial.commitd7cae122008-07-26 21:49:38128
[email protected]f6abeba2008-08-08 13:27:28129// Helper functions to wrap platform differences.
130
[email protected]f8588472008-11-05 23:17:24131int32 CurrentProcessId() {
132#if defined(OS_WIN)
133 return GetCurrentProcessId();
134#elif defined(OS_POSIX)
135 return getpid();
136#endif
137}
138
[email protected]f8588472008-11-05 23:17:24139uint64 TickCount() {
140#if defined(OS_WIN)
141 return GetTickCount();
142#elif defined(OS_MACOSX)
143 return mach_absolute_time();
[email protected]19ea84ca2010-11-12 08:37:08144#elif defined(OS_NACL)
145 // NaCl sadly does not have _POSIX_TIMERS enabled in sys/features.h
146 // So we have to use clock() for now.
147 return clock();
[email protected]e43eddf12009-12-29 00:32:52148#elif defined(OS_POSIX)
[email protected]052f1b52008-11-06 21:43:07149 struct timespec ts;
150 clock_gettime(CLOCK_MONOTONIC, &ts);
151
152 uint64 absolute_micro =
153 static_cast<int64>(ts.tv_sec) * 1000000 +
154 static_cast<int64>(ts.tv_nsec) / 1000;
155
156 return absolute_micro;
[email protected]f8588472008-11-05 23:17:24157#endif
158}
159
[email protected]614e9fa2008-08-11 22:52:59160void DeleteFilePath(const PathString& log_name) {
[email protected]f6abeba2008-08-08 13:27:28161#if defined(OS_WIN)
[email protected]614e9fa2008-08-11 22:52:59162 DeleteFile(log_name.c_str());
thestig75f87352014-12-03 21:42:27163#elif defined(OS_NACL)
[email protected]ac07ec52013-04-22 17:32:45164 // Do nothing; unlink() isn't supported on NaCl.
[email protected]f6abeba2008-08-08 13:27:28165#else
[email protected]614e9fa2008-08-11 22:52:59166 unlink(log_name.c_str());
[email protected]f6abeba2008-08-08 13:27:28167#endif
168}
initial.commitd7cae122008-07-26 21:49:38169
[email protected]5f95d532010-10-01 17:16:58170PathString GetDefaultLogFile() {
[email protected]5b84fe32010-09-14 22:24:55171#if defined(OS_WIN)
172 // On Windows we use the same path as the exe.
173 wchar_t module_name[MAX_PATH];
thestig3e4787d2015-05-19 19:31:52174 GetModuleFileName(nullptr, module_name, MAX_PATH);
[email protected]5f95d532010-10-01 17:16:58175
scottmgfc5b7072015-01-27 21:46:28176 PathString log_name = module_name;
177 PathString::size_type last_backslash = log_name.rfind('\\', log_name.size());
[email protected]5f95d532010-10-01 17:16:58178 if (last_backslash != PathString::npos)
scottmgfc5b7072015-01-27 21:46:28179 log_name.erase(last_backslash + 1);
180 log_name += L"debug.log";
181 return log_name;
[email protected]5b84fe32010-09-14 22:24:55182#elif defined(OS_POSIX)
183 // On other platforms we just use the current directory.
[email protected]5f95d532010-10-01 17:16:58184 return PathString("debug.log");
[email protected]5b84fe32010-09-14 22:24:55185#endif
186}
187
ananta61762fb2015-09-18 01:00:09188// We don't need locks on Windows for atomically appending to files. The OS
189// provides this functionality.
190#if !defined(OS_WIN)
[email protected]5b84fe32010-09-14 22:24:55191// This class acts as a wrapper for locking the logging files.
192// LoggingLock::Init() should be called from the main thread before any logging
193// is done. Then whenever logging, be sure to have a local LoggingLock
194// instance on the stack. This will ensure that the lock is unlocked upon
195// exiting the frame.
196// LoggingLocks can not be nested.
197class LoggingLock {
198 public:
199 LoggingLock() {
200 LockLogging();
201 }
202
203 ~LoggingLock() {
204 UnlockLogging();
205 }
206
207 static void Init(LogLockingState lock_log, const PathChar* new_log_file) {
208 if (initialized)
209 return;
210 lock_log_file = lock_log;
[email protected]5f95d532010-10-01 17:16:58211
ananta61762fb2015-09-18 01:00:09212 if (lock_log_file != LOCK_LOG_FILE)
[email protected]bc581a682011-01-01 23:16:20213 log_lock = new base::internal::LockImpl();
ananta61762fb2015-09-18 01:00:09214
[email protected]5b84fe32010-09-14 22:24:55215 initialized = true;
216 }
217
218 private:
219 static void LockLogging() {
220 if (lock_log_file == LOCK_LOG_FILE) {
ananta61762fb2015-09-18 01:00:09221#if defined(OS_POSIX)
[email protected]5b84fe32010-09-14 22:24:55222 pthread_mutex_lock(&log_mutex);
223#endif
224 } else {
225 // use the lock
226 log_lock->Lock();
227 }
228 }
229
230 static void UnlockLogging() {
231 if (lock_log_file == LOCK_LOG_FILE) {
ananta61762fb2015-09-18 01:00:09232#if defined(OS_POSIX)
[email protected]5b84fe32010-09-14 22:24:55233 pthread_mutex_unlock(&log_mutex);
234#endif
235 } else {
236 log_lock->Unlock();
237 }
238 }
239
240 // The lock is used if log file locking is false. It helps us avoid problems
241 // with multiple threads writing to the log file at the same time. Use
242 // LockImpl directly instead of using Lock, because Lock makes logging calls.
[email protected]bc581a682011-01-01 23:16:20243 static base::internal::LockImpl* log_lock;
[email protected]5b84fe32010-09-14 22:24:55244
245 // When we don't use a lock, we are using a global mutex. We need to do this
246 // because LockFileEx is not thread safe.
ananta61762fb2015-09-18 01:00:09247#if defined(OS_POSIX)
[email protected]5b84fe32010-09-14 22:24:55248 static pthread_mutex_t log_mutex;
249#endif
250
251 static bool initialized;
252 static LogLockingState lock_log_file;
253};
254
255// static
256bool LoggingLock::initialized = false;
257// static
thestig3e4787d2015-05-19 19:31:52258base::internal::LockImpl* LoggingLock::log_lock = nullptr;
[email protected]5b84fe32010-09-14 22:24:55259// static
260LogLockingState LoggingLock::lock_log_file = LOCK_LOG_FILE;
261
ananta61762fb2015-09-18 01:00:09262#if defined(OS_POSIX)
[email protected]5b84fe32010-09-14 22:24:55263pthread_mutex_t LoggingLock::log_mutex = PTHREAD_MUTEX_INITIALIZER;
264#endif
265
ananta61762fb2015-09-18 01:00:09266#endif // OS_WIN
267
thestig3e4787d2015-05-19 19:31:52268// Called by logging functions to ensure that |g_log_file| is initialized
initial.commitd7cae122008-07-26 21:49:38269// and can be used for writing. Returns false if the file could not be
thestig3e4787d2015-05-19 19:31:52270// initialized. |g_log_file| will be nullptr in this case.
initial.commitd7cae122008-07-26 21:49:38271bool InitializeLogFileHandle() {
thestig3e4787d2015-05-19 19:31:52272 if (g_log_file)
initial.commitd7cae122008-07-26 21:49:38273 return true;
274
thestig3e4787d2015-05-19 19:31:52275 if (!g_log_file_name) {
[email protected]614e9fa2008-08-11 22:52:59276 // Nobody has called InitLogging to specify a debug log file, so here we
277 // initialize the log file name to a default.
thestig3e4787d2015-05-19 19:31:52278 g_log_file_name = new PathString(GetDefaultLogFile());
initial.commitd7cae122008-07-26 21:49:38279 }
280
thestig3e4787d2015-05-19 19:31:52281 if ((g_logging_destination & LOG_TO_FILE) != 0) {
[email protected]614e9fa2008-08-11 22:52:59282#if defined(OS_WIN)
ananta61762fb2015-09-18 01:00:09283 // The FILE_APPEND_DATA access mask ensures that the file is atomically
284 // appended to across accesses from multiple threads.
285 // https://msdn.microsoft.com/en-us/library/windows/desktop/aa364399(v=vs.85).aspx
286 // https://msdn.microsoft.com/en-us/library/windows/desktop/aa363858(v=vs.85).aspx
287 g_log_file = CreateFile(g_log_file_name->c_str(), FILE_APPEND_DATA,
thestig3e4787d2015-05-19 19:31:52288 FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr,
289 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr);
290 if (g_log_file == INVALID_HANDLE_VALUE || g_log_file == nullptr) {
[email protected]1d8c2702008-08-19 23:39:32291 // try the current directory
ananta61762fb2015-09-18 01:00:09292 base::FilePath file_path;
293 if (!base::GetCurrentDirectory(&file_path))
294 return false;
295
296 *g_log_file_name = file_path.Append(
297 FILE_PATH_LITERAL("debug.log")).value();
298
299 g_log_file = CreateFile(g_log_file_name->c_str(), FILE_APPEND_DATA,
thestig3e4787d2015-05-19 19:31:52300 FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr,
301 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr);
302 if (g_log_file == INVALID_HANDLE_VALUE || g_log_file == nullptr) {
303 g_log_file = nullptr;
[email protected]1d8c2702008-08-19 23:39:32304 return false;
305 }
initial.commitd7cae122008-07-26 21:49:38306 }
[email protected]78c6dd62009-06-08 23:29:11307#elif defined(OS_POSIX)
thestig3e4787d2015-05-19 19:31:52308 g_log_file = fopen(g_log_file_name->c_str(), "a");
309 if (g_log_file == nullptr)
[email protected]78c6dd62009-06-08 23:29:11310 return false;
[email protected]f6abeba2008-08-08 13:27:28311#endif
[email protected]1d8c2702008-08-19 23:39:32312 }
313
initial.commitd7cae122008-07-26 21:49:38314 return true;
315}
316
[email protected]17dcf752013-07-15 21:47:09317void CloseFile(FileHandle log) {
318#if defined(OS_WIN)
319 CloseHandle(log);
320#else
321 fclose(log);
322#endif
323}
324
325void CloseLogFileUnlocked() {
thestig3e4787d2015-05-19 19:31:52326 if (!g_log_file)
[email protected]17dcf752013-07-15 21:47:09327 return;
328
thestig3e4787d2015-05-19 19:31:52329 CloseFile(g_log_file);
330 g_log_file = nullptr;
[email protected]17dcf752013-07-15 21:47:09331}
332
[email protected]064aa162011-12-03 00:30:08333} // namespace
334
[email protected]5e3f7c22013-06-21 21:15:33335LoggingSettings::LoggingSettings()
336 : logging_dest(LOG_DEFAULT),
thestig3e4787d2015-05-19 19:31:52337 log_file(nullptr),
[email protected]5e3f7c22013-06-21 21:15:33338 lock_log(LOCK_LOG_FILE),
[email protected]1a1505512014-03-10 18:23:38339 delete_old(APPEND_TO_OLD_LOG_FILE) {}
[email protected]064aa162011-12-03 00:30:08340
[email protected]5e3f7c22013-06-21 21:15:33341bool BaseInitLoggingImpl(const LoggingSettings& settings) {
[email protected]ac07ec52013-04-22 17:32:45342#if defined(OS_NACL)
[email protected]5e3f7c22013-06-21 21:15:33343 // Can log only to the system debug log.
344 CHECK_EQ(settings.logging_dest & ~LOG_TO_SYSTEM_DEBUG_LOG, 0);
[email protected]ac07ec52013-04-22 17:32:45345#endif
pgal.u-szeged421dddb2014-11-25 12:55:02346 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
thestig3e4787d2015-05-19 19:31:52347 // Don't bother initializing |g_vlog_info| unless we use one of the
[email protected]99b7c57f2010-09-29 19:26:36348 // vlog switches.
349 if (command_line->HasSwitch(switches::kV) ||
350 command_line->HasSwitch(switches::kVModule)) {
thestig3e4787d2015-05-19 19:31:52351 // NOTE: If |g_vlog_info| has already been initialized, it might be in use
[email protected]064aa162011-12-03 00:30:08352 // by another thread. Don't delete the old VLogInfo, just create a second
353 // one. We keep track of both to avoid memory leak warnings.
354 CHECK(!g_vlog_info_prev);
355 g_vlog_info_prev = g_vlog_info;
356
[email protected]99b7c57f2010-09-29 19:26:36357 g_vlog_info =
358 new VlogInfo(command_line->GetSwitchValueASCII(switches::kV),
[email protected]162ac0f2010-11-04 15:50:49359 command_line->GetSwitchValueASCII(switches::kVModule),
thestig3e4787d2015-05-19 19:31:52360 &g_min_log_level);
[email protected]99b7c57f2010-09-29 19:26:36361 }
362
thestig3e4787d2015-05-19 19:31:52363 g_logging_destination = settings.logging_dest;
initial.commitd7cae122008-07-26 21:49:38364
[email protected]5e3f7c22013-06-21 21:15:33365 // ignore file options unless logging to file is set.
thestig3e4787d2015-05-19 19:31:52366 if ((g_logging_destination & LOG_TO_FILE) == 0)
[email protected]c7d5da992010-10-28 00:20:21367 return true;
initial.commitd7cae122008-07-26 21:49:38368
ananta61762fb2015-09-18 01:00:09369#if !defined(OS_WIN)
[email protected]17dcf752013-07-15 21:47:09370 LoggingLock::Init(settings.lock_log, settings.log_file);
371 LoggingLock logging_lock;
ananta61762fb2015-09-18 01:00:09372#endif
[email protected]17dcf752013-07-15 21:47:09373
374 // Calling InitLogging twice or after some log call has already opened the
375 // default log file will re-initialize to the new options.
376 CloseLogFileUnlocked();
377
thestig3e4787d2015-05-19 19:31:52378 if (!g_log_file_name)
379 g_log_file_name = new PathString();
380 *g_log_file_name = settings.log_file;
[email protected]5e3f7c22013-06-21 21:15:33381 if (settings.delete_old == DELETE_OLD_LOG_FILE)
thestig3e4787d2015-05-19 19:31:52382 DeleteFilePath(*g_log_file_name);
initial.commitd7cae122008-07-26 21:49:38383
[email protected]c7d5da992010-10-28 00:20:21384 return InitializeLogFileHandle();
initial.commitd7cae122008-07-26 21:49:38385}
386
387void SetMinLogLevel(int level) {
thestig3e4787d2015-05-19 19:31:52388 g_min_log_level = std::min(LOG_FATAL, level);
initial.commitd7cae122008-07-26 21:49:38389}
390
391int GetMinLogLevel() {
thestig3e4787d2015-05-19 19:31:52392 return g_min_log_level;
initial.commitd7cae122008-07-26 21:49:38393}
394
skobesc78c0ad72015-12-07 20:21:23395bool ShouldCreateLogMessage(int severity) {
396 if (severity < g_min_log_level)
397 return false;
398
399 // Return true here unless we know ~LogMessage won't do anything. Note that
400 // ~LogMessage writes to stderr if severity_ >= kAlwaysPrintErrorLevel, even
401 // when g_logging_destination is LOG_NONE.
402 return g_logging_destination != LOG_NONE || log_message_handler ||
403 severity >= kAlwaysPrintErrorLevel;
404}
405
[email protected]162ac0f2010-11-04 15:50:49406int GetVlogVerbosity() {
407 return std::max(-1, LOG_INFO - GetMinLogLevel());
408}
409
[email protected]99b7c57f2010-09-29 19:26:36410int GetVlogLevelHelper(const char* file, size_t N) {
411 DCHECK_GT(N, 0U);
thestig3e4787d2015-05-19 19:31:52412 // Note: |g_vlog_info| may change on a different thread during startup
413 // (but will always be valid or nullptr).
[email protected]064aa162011-12-03 00:30:08414 VlogInfo* vlog_info = g_vlog_info;
415 return vlog_info ?
416 vlog_info->GetVlogLevel(base::StringPiece(file, N - 1)) :
[email protected]162ac0f2010-11-04 15:50:49417 GetVlogVerbosity();
[email protected]99b7c57f2010-09-29 19:26:36418}
419
initial.commitd7cae122008-07-26 21:49:38420void SetLogItems(bool enable_process_id, bool enable_thread_id,
421 bool enable_timestamp, bool enable_tickcount) {
thestig3e4787d2015-05-19 19:31:52422 g_log_process_id = enable_process_id;
423 g_log_thread_id = enable_thread_id;
424 g_log_timestamp = enable_timestamp;
425 g_log_tickcount = enable_tickcount;
initial.commitd7cae122008-07-26 21:49:38426}
427
[email protected]81e0a852010-08-17 00:38:12428void SetShowErrorDialogs(bool enable_dialogs) {
429 show_error_dialogs = enable_dialogs;
430}
431
initial.commitd7cae122008-07-26 21:49:38432void SetLogAssertHandler(LogAssertHandlerFunction handler) {
433 log_assert_handler = handler;
434}
435
[email protected]2b07b8412009-11-25 15:26:34436void SetLogMessageHandler(LogMessageHandlerFunction handler) {
437 log_message_handler = handler;
438}
439
[email protected]64e5cc02010-11-03 19:20:27440LogMessageHandlerFunction GetLogMessageHandler() {
441 return log_message_handler;
442}
443
[email protected]6d445d32010-09-30 19:10:03444// Explicit instantiations for commonly used comparisons.
445template std::string* MakeCheckOpString<int, int>(
446 const int&, const int&, const char* names);
447template std::string* MakeCheckOpString<unsigned long, unsigned long>(
448 const unsigned long&, const unsigned long&, const char* names);
449template std::string* MakeCheckOpString<unsigned long, unsigned int>(
450 const unsigned long&, const unsigned int&, const char* names);
451template std::string* MakeCheckOpString<unsigned int, unsigned long>(
452 const unsigned int&, const unsigned long&, const char* names);
453template std::string* MakeCheckOpString<std::string, std::string>(
454 const std::string&, const std::string&, const char* name);
[email protected]2b07b8412009-11-25 15:26:34455
[email protected]f2c05492014-06-17 12:04:23456#if !defined(NDEBUG)
[email protected]d81baca42010-03-01 13:10:22457// Displays a message box to the user with the error message in it.
458// Used for fatal messages, where we close the app simultaneously.
[email protected]561513f2010-12-16 23:29:25459// This is for developers only; we don't use this in circumstances
460// (like release builds) where users could see it, since users don't
461// understand these messages anyway.
[email protected]d81baca42010-03-01 13:10:22462void DisplayDebugMessageInDialog(const std::string& str) {
initial.commitd7cae122008-07-26 21:49:38463 if (str.empty())
464 return;
465
[email protected]81e0a852010-08-17 00:38:12466 if (!show_error_dialogs)
[email protected]846ed9c32010-07-29 20:33:44467 return;
468
[email protected]f6abeba2008-08-08 13:27:28469#if defined(OS_WIN)
brettwe33526852015-10-03 00:46:01470 MessageBoxW(nullptr, base::UTF8ToUTF16(str).c_str(), L"Fatal error",
471 MB_OK | MB_ICONHAND | MB_TOPMOST);
[email protected]f6abeba2008-08-08 13:27:28472#else
[email protected]561513f2010-12-16 23:29:25473 // We intentionally don't implement a dialog on other platforms.
474 // You can just look at stderr.
thestig3e4787d2015-05-19 19:31:52475#endif // defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:38476}
[email protected]f2c05492014-06-17 12:04:23477#endif // !defined(NDEBUG)
initial.commitd7cae122008-07-26 21:49:38478
[email protected]3f85caa2009-04-14 16:52:11479#if defined(OS_WIN)
480LogMessage::SaveLastError::SaveLastError() : last_error_(::GetLastError()) {
481}
482
483LogMessage::SaveLastError::~SaveLastError() {
484 ::SetLastError(last_error_);
485}
486#endif // defined(OS_WIN)
487
[email protected]eae9c062011-01-11 00:50:59488LogMessage::LogMessage(const char* file, int line, LogSeverity severity)
489 : severity_(severity), file_(file), line_(line) {
490 Init(file, line);
491}
492
tnagel4a045d3f2015-07-12 14:19:28493LogMessage::LogMessage(const char* file, int line, const char* condition)
494 : severity_(LOG_FATAL), file_(file), line_(line) {
495 Init(file, line);
496 stream_ << "Check failed: " << condition << ". ";
497}
498
[email protected]9c7132e2011-02-08 07:39:08499LogMessage::LogMessage(const char* file, int line, std::string* result)
[email protected]162ac0f2010-11-04 15:50:49500 : severity_(LOG_FATAL), file_(file), line_(line) {
initial.commitd7cae122008-07-26 21:49:38501 Init(file, line);
[email protected]9c7132e2011-02-08 07:39:08502 stream_ << "Check failed: " << *result;
503 delete result;
initial.commitd7cae122008-07-26 21:49:38504}
505
[email protected]fb62a532009-02-12 01:19:05506LogMessage::LogMessage(const char* file, int line, LogSeverity severity,
[email protected]9c7132e2011-02-08 07:39:08507 std::string* result)
[email protected]162ac0f2010-11-04 15:50:49508 : severity_(severity), file_(file), line_(line) {
[email protected]fb62a532009-02-12 01:19:05509 Init(file, line);
[email protected]9c7132e2011-02-08 07:39:08510 stream_ << "Check failed: " << *result;
511 delete result;
[email protected]fb62a532009-02-12 01:19:05512}
513
initial.commitd7cae122008-07-26 21:49:38514LogMessage::~LogMessage() {
jam79dc59a2015-08-17 03:38:16515#if !defined(OFFICIAL_BUILD) && !defined(OS_NACL) && !defined(__UCLIBC__)
brucedawson7c559eb2015-09-05 00:34:42516 if (severity_ == LOG_FATAL && !base::debug::BeingDebugged()) {
517 // Include a stack trace on a fatal, unless a debugger is attached.
[email protected]58580352010-10-26 04:07:50518 base::debug::StackTrace trace;
[email protected]d1ccc35a2010-03-24 05:03:24519 stream_ << std::endl; // Newline to separate from log message.
520 trace.OutputToStream(&stream_);
521 }
[email protected]1d8c2702008-08-19 23:39:32522#endif
[email protected]d1ccc35a2010-03-24 05:03:24523 stream_ << std::endl;
524 std::string str_newline(stream_.str());
525
[email protected]2b07b8412009-11-25 15:26:34526 // Give any log message handler first dibs on the message.
[email protected]5e3f7c22013-06-21 21:15:33527 if (log_message_handler &&
528 log_message_handler(severity_, file_, line_,
529 message_start_, str_newline)) {
[email protected]162ac0f2010-11-04 15:50:49530 // The handler took care of it, no further processing.
[email protected]2b07b8412009-11-25 15:26:34531 return;
[email protected]162ac0f2010-11-04 15:50:49532 }
initial.commitd7cae122008-07-26 21:49:38533
thestig3e4787d2015-05-19 19:31:52534 if ((g_logging_destination & LOG_TO_SYSTEM_DEBUG_LOG) != 0) {
[email protected]f6abeba2008-08-08 13:27:28535#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:38536 OutputDebugStringA(str_newline.c_str());
mark4c7449c2015-11-10 19:53:42537#elif defined(OS_MACOSX)
538 // In LOG_TO_SYSTEM_DEBUG_LOG mode, log messages are always written to
539 // stderr. If stderr is /dev/null, also log via ASL (Apple System Log). If
540 // there's something weird about stderr, assume that log messages are going
541 // nowhere and log via ASL too. Messages logged via ASL show up in
542 // Console.app.
543 //
544 // Programs started by launchd, as UI applications normally are, have had
545 // stderr connected to /dev/null since OS X 10.8. Prior to that, stderr was
546 // a pipe to launchd, which logged what it received (see log_redirect_fd in
547 // 10.7.5 launchd-392.39/launchd/src/launchd_core_logic.c).
548 //
549 // Another alternative would be to determine whether stderr is a pipe to
550 // launchd and avoid logging via ASL only in that case. See 10.7.5
551 // CF-635.21/CFUtilities.c also_do_stderr(). This would result in logging to
552 // both stderr and ASL even in tests, where it's undesirable to log to the
553 // system log at all.
554 //
555 // Note that the ASL client by default discards messages whose levels are
556 // below ASL_LEVEL_NOTICE. It's possible to change that with
557 // asl_set_filter(), but this is pointless because syslogd normally applies
558 // the same filter.
559 const bool log_via_asl = []() {
560 struct stat stderr_stat;
561 if (fstat(fileno(stderr), &stderr_stat) == -1) {
562 return true;
563 }
564 if (!S_ISCHR(stderr_stat.st_mode)) {
565 return false;
566 }
567
568 struct stat dev_null_stat;
569 if (stat(_PATH_DEVNULL, &dev_null_stat) == -1) {
570 return true;
571 }
572
573 return !S_ISCHR(dev_null_stat.st_mode) ||
574 stderr_stat.st_rdev == dev_null_stat.st_rdev;
575 }();
576
577 if (log_via_asl) {
578 // Log roughly the same way that CFLog() and NSLog() would. See 10.10.5
579 // CF-1153.18/CFUtilities.c __CFLogCString().
580 //
581 // The ASL facility is set to the main bundle ID if available. Otherwise,
582 // "com.apple.console" is used.
583 CFBundleRef main_bundle = CFBundleGetMainBundle();
584 CFStringRef main_bundle_id_cf =
585 main_bundle ? CFBundleGetIdentifier(main_bundle) : nullptr;
586 std::string asl_facility =
587 main_bundle_id_cf ? base::SysCFStringRefToUTF8(main_bundle_id_cf)
588 : std::string("com.apple.console");
589
590 class ASLClient {
591 public:
592 explicit ASLClient(const std::string& asl_facility)
593 : client_(asl_open(nullptr,
594 asl_facility.c_str(),
595 ASL_OPT_NO_DELAY)) {}
596 ~ASLClient() { asl_close(client_); }
597
598 aslclient get() const { return client_; }
599
600 private:
601 aslclient client_;
602 DISALLOW_COPY_AND_ASSIGN(ASLClient);
603 } asl_client(asl_facility);
604
605 class ASLMessage {
606 public:
607 ASLMessage() : message_(asl_new(ASL_TYPE_MSG)) {}
608 ~ASLMessage() { asl_free(message_); }
609
610 aslmsg get() const { return message_; }
611
612 private:
613 aslmsg message_;
614 DISALLOW_COPY_AND_ASSIGN(ASLMessage);
615 } asl_message;
616
617 // By default, messages are only readable by the admin group. Explicitly
618 // make them readable by the user generating the messages.
619 char euid_string[12];
620 snprintf(euid_string, arraysize(euid_string), "%d", geteuid());
621 asl_set(asl_message.get(), ASL_KEY_READ_UID, euid_string);
622
623 // Map Chrome log severities to ASL log levels.
624 const char* const asl_level_string = [](LogSeverity severity) {
625 // ASL_LEVEL_* are ints, but ASL needs equivalent strings. This
626 // non-obvious two-step macro trick achieves what's needed.
627 // https://gcc.gnu.org/onlinedocs/cpp/Stringification.html
628#define ASL_LEVEL_STR(level) ASL_LEVEL_STR_X(level)
629#define ASL_LEVEL_STR_X(level) #level
630 switch (severity) {
631 case LOG_INFO:
632 return ASL_LEVEL_STR(ASL_LEVEL_INFO);
633 case LOG_WARNING:
634 return ASL_LEVEL_STR(ASL_LEVEL_WARNING);
635 case LOG_ERROR:
636 return ASL_LEVEL_STR(ASL_LEVEL_ERR);
637 case LOG_FATAL:
638 return ASL_LEVEL_STR(ASL_LEVEL_CRIT);
639 default:
640 return severity < 0 ? ASL_LEVEL_STR(ASL_LEVEL_DEBUG)
641 : ASL_LEVEL_STR(ASL_LEVEL_NOTICE);
642 }
643#undef ASL_LEVEL_STR
644#undef ASL_LEVEL_STR_X
645 }(severity_);
646 asl_set(asl_message.get(), ASL_KEY_LEVEL, asl_level_string);
647
648 asl_set(asl_message.get(), ASL_KEY_MSG, str_newline.c_str());
649
650 asl_send(asl_client.get(), asl_message.get());
651 }
[email protected]3132e35c2011-07-07 20:46:50652#elif defined(OS_ANDROID)
[email protected]efbae7da2013-05-21 22:39:25653 android_LogPriority priority =
654 (severity_ < 0) ? ANDROID_LOG_VERBOSE : ANDROID_LOG_UNKNOWN;
[email protected]3132e35c2011-07-07 20:46:50655 switch (severity_) {
656 case LOG_INFO:
657 priority = ANDROID_LOG_INFO;
658 break;
659 case LOG_WARNING:
660 priority = ANDROID_LOG_WARN;
661 break;
662 case LOG_ERROR:
[email protected]3132e35c2011-07-07 20:46:50663 priority = ANDROID_LOG_ERROR;
664 break;
665 case LOG_FATAL:
666 priority = ANDROID_LOG_FATAL;
667 break;
668 }
669 __android_log_write(priority, "chromium", str_newline.c_str());
[email protected]107bc0f12008-08-26 17:48:18670#endif
[email protected]51105382014-03-14 17:02:15671 ignore_result(fwrite(str_newline.data(), str_newline.size(), 1, stderr));
[email protected]469006c2010-09-24 15:43:06672 fflush(stderr);
[email protected]a33c9892008-08-25 20:10:31673 } else if (severity_ >= kAlwaysPrintErrorLevel) {
674 // When we're only outputting to a log file, above a certain log level, we
675 // should still output to stderr so that we can better detect and diagnose
676 // problems with unit tests, especially on the buildbots.
[email protected]51105382014-03-14 17:02:15677 ignore_result(fwrite(str_newline.data(), str_newline.size(), 1, stderr));
[email protected]1ce41052009-12-02 00:34:02678 fflush(stderr);
[email protected]f6abeba2008-08-08 13:27:28679 }
[email protected]52a261f2009-03-03 15:01:12680
initial.commitd7cae122008-07-26 21:49:38681 // write to log file
thestig3e4787d2015-05-19 19:31:52682 if ((g_logging_destination & LOG_TO_FILE) != 0) {
[email protected]17dcf752013-07-15 21:47:09683 // We can have multiple threads and/or processes, so try to prevent them
684 // from clobbering each other's writes.
685 // If the client app did not call InitLogging, and the lock has not
686 // been created do it now. We do this on demand, but if two threads try
687 // to do this at the same time, there will be a race condition to create
688 // the lock. This is why InitLogging should be called from the main
689 // thread at the beginning of execution.
ananta61762fb2015-09-18 01:00:09690#if !defined(OS_WIN)
thestig3e4787d2015-05-19 19:31:52691 LoggingLock::Init(LOCK_LOG_FILE, nullptr);
[email protected]5b84fe32010-09-14 22:24:55692 LoggingLock logging_lock;
ananta61762fb2015-09-18 01:00:09693#endif
[email protected]5b84fe32010-09-14 22:24:55694 if (InitializeLogFileHandle()) {
[email protected]f6abeba2008-08-08 13:27:28695#if defined(OS_WIN)
[email protected]5b84fe32010-09-14 22:24:55696 DWORD num_written;
thestig3e4787d2015-05-19 19:31:52697 WriteFile(g_log_file,
[email protected]5b84fe32010-09-14 22:24:55698 static_cast<const void*>(str_newline.c_str()),
699 static_cast<DWORD>(str_newline.length()),
700 &num_written,
thestig3e4787d2015-05-19 19:31:52701 nullptr);
[email protected]cba21962010-08-31 22:35:55702#else
[email protected]51105382014-03-14 17:02:15703 ignore_result(fwrite(
thestig3e4787d2015-05-19 19:31:52704 str_newline.data(), str_newline.size(), 1, g_log_file));
705 fflush(g_log_file);
[email protected]cba21962010-08-31 22:35:55706#endif
initial.commitd7cae122008-07-26 21:49:38707 }
708 }
709
710 if (severity_ == LOG_FATAL) {
[email protected]eb4c4d032012-04-03 18:45:05711 // Ensure the first characters of the string are on the stack so they
712 // are contained in minidumps for diagnostic purposes.
713 char str_stack[1024];
714 str_newline.copy(str_stack, arraysize(str_stack));
715 base::debug::Alias(str_stack);
716
[email protected]82d89ab2014-02-28 18:25:34717 if (log_assert_handler) {
718 // Make a copy of the string for the handler out of paranoia.
719 log_assert_handler(std::string(stream_.str()));
[email protected]1ffe08c12008-08-13 11:15:11720 } else {
[email protected]82d89ab2014-02-28 18:25:34721 // Don't use the string with the newline, get a fresh version to send to
722 // the debug message process. We also don't display assertions to the
723 // user in release mode. The enduser can't do anything with this
724 // information, and displaying message boxes when the application is
725 // hosed can cause additional problems.
[email protected]4d5901272008-11-06 00:33:50726#ifndef NDEBUG
brucedawson7c559eb2015-09-05 00:34:42727 if (!base::debug::BeingDebugged()) {
728 // Displaying a dialog is unnecessary when debugging and can complicate
729 // debugging.
730 DisplayDebugMessageInDialog(stream_.str());
731 }
[email protected]4d5901272008-11-06 00:33:50732#endif
[email protected]82d89ab2014-02-28 18:25:34733 // Crash the process to generate a dump.
734 base::debug::BreakDebugger();
initial.commitd7cae122008-07-26 21:49:38735 }
736 }
737}
738
[email protected]eae9c062011-01-11 00:50:59739// writes the common header info to the stream
740void LogMessage::Init(const char* file, int line) {
741 base::StringPiece filename(file);
742 size_t last_slash_pos = filename.find_last_of("\\/");
743 if (last_slash_pos != base::StringPiece::npos)
744 filename.remove_prefix(last_slash_pos + 1);
745
746 // TODO(darin): It might be nice if the columns were fixed width.
747
748 stream_ << '[';
thestig3e4787d2015-05-19 19:31:52749 if (g_log_process_id)
[email protected]eae9c062011-01-11 00:50:59750 stream_ << CurrentProcessId() << ':';
thestig3e4787d2015-05-19 19:31:52751 if (g_log_thread_id)
[email protected]63e66802012-01-18 21:21:09752 stream_ << base::PlatformThread::CurrentId() << ':';
thestig3e4787d2015-05-19 19:31:52753 if (g_log_timestamp) {
754 time_t t = time(nullptr);
[email protected]eae9c062011-01-11 00:50:59755 struct tm local_time = {0};
mostynb7e42a8f2014-12-19 12:47:46756#ifdef _MSC_VER
[email protected]eae9c062011-01-11 00:50:59757 localtime_s(&local_time, &t);
758#else
759 localtime_r(&t, &local_time);
760#endif
761 struct tm* tm_time = &local_time;
762 stream_ << std::setfill('0')
763 << std::setw(2) << 1 + tm_time->tm_mon
764 << std::setw(2) << tm_time->tm_mday
765 << '/'
766 << std::setw(2) << tm_time->tm_hour
767 << std::setw(2) << tm_time->tm_min
768 << std::setw(2) << tm_time->tm_sec
769 << ':';
770 }
thestig3e4787d2015-05-19 19:31:52771 if (g_log_tickcount)
[email protected]eae9c062011-01-11 00:50:59772 stream_ << TickCount() << ':';
773 if (severity_ >= 0)
[email protected]80f360a2014-01-23 01:36:19774 stream_ << log_severity_name(severity_);
[email protected]eae9c062011-01-11 00:50:59775 else
776 stream_ << "VERBOSE" << -severity_;
777
778 stream_ << ":" << filename << "(" << line << ")] ";
779
pkasting9cf9b94a2014-10-01 22:18:43780 message_start_ = stream_.str().length();
[email protected]eae9c062011-01-11 00:50:59781}
782
[email protected]d8617a62009-10-09 23:52:20783#if defined(OS_WIN)
784// This has already been defined in the header, but defining it again as DWORD
785// ensures that the type used in the header is equivalent to DWORD. If not,
786// the redefinition is a compile error.
787typedef DWORD SystemErrorCode;
788#endif
789
790SystemErrorCode GetLastSystemErrorCode() {
791#if defined(OS_WIN)
792 return ::GetLastError();
793#elif defined(OS_POSIX)
794 return errno;
795#else
796#error Not implemented
797#endif
798}
799
800#if defined(OS_WIN)
[email protected]c914d8a2014-04-23 01:11:01801BASE_EXPORT std::string SystemErrorCodeToString(SystemErrorCode error_code) {
thestig75f87352014-12-03 21:42:27802 const int kErrorMessageBufferSize = 256;
803 char msgbuf[kErrorMessageBufferSize];
[email protected]c914d8a2014-04-23 01:11:01804 DWORD flags = FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS;
thestig3e4787d2015-05-19 19:31:52805 DWORD len = FormatMessageA(flags, nullptr, error_code, 0, msgbuf,
806 arraysize(msgbuf), nullptr);
[email protected]c914d8a2014-04-23 01:11:01807 if (len) {
808 // Messages returned by system end with line breaks.
809 return base::CollapseWhitespaceASCII(msgbuf, true) +
810 base::StringPrintf(" (0x%X)", error_code);
811 }
812 return base::StringPrintf("Error (0x%X) while retrieving error. (0x%X)",
813 GetLastError(), error_code);
[email protected]d8617a62009-10-09 23:52:20814}
[email protected]c914d8a2014-04-23 01:11:01815#elif defined(OS_POSIX)
816BASE_EXPORT std::string SystemErrorCodeToString(SystemErrorCode error_code) {
brettw6ee6fd62015-06-09 18:05:24817 return base::safe_strerror(error_code);
[email protected]c914d8a2014-04-23 01:11:01818}
819#else
820#error Not implemented
thestig3e4787d2015-05-19 19:31:52821#endif // defined(OS_WIN)
[email protected]d8617a62009-10-09 23:52:20822
[email protected]c914d8a2014-04-23 01:11:01823
824#if defined(OS_WIN)
[email protected]d8617a62009-10-09 23:52:20825Win32ErrorLogMessage::Win32ErrorLogMessage(const char* file,
826 int line,
827 LogSeverity severity,
828 SystemErrorCode err)
829 : err_(err),
[email protected]d8617a62009-10-09 23:52:20830 log_message_(file, line, severity) {
831}
832
833Win32ErrorLogMessage::~Win32ErrorLogMessage() {
[email protected]c914d8a2014-04-23 01:11:01834 stream() << ": " << SystemErrorCodeToString(err_);
[email protected]20909e72012-04-05 16:57:06835 // We're about to crash (CHECK). Put |err_| on the stack (by placing it in a
836 // field) and use Alias in hopes that it makes it into crash dumps.
837 DWORD last_error = err_;
838 base::debug::Alias(&last_error);
[email protected]d8617a62009-10-09 23:52:20839}
840#elif defined(OS_POSIX)
841ErrnoLogMessage::ErrnoLogMessage(const char* file,
842 int line,
843 LogSeverity severity,
844 SystemErrorCode err)
845 : err_(err),
846 log_message_(file, line, severity) {
847}
848
849ErrnoLogMessage::~ErrnoLogMessage() {
[email protected]c914d8a2014-04-23 01:11:01850 stream() << ": " << SystemErrorCodeToString(err_);
[email protected]d8617a62009-10-09 23:52:20851}
thestig3e4787d2015-05-19 19:31:52852#endif // defined(OS_WIN)
[email protected]d8617a62009-10-09 23:52:20853
initial.commitd7cae122008-07-26 21:49:38854void CloseLogFile() {
ananta61762fb2015-09-18 01:00:09855#if !defined(OS_WIN)
[email protected]5b84fe32010-09-14 22:24:55856 LoggingLock logging_lock;
ananta61762fb2015-09-18 01:00:09857#endif
[email protected]17dcf752013-07-15 21:47:09858 CloseLogFileUnlocked();
initial.commitd7cae122008-07-26 21:49:38859}
860
[email protected]e36ddc82009-12-08 04:22:50861void RawLog(int level, const char* message) {
thestig3e4787d2015-05-19 19:31:52862 if (level >= g_min_log_level) {
[email protected]e36ddc82009-12-08 04:22:50863 size_t bytes_written = 0;
864 const size_t message_len = strlen(message);
865 int rv;
866 while (bytes_written < message_len) {
867 rv = HANDLE_EINTR(
868 write(STDERR_FILENO, message + bytes_written,
869 message_len - bytes_written));
870 if (rv < 0) {
871 // Give up, nothing we can do now.
872 break;
873 }
874 bytes_written += rv;
875 }
876
877 if (message_len > 0 && message[message_len - 1] != '\n') {
878 do {
879 rv = HANDLE_EINTR(write(STDERR_FILENO, "\n", 1));
880 if (rv < 0) {
881 // Give up, nothing we can do now.
882 break;
883 }
884 } while (rv != 1);
885 }
886 }
887
888 if (level == LOG_FATAL)
[email protected]58580352010-10-26 04:07:50889 base::debug::BreakDebugger();
[email protected]e36ddc82009-12-08 04:22:50890}
891
[email protected]34a907732012-01-20 06:33:27892// This was defined at the beginning of this file.
893#undef write
894
[email protected]f01b88a2013-02-27 22:04:00895#if defined(OS_WIN)
ananta61762fb2015-09-18 01:00:09896bool IsLoggingToFileEnabled() {
897 return g_logging_destination & LOG_TO_FILE;
898}
899
[email protected]f01b88a2013-02-27 22:04:00900std::wstring GetLogFileFullPath() {
thestig3e4787d2015-05-19 19:31:52901 if (g_log_file_name)
902 return *g_log_file_name;
[email protected]f01b88a2013-02-27 22:04:00903 return std::wstring();
904}
905#endif
906
tnagel80388e682015-05-26 13:27:56907BASE_EXPORT void LogErrorNotReached(const char* file, int line) {
tnagelff3f34a2015-05-24 12:59:14908 LogMessage(file, line, LOG_ERROR).stream()
909 << "NOTREACHED() hit.";
910}
911
[email protected]96fd0032009-04-24 00:13:08912} // namespace logging
initial.commitd7cae122008-07-26 21:49:38913
[email protected]81411c62014-07-08 23:03:06914std::ostream& std::operator<<(std::ostream& out, const wchar_t* wstr) {
thestig75f87352014-12-03 21:42:27915 return out << base::WideToUTF8(wstr);
initial.commitd7cae122008-07-26 21:49:38916}