[email protected] | 96fd003 | 2009-04-24 00:13:08 | [diff] [blame] | 1 | // Copyright (c) 2006-2009 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 | |
[email protected] | b16ef31 | 2008-08-19 18:36:23 | [diff] [blame] | 7 | #if defined(OS_WIN) |
[email protected] | e36ddc8 | 2009-12-08 04:22:50 | [diff] [blame] | 8 | #include <io.h> |
[email protected] | f6abeba | 2008-08-08 13:27:28 | [diff] [blame] | 9 | #include <windows.h> |
| 10 | typedef HANDLE FileHandle; |
| 11 | typedef HANDLE MutexHandle; |
[email protected] | e36ddc8 | 2009-12-08 04:22:50 | [diff] [blame] | 12 | // Windows warns on using write(). It prefers _write(). |
| 13 | #define write(fd, buf, count) _write(fd, buf, static_cast<unsigned int>(count)) |
| 14 | // Windows doesn't define STDERR_FILENO. Define it here. |
| 15 | #define STDERR_FILENO 2 |
[email protected] | 052f1b5 | 2008-11-06 21:43:07 | [diff] [blame] | 16 | #elif defined(OS_MACOSX) |
[email protected] | f6abeba | 2008-08-08 13:27:28 | [diff] [blame] | 17 | #include <CoreFoundation/CoreFoundation.h> |
| 18 | #include <mach/mach.h> |
| 19 | #include <mach/mach_time.h> |
| 20 | #include <mach-o/dyld.h> |
[email protected] | e43eddf1 | 2009-12-29 00:32:52 | [diff] [blame] | 21 | #elif defined(OS_POSIX) |
[email protected] | 052f1b5 | 2008-11-06 21:43:07 | [diff] [blame] | 22 | #include <sys/syscall.h> |
| 23 | #include <time.h> |
[email protected] | 614e9fa | 2008-08-11 22:52:59 | [diff] [blame] | 24 | #endif |
| 25 | |
| 26 | #if defined(OS_POSIX) |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 27 | #include <errno.h> |
[email protected] | f6abeba | 2008-08-08 13:27:28 | [diff] [blame] | 28 | #include <stdlib.h> |
| 29 | #include <stdio.h> |
| 30 | #include <string.h> |
| 31 | #include <unistd.h> |
| 32 | #define MAX_PATH PATH_MAX |
| 33 | typedef FILE* FileHandle; |
| 34 | typedef pthread_mutex_t* MutexHandle; |
| 35 | #endif |
| 36 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 37 | #include <ctime> |
| 38 | #include <iomanip> |
| 39 | #include <cstring> |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 40 | #include <algorithm> |
[email protected] | b16ef31 | 2008-08-19 18:36:23 | [diff] [blame] | 41 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 42 | #include "base/base_switches.h" |
| 43 | #include "base/command_line.h" |
[email protected] | 1ffe08c1 | 2008-08-13 11:15:11 | [diff] [blame] | 44 | #include "base/debug_util.h" |
[email protected] | e36ddc8 | 2009-12-08 04:22:50 | [diff] [blame] | 45 | #include "base/eintr_wrapper.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 46 | #include "base/lock_impl.h" |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 47 | #if defined(OS_POSIX) |
| 48 | #include "base/safe_strerror_posix.h" |
| 49 | #endif |
[email protected] | d81baca4 | 2010-03-01 13:10:22 | [diff] [blame] | 50 | #include "base/process_util.h" |
[email protected] | 4bdaceb4 | 2008-08-19 13:19:24 | [diff] [blame] | 51 | #include "base/string_piece.h" |
[email protected] | f6abeba | 2008-08-08 13:27:28 | [diff] [blame] | 52 | #include "base/string_util.h" |
[email protected] | 047a03f | 2009-10-07 02:10:20 | [diff] [blame] | 53 | #include "base/utf_string_conversions.h" |
[email protected] | 52a261f | 2009-03-03 15:01:12 | [diff] [blame] | 54 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 55 | namespace logging { |
| 56 | |
| 57 | bool g_enable_dcheck = false; |
| 58 | |
| 59 | const char* const log_severity_names[LOG_NUM_SEVERITIES] = { |
[email protected] | fb62a53 | 2009-02-12 01:19:05 | [diff] [blame] | 60 | "INFO", "WARNING", "ERROR", "ERROR_REPORT", "FATAL" }; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 61 | |
| 62 | int min_log_level = 0; |
| 63 | LogLockingState lock_log_file = LOCK_LOG_FILE; |
[email protected] | 1d8c270 | 2008-08-19 23:39:32 | [diff] [blame] | 64 | |
| 65 | // The default set here for logging_destination will only be used if |
| 66 | // InitLogging is not called. On Windows, use a file next to the exe; |
| 67 | // on POSIX platforms, where it may not even be possible to locate the |
| 68 | // executable on disk, use stderr. |
[email protected] | 4c0040c | 2008-08-15 01:04:11 | [diff] [blame] | 69 | #if defined(OS_WIN) |
[email protected] | fe61352 | 2008-08-22 17:09:34 | [diff] [blame] | 70 | LoggingDestination logging_destination = LOG_ONLY_TO_FILE; |
[email protected] | 4c0040c | 2008-08-15 01:04:11 | [diff] [blame] | 71 | #elif defined(OS_POSIX) |
[email protected] | 1d8c270 | 2008-08-19 23:39:32 | [diff] [blame] | 72 | LoggingDestination logging_destination = LOG_ONLY_TO_SYSTEM_DEBUG_LOG; |
[email protected] | 4c0040c | 2008-08-15 01:04:11 | [diff] [blame] | 73 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 74 | |
| 75 | const int kMaxFilteredLogLevel = LOG_WARNING; |
[email protected] | 614e9fa | 2008-08-11 22:52:59 | [diff] [blame] | 76 | std::string* log_filter_prefix; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 77 | |
[email protected] | a33c989 | 2008-08-25 20:10:31 | [diff] [blame] | 78 | // For LOG_ERROR and above, always print to stderr. |
| 79 | const int kAlwaysPrintErrorLevel = LOG_ERROR; |
| 80 | |
[email protected] | 614e9fa | 2008-08-11 22:52:59 | [diff] [blame] | 81 | // Which log file to use? This is initialized by InitLogging or |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 82 | // will be lazily initialized to the default value when it is |
| 83 | // first needed. |
[email protected] | f6abeba | 2008-08-08 13:27:28 | [diff] [blame] | 84 | #if defined(OS_WIN) |
| 85 | typedef wchar_t PathChar; |
[email protected] | 614e9fa | 2008-08-11 22:52:59 | [diff] [blame] | 86 | typedef std::wstring PathString; |
[email protected] | f6abeba | 2008-08-08 13:27:28 | [diff] [blame] | 87 | #else |
| 88 | typedef char PathChar; |
[email protected] | 614e9fa | 2008-08-11 22:52:59 | [diff] [blame] | 89 | typedef std::string PathString; |
[email protected] | f6abeba | 2008-08-08 13:27:28 | [diff] [blame] | 90 | #endif |
[email protected] | 614e9fa | 2008-08-11 22:52:59 | [diff] [blame] | 91 | PathString* log_file_name = NULL; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 92 | |
| 93 | // this file is lazily opened and the handle may be NULL |
[email protected] | f6abeba | 2008-08-08 13:27:28 | [diff] [blame] | 94 | FileHandle log_file = NULL; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 95 | |
| 96 | // what should be prepended to each message? |
| 97 | bool log_process_id = false; |
| 98 | bool log_thread_id = false; |
| 99 | bool log_timestamp = true; |
| 100 | bool log_tickcount = false; |
| 101 | |
| 102 | // An assert handler override specified by the client to be called instead of |
[email protected] | fb62a53 | 2009-02-12 01:19:05 | [diff] [blame] | 103 | // the debug message dialog and process termination. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 104 | LogAssertHandlerFunction log_assert_handler = NULL; |
[email protected] | fb62a53 | 2009-02-12 01:19:05 | [diff] [blame] | 105 | // An report handler override specified by the client to be called instead of |
| 106 | // the debug message dialog. |
| 107 | LogReportHandlerFunction log_report_handler = NULL; |
[email protected] | 2b07b841 | 2009-11-25 15:26:34 | [diff] [blame] | 108 | // A log message handler that gets notified of every log message we process. |
| 109 | LogMessageHandlerFunction log_message_handler = NULL; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 110 | |
| 111 | // The lock is used if log file locking is false. It helps us avoid problems |
| 112 | // with multiple threads writing to the log file at the same time. Use |
| 113 | // LockImpl directly instead of using Lock, because Lock makes logging calls. |
| 114 | static LockImpl* log_lock = NULL; |
| 115 | |
| 116 | // When we don't use a lock, we are using a global mutex. We need to do this |
| 117 | // because LockFileEx is not thread safe. |
[email protected] | f6abeba | 2008-08-08 13:27:28 | [diff] [blame] | 118 | #if defined(OS_WIN) |
| 119 | MutexHandle log_mutex = NULL; |
| 120 | #elif defined(OS_POSIX) |
| 121 | pthread_mutex_t log_mutex = PTHREAD_MUTEX_INITIALIZER; |
| 122 | #endif |
| 123 | |
| 124 | // Helper functions to wrap platform differences. |
| 125 | |
[email protected] | f858847 | 2008-11-05 23:17:24 | [diff] [blame] | 126 | int32 CurrentProcessId() { |
| 127 | #if defined(OS_WIN) |
| 128 | return GetCurrentProcessId(); |
| 129 | #elif defined(OS_POSIX) |
| 130 | return getpid(); |
| 131 | #endif |
| 132 | } |
| 133 | |
| 134 | int32 CurrentThreadId() { |
| 135 | #if defined(OS_WIN) |
| 136 | return GetCurrentThreadId(); |
| 137 | #elif defined(OS_MACOSX) |
| 138 | return mach_thread_self(); |
[email protected] | 052f1b5 | 2008-11-06 21:43:07 | [diff] [blame] | 139 | #elif defined(OS_LINUX) |
| 140 | return syscall(__NR_gettid); |
[email protected] | e43eddf1 | 2009-12-29 00:32:52 | [diff] [blame] | 141 | #elif defined(OS_FREEBSD) |
| 142 | // TODO(BSD): find a better thread ID |
| 143 | return reinterpret_cast<int64>(pthread_self()); |
[email protected] | f858847 | 2008-11-05 23:17:24 | [diff] [blame] | 144 | #endif |
| 145 | } |
| 146 | |
| 147 | uint64 TickCount() { |
| 148 | #if defined(OS_WIN) |
| 149 | return GetTickCount(); |
| 150 | #elif defined(OS_MACOSX) |
| 151 | return mach_absolute_time(); |
[email protected] | e43eddf1 | 2009-12-29 00:32:52 | [diff] [blame] | 152 | #elif defined(OS_POSIX) |
[email protected] | 052f1b5 | 2008-11-06 21:43:07 | [diff] [blame] | 153 | struct timespec ts; |
| 154 | clock_gettime(CLOCK_MONOTONIC, &ts); |
| 155 | |
| 156 | uint64 absolute_micro = |
| 157 | static_cast<int64>(ts.tv_sec) * 1000000 + |
| 158 | static_cast<int64>(ts.tv_nsec) / 1000; |
| 159 | |
| 160 | return absolute_micro; |
[email protected] | f858847 | 2008-11-05 23:17:24 | [diff] [blame] | 161 | #endif |
| 162 | } |
| 163 | |
[email protected] | f6abeba | 2008-08-08 13:27:28 | [diff] [blame] | 164 | void CloseFile(FileHandle log) { |
| 165 | #if defined(OS_WIN) |
| 166 | CloseHandle(log); |
| 167 | #else |
| 168 | fclose(log); |
| 169 | #endif |
| 170 | } |
| 171 | |
[email protected] | 614e9fa | 2008-08-11 22:52:59 | [diff] [blame] | 172 | void DeleteFilePath(const PathString& log_name) { |
[email protected] | f6abeba | 2008-08-08 13:27:28 | [diff] [blame] | 173 | #if defined(OS_WIN) |
[email protected] | 614e9fa | 2008-08-11 22:52:59 | [diff] [blame] | 174 | DeleteFile(log_name.c_str()); |
[email protected] | f6abeba | 2008-08-08 13:27:28 | [diff] [blame] | 175 | #else |
[email protected] | 614e9fa | 2008-08-11 22:52:59 | [diff] [blame] | 176 | unlink(log_name.c_str()); |
[email protected] | f6abeba | 2008-08-08 13:27:28 | [diff] [blame] | 177 | #endif |
| 178 | } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 179 | |
| 180 | // Called by logging functions to ensure that debug_file is initialized |
| 181 | // and can be used for writing. Returns false if the file could not be |
| 182 | // initialized. debug_file will be NULL in this case. |
| 183 | bool InitializeLogFileHandle() { |
| 184 | if (log_file) |
| 185 | return true; |
| 186 | |
[email protected] | 614e9fa | 2008-08-11 22:52:59 | [diff] [blame] | 187 | if (!log_file_name) { |
| 188 | // Nobody has called InitLogging to specify a debug log file, so here we |
| 189 | // initialize the log file name to a default. |
[email protected] | f6abeba | 2008-08-08 13:27:28 | [diff] [blame] | 190 | #if defined(OS_WIN) |
[email protected] | 614e9fa | 2008-08-11 22:52:59 | [diff] [blame] | 191 | // On Windows we use the same path as the exe. |
| 192 | wchar_t module_name[MAX_PATH]; |
| 193 | GetModuleFileName(NULL, module_name, MAX_PATH); |
| 194 | log_file_name = new std::wstring(module_name); |
| 195 | std::wstring::size_type last_backslash = |
| 196 | log_file_name->rfind('\\', log_file_name->size()); |
| 197 | if (last_backslash != std::wstring::npos) |
| 198 | log_file_name->erase(last_backslash + 1); |
| 199 | *log_file_name += L"debug.log"; |
| 200 | #elif defined(OS_POSIX) |
| 201 | // On other platforms we just use the current directory. |
| 202 | log_file_name = new std::string("debug.log"); |
| 203 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 204 | } |
| 205 | |
[email protected] | 1d8c270 | 2008-08-19 23:39:32 | [diff] [blame] | 206 | if (logging_destination == LOG_ONLY_TO_FILE || |
| 207 | logging_destination == LOG_TO_BOTH_FILE_AND_SYSTEM_DEBUG_LOG) { |
[email protected] | 614e9fa | 2008-08-11 22:52:59 | [diff] [blame] | 208 | #if defined(OS_WIN) |
[email protected] | 1d8c270 | 2008-08-19 23:39:32 | [diff] [blame] | 209 | log_file = CreateFile(log_file_name->c_str(), GENERIC_WRITE, |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 210 | FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, |
| 211 | OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); |
| 212 | if (log_file == INVALID_HANDLE_VALUE || log_file == NULL) { |
[email protected] | 1d8c270 | 2008-08-19 23:39:32 | [diff] [blame] | 213 | // try the current directory |
| 214 | log_file = CreateFile(L".\\debug.log", GENERIC_WRITE, |
| 215 | FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, |
| 216 | OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); |
| 217 | if (log_file == INVALID_HANDLE_VALUE || log_file == NULL) { |
| 218 | log_file = NULL; |
| 219 | return false; |
| 220 | } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 221 | } |
[email protected] | 1d8c270 | 2008-08-19 23:39:32 | [diff] [blame] | 222 | SetFilePointer(log_file, 0, 0, FILE_END); |
[email protected] | 78c6dd6 | 2009-06-08 23:29:11 | [diff] [blame] | 223 | #elif defined(OS_POSIX) |
| 224 | log_file = fopen(log_file_name->c_str(), "a"); |
| 225 | if (log_file == NULL) |
| 226 | return false; |
[email protected] | f6abeba | 2008-08-08 13:27:28 | [diff] [blame] | 227 | #endif |
[email protected] | 1d8c270 | 2008-08-19 23:39:32 | [diff] [blame] | 228 | } |
| 229 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 230 | return true; |
| 231 | } |
| 232 | |
| 233 | void InitLogMutex() { |
[email protected] | f6abeba | 2008-08-08 13:27:28 | [diff] [blame] | 234 | #if defined(OS_WIN) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 235 | if (!log_mutex) { |
| 236 | // \ is not a legal character in mutex names so we replace \ with / |
[email protected] | 614e9fa | 2008-08-11 22:52:59 | [diff] [blame] | 237 | std::wstring safe_name(*log_file_name); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 238 | std::replace(safe_name.begin(), safe_name.end(), '\\', '/'); |
| 239 | std::wstring t(L"Global\\"); |
| 240 | t.append(safe_name); |
| 241 | log_mutex = ::CreateMutex(NULL, FALSE, t.c_str()); |
| 242 | } |
[email protected] | f6abeba | 2008-08-08 13:27:28 | [diff] [blame] | 243 | #elif defined(OS_POSIX) |
| 244 | // statically initialized |
| 245 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 246 | } |
| 247 | |
[email protected] | f6abeba | 2008-08-08 13:27:28 | [diff] [blame] | 248 | void InitLogging(const PathChar* new_log_file, LoggingDestination logging_dest, |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 249 | LogLockingState lock_log, OldFileDeletionState delete_old) { |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 250 | g_enable_dcheck = |
| 251 | CommandLine::ForCurrentProcess()->HasSwitch(switches::kEnableDCHECK); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 252 | |
| 253 | if (log_file) { |
| 254 | // calling InitLogging twice or after some log call has already opened the |
| 255 | // default log file will re-initialize to the new options |
[email protected] | f6abeba | 2008-08-08 13:27:28 | [diff] [blame] | 256 | CloseFile(log_file); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 257 | log_file = NULL; |
| 258 | } |
| 259 | |
| 260 | lock_log_file = lock_log; |
| 261 | logging_destination = logging_dest; |
| 262 | |
| 263 | // ignore file options if logging is disabled or only to system |
| 264 | if (logging_destination == LOG_NONE || |
| 265 | logging_destination == LOG_ONLY_TO_SYSTEM_DEBUG_LOG) |
| 266 | return; |
| 267 | |
[email protected] | 614e9fa | 2008-08-11 22:52:59 | [diff] [blame] | 268 | if (!log_file_name) |
| 269 | log_file_name = new PathString(); |
| 270 | *log_file_name = new_log_file; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 271 | if (delete_old == DELETE_OLD_LOG_FILE) |
[email protected] | 614e9fa | 2008-08-11 22:52:59 | [diff] [blame] | 272 | DeleteFilePath(*log_file_name); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 273 | |
| 274 | if (lock_log_file == LOCK_LOG_FILE) { |
| 275 | InitLogMutex(); |
| 276 | } else if (!log_lock) { |
| 277 | log_lock = new LockImpl(); |
| 278 | } |
| 279 | |
| 280 | InitializeLogFileHandle(); |
| 281 | } |
| 282 | |
| 283 | void SetMinLogLevel(int level) { |
| 284 | min_log_level = level; |
| 285 | } |
| 286 | |
| 287 | int GetMinLogLevel() { |
| 288 | return min_log_level; |
| 289 | } |
| 290 | |
| 291 | void SetLogFilterPrefix(const char* filter) { |
| 292 | if (log_filter_prefix) { |
[email protected] | 614e9fa | 2008-08-11 22:52:59 | [diff] [blame] | 293 | delete log_filter_prefix; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 294 | log_filter_prefix = NULL; |
| 295 | } |
| 296 | |
[email protected] | 614e9fa | 2008-08-11 22:52:59 | [diff] [blame] | 297 | if (filter) |
| 298 | log_filter_prefix = new std::string(filter); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | void SetLogItems(bool enable_process_id, bool enable_thread_id, |
| 302 | bool enable_timestamp, bool enable_tickcount) { |
| 303 | log_process_id = enable_process_id; |
| 304 | log_thread_id = enable_thread_id; |
| 305 | log_timestamp = enable_timestamp; |
| 306 | log_tickcount = enable_tickcount; |
| 307 | } |
| 308 | |
| 309 | void SetLogAssertHandler(LogAssertHandlerFunction handler) { |
| 310 | log_assert_handler = handler; |
| 311 | } |
| 312 | |
[email protected] | fb62a53 | 2009-02-12 01:19:05 | [diff] [blame] | 313 | void SetLogReportHandler(LogReportHandlerFunction handler) { |
| 314 | log_report_handler = handler; |
| 315 | } |
| 316 | |
[email protected] | 2b07b841 | 2009-11-25 15:26:34 | [diff] [blame] | 317 | void SetLogMessageHandler(LogMessageHandlerFunction handler) { |
| 318 | log_message_handler = handler; |
| 319 | } |
| 320 | |
| 321 | |
[email protected] | d81baca4 | 2010-03-01 13:10:22 | [diff] [blame] | 322 | // Displays a message box to the user with the error message in it. |
| 323 | // Used for fatal messages, where we close the app simultaneously. |
| 324 | void DisplayDebugMessageInDialog(const std::string& str) { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 325 | if (str.empty()) |
| 326 | return; |
| 327 | |
[email protected] | 846ed9c3 | 2010-07-29 20:33:44 | [diff] [blame^] | 328 | if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kNoErrorDialogs)) |
| 329 | return; |
| 330 | |
[email protected] | f6abeba | 2008-08-08 13:27:28 | [diff] [blame] | 331 | #if defined(OS_WIN) |
[email protected] | d81baca4 | 2010-03-01 13:10:22 | [diff] [blame] | 332 | // For Windows programs, it's possible that the message loop is |
| 333 | // messed up on a fatal error, and creating a MessageBox will cause |
| 334 | // that message loop to be run. Instead, we try to spawn another |
| 335 | // process that displays its command line. We look for "Debug |
| 336 | // Message.exe" in the same directory as the application. If it |
| 337 | // exists, we use it, otherwise, we use a regular message box. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 338 | wchar_t prog_name[MAX_PATH]; |
| 339 | GetModuleFileNameW(NULL, prog_name, MAX_PATH); |
| 340 | wchar_t* backslash = wcsrchr(prog_name, '\\'); |
| 341 | if (backslash) |
| 342 | backslash[1] = 0; |
| 343 | wcscat_s(prog_name, MAX_PATH, L"debug_message.exe"); |
| 344 | |
[email protected] | 047a03f | 2009-10-07 02:10:20 | [diff] [blame] | 345 | std::wstring cmdline = UTF8ToWide(str); |
[email protected] | 3ca4214c1 | 2009-03-25 22:12:02 | [diff] [blame] | 346 | if (cmdline.empty()) |
| 347 | return; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 348 | |
| 349 | STARTUPINFO startup_info; |
| 350 | memset(&startup_info, 0, sizeof(startup_info)); |
| 351 | startup_info.cb = sizeof(startup_info); |
| 352 | |
| 353 | PROCESS_INFORMATION process_info; |
[email protected] | 3ca4214c1 | 2009-03-25 22:12:02 | [diff] [blame] | 354 | if (CreateProcessW(prog_name, &cmdline[0], NULL, NULL, false, 0, NULL, |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 355 | NULL, &startup_info, &process_info)) { |
| 356 | WaitForSingleObject(process_info.hProcess, INFINITE); |
| 357 | CloseHandle(process_info.hThread); |
| 358 | CloseHandle(process_info.hProcess); |
| 359 | } else { |
| 360 | // debug process broken, let's just do a message box |
[email protected] | 3ca4214c1 | 2009-03-25 22:12:02 | [diff] [blame] | 361 | MessageBoxW(NULL, &cmdline[0], L"Fatal error", |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 362 | MB_OK | MB_ICONHAND | MB_TOPMOST); |
| 363 | } |
[email protected] | d81baca4 | 2010-03-01 13:10:22 | [diff] [blame] | 364 | #elif defined(USE_X11) |
| 365 | // Shell out to xmessage, which behaves like debug_message.exe, but is |
| 366 | // way more retro. We could use zenity/kdialog but then we're starting |
| 367 | // to get into needing to check the desktop env and this dialog should |
| 368 | // only be coming up in Very Bad situations. |
| 369 | std::vector<std::string> argv; |
| 370 | argv.push_back("xmessage"); |
| 371 | argv.push_back(str); |
| 372 | base::LaunchApp(argv, base::file_handle_mapping_vector(), true /* wait */, |
| 373 | NULL); |
[email protected] | f6abeba | 2008-08-08 13:27:28 | [diff] [blame] | 374 | #else |
[email protected] | d81baca4 | 2010-03-01 13:10:22 | [diff] [blame] | 375 | // http://code.google.com/p/chromium/issues/detail?id=37026 |
| 376 | NOTIMPLEMENTED(); |
[email protected] | f6abeba | 2008-08-08 13:27:28 | [diff] [blame] | 377 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 378 | } |
| 379 | |
[email protected] | 3f85caa | 2009-04-14 16:52:11 | [diff] [blame] | 380 | #if defined(OS_WIN) |
| 381 | LogMessage::SaveLastError::SaveLastError() : last_error_(::GetLastError()) { |
| 382 | } |
| 383 | |
| 384 | LogMessage::SaveLastError::~SaveLastError() { |
| 385 | ::SetLastError(last_error_); |
| 386 | } |
| 387 | #endif // defined(OS_WIN) |
| 388 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 389 | LogMessage::LogMessage(const char* file, int line, LogSeverity severity, |
| 390 | int ctr) |
| 391 | : severity_(severity) { |
| 392 | Init(file, line); |
| 393 | } |
| 394 | |
| 395 | LogMessage::LogMessage(const char* file, int line, const CheckOpString& result) |
| 396 | : severity_(LOG_FATAL) { |
| 397 | Init(file, line); |
| 398 | stream_ << "Check failed: " << (*result.str_); |
| 399 | } |
| 400 | |
[email protected] | fb62a53 | 2009-02-12 01:19:05 | [diff] [blame] | 401 | LogMessage::LogMessage(const char* file, int line, LogSeverity severity, |
| 402 | const CheckOpString& result) |
| 403 | : severity_(severity) { |
| 404 | Init(file, line); |
| 405 | stream_ << "Check failed: " << (*result.str_); |
| 406 | } |
| 407 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 408 | LogMessage::LogMessage(const char* file, int line) |
| 409 | : severity_(LOG_INFO) { |
| 410 | Init(file, line); |
| 411 | } |
| 412 | |
| 413 | LogMessage::LogMessage(const char* file, int line, LogSeverity severity) |
| 414 | : severity_(severity) { |
| 415 | Init(file, line); |
| 416 | } |
| 417 | |
| 418 | // writes the common header info to the stream |
| 419 | void LogMessage::Init(const char* file, int line) { |
| 420 | // log only the filename |
| 421 | const char* last_slash = strrchr(file, '\\'); |
| 422 | if (last_slash) |
| 423 | file = last_slash + 1; |
| 424 | |
| 425 | // TODO(darin): It might be nice if the columns were fixed width. |
| 426 | |
| 427 | stream_ << '['; |
| 428 | if (log_process_id) |
[email protected] | f858847 | 2008-11-05 23:17:24 | [diff] [blame] | 429 | stream_ << CurrentProcessId() << ':'; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 430 | if (log_thread_id) |
[email protected] | f858847 | 2008-11-05 23:17:24 | [diff] [blame] | 431 | stream_ << CurrentThreadId() << ':'; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 432 | if (log_timestamp) { |
[email protected] | defcd8f3 | 2009-05-13 00:03:43 | [diff] [blame] | 433 | time_t t = time(NULL); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 434 | struct tm local_time = {0}; |
[email protected] | defcd8f3 | 2009-05-13 00:03:43 | [diff] [blame] | 435 | #if _MSC_VER >= 1400 |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 436 | localtime_s(&local_time, &t); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 437 | #else |
[email protected] | defcd8f3 | 2009-05-13 00:03:43 | [diff] [blame] | 438 | localtime_r(&t, &local_time); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 439 | #endif |
[email protected] | defcd8f3 | 2009-05-13 00:03:43 | [diff] [blame] | 440 | struct tm* tm_time = &local_time; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 441 | stream_ << std::setfill('0') |
| 442 | << std::setw(2) << 1 + tm_time->tm_mon |
| 443 | << std::setw(2) << tm_time->tm_mday |
| 444 | << '/' |
| 445 | << std::setw(2) << tm_time->tm_hour |
| 446 | << std::setw(2) << tm_time->tm_min |
| 447 | << std::setw(2) << tm_time->tm_sec |
| 448 | << ':'; |
| 449 | } |
| 450 | if (log_tickcount) |
[email protected] | f858847 | 2008-11-05 23:17:24 | [diff] [blame] | 451 | stream_ << TickCount() << ':'; |
[email protected] | 52a261f | 2009-03-03 15:01:12 | [diff] [blame] | 452 | stream_ << log_severity_names[severity_] << ":" << file << |
| 453 | "(" << line << ")] "; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 454 | |
| 455 | message_start_ = stream_.tellp(); |
| 456 | } |
| 457 | |
| 458 | LogMessage::~LogMessage() { |
| 459 | // TODO(brettw) modify the macros so that nothing is executed when the log |
| 460 | // level is too high. |
| 461 | if (severity_ < min_log_level) |
| 462 | return; |
| 463 | |
[email protected] | d1ccc35a | 2010-03-24 05:03:24 | [diff] [blame] | 464 | #ifndef NDEBUG |
| 465 | if (severity_ == LOG_FATAL) { |
| 466 | // Include a stack trace on a fatal. |
| 467 | StackTrace trace; |
| 468 | stream_ << std::endl; // Newline to separate from log message. |
| 469 | trace.OutputToStream(&stream_); |
| 470 | } |
[email protected] | 1d8c270 | 2008-08-19 23:39:32 | [diff] [blame] | 471 | #endif |
[email protected] | d1ccc35a | 2010-03-24 05:03:24 | [diff] [blame] | 472 | stream_ << std::endl; |
| 473 | std::string str_newline(stream_.str()); |
| 474 | |
[email protected] | 2b07b841 | 2009-11-25 15:26:34 | [diff] [blame] | 475 | // Give any log message handler first dibs on the message. |
| 476 | if (log_message_handler && log_message_handler(severity_, str_newline)) |
| 477 | return; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 478 | |
| 479 | if (log_filter_prefix && severity_ <= kMaxFilteredLogLevel && |
[email protected] | 614e9fa | 2008-08-11 22:52:59 | [diff] [blame] | 480 | str_newline.compare(message_start_, log_filter_prefix->size(), |
| 481 | log_filter_prefix->data()) != 0) { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 482 | return; |
| 483 | } |
| 484 | |
| 485 | if (logging_destination == LOG_ONLY_TO_SYSTEM_DEBUG_LOG || |
[email protected] | f6abeba | 2008-08-08 13:27:28 | [diff] [blame] | 486 | logging_destination == LOG_TO_BOTH_FILE_AND_SYSTEM_DEBUG_LOG) { |
| 487 | #if defined(OS_WIN) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 488 | OutputDebugStringA(str_newline.c_str()); |
[email protected] | 1ce4105 | 2009-12-02 00:34:02 | [diff] [blame] | 489 | if (severity_ >= kAlwaysPrintErrorLevel) { |
| 490 | #else |
| 491 | { |
[email protected] | 107bc0f1 | 2008-08-26 17:48:18 | [diff] [blame] | 492 | #endif |
[email protected] | 1ce4105 | 2009-12-02 00:34:02 | [diff] [blame] | 493 | // TODO(erikkay): this interferes with the layout tests since it grabs |
| 494 | // stderr and stdout and diffs them against known data. Our info and warn |
| 495 | // logs add noise to that. Ideally, the layout tests would set the log |
| 496 | // level to ignore anything below error. When that happens, we should |
| 497 | // take this fprintf out of the #else so that Windows users can benefit |
| 498 | // from the output when running tests from the command-line. In the |
| 499 | // meantime, we leave this in for Mac and Linux, but until this is fixed |
| 500 | // they won't be able to pass any layout tests that have info or warn |
| 501 | // logs. See http://b/1343647 |
| 502 | fprintf(stderr, "%s", str_newline.c_str()); |
| 503 | fflush(stderr); |
| 504 | } |
[email protected] | a33c989 | 2008-08-25 20:10:31 | [diff] [blame] | 505 | } else if (severity_ >= kAlwaysPrintErrorLevel) { |
| 506 | // When we're only outputting to a log file, above a certain log level, we |
| 507 | // should still output to stderr so that we can better detect and diagnose |
| 508 | // problems with unit tests, especially on the buildbots. |
| 509 | fprintf(stderr, "%s", str_newline.c_str()); |
[email protected] | 1ce4105 | 2009-12-02 00:34:02 | [diff] [blame] | 510 | fflush(stderr); |
[email protected] | f6abeba | 2008-08-08 13:27:28 | [diff] [blame] | 511 | } |
[email protected] | 52a261f | 2009-03-03 15:01:12 | [diff] [blame] | 512 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 513 | // write to log file |
| 514 | if (logging_destination != LOG_NONE && |
| 515 | logging_destination != LOG_ONLY_TO_SYSTEM_DEBUG_LOG && |
| 516 | InitializeLogFileHandle()) { |
[email protected] | 52a261f | 2009-03-03 15:01:12 | [diff] [blame] | 517 | // We can have multiple threads and/or processes, so try to prevent them |
| 518 | // from clobbering each other's writes. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 519 | if (lock_log_file == LOCK_LOG_FILE) { |
| 520 | // Ensure that the mutex is initialized in case the client app did not |
[email protected] | 52a261f | 2009-03-03 15:01:12 | [diff] [blame] | 521 | // call InitLogging. This is not thread safe. See below. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 522 | InitLogMutex(); |
| 523 | |
[email protected] | f6abeba | 2008-08-08 13:27:28 | [diff] [blame] | 524 | #if defined(OS_WIN) |
[email protected] | c0e2715 | 2009-08-03 18:35:53 | [diff] [blame] | 525 | ::WaitForSingleObject(log_mutex, INFINITE); |
| 526 | // WaitForSingleObject could have returned WAIT_ABANDONED. We don't |
| 527 | // abort the process here. UI tests might be crashy sometimes, |
| 528 | // and aborting the test binary only makes the problem worse. |
| 529 | // We also don't use LOG macros because that might lead to an infinite |
| 530 | // loop. For more info see http://crbug.com/18028. |
[email protected] | f6abeba | 2008-08-08 13:27:28 | [diff] [blame] | 531 | #elif defined(OS_POSIX) |
| 532 | pthread_mutex_lock(&log_mutex); |
| 533 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 534 | } else { |
| 535 | // use the lock |
| 536 | if (!log_lock) { |
| 537 | // The client app did not call InitLogging, and so the lock has not |
| 538 | // been created. We do this on demand, but if two threads try to do |
| 539 | // this at the same time, there will be a race condition to create |
| 540 | // the lock. This is why InitLogging should be called from the main |
| 541 | // thread at the beginning of execution. |
| 542 | log_lock = new LockImpl(); |
| 543 | } |
| 544 | log_lock->Lock(); |
| 545 | } |
| 546 | |
[email protected] | f6abeba | 2008-08-08 13:27:28 | [diff] [blame] | 547 | #if defined(OS_WIN) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 548 | SetFilePointer(log_file, 0, 0, SEEK_END); |
| 549 | DWORD num_written; |
[email protected] | 52a261f | 2009-03-03 15:01:12 | [diff] [blame] | 550 | WriteFile(log_file, |
[email protected] | 5982f1f3 | 2009-03-03 15:10:58 | [diff] [blame] | 551 | static_cast<const void*>(str_newline.c_str()), |
[email protected] | 52a261f | 2009-03-03 15:01:12 | [diff] [blame] | 552 | static_cast<DWORD>(str_newline.length()), |
| 553 | &num_written, |
| 554 | NULL); |
[email protected] | f6abeba | 2008-08-08 13:27:28 | [diff] [blame] | 555 | #else |
| 556 | fprintf(log_file, "%s", str_newline.c_str()); |
[email protected] | 1ce4105 | 2009-12-02 00:34:02 | [diff] [blame] | 557 | fflush(log_file); |
[email protected] | f6abeba | 2008-08-08 13:27:28 | [diff] [blame] | 558 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 559 | |
| 560 | if (lock_log_file == LOCK_LOG_FILE) { |
[email protected] | f6abeba | 2008-08-08 13:27:28 | [diff] [blame] | 561 | #if defined(OS_WIN) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 562 | ReleaseMutex(log_mutex); |
[email protected] | f6abeba | 2008-08-08 13:27:28 | [diff] [blame] | 563 | #elif defined(OS_POSIX) |
| 564 | pthread_mutex_unlock(&log_mutex); |
| 565 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 566 | } else { |
| 567 | log_lock->Unlock(); |
| 568 | } |
| 569 | } |
| 570 | |
| 571 | if (severity_ == LOG_FATAL) { |
| 572 | // display a message or break into the debugger on a fatal error |
[email protected] | 1ffe08c1 | 2008-08-13 11:15:11 | [diff] [blame] | 573 | if (DebugUtil::BeingDebugged()) { |
| 574 | DebugUtil::BreakDebugger(); |
| 575 | } else { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 576 | if (log_assert_handler) { |
| 577 | // make a copy of the string for the handler out of paranoia |
| 578 | log_assert_handler(std::string(stream_.str())); |
| 579 | } else { |
[email protected] | 4d590127 | 2008-11-06 00:33:50 | [diff] [blame] | 580 | // Don't use the string with the newline, get a fresh version to send to |
| 581 | // the debug message process. We also don't display assertions to the |
| 582 | // user in release mode. The enduser can't do anything with this |
| 583 | // information, and displaying message boxes when the application is |
| 584 | // hosed can cause additional problems. |
| 585 | #ifndef NDEBUG |
[email protected] | d81baca4 | 2010-03-01 13:10:22 | [diff] [blame] | 586 | DisplayDebugMessageInDialog(stream_.str()); |
[email protected] | 4d590127 | 2008-11-06 00:33:50 | [diff] [blame] | 587 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 588 | // Crash the process to generate a dump. |
[email protected] | 1ffe08c1 | 2008-08-13 11:15:11 | [diff] [blame] | 589 | DebugUtil::BreakDebugger(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 590 | } |
| 591 | } |
[email protected] | fb62a53 | 2009-02-12 01:19:05 | [diff] [blame] | 592 | } else if (severity_ == LOG_ERROR_REPORT) { |
| 593 | // We are here only if the user runs with --enable-dcheck in release mode. |
| 594 | if (log_report_handler) { |
| 595 | log_report_handler(std::string(stream_.str())); |
| 596 | } else { |
[email protected] | d81baca4 | 2010-03-01 13:10:22 | [diff] [blame] | 597 | DisplayDebugMessageInDialog(stream_.str()); |
[email protected] | fb62a53 | 2009-02-12 01:19:05 | [diff] [blame] | 598 | } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 599 | } |
| 600 | } |
| 601 | |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 602 | #if defined(OS_WIN) |
| 603 | // This has already been defined in the header, but defining it again as DWORD |
| 604 | // ensures that the type used in the header is equivalent to DWORD. If not, |
| 605 | // the redefinition is a compile error. |
| 606 | typedef DWORD SystemErrorCode; |
| 607 | #endif |
| 608 | |
| 609 | SystemErrorCode GetLastSystemErrorCode() { |
| 610 | #if defined(OS_WIN) |
| 611 | return ::GetLastError(); |
| 612 | #elif defined(OS_POSIX) |
| 613 | return errno; |
| 614 | #else |
| 615 | #error Not implemented |
| 616 | #endif |
| 617 | } |
| 618 | |
| 619 | #if defined(OS_WIN) |
| 620 | Win32ErrorLogMessage::Win32ErrorLogMessage(const char* file, |
| 621 | int line, |
| 622 | LogSeverity severity, |
| 623 | SystemErrorCode err, |
| 624 | const char* module) |
| 625 | : err_(err), |
| 626 | module_(module), |
| 627 | log_message_(file, line, severity) { |
| 628 | } |
| 629 | |
| 630 | Win32ErrorLogMessage::Win32ErrorLogMessage(const char* file, |
| 631 | int line, |
| 632 | LogSeverity severity, |
| 633 | SystemErrorCode err) |
| 634 | : err_(err), |
| 635 | module_(NULL), |
| 636 | log_message_(file, line, severity) { |
| 637 | } |
| 638 | |
| 639 | Win32ErrorLogMessage::~Win32ErrorLogMessage() { |
| 640 | const int error_message_buffer_size = 256; |
| 641 | char msgbuf[error_message_buffer_size]; |
| 642 | DWORD flags = FORMAT_MESSAGE_FROM_SYSTEM; |
| 643 | HMODULE hmod; |
| 644 | if (module_) { |
| 645 | hmod = GetModuleHandleA(module_); |
| 646 | if (hmod) { |
| 647 | flags |= FORMAT_MESSAGE_FROM_HMODULE; |
| 648 | } else { |
| 649 | // This makes a nested Win32ErrorLogMessage. It will have module_ of NULL |
| 650 | // so it will not call GetModuleHandle, so recursive errors are |
| 651 | // impossible. |
| 652 | DPLOG(WARNING) << "Couldn't open module " << module_ |
| 653 | << " for error message query"; |
| 654 | } |
| 655 | } else { |
| 656 | hmod = NULL; |
| 657 | } |
| 658 | DWORD len = FormatMessageA(flags, |
| 659 | hmod, |
| 660 | err_, |
| 661 | MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), |
| 662 | msgbuf, |
| 663 | sizeof(msgbuf) / sizeof(msgbuf[0]), |
| 664 | NULL); |
| 665 | if (len) { |
| 666 | while ((len > 0) && |
| 667 | isspace(static_cast<unsigned char>(msgbuf[len - 1]))) { |
| 668 | msgbuf[--len] = 0; |
| 669 | } |
| 670 | stream() << ": " << msgbuf; |
| 671 | } else { |
| 672 | stream() << ": Error " << GetLastError() << " while retrieving error " |
| 673 | << err_; |
| 674 | } |
| 675 | } |
| 676 | #elif defined(OS_POSIX) |
| 677 | ErrnoLogMessage::ErrnoLogMessage(const char* file, |
| 678 | int line, |
| 679 | LogSeverity severity, |
| 680 | SystemErrorCode err) |
| 681 | : err_(err), |
| 682 | log_message_(file, line, severity) { |
| 683 | } |
| 684 | |
| 685 | ErrnoLogMessage::~ErrnoLogMessage() { |
| 686 | stream() << ": " << safe_strerror(err_); |
| 687 | } |
| 688 | #endif // OS_WIN |
| 689 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 690 | void CloseLogFile() { |
| 691 | if (!log_file) |
| 692 | return; |
| 693 | |
[email protected] | f6abeba | 2008-08-08 13:27:28 | [diff] [blame] | 694 | CloseFile(log_file); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 695 | log_file = NULL; |
| 696 | } |
| 697 | |
[email protected] | e36ddc8 | 2009-12-08 04:22:50 | [diff] [blame] | 698 | void RawLog(int level, const char* message) { |
| 699 | if (level >= min_log_level) { |
| 700 | size_t bytes_written = 0; |
| 701 | const size_t message_len = strlen(message); |
| 702 | int rv; |
| 703 | while (bytes_written < message_len) { |
| 704 | rv = HANDLE_EINTR( |
| 705 | write(STDERR_FILENO, message + bytes_written, |
| 706 | message_len - bytes_written)); |
| 707 | if (rv < 0) { |
| 708 | // Give up, nothing we can do now. |
| 709 | break; |
| 710 | } |
| 711 | bytes_written += rv; |
| 712 | } |
| 713 | |
| 714 | if (message_len > 0 && message[message_len - 1] != '\n') { |
| 715 | do { |
| 716 | rv = HANDLE_EINTR(write(STDERR_FILENO, "\n", 1)); |
| 717 | if (rv < 0) { |
| 718 | // Give up, nothing we can do now. |
| 719 | break; |
| 720 | } |
| 721 | } while (rv != 1); |
| 722 | } |
| 723 | } |
| 724 | |
| 725 | if (level == LOG_FATAL) |
| 726 | DebugUtil::BreakDebugger(); |
| 727 | } |
| 728 | |
[email protected] | 96fd003 | 2009-04-24 00:13:08 | [diff] [blame] | 729 | } // namespace logging |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 730 | |
| 731 | std::ostream& operator<<(std::ostream& out, const wchar_t* wstr) { |
[email protected] | 047a03f | 2009-10-07 02:10:20 | [diff] [blame] | 732 | return out << WideToUTF8(std::wstring(wstr)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 733 | } |