[email protected] | 63e6680 | 2012-01-18 21:21:09 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 4 | |
[email protected] | b16ef31 | 2008-08-19 18:36:23 | [diff] [blame] | 5 | #include "base/logging.h" |
[email protected] | f6abeba | 2008-08-08 13:27:28 | [diff] [blame] | 6 | |
avi | 51ba3e69 | 2015-12-26 17:30:50 | [diff] [blame] | 7 | #include <limits.h> |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 8 | #include <stdint.h> |
| 9 | |
| 10 | #include "base/macros.h" |
| 11 | #include "build/build_config.h" |
avi | 51ba3e69 | 2015-12-26 17:30:50 | [diff] [blame] | 12 | |
[email protected] | b16ef31 | 2008-08-19 18:36:23 | [diff] [blame] | 13 | #if defined(OS_WIN) |
[email protected] | e36ddc8 | 2009-12-08 04:22:50 | [diff] [blame] | 14 | #include <io.h> |
[email protected] | f6abeba | 2008-08-08 13:27:28 | [diff] [blame] | 15 | #include <windows.h> |
ananta | 61762fb | 2015-09-18 01:00:09 | [diff] [blame] | 16 | #include "base/files/file_path.h" |
| 17 | #include "base/files/file_util.h" |
[email protected] | f6abeba | 2008-08-08 13:27:28 | [diff] [blame] | 18 | typedef HANDLE FileHandle; |
| 19 | typedef HANDLE MutexHandle; |
[email protected] | e36ddc8 | 2009-12-08 04:22:50 | [diff] [blame] | 20 | // 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] | 052f1b5 | 2008-11-06 21:43:07 | [diff] [blame] | 24 | #elif defined(OS_MACOSX) |
mark | 4c7449c | 2015-11-10 19:53:42 | [diff] [blame] | 25 | #include <asl.h> |
| 26 | #include <CoreFoundation/CoreFoundation.h> |
[email protected] | f6abeba | 2008-08-08 13:27:28 | [diff] [blame] | 27 | #include <mach/mach.h> |
| 28 | #include <mach/mach_time.h> |
| 29 | #include <mach-o/dyld.h> |
[email protected] | e43eddf1 | 2009-12-29 00:32:52 | [diff] [blame] | 30 | #elif defined(OS_POSIX) |
[email protected] | 19ea84ca | 2010-11-12 08:37:08 | [diff] [blame] | 31 | #if defined(OS_NACL) |
thestig | 75f8735 | 2014-12-03 21:42:27 | [diff] [blame] | 32 | #include <sys/time.h> // timespec doesn't seem to be in <time.h> |
[email protected] | 19ea84ca | 2010-11-12 08:37:08 | [diff] [blame] | 33 | #endif |
[email protected] | 052f1b5 | 2008-11-06 21:43:07 | [diff] [blame] | 34 | #include <time.h> |
[email protected] | 614e9fa | 2008-08-11 22:52:59 | [diff] [blame] | 35 | #endif |
| 36 | |
| 37 | #if defined(OS_POSIX) |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 38 | #include <errno.h> |
mark | 4c7449c | 2015-11-10 19:53:42 | [diff] [blame] | 39 | #include <paths.h> |
[email protected] | 166326c6 | 2010-08-05 15:50:23 | [diff] [blame] | 40 | #include <pthread.h> |
[email protected] | f6abeba | 2008-08-08 13:27:28 | [diff] [blame] | 41 | #include <stdio.h> |
[email protected] | eb62f726 | 2013-03-30 14:29:00 | [diff] [blame] | 42 | #include <stdlib.h> |
[email protected] | f6abeba | 2008-08-08 13:27:28 | [diff] [blame] | 43 | #include <string.h> |
mark | 4c7449c | 2015-11-10 19:53:42 | [diff] [blame] | 44 | #include <sys/stat.h> |
[email protected] | f6abeba | 2008-08-08 13:27:28 | [diff] [blame] | 45 | #include <unistd.h> |
| 46 | #define MAX_PATH PATH_MAX |
| 47 | typedef FILE* FileHandle; |
| 48 | typedef pthread_mutex_t* MutexHandle; |
| 49 | #endif |
| 50 | |
[email protected] | 1f88b516 | 2011-04-01 00:02:29 | [diff] [blame] | 51 | #include <algorithm> |
| 52 | #include <cstring> |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 53 | #include <ctime> |
| 54 | #include <iomanip> |
[email protected] | 1f88b516 | 2011-04-01 00:02:29 | [diff] [blame] | 55 | #include <ostream> |
[email protected] | c914d8a | 2014-04-23 01:11:01 | [diff] [blame] | 56 | #include <string> |
[email protected] | b16ef31 | 2008-08-19 18:36:23 | [diff] [blame] | 57 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 58 | #include "base/base_switches.h" |
| 59 | #include "base/command_line.h" |
[email protected] | eb4c4d03 | 2012-04-03 18:45:05 | [diff] [blame] | 60 | #include "base/debug/alias.h" |
[email protected] | 5858035 | 2010-10-26 04:07:50 | [diff] [blame] | 61 | #include "base/debug/debugger.h" |
| 62 | #include "base/debug/stack_trace.h" |
[email protected] | 2025d00 | 2012-11-14 20:54:35 | [diff] [blame] | 63 | #include "base/posix/eintr_wrapper.h" |
[email protected] | eb62f726 | 2013-03-30 14:29:00 | [diff] [blame] | 64 | #include "base/strings/string_piece.h" |
[email protected] | c914d8a | 2014-04-23 01:11:01 | [diff] [blame] | 65 | #include "base/strings/string_util.h" |
| 66 | #include "base/strings/stringprintf.h" |
mark | 4c7449c | 2015-11-10 19:53:42 | [diff] [blame] | 67 | #include "base/strings/sys_string_conversions.h" |
[email protected] | a4ea1f1 | 2013-06-07 18:37:07 | [diff] [blame] | 68 | #include "base/strings/utf_string_conversions.h" |
[email protected] | bc581a68 | 2011-01-01 23:16:20 | [diff] [blame] | 69 | #include "base/synchronization/lock_impl.h" |
[email protected] | 63e6680 | 2012-01-18 21:21:09 | [diff] [blame] | 70 | #include "base/threading/platform_thread.h" |
[email protected] | 99b7c57f | 2010-09-29 19:26:36 | [diff] [blame] | 71 | #include "base/vlog.h" |
[email protected] | 53c7ce4 | 2010-12-14 16:20:04 | [diff] [blame] | 72 | #if defined(OS_POSIX) |
brettw | 6ee6fd6 | 2015-06-09 18:05:24 | [diff] [blame] | 73 | #include "base/posix/safe_strerror.h" |
[email protected] | 53c7ce4 | 2010-12-14 16:20:04 | [diff] [blame] | 74 | #endif |
[email protected] | 52a261f | 2009-03-03 15:01:12 | [diff] [blame] | 75 | |
[email protected] | 3132e35c | 2011-07-07 20:46:50 | [diff] [blame] | 76 | #if defined(OS_ANDROID) |
| 77 | #include <android/log.h> |
| 78 | #endif |
| 79 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 80 | namespace logging { |
| 81 | |
[email protected] | 064aa16 | 2011-12-03 00:30:08 | [diff] [blame] | 82 | namespace { |
| 83 | |
thestig | 3e4787d | 2015-05-19 19:31:52 | [diff] [blame] | 84 | VlogInfo* g_vlog_info = nullptr; |
| 85 | VlogInfo* g_vlog_info_prev = nullptr; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 86 | |
| 87 | const char* const log_severity_names[LOG_NUM_SEVERITIES] = { |
[email protected] | f2c0549 | 2014-06-17 12:04:23 | [diff] [blame] | 88 | "INFO", "WARNING", "ERROR", "FATAL" }; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 89 | |
thestig | 75f8735 | 2014-12-03 21:42:27 | [diff] [blame] | 90 | const char* log_severity_name(int severity) { |
[email protected] | 80f360a | 2014-01-23 01:36:19 | [diff] [blame] | 91 | if (severity >= 0 && severity < LOG_NUM_SEVERITIES) |
| 92 | return log_severity_names[severity]; |
| 93 | return "UNKNOWN"; |
| 94 | } |
| 95 | |
thestig | 3e4787d | 2015-05-19 19:31:52 | [diff] [blame] | 96 | int g_min_log_level = 0; |
[email protected] | 1d8c270 | 2008-08-19 23:39:32 | [diff] [blame] | 97 | |
thestig | 3e4787d | 2015-05-19 19:31:52 | [diff] [blame] | 98 | LoggingDestination g_logging_destination = LOG_DEFAULT; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 99 | |
[email protected] | a33c989 | 2008-08-25 20:10:31 | [diff] [blame] | 100 | // For LOG_ERROR and above, always print to stderr. |
| 101 | const int kAlwaysPrintErrorLevel = LOG_ERROR; |
| 102 | |
[email protected] | 614e9fa | 2008-08-11 22:52:59 | [diff] [blame] | 103 | // Which log file to use? This is initialized by InitLogging or |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 104 | // will be lazily initialized to the default value when it is |
| 105 | // first needed. |
[email protected] | f6abeba | 2008-08-08 13:27:28 | [diff] [blame] | 106 | #if defined(OS_WIN) |
[email protected] | 614e9fa | 2008-08-11 22:52:59 | [diff] [blame] | 107 | typedef std::wstring PathString; |
[email protected] | f6abeba | 2008-08-08 13:27:28 | [diff] [blame] | 108 | #else |
[email protected] | 614e9fa | 2008-08-11 22:52:59 | [diff] [blame] | 109 | typedef std::string PathString; |
[email protected] | f6abeba | 2008-08-08 13:27:28 | [diff] [blame] | 110 | #endif |
thestig | 3e4787d | 2015-05-19 19:31:52 | [diff] [blame] | 111 | PathString* g_log_file_name = nullptr; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 112 | |
thestig | 3e4787d | 2015-05-19 19:31:52 | [diff] [blame] | 113 | // This file is lazily opened and the handle may be nullptr |
| 114 | FileHandle g_log_file = nullptr; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 115 | |
thestig | 3e4787d | 2015-05-19 19:31:52 | [diff] [blame] | 116 | // What should be prepended to each message? |
| 117 | bool g_log_process_id = false; |
| 118 | bool g_log_thread_id = false; |
| 119 | bool g_log_timestamp = true; |
| 120 | bool g_log_tickcount = false; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 121 | |
[email protected] | 81e0a85 | 2010-08-17 00:38:12 | [diff] [blame] | 122 | // Should we pop up fatal debug messages in a dialog? |
| 123 | bool show_error_dialogs = false; |
| 124 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 125 | // An assert handler override specified by the client to be called instead of |
[email protected] | fb62a53 | 2009-02-12 01:19:05 | [diff] [blame] | 126 | // the debug message dialog and process termination. |
thestig | 3e4787d | 2015-05-19 19:31:52 | [diff] [blame] | 127 | LogAssertHandlerFunction log_assert_handler = nullptr; |
[email protected] | 2b07b841 | 2009-11-25 15:26:34 | [diff] [blame] | 128 | // A log message handler that gets notified of every log message we process. |
thestig | 3e4787d | 2015-05-19 19:31:52 | [diff] [blame] | 129 | LogMessageHandlerFunction log_message_handler = nullptr; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 130 | |
[email protected] | f6abeba | 2008-08-08 13:27:28 | [diff] [blame] | 131 | // Helper functions to wrap platform differences. |
| 132 | |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 133 | int32_t CurrentProcessId() { |
[email protected] | f858847 | 2008-11-05 23:17:24 | [diff] [blame] | 134 | #if defined(OS_WIN) |
| 135 | return GetCurrentProcessId(); |
| 136 | #elif defined(OS_POSIX) |
| 137 | return getpid(); |
| 138 | #endif |
| 139 | } |
| 140 | |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 141 | uint64_t TickCount() { |
[email protected] | f858847 | 2008-11-05 23:17:24 | [diff] [blame] | 142 | #if defined(OS_WIN) |
| 143 | return GetTickCount(); |
| 144 | #elif defined(OS_MACOSX) |
| 145 | return mach_absolute_time(); |
[email protected] | 19ea84ca | 2010-11-12 08:37:08 | [diff] [blame] | 146 | #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] | e43eddf1 | 2009-12-29 00:32:52 | [diff] [blame] | 150 | #elif defined(OS_POSIX) |
[email protected] | 052f1b5 | 2008-11-06 21:43:07 | [diff] [blame] | 151 | struct timespec ts; |
| 152 | clock_gettime(CLOCK_MONOTONIC, &ts); |
| 153 | |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 154 | uint64_t absolute_micro = static_cast<int64_t>(ts.tv_sec) * 1000000 + |
| 155 | static_cast<int64_t>(ts.tv_nsec) / 1000; |
[email protected] | 052f1b5 | 2008-11-06 21:43:07 | [diff] [blame] | 156 | |
| 157 | return absolute_micro; |
[email protected] | f858847 | 2008-11-05 23:17:24 | [diff] [blame] | 158 | #endif |
| 159 | } |
| 160 | |
[email protected] | 614e9fa | 2008-08-11 22:52:59 | [diff] [blame] | 161 | void DeleteFilePath(const PathString& log_name) { |
[email protected] | f6abeba | 2008-08-08 13:27:28 | [diff] [blame] | 162 | #if defined(OS_WIN) |
[email protected] | 614e9fa | 2008-08-11 22:52:59 | [diff] [blame] | 163 | DeleteFile(log_name.c_str()); |
thestig | 75f8735 | 2014-12-03 21:42:27 | [diff] [blame] | 164 | #elif defined(OS_NACL) |
[email protected] | ac07ec5 | 2013-04-22 17:32:45 | [diff] [blame] | 165 | // Do nothing; unlink() isn't supported on NaCl. |
[email protected] | f6abeba | 2008-08-08 13:27:28 | [diff] [blame] | 166 | #else |
[email protected] | 614e9fa | 2008-08-11 22:52:59 | [diff] [blame] | 167 | unlink(log_name.c_str()); |
[email protected] | f6abeba | 2008-08-08 13:27:28 | [diff] [blame] | 168 | #endif |
| 169 | } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 170 | |
[email protected] | 5f95d53 | 2010-10-01 17:16:58 | [diff] [blame] | 171 | PathString GetDefaultLogFile() { |
[email protected] | 5b84fe3 | 2010-09-14 22:24:55 | [diff] [blame] | 172 | #if defined(OS_WIN) |
| 173 | // On Windows we use the same path as the exe. |
| 174 | wchar_t module_name[MAX_PATH]; |
thestig | 3e4787d | 2015-05-19 19:31:52 | [diff] [blame] | 175 | GetModuleFileName(nullptr, module_name, MAX_PATH); |
[email protected] | 5f95d53 | 2010-10-01 17:16:58 | [diff] [blame] | 176 | |
scottmg | fc5b707 | 2015-01-27 21:46:28 | [diff] [blame] | 177 | PathString log_name = module_name; |
| 178 | PathString::size_type last_backslash = log_name.rfind('\\', log_name.size()); |
[email protected] | 5f95d53 | 2010-10-01 17:16:58 | [diff] [blame] | 179 | if (last_backslash != PathString::npos) |
scottmg | fc5b707 | 2015-01-27 21:46:28 | [diff] [blame] | 180 | log_name.erase(last_backslash + 1); |
| 181 | log_name += L"debug.log"; |
| 182 | return log_name; |
[email protected] | 5b84fe3 | 2010-09-14 22:24:55 | [diff] [blame] | 183 | #elif defined(OS_POSIX) |
| 184 | // On other platforms we just use the current directory. |
[email protected] | 5f95d53 | 2010-10-01 17:16:58 | [diff] [blame] | 185 | return PathString("debug.log"); |
[email protected] | 5b84fe3 | 2010-09-14 22:24:55 | [diff] [blame] | 186 | #endif |
| 187 | } |
| 188 | |
ananta | 61762fb | 2015-09-18 01:00:09 | [diff] [blame] | 189 | // 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] | 5b84fe3 | 2010-09-14 22:24:55 | [diff] [blame] | 192 | // 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. |
| 198 | class 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] | 5f95d53 | 2010-10-01 17:16:58 | [diff] [blame] | 212 | |
ananta | 61762fb | 2015-09-18 01:00:09 | [diff] [blame] | 213 | if (lock_log_file != LOCK_LOG_FILE) |
[email protected] | bc581a68 | 2011-01-01 23:16:20 | [diff] [blame] | 214 | log_lock = new base::internal::LockImpl(); |
ananta | 61762fb | 2015-09-18 01:00:09 | [diff] [blame] | 215 | |
[email protected] | 5b84fe3 | 2010-09-14 22:24:55 | [diff] [blame] | 216 | initialized = true; |
| 217 | } |
| 218 | |
| 219 | private: |
| 220 | static void LockLogging() { |
| 221 | if (lock_log_file == LOCK_LOG_FILE) { |
ananta | 61762fb | 2015-09-18 01:00:09 | [diff] [blame] | 222 | #if defined(OS_POSIX) |
[email protected] | 5b84fe3 | 2010-09-14 22:24:55 | [diff] [blame] | 223 | 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) { |
ananta | 61762fb | 2015-09-18 01:00:09 | [diff] [blame] | 233 | #if defined(OS_POSIX) |
[email protected] | 5b84fe3 | 2010-09-14 22:24:55 | [diff] [blame] | 234 | 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] | bc581a68 | 2011-01-01 23:16:20 | [diff] [blame] | 244 | static base::internal::LockImpl* log_lock; |
[email protected] | 5b84fe3 | 2010-09-14 22:24:55 | [diff] [blame] | 245 | |
| 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. |
ananta | 61762fb | 2015-09-18 01:00:09 | [diff] [blame] | 248 | #if defined(OS_POSIX) |
[email protected] | 5b84fe3 | 2010-09-14 22:24:55 | [diff] [blame] | 249 | static pthread_mutex_t log_mutex; |
| 250 | #endif |
| 251 | |
| 252 | static bool initialized; |
| 253 | static LogLockingState lock_log_file; |
| 254 | }; |
| 255 | |
| 256 | // static |
| 257 | bool LoggingLock::initialized = false; |
| 258 | // static |
thestig | 3e4787d | 2015-05-19 19:31:52 | [diff] [blame] | 259 | base::internal::LockImpl* LoggingLock::log_lock = nullptr; |
[email protected] | 5b84fe3 | 2010-09-14 22:24:55 | [diff] [blame] | 260 | // static |
| 261 | LogLockingState LoggingLock::lock_log_file = LOCK_LOG_FILE; |
| 262 | |
ananta | 61762fb | 2015-09-18 01:00:09 | [diff] [blame] | 263 | #if defined(OS_POSIX) |
[email protected] | 5b84fe3 | 2010-09-14 22:24:55 | [diff] [blame] | 264 | pthread_mutex_t LoggingLock::log_mutex = PTHREAD_MUTEX_INITIALIZER; |
| 265 | #endif |
| 266 | |
ananta | 61762fb | 2015-09-18 01:00:09 | [diff] [blame] | 267 | #endif // OS_WIN |
| 268 | |
thestig | 3e4787d | 2015-05-19 19:31:52 | [diff] [blame] | 269 | // Called by logging functions to ensure that |g_log_file| is initialized |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 270 | // and can be used for writing. Returns false if the file could not be |
thestig | 3e4787d | 2015-05-19 19:31:52 | [diff] [blame] | 271 | // initialized. |g_log_file| will be nullptr in this case. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 272 | bool InitializeLogFileHandle() { |
thestig | 3e4787d | 2015-05-19 19:31:52 | [diff] [blame] | 273 | if (g_log_file) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 274 | return true; |
| 275 | |
thestig | 3e4787d | 2015-05-19 19:31:52 | [diff] [blame] | 276 | if (!g_log_file_name) { |
[email protected] | 614e9fa | 2008-08-11 22:52:59 | [diff] [blame] | 277 | // Nobody has called InitLogging to specify a debug log file, so here we |
| 278 | // initialize the log file name to a default. |
thestig | 3e4787d | 2015-05-19 19:31:52 | [diff] [blame] | 279 | g_log_file_name = new PathString(GetDefaultLogFile()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 280 | } |
| 281 | |
thestig | 3e4787d | 2015-05-19 19:31:52 | [diff] [blame] | 282 | if ((g_logging_destination & LOG_TO_FILE) != 0) { |
[email protected] | 614e9fa | 2008-08-11 22:52:59 | [diff] [blame] | 283 | #if defined(OS_WIN) |
ananta | 61762fb | 2015-09-18 01:00:09 | [diff] [blame] | 284 | // 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, |
thestig | 3e4787d | 2015-05-19 19:31:52 | [diff] [blame] | 289 | 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] | 1d8c270 | 2008-08-19 23:39:32 | [diff] [blame] | 292 | // try the current directory |
ananta | 61762fb | 2015-09-18 01:00:09 | [diff] [blame] | 293 | 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, |
thestig | 3e4787d | 2015-05-19 19:31:52 | [diff] [blame] | 301 | 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] | 1d8c270 | 2008-08-19 23:39:32 | [diff] [blame] | 305 | return false; |
| 306 | } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 307 | } |
[email protected] | 78c6dd6 | 2009-06-08 23:29:11 | [diff] [blame] | 308 | #elif defined(OS_POSIX) |
thestig | 3e4787d | 2015-05-19 19:31:52 | [diff] [blame] | 309 | g_log_file = fopen(g_log_file_name->c_str(), "a"); |
| 310 | if (g_log_file == nullptr) |
[email protected] | 78c6dd6 | 2009-06-08 23:29:11 | [diff] [blame] | 311 | return false; |
[email protected] | f6abeba | 2008-08-08 13:27:28 | [diff] [blame] | 312 | #endif |
[email protected] | 1d8c270 | 2008-08-19 23:39:32 | [diff] [blame] | 313 | } |
| 314 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 315 | return true; |
| 316 | } |
| 317 | |
[email protected] | 17dcf75 | 2013-07-15 21:47:09 | [diff] [blame] | 318 | void CloseFile(FileHandle log) { |
| 319 | #if defined(OS_WIN) |
| 320 | CloseHandle(log); |
| 321 | #else |
| 322 | fclose(log); |
| 323 | #endif |
| 324 | } |
| 325 | |
| 326 | void CloseLogFileUnlocked() { |
thestig | 3e4787d | 2015-05-19 19:31:52 | [diff] [blame] | 327 | if (!g_log_file) |
[email protected] | 17dcf75 | 2013-07-15 21:47:09 | [diff] [blame] | 328 | return; |
| 329 | |
thestig | 3e4787d | 2015-05-19 19:31:52 | [diff] [blame] | 330 | CloseFile(g_log_file); |
| 331 | g_log_file = nullptr; |
[email protected] | 17dcf75 | 2013-07-15 21:47:09 | [diff] [blame] | 332 | } |
| 333 | |
[email protected] | 064aa16 | 2011-12-03 00:30:08 | [diff] [blame] | 334 | } // namespace |
| 335 | |
[email protected] | 5e3f7c2 | 2013-06-21 21:15:33 | [diff] [blame] | 336 | LoggingSettings::LoggingSettings() |
| 337 | : logging_dest(LOG_DEFAULT), |
thestig | 3e4787d | 2015-05-19 19:31:52 | [diff] [blame] | 338 | log_file(nullptr), |
[email protected] | 5e3f7c2 | 2013-06-21 21:15:33 | [diff] [blame] | 339 | lock_log(LOCK_LOG_FILE), |
[email protected] | 1a150551 | 2014-03-10 18:23:38 | [diff] [blame] | 340 | delete_old(APPEND_TO_OLD_LOG_FILE) {} |
[email protected] | 064aa16 | 2011-12-03 00:30:08 | [diff] [blame] | 341 | |
[email protected] | 5e3f7c2 | 2013-06-21 21:15:33 | [diff] [blame] | 342 | bool BaseInitLoggingImpl(const LoggingSettings& settings) { |
[email protected] | ac07ec5 | 2013-04-22 17:32:45 | [diff] [blame] | 343 | #if defined(OS_NACL) |
[email protected] | 5e3f7c2 | 2013-06-21 21:15:33 | [diff] [blame] | 344 | // Can log only to the system debug log. |
| 345 | CHECK_EQ(settings.logging_dest & ~LOG_TO_SYSTEM_DEBUG_LOG, 0); |
[email protected] | ac07ec5 | 2013-04-22 17:32:45 | [diff] [blame] | 346 | #endif |
pgal.u-szeged | 421dddb | 2014-11-25 12:55:02 | [diff] [blame] | 347 | base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); |
thestig | 3e4787d | 2015-05-19 19:31:52 | [diff] [blame] | 348 | // Don't bother initializing |g_vlog_info| unless we use one of the |
[email protected] | 99b7c57f | 2010-09-29 19:26:36 | [diff] [blame] | 349 | // vlog switches. |
| 350 | if (command_line->HasSwitch(switches::kV) || |
| 351 | command_line->HasSwitch(switches::kVModule)) { |
thestig | 3e4787d | 2015-05-19 19:31:52 | [diff] [blame] | 352 | // NOTE: If |g_vlog_info| has already been initialized, it might be in use |
[email protected] | 064aa16 | 2011-12-03 00:30:08 | [diff] [blame] | 353 | // 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] | 99b7c57f | 2010-09-29 19:26:36 | [diff] [blame] | 358 | g_vlog_info = |
| 359 | new VlogInfo(command_line->GetSwitchValueASCII(switches::kV), |
[email protected] | 162ac0f | 2010-11-04 15:50:49 | [diff] [blame] | 360 | command_line->GetSwitchValueASCII(switches::kVModule), |
thestig | 3e4787d | 2015-05-19 19:31:52 | [diff] [blame] | 361 | &g_min_log_level); |
[email protected] | 99b7c57f | 2010-09-29 19:26:36 | [diff] [blame] | 362 | } |
| 363 | |
thestig | 3e4787d | 2015-05-19 19:31:52 | [diff] [blame] | 364 | g_logging_destination = settings.logging_dest; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 365 | |
[email protected] | 5e3f7c2 | 2013-06-21 21:15:33 | [diff] [blame] | 366 | // ignore file options unless logging to file is set. |
thestig | 3e4787d | 2015-05-19 19:31:52 | [diff] [blame] | 367 | if ((g_logging_destination & LOG_TO_FILE) == 0) |
[email protected] | c7d5da99 | 2010-10-28 00:20:21 | [diff] [blame] | 368 | return true; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 369 | |
ananta | 61762fb | 2015-09-18 01:00:09 | [diff] [blame] | 370 | #if !defined(OS_WIN) |
[email protected] | 17dcf75 | 2013-07-15 21:47:09 | [diff] [blame] | 371 | LoggingLock::Init(settings.lock_log, settings.log_file); |
| 372 | LoggingLock logging_lock; |
ananta | 61762fb | 2015-09-18 01:00:09 | [diff] [blame] | 373 | #endif |
[email protected] | 17dcf75 | 2013-07-15 21:47:09 | [diff] [blame] | 374 | |
| 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 | |
thestig | 3e4787d | 2015-05-19 19:31:52 | [diff] [blame] | 379 | if (!g_log_file_name) |
| 380 | g_log_file_name = new PathString(); |
| 381 | *g_log_file_name = settings.log_file; |
[email protected] | 5e3f7c2 | 2013-06-21 21:15:33 | [diff] [blame] | 382 | if (settings.delete_old == DELETE_OLD_LOG_FILE) |
thestig | 3e4787d | 2015-05-19 19:31:52 | [diff] [blame] | 383 | DeleteFilePath(*g_log_file_name); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 384 | |
[email protected] | c7d5da99 | 2010-10-28 00:20:21 | [diff] [blame] | 385 | return InitializeLogFileHandle(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | void SetMinLogLevel(int level) { |
thestig | 3e4787d | 2015-05-19 19:31:52 | [diff] [blame] | 389 | g_min_log_level = std::min(LOG_FATAL, level); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | int GetMinLogLevel() { |
thestig | 3e4787d | 2015-05-19 19:31:52 | [diff] [blame] | 393 | return g_min_log_level; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 394 | } |
| 395 | |
skobes | c78c0ad7 | 2015-12-07 20:21:23 | [diff] [blame] | 396 | bool 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] | 162ac0f | 2010-11-04 15:50:49 | [diff] [blame] | 407 | int GetVlogVerbosity() { |
| 408 | return std::max(-1, LOG_INFO - GetMinLogLevel()); |
| 409 | } |
| 410 | |
[email protected] | 99b7c57f | 2010-09-29 19:26:36 | [diff] [blame] | 411 | int GetVlogLevelHelper(const char* file, size_t N) { |
| 412 | DCHECK_GT(N, 0U); |
thestig | 3e4787d | 2015-05-19 19:31:52 | [diff] [blame] | 413 | // Note: |g_vlog_info| may change on a different thread during startup |
| 414 | // (but will always be valid or nullptr). |
[email protected] | 064aa16 | 2011-12-03 00:30:08 | [diff] [blame] | 415 | VlogInfo* vlog_info = g_vlog_info; |
| 416 | return vlog_info ? |
| 417 | vlog_info->GetVlogLevel(base::StringPiece(file, N - 1)) : |
[email protected] | 162ac0f | 2010-11-04 15:50:49 | [diff] [blame] | 418 | GetVlogVerbosity(); |
[email protected] | 99b7c57f | 2010-09-29 19:26:36 | [diff] [blame] | 419 | } |
| 420 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 421 | void SetLogItems(bool enable_process_id, bool enable_thread_id, |
| 422 | bool enable_timestamp, bool enable_tickcount) { |
thestig | 3e4787d | 2015-05-19 19:31:52 | [diff] [blame] | 423 | 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.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 427 | } |
| 428 | |
[email protected] | 81e0a85 | 2010-08-17 00:38:12 | [diff] [blame] | 429 | void SetShowErrorDialogs(bool enable_dialogs) { |
| 430 | show_error_dialogs = enable_dialogs; |
| 431 | } |
| 432 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 433 | void SetLogAssertHandler(LogAssertHandlerFunction handler) { |
| 434 | log_assert_handler = handler; |
| 435 | } |
| 436 | |
[email protected] | 2b07b841 | 2009-11-25 15:26:34 | [diff] [blame] | 437 | void SetLogMessageHandler(LogMessageHandlerFunction handler) { |
| 438 | log_message_handler = handler; |
| 439 | } |
| 440 | |
[email protected] | 64e5cc0 | 2010-11-03 19:20:27 | [diff] [blame] | 441 | LogMessageHandlerFunction GetLogMessageHandler() { |
| 442 | return log_message_handler; |
| 443 | } |
| 444 | |
[email protected] | 6d445d3 | 2010-09-30 19:10:03 | [diff] [blame] | 445 | // Explicit instantiations for commonly used comparisons. |
| 446 | template std::string* MakeCheckOpString<int, int>( |
| 447 | const int&, const int&, const char* names); |
| 448 | template std::string* MakeCheckOpString<unsigned long, unsigned long>( |
| 449 | const unsigned long&, const unsigned long&, const char* names); |
| 450 | template std::string* MakeCheckOpString<unsigned long, unsigned int>( |
| 451 | const unsigned long&, const unsigned int&, const char* names); |
| 452 | template std::string* MakeCheckOpString<unsigned int, unsigned long>( |
| 453 | const unsigned int&, const unsigned long&, const char* names); |
| 454 | template std::string* MakeCheckOpString<std::string, std::string>( |
| 455 | const std::string&, const std::string&, const char* name); |
[email protected] | 2b07b841 | 2009-11-25 15:26:34 | [diff] [blame] | 456 | |
jbroman | 6bcfec42 | 2016-05-26 00:28:46 | [diff] [blame^] | 457 | void MakeCheckOpValueString(std::ostream* os, std::nullptr_t p) { |
brucedawson | 93a60b8c | 2016-04-28 20:46:16 | [diff] [blame] | 458 | (*os) << "nullptr"; |
| 459 | } |
| 460 | |
[email protected] | f2c0549 | 2014-06-17 12:04:23 | [diff] [blame] | 461 | #if !defined(NDEBUG) |
[email protected] | d81baca4 | 2010-03-01 13:10:22 | [diff] [blame] | 462 | // 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] | 561513f | 2010-12-16 23:29:25 | [diff] [blame] | 464 | // 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] | d81baca4 | 2010-03-01 13:10:22 | [diff] [blame] | 467 | void DisplayDebugMessageInDialog(const std::string& str) { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 468 | if (str.empty()) |
| 469 | return; |
| 470 | |
[email protected] | 81e0a85 | 2010-08-17 00:38:12 | [diff] [blame] | 471 | if (!show_error_dialogs) |
[email protected] | 846ed9c3 | 2010-07-29 20:33:44 | [diff] [blame] | 472 | return; |
| 473 | |
[email protected] | f6abeba | 2008-08-08 13:27:28 | [diff] [blame] | 474 | #if defined(OS_WIN) |
brettw | e3352685 | 2015-10-03 00:46:01 | [diff] [blame] | 475 | MessageBoxW(nullptr, base::UTF8ToUTF16(str).c_str(), L"Fatal error", |
| 476 | MB_OK | MB_ICONHAND | MB_TOPMOST); |
[email protected] | f6abeba | 2008-08-08 13:27:28 | [diff] [blame] | 477 | #else |
[email protected] | 561513f | 2010-12-16 23:29:25 | [diff] [blame] | 478 | // We intentionally don't implement a dialog on other platforms. |
| 479 | // You can just look at stderr. |
thestig | 3e4787d | 2015-05-19 19:31:52 | [diff] [blame] | 480 | #endif // defined(OS_WIN) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 481 | } |
[email protected] | f2c0549 | 2014-06-17 12:04:23 | [diff] [blame] | 482 | #endif // !defined(NDEBUG) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 483 | |
[email protected] | 3f85caa | 2009-04-14 16:52:11 | [diff] [blame] | 484 | #if defined(OS_WIN) |
| 485 | LogMessage::SaveLastError::SaveLastError() : last_error_(::GetLastError()) { |
| 486 | } |
| 487 | |
| 488 | LogMessage::SaveLastError::~SaveLastError() { |
| 489 | ::SetLastError(last_error_); |
| 490 | } |
| 491 | #endif // defined(OS_WIN) |
| 492 | |
[email protected] | eae9c06 | 2011-01-11 00:50:59 | [diff] [blame] | 493 | LogMessage::LogMessage(const char* file, int line, LogSeverity severity) |
| 494 | : severity_(severity), file_(file), line_(line) { |
| 495 | Init(file, line); |
| 496 | } |
| 497 | |
tnagel | 4a045d3f | 2015-07-12 14:19:28 | [diff] [blame] | 498 | LogMessage::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] | 9c7132e | 2011-02-08 07:39:08 | [diff] [blame] | 504 | LogMessage::LogMessage(const char* file, int line, std::string* result) |
[email protected] | 162ac0f | 2010-11-04 15:50:49 | [diff] [blame] | 505 | : severity_(LOG_FATAL), file_(file), line_(line) { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 506 | Init(file, line); |
[email protected] | 9c7132e | 2011-02-08 07:39:08 | [diff] [blame] | 507 | stream_ << "Check failed: " << *result; |
| 508 | delete result; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 509 | } |
| 510 | |
[email protected] | fb62a53 | 2009-02-12 01:19:05 | [diff] [blame] | 511 | LogMessage::LogMessage(const char* file, int line, LogSeverity severity, |
[email protected] | 9c7132e | 2011-02-08 07:39:08 | [diff] [blame] | 512 | std::string* result) |
[email protected] | 162ac0f | 2010-11-04 15:50:49 | [diff] [blame] | 513 | : severity_(severity), file_(file), line_(line) { |
[email protected] | fb62a53 | 2009-02-12 01:19:05 | [diff] [blame] | 514 | Init(file, line); |
[email protected] | 9c7132e | 2011-02-08 07:39:08 | [diff] [blame] | 515 | stream_ << "Check failed: " << *result; |
| 516 | delete result; |
[email protected] | fb62a53 | 2009-02-12 01:19:05 | [diff] [blame] | 517 | } |
| 518 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 519 | LogMessage::~LogMessage() { |
jam | 79dc59a | 2015-08-17 03:38:16 | [diff] [blame] | 520 | #if !defined(OFFICIAL_BUILD) && !defined(OS_NACL) && !defined(__UCLIBC__) |
brucedawson | 7c559eb | 2015-09-05 00:34:42 | [diff] [blame] | 521 | if (severity_ == LOG_FATAL && !base::debug::BeingDebugged()) { |
| 522 | // Include a stack trace on a fatal, unless a debugger is attached. |
[email protected] | 5858035 | 2010-10-26 04:07:50 | [diff] [blame] | 523 | base::debug::StackTrace trace; |
[email protected] | d1ccc35a | 2010-03-24 05:03:24 | [diff] [blame] | 524 | stream_ << std::endl; // Newline to separate from log message. |
| 525 | trace.OutputToStream(&stream_); |
| 526 | } |
[email protected] | 1d8c270 | 2008-08-19 23:39:32 | [diff] [blame] | 527 | #endif |
[email protected] | d1ccc35a | 2010-03-24 05:03:24 | [diff] [blame] | 528 | stream_ << std::endl; |
| 529 | std::string str_newline(stream_.str()); |
| 530 | |
[email protected] | 2b07b841 | 2009-11-25 15:26:34 | [diff] [blame] | 531 | // Give any log message handler first dibs on the message. |
[email protected] | 5e3f7c2 | 2013-06-21 21:15:33 | [diff] [blame] | 532 | if (log_message_handler && |
| 533 | log_message_handler(severity_, file_, line_, |
| 534 | message_start_, str_newline)) { |
[email protected] | 162ac0f | 2010-11-04 15:50:49 | [diff] [blame] | 535 | // The handler took care of it, no further processing. |
[email protected] | 2b07b841 | 2009-11-25 15:26:34 | [diff] [blame] | 536 | return; |
[email protected] | 162ac0f | 2010-11-04 15:50:49 | [diff] [blame] | 537 | } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 538 | |
thestig | 3e4787d | 2015-05-19 19:31:52 | [diff] [blame] | 539 | if ((g_logging_destination & LOG_TO_SYSTEM_DEBUG_LOG) != 0) { |
[email protected] | f6abeba | 2008-08-08 13:27:28 | [diff] [blame] | 540 | #if defined(OS_WIN) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 541 | OutputDebugStringA(str_newline.c_str()); |
mark | 4c7449c | 2015-11-10 19:53:42 | [diff] [blame] | 542 | #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] | 3132e35c | 2011-07-07 20:46:50 | [diff] [blame] | 657 | #elif defined(OS_ANDROID) |
[email protected] | efbae7da | 2013-05-21 22:39:25 | [diff] [blame] | 658 | android_LogPriority priority = |
| 659 | (severity_ < 0) ? ANDROID_LOG_VERBOSE : ANDROID_LOG_UNKNOWN; |
[email protected] | 3132e35c | 2011-07-07 20:46:50 | [diff] [blame] | 660 | 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] | 3132e35c | 2011-07-07 20:46:50 | [diff] [blame] | 668 | 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] | 107bc0f1 | 2008-08-26 17:48:18 | [diff] [blame] | 675 | #endif |
[email protected] | 5110538 | 2014-03-14 17:02:15 | [diff] [blame] | 676 | ignore_result(fwrite(str_newline.data(), str_newline.size(), 1, stderr)); |
[email protected] | 469006c | 2010-09-24 15:43:06 | [diff] [blame] | 677 | fflush(stderr); |
[email protected] | a33c989 | 2008-08-25 20:10:31 | [diff] [blame] | 678 | } 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] | 5110538 | 2014-03-14 17:02:15 | [diff] [blame] | 682 | ignore_result(fwrite(str_newline.data(), str_newline.size(), 1, stderr)); |
[email protected] | 1ce4105 | 2009-12-02 00:34:02 | [diff] [blame] | 683 | fflush(stderr); |
[email protected] | f6abeba | 2008-08-08 13:27:28 | [diff] [blame] | 684 | } |
[email protected] | 52a261f | 2009-03-03 15:01:12 | [diff] [blame] | 685 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 686 | // write to log file |
thestig | 3e4787d | 2015-05-19 19:31:52 | [diff] [blame] | 687 | if ((g_logging_destination & LOG_TO_FILE) != 0) { |
[email protected] | 17dcf75 | 2013-07-15 21:47:09 | [diff] [blame] | 688 | // 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. |
ananta | 61762fb | 2015-09-18 01:00:09 | [diff] [blame] | 695 | #if !defined(OS_WIN) |
thestig | 3e4787d | 2015-05-19 19:31:52 | [diff] [blame] | 696 | LoggingLock::Init(LOCK_LOG_FILE, nullptr); |
[email protected] | 5b84fe3 | 2010-09-14 22:24:55 | [diff] [blame] | 697 | LoggingLock logging_lock; |
ananta | 61762fb | 2015-09-18 01:00:09 | [diff] [blame] | 698 | #endif |
[email protected] | 5b84fe3 | 2010-09-14 22:24:55 | [diff] [blame] | 699 | if (InitializeLogFileHandle()) { |
[email protected] | f6abeba | 2008-08-08 13:27:28 | [diff] [blame] | 700 | #if defined(OS_WIN) |
[email protected] | 5b84fe3 | 2010-09-14 22:24:55 | [diff] [blame] | 701 | DWORD num_written; |
thestig | 3e4787d | 2015-05-19 19:31:52 | [diff] [blame] | 702 | WriteFile(g_log_file, |
[email protected] | 5b84fe3 | 2010-09-14 22:24:55 | [diff] [blame] | 703 | static_cast<const void*>(str_newline.c_str()), |
| 704 | static_cast<DWORD>(str_newline.length()), |
| 705 | &num_written, |
thestig | 3e4787d | 2015-05-19 19:31:52 | [diff] [blame] | 706 | nullptr); |
[email protected] | cba2196 | 2010-08-31 22:35:55 | [diff] [blame] | 707 | #else |
[email protected] | 5110538 | 2014-03-14 17:02:15 | [diff] [blame] | 708 | ignore_result(fwrite( |
thestig | 3e4787d | 2015-05-19 19:31:52 | [diff] [blame] | 709 | str_newline.data(), str_newline.size(), 1, g_log_file)); |
| 710 | fflush(g_log_file); |
[email protected] | cba2196 | 2010-08-31 22:35:55 | [diff] [blame] | 711 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 712 | } |
| 713 | } |
| 714 | |
| 715 | if (severity_ == LOG_FATAL) { |
[email protected] | eb4c4d03 | 2012-04-03 18:45:05 | [diff] [blame] | 716 | // 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] | 82d89ab | 2014-02-28 18:25:34 | [diff] [blame] | 722 | 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] | 1ffe08c1 | 2008-08-13 11:15:11 | [diff] [blame] | 725 | } else { |
[email protected] | 82d89ab | 2014-02-28 18:25:34 | [diff] [blame] | 726 | // 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] | 4d590127 | 2008-11-06 00:33:50 | [diff] [blame] | 731 | #ifndef NDEBUG |
brucedawson | 7c559eb | 2015-09-05 00:34:42 | [diff] [blame] | 732 | if (!base::debug::BeingDebugged()) { |
| 733 | // Displaying a dialog is unnecessary when debugging and can complicate |
| 734 | // debugging. |
| 735 | DisplayDebugMessageInDialog(stream_.str()); |
| 736 | } |
[email protected] | 4d590127 | 2008-11-06 00:33:50 | [diff] [blame] | 737 | #endif |
[email protected] | 82d89ab | 2014-02-28 18:25:34 | [diff] [blame] | 738 | // Crash the process to generate a dump. |
| 739 | base::debug::BreakDebugger(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 740 | } |
| 741 | } |
| 742 | } |
| 743 | |
[email protected] | eae9c06 | 2011-01-11 00:50:59 | [diff] [blame] | 744 | // writes the common header info to the stream |
| 745 | void 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_ << '['; |
thestig | 3e4787d | 2015-05-19 19:31:52 | [diff] [blame] | 754 | if (g_log_process_id) |
[email protected] | eae9c06 | 2011-01-11 00:50:59 | [diff] [blame] | 755 | stream_ << CurrentProcessId() << ':'; |
thestig | 3e4787d | 2015-05-19 19:31:52 | [diff] [blame] | 756 | if (g_log_thread_id) |
[email protected] | 63e6680 | 2012-01-18 21:21:09 | [diff] [blame] | 757 | stream_ << base::PlatformThread::CurrentId() << ':'; |
thestig | 3e4787d | 2015-05-19 19:31:52 | [diff] [blame] | 758 | if (g_log_timestamp) { |
| 759 | time_t t = time(nullptr); |
[email protected] | eae9c06 | 2011-01-11 00:50:59 | [diff] [blame] | 760 | struct tm local_time = {0}; |
mostynb | 7e42a8f | 2014-12-19 12:47:46 | [diff] [blame] | 761 | #ifdef _MSC_VER |
[email protected] | eae9c06 | 2011-01-11 00:50:59 | [diff] [blame] | 762 | 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 | } |
thestig | 3e4787d | 2015-05-19 19:31:52 | [diff] [blame] | 776 | if (g_log_tickcount) |
[email protected] | eae9c06 | 2011-01-11 00:50:59 | [diff] [blame] | 777 | stream_ << TickCount() << ':'; |
| 778 | if (severity_ >= 0) |
[email protected] | 80f360a | 2014-01-23 01:36:19 | [diff] [blame] | 779 | stream_ << log_severity_name(severity_); |
[email protected] | eae9c06 | 2011-01-11 00:50:59 | [diff] [blame] | 780 | else |
| 781 | stream_ << "VERBOSE" << -severity_; |
| 782 | |
| 783 | stream_ << ":" << filename << "(" << line << ")] "; |
| 784 | |
pkasting | 9cf9b94 | 2014-10-01 22:18:43 | [diff] [blame] | 785 | message_start_ = stream_.str().length(); |
[email protected] | eae9c06 | 2011-01-11 00:50:59 | [diff] [blame] | 786 | } |
| 787 | |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 788 | #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. |
| 792 | typedef DWORD SystemErrorCode; |
| 793 | #endif |
| 794 | |
| 795 | SystemErrorCode 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] | c914d8a | 2014-04-23 01:11:01 | [diff] [blame] | 806 | BASE_EXPORT std::string SystemErrorCodeToString(SystemErrorCode error_code) { |
thestig | 75f8735 | 2014-12-03 21:42:27 | [diff] [blame] | 807 | const int kErrorMessageBufferSize = 256; |
| 808 | char msgbuf[kErrorMessageBufferSize]; |
[email protected] | c914d8a | 2014-04-23 01:11:01 | [diff] [blame] | 809 | DWORD flags = FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS; |
thestig | 3e4787d | 2015-05-19 19:31:52 | [diff] [blame] | 810 | DWORD len = FormatMessageA(flags, nullptr, error_code, 0, msgbuf, |
| 811 | arraysize(msgbuf), nullptr); |
[email protected] | c914d8a | 2014-04-23 01:11:01 | [diff] [blame] | 812 | 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] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 819 | } |
[email protected] | c914d8a | 2014-04-23 01:11:01 | [diff] [blame] | 820 | #elif defined(OS_POSIX) |
| 821 | BASE_EXPORT std::string SystemErrorCodeToString(SystemErrorCode error_code) { |
brettw | 6ee6fd6 | 2015-06-09 18:05:24 | [diff] [blame] | 822 | return base::safe_strerror(error_code); |
[email protected] | c914d8a | 2014-04-23 01:11:01 | [diff] [blame] | 823 | } |
| 824 | #else |
| 825 | #error Not implemented |
thestig | 3e4787d | 2015-05-19 19:31:52 | [diff] [blame] | 826 | #endif // defined(OS_WIN) |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 827 | |
[email protected] | c914d8a | 2014-04-23 01:11:01 | [diff] [blame] | 828 | |
| 829 | #if defined(OS_WIN) |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 830 | Win32ErrorLogMessage::Win32ErrorLogMessage(const char* file, |
| 831 | int line, |
| 832 | LogSeverity severity, |
| 833 | SystemErrorCode err) |
| 834 | : err_(err), |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 835 | log_message_(file, line, severity) { |
| 836 | } |
| 837 | |
| 838 | Win32ErrorLogMessage::~Win32ErrorLogMessage() { |
[email protected] | c914d8a | 2014-04-23 01:11:01 | [diff] [blame] | 839 | stream() << ": " << SystemErrorCodeToString(err_); |
[email protected] | 20909e7 | 2012-04-05 16:57:06 | [diff] [blame] | 840 | // 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] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 844 | } |
| 845 | #elif defined(OS_POSIX) |
| 846 | ErrnoLogMessage::ErrnoLogMessage(const char* file, |
| 847 | int line, |
| 848 | LogSeverity severity, |
| 849 | SystemErrorCode err) |
| 850 | : err_(err), |
| 851 | log_message_(file, line, severity) { |
| 852 | } |
| 853 | |
| 854 | ErrnoLogMessage::~ErrnoLogMessage() { |
[email protected] | c914d8a | 2014-04-23 01:11:01 | [diff] [blame] | 855 | stream() << ": " << SystemErrorCodeToString(err_); |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 856 | } |
thestig | 3e4787d | 2015-05-19 19:31:52 | [diff] [blame] | 857 | #endif // defined(OS_WIN) |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 858 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 859 | void CloseLogFile() { |
ananta | 61762fb | 2015-09-18 01:00:09 | [diff] [blame] | 860 | #if !defined(OS_WIN) |
[email protected] | 5b84fe3 | 2010-09-14 22:24:55 | [diff] [blame] | 861 | LoggingLock logging_lock; |
ananta | 61762fb | 2015-09-18 01:00:09 | [diff] [blame] | 862 | #endif |
[email protected] | 17dcf75 | 2013-07-15 21:47:09 | [diff] [blame] | 863 | CloseLogFileUnlocked(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 864 | } |
| 865 | |
[email protected] | e36ddc8 | 2009-12-08 04:22:50 | [diff] [blame] | 866 | void RawLog(int level, const char* message) { |
erikchen | 0c9fe71 | 2016-03-11 22:07:49 | [diff] [blame] | 867 | if (level >= g_min_log_level && message) { |
[email protected] | e36ddc8 | 2009-12-08 04:22:50 | [diff] [blame] | 868 | 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] | 5858035 | 2010-10-26 04:07:50 | [diff] [blame] | 894 | base::debug::BreakDebugger(); |
[email protected] | e36ddc8 | 2009-12-08 04:22:50 | [diff] [blame] | 895 | } |
| 896 | |
[email protected] | 34a90773 | 2012-01-20 06:33:27 | [diff] [blame] | 897 | // This was defined at the beginning of this file. |
| 898 | #undef write |
| 899 | |
[email protected] | f01b88a | 2013-02-27 22:04:00 | [diff] [blame] | 900 | #if defined(OS_WIN) |
ananta | 61762fb | 2015-09-18 01:00:09 | [diff] [blame] | 901 | bool IsLoggingToFileEnabled() { |
| 902 | return g_logging_destination & LOG_TO_FILE; |
| 903 | } |
| 904 | |
[email protected] | f01b88a | 2013-02-27 22:04:00 | [diff] [blame] | 905 | std::wstring GetLogFileFullPath() { |
thestig | 3e4787d | 2015-05-19 19:31:52 | [diff] [blame] | 906 | if (g_log_file_name) |
| 907 | return *g_log_file_name; |
[email protected] | f01b88a | 2013-02-27 22:04:00 | [diff] [blame] | 908 | return std::wstring(); |
| 909 | } |
| 910 | #endif |
| 911 | |
tnagel | 80388e68 | 2015-05-26 13:27:56 | [diff] [blame] | 912 | BASE_EXPORT void LogErrorNotReached(const char* file, int line) { |
tnagel | ff3f34a | 2015-05-24 12:59:14 | [diff] [blame] | 913 | LogMessage(file, line, LOG_ERROR).stream() |
| 914 | << "NOTREACHED() hit."; |
| 915 | } |
| 916 | |
[email protected] | 96fd003 | 2009-04-24 00:13:08 | [diff] [blame] | 917 | } // namespace logging |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 918 | |
[email protected] | 81411c6 | 2014-07-08 23:03:06 | [diff] [blame] | 919 | std::ostream& std::operator<<(std::ostream& out, const wchar_t* wstr) { |
erikchen | 0c9fe71 | 2016-03-11 22:07:49 | [diff] [blame] | 920 | return out << (wstr ? base::WideToUTF8(wstr) : std::string()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 921 | } |