[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 | |
[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 <mach/mach.h> |
| 18 | #include <mach/mach_time.h> |
| 19 | #include <mach-o/dyld.h> |
[email protected] | e43eddf1 | 2009-12-29 00:32:52 | [diff] [blame] | 20 | #elif defined(OS_POSIX) |
[email protected] | 19ea84ca | 2010-11-12 08:37:08 | [diff] [blame] | 21 | #if defined(OS_NACL) |
[email protected] | 19ea84ca | 2010-11-12 08:37:08 | [diff] [blame] | 22 | #include <sys/time.h> // timespec doesn't seem to be in <time.h> |
| 23 | #else |
[email protected] | 052f1b5 | 2008-11-06 21:43:07 | [diff] [blame] | 24 | #include <sys/syscall.h> |
[email protected] | 19ea84ca | 2010-11-12 08:37:08 | [diff] [blame] | 25 | #endif |
[email protected] | 052f1b5 | 2008-11-06 21:43:07 | [diff] [blame] | 26 | #include <time.h> |
[email protected] | 614e9fa | 2008-08-11 22:52:59 | [diff] [blame] | 27 | #endif |
| 28 | |
| 29 | #if defined(OS_POSIX) |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 30 | #include <errno.h> |
[email protected] | 166326c6 | 2010-08-05 15:50:23 | [diff] [blame] | 31 | #include <pthread.h> |
[email protected] | f6abeba | 2008-08-08 13:27:28 | [diff] [blame] | 32 | #include <stdlib.h> |
| 33 | #include <stdio.h> |
| 34 | #include <string.h> |
| 35 | #include <unistd.h> |
| 36 | #define MAX_PATH PATH_MAX |
| 37 | typedef FILE* FileHandle; |
| 38 | typedef pthread_mutex_t* MutexHandle; |
| 39 | #endif |
| 40 | |
[email protected] | 1f88b516 | 2011-04-01 00:02:29 | [diff] [blame] | 41 | #include <algorithm> |
| 42 | #include <cstring> |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 43 | #include <ctime> |
| 44 | #include <iomanip> |
[email protected] | 1f88b516 | 2011-04-01 00:02:29 | [diff] [blame] | 45 | #include <ostream> |
[email protected] | b16ef31 | 2008-08-19 18:36:23 | [diff] [blame] | 46 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 47 | #include "base/base_switches.h" |
| 48 | #include "base/command_line.h" |
[email protected] | eb4c4d03 | 2012-04-03 18:45:05 | [diff] [blame] | 49 | #include "base/debug/alias.h" |
[email protected] | 5858035 | 2010-10-26 04:07:50 | [diff] [blame] | 50 | #include "base/debug/debugger.h" |
| 51 | #include "base/debug/stack_trace.h" |
[email protected] | e36ddc8 | 2009-12-08 04:22:50 | [diff] [blame] | 52 | #include "base/eintr_wrapper.h" |
[email protected] | 4bdaceb4 | 2008-08-19 13:19:24 | [diff] [blame] | 53 | #include "base/string_piece.h" |
[email protected] | bc581a68 | 2011-01-01 23:16:20 | [diff] [blame] | 54 | #include "base/synchronization/lock_impl.h" |
[email protected] | 63e6680 | 2012-01-18 21:21:09 | [diff] [blame] | 55 | #include "base/threading/platform_thread.h" |
[email protected] | 047a03f | 2009-10-07 02:10:20 | [diff] [blame] | 56 | #include "base/utf_string_conversions.h" |
[email protected] | 99b7c57f | 2010-09-29 19:26:36 | [diff] [blame] | 57 | #include "base/vlog.h" |
[email protected] | 53c7ce4 | 2010-12-14 16:20:04 | [diff] [blame] | 58 | #if defined(OS_POSIX) |
| 59 | #include "base/safe_strerror_posix.h" |
| 60 | #endif |
[email protected] | 52a261f | 2009-03-03 15:01:12 | [diff] [blame] | 61 | |
[email protected] | 3132e35c | 2011-07-07 20:46:50 | [diff] [blame] | 62 | #if defined(OS_ANDROID) |
| 63 | #include <android/log.h> |
| 64 | #endif |
| 65 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 66 | namespace logging { |
| 67 | |
[email protected] | 7c10f755 | 2011-01-11 01:03:36 | [diff] [blame] | 68 | DcheckState g_dcheck_state = DISABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS; |
[email protected] | 064aa16 | 2011-12-03 00:30:08 | [diff] [blame] | 69 | |
| 70 | namespace { |
| 71 | |
[email protected] | 99b7c57f | 2010-09-29 19:26:36 | [diff] [blame] | 72 | VlogInfo* g_vlog_info = NULL; |
[email protected] | 064aa16 | 2011-12-03 00:30:08 | [diff] [blame] | 73 | VlogInfo* g_vlog_info_prev = NULL; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 74 | |
| 75 | const char* const log_severity_names[LOG_NUM_SEVERITIES] = { |
[email protected] | fb62a53 | 2009-02-12 01:19:05 | [diff] [blame] | 76 | "INFO", "WARNING", "ERROR", "ERROR_REPORT", "FATAL" }; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 77 | |
| 78 | int min_log_level = 0; |
[email protected] | 1d8c270 | 2008-08-19 23:39:32 | [diff] [blame] | 79 | |
| 80 | // The default set here for logging_destination will only be used if |
| 81 | // InitLogging is not called. On Windows, use a file next to the exe; |
| 82 | // on POSIX platforms, where it may not even be possible to locate the |
| 83 | // executable on disk, use stderr. |
[email protected] | 4c0040c | 2008-08-15 01:04:11 | [diff] [blame] | 84 | #if defined(OS_WIN) |
[email protected] | fe61352 | 2008-08-22 17:09:34 | [diff] [blame] | 85 | LoggingDestination logging_destination = LOG_ONLY_TO_FILE; |
[email protected] | 4c0040c | 2008-08-15 01:04:11 | [diff] [blame] | 86 | #elif defined(OS_POSIX) |
[email protected] | 1d8c270 | 2008-08-19 23:39:32 | [diff] [blame] | 87 | LoggingDestination logging_destination = LOG_ONLY_TO_SYSTEM_DEBUG_LOG; |
[email protected] | 4c0040c | 2008-08-15 01:04:11 | [diff] [blame] | 88 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 89 | |
[email protected] | a33c989 | 2008-08-25 20:10:31 | [diff] [blame] | 90 | // For LOG_ERROR and above, always print to stderr. |
| 91 | const int kAlwaysPrintErrorLevel = LOG_ERROR; |
| 92 | |
[email protected] | 614e9fa | 2008-08-11 22:52:59 | [diff] [blame] | 93 | // Which log file to use? This is initialized by InitLogging or |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 94 | // will be lazily initialized to the default value when it is |
| 95 | // first needed. |
[email protected] | f6abeba | 2008-08-08 13:27:28 | [diff] [blame] | 96 | #if defined(OS_WIN) |
[email protected] | 614e9fa | 2008-08-11 22:52:59 | [diff] [blame] | 97 | typedef std::wstring PathString; |
[email protected] | f6abeba | 2008-08-08 13:27:28 | [diff] [blame] | 98 | #else |
[email protected] | 614e9fa | 2008-08-11 22:52:59 | [diff] [blame] | 99 | typedef std::string PathString; |
[email protected] | f6abeba | 2008-08-08 13:27:28 | [diff] [blame] | 100 | #endif |
[email protected] | 614e9fa | 2008-08-11 22:52:59 | [diff] [blame] | 101 | PathString* log_file_name = NULL; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 102 | |
| 103 | // this file is lazily opened and the handle may be NULL |
[email protected] | f6abeba | 2008-08-08 13:27:28 | [diff] [blame] | 104 | FileHandle log_file = NULL; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 105 | |
| 106 | // what should be prepended to each message? |
| 107 | bool log_process_id = false; |
| 108 | bool log_thread_id = false; |
| 109 | bool log_timestamp = true; |
| 110 | bool log_tickcount = false; |
| 111 | |
[email protected] | 81e0a85 | 2010-08-17 00:38:12 | [diff] [blame] | 112 | // Should we pop up fatal debug messages in a dialog? |
| 113 | bool show_error_dialogs = false; |
| 114 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 115 | // An assert handler override specified by the client to be called instead of |
[email protected] | fb62a53 | 2009-02-12 01:19:05 | [diff] [blame] | 116 | // the debug message dialog and process termination. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 117 | LogAssertHandlerFunction log_assert_handler = NULL; |
[email protected] | fb62a53 | 2009-02-12 01:19:05 | [diff] [blame] | 118 | // An report handler override specified by the client to be called instead of |
| 119 | // the debug message dialog. |
| 120 | LogReportHandlerFunction log_report_handler = NULL; |
[email protected] | 2b07b841 | 2009-11-25 15:26:34 | [diff] [blame] | 121 | // A log message handler that gets notified of every log message we process. |
| 122 | LogMessageHandlerFunction log_message_handler = NULL; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 123 | |
[email protected] | f6abeba | 2008-08-08 13:27:28 | [diff] [blame] | 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 | |
[email protected] | f858847 | 2008-11-05 23:17:24 | [diff] [blame] | 134 | uint64 TickCount() { |
| 135 | #if defined(OS_WIN) |
| 136 | return GetTickCount(); |
| 137 | #elif defined(OS_MACOSX) |
| 138 | return mach_absolute_time(); |
[email protected] | 19ea84ca | 2010-11-12 08:37:08 | [diff] [blame] | 139 | #elif defined(OS_NACL) |
| 140 | // NaCl sadly does not have _POSIX_TIMERS enabled in sys/features.h |
| 141 | // So we have to use clock() for now. |
| 142 | return clock(); |
[email protected] | e43eddf1 | 2009-12-29 00:32:52 | [diff] [blame] | 143 | #elif defined(OS_POSIX) |
[email protected] | 052f1b5 | 2008-11-06 21:43:07 | [diff] [blame] | 144 | struct timespec ts; |
| 145 | clock_gettime(CLOCK_MONOTONIC, &ts); |
| 146 | |
| 147 | uint64 absolute_micro = |
| 148 | static_cast<int64>(ts.tv_sec) * 1000000 + |
| 149 | static_cast<int64>(ts.tv_nsec) / 1000; |
| 150 | |
| 151 | return absolute_micro; |
[email protected] | f858847 | 2008-11-05 23:17:24 | [diff] [blame] | 152 | #endif |
| 153 | } |
| 154 | |
[email protected] | f6abeba | 2008-08-08 13:27:28 | [diff] [blame] | 155 | void CloseFile(FileHandle log) { |
| 156 | #if defined(OS_WIN) |
| 157 | CloseHandle(log); |
| 158 | #else |
| 159 | fclose(log); |
| 160 | #endif |
| 161 | } |
| 162 | |
[email protected] | 614e9fa | 2008-08-11 22:52:59 | [diff] [blame] | 163 | void DeleteFilePath(const PathString& log_name) { |
[email protected] | f6abeba | 2008-08-08 13:27:28 | [diff] [blame] | 164 | #if defined(OS_WIN) |
[email protected] | 614e9fa | 2008-08-11 22:52:59 | [diff] [blame] | 165 | DeleteFile(log_name.c_str()); |
[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]; |
| 175 | GetModuleFileName(NULL, module_name, MAX_PATH); |
[email protected] | 5f95d53 | 2010-10-01 17:16:58 | [diff] [blame] | 176 | |
| 177 | PathString log_file = module_name; |
| 178 | PathString::size_type last_backslash = |
| 179 | log_file.rfind('\\', log_file.size()); |
| 180 | if (last_backslash != PathString::npos) |
| 181 | log_file.erase(last_backslash + 1); |
| 182 | log_file += L"debug.log"; |
| 183 | return log_file; |
[email protected] | 5b84fe3 | 2010-09-14 22:24:55 | [diff] [blame] | 184 | #elif defined(OS_POSIX) |
| 185 | // On other platforms we just use the current directory. |
[email protected] | 5f95d53 | 2010-10-01 17:16:58 | [diff] [blame] | 186 | return PathString("debug.log"); |
[email protected] | 5b84fe3 | 2010-09-14 22:24:55 | [diff] [blame] | 187 | #endif |
| 188 | } |
| 189 | |
| 190 | // This class acts as a wrapper for locking the logging files. |
| 191 | // LoggingLock::Init() should be called from the main thread before any logging |
| 192 | // is done. Then whenever logging, be sure to have a local LoggingLock |
| 193 | // instance on the stack. This will ensure that the lock is unlocked upon |
| 194 | // exiting the frame. |
| 195 | // LoggingLocks can not be nested. |
| 196 | class LoggingLock { |
| 197 | public: |
| 198 | LoggingLock() { |
| 199 | LockLogging(); |
| 200 | } |
| 201 | |
| 202 | ~LoggingLock() { |
| 203 | UnlockLogging(); |
| 204 | } |
| 205 | |
| 206 | static void Init(LogLockingState lock_log, const PathChar* new_log_file) { |
| 207 | if (initialized) |
| 208 | return; |
| 209 | lock_log_file = lock_log; |
| 210 | if (lock_log_file == LOCK_LOG_FILE) { |
| 211 | #if defined(OS_WIN) |
| 212 | if (!log_mutex) { |
| 213 | std::wstring safe_name; |
| 214 | if (new_log_file) |
| 215 | safe_name = new_log_file; |
| 216 | else |
[email protected] | 5f95d53 | 2010-10-01 17:16:58 | [diff] [blame] | 217 | safe_name = GetDefaultLogFile(); |
[email protected] | 5b84fe3 | 2010-09-14 22:24:55 | [diff] [blame] | 218 | // \ is not a legal character in mutex names so we replace \ with / |
| 219 | std::replace(safe_name.begin(), safe_name.end(), '\\', '/'); |
| 220 | std::wstring t(L"Global\\"); |
| 221 | t.append(safe_name); |
| 222 | log_mutex = ::CreateMutex(NULL, FALSE, t.c_str()); |
[email protected] | 5f95d53 | 2010-10-01 17:16:58 | [diff] [blame] | 223 | |
| 224 | if (log_mutex == NULL) { |
| 225 | #if DEBUG |
| 226 | // Keep the error code for debugging |
| 227 | int error = GetLastError(); // NOLINT |
[email protected] | 5858035 | 2010-10-26 04:07:50 | [diff] [blame] | 228 | base::debug::BreakDebugger(); |
[email protected] | 5f95d53 | 2010-10-01 17:16:58 | [diff] [blame] | 229 | #endif |
| 230 | // Return nicely without putting initialized to true. |
| 231 | return; |
| 232 | } |
[email protected] | 5b84fe3 | 2010-09-14 22:24:55 | [diff] [blame] | 233 | } |
| 234 | #endif |
| 235 | } else { |
[email protected] | bc581a68 | 2011-01-01 23:16:20 | [diff] [blame] | 236 | log_lock = new base::internal::LockImpl(); |
[email protected] | 5b84fe3 | 2010-09-14 22:24:55 | [diff] [blame] | 237 | } |
| 238 | initialized = true; |
| 239 | } |
| 240 | |
| 241 | private: |
| 242 | static void LockLogging() { |
| 243 | if (lock_log_file == LOCK_LOG_FILE) { |
| 244 | #if defined(OS_WIN) |
| 245 | ::WaitForSingleObject(log_mutex, INFINITE); |
| 246 | // WaitForSingleObject could have returned WAIT_ABANDONED. We don't |
| 247 | // abort the process here. UI tests might be crashy sometimes, |
| 248 | // and aborting the test binary only makes the problem worse. |
| 249 | // We also don't use LOG macros because that might lead to an infinite |
| 250 | // loop. For more info see http://crbug.com/18028. |
| 251 | #elif defined(OS_POSIX) |
| 252 | pthread_mutex_lock(&log_mutex); |
| 253 | #endif |
| 254 | } else { |
| 255 | // use the lock |
| 256 | log_lock->Lock(); |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | static void UnlockLogging() { |
| 261 | if (lock_log_file == LOCK_LOG_FILE) { |
| 262 | #if defined(OS_WIN) |
| 263 | ReleaseMutex(log_mutex); |
| 264 | #elif defined(OS_POSIX) |
| 265 | pthread_mutex_unlock(&log_mutex); |
| 266 | #endif |
| 267 | } else { |
| 268 | log_lock->Unlock(); |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | // The lock is used if log file locking is false. It helps us avoid problems |
| 273 | // with multiple threads writing to the log file at the same time. Use |
| 274 | // LockImpl directly instead of using Lock, because Lock makes logging calls. |
[email protected] | bc581a68 | 2011-01-01 23:16:20 | [diff] [blame] | 275 | static base::internal::LockImpl* log_lock; |
[email protected] | 5b84fe3 | 2010-09-14 22:24:55 | [diff] [blame] | 276 | |
| 277 | // When we don't use a lock, we are using a global mutex. We need to do this |
| 278 | // because LockFileEx is not thread safe. |
| 279 | #if defined(OS_WIN) |
| 280 | static MutexHandle log_mutex; |
| 281 | #elif defined(OS_POSIX) |
| 282 | static pthread_mutex_t log_mutex; |
| 283 | #endif |
| 284 | |
| 285 | static bool initialized; |
| 286 | static LogLockingState lock_log_file; |
| 287 | }; |
| 288 | |
| 289 | // static |
| 290 | bool LoggingLock::initialized = false; |
| 291 | // static |
[email protected] | bc581a68 | 2011-01-01 23:16:20 | [diff] [blame] | 292 | base::internal::LockImpl* LoggingLock::log_lock = NULL; |
[email protected] | 5b84fe3 | 2010-09-14 22:24:55 | [diff] [blame] | 293 | // static |
| 294 | LogLockingState LoggingLock::lock_log_file = LOCK_LOG_FILE; |
| 295 | |
| 296 | #if defined(OS_WIN) |
| 297 | // static |
| 298 | MutexHandle LoggingLock::log_mutex = NULL; |
| 299 | #elif defined(OS_POSIX) |
| 300 | pthread_mutex_t LoggingLock::log_mutex = PTHREAD_MUTEX_INITIALIZER; |
| 301 | #endif |
| 302 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 303 | // Called by logging functions to ensure that debug_file is initialized |
| 304 | // and can be used for writing. Returns false if the file could not be |
| 305 | // initialized. debug_file will be NULL in this case. |
| 306 | bool InitializeLogFileHandle() { |
| 307 | if (log_file) |
| 308 | return true; |
| 309 | |
[email protected] | 614e9fa | 2008-08-11 22:52:59 | [diff] [blame] | 310 | if (!log_file_name) { |
| 311 | // Nobody has called InitLogging to specify a debug log file, so here we |
| 312 | // initialize the log file name to a default. |
[email protected] | 5f95d53 | 2010-10-01 17:16:58 | [diff] [blame] | 313 | log_file_name = new PathString(GetDefaultLogFile()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 314 | } |
| 315 | |
[email protected] | 1d8c270 | 2008-08-19 23:39:32 | [diff] [blame] | 316 | if (logging_destination == LOG_ONLY_TO_FILE || |
| 317 | logging_destination == LOG_TO_BOTH_FILE_AND_SYSTEM_DEBUG_LOG) { |
[email protected] | 614e9fa | 2008-08-11 22:52:59 | [diff] [blame] | 318 | #if defined(OS_WIN) |
[email protected] | 1d8c270 | 2008-08-19 23:39:32 | [diff] [blame] | 319 | log_file = CreateFile(log_file_name->c_str(), GENERIC_WRITE, |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 320 | FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, |
| 321 | OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); |
| 322 | if (log_file == INVALID_HANDLE_VALUE || log_file == NULL) { |
[email protected] | 1d8c270 | 2008-08-19 23:39:32 | [diff] [blame] | 323 | // try the current directory |
| 324 | log_file = CreateFile(L".\\debug.log", GENERIC_WRITE, |
| 325 | FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, |
| 326 | OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); |
| 327 | if (log_file == INVALID_HANDLE_VALUE || log_file == NULL) { |
| 328 | log_file = NULL; |
| 329 | return false; |
| 330 | } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 331 | } |
[email protected] | 1d8c270 | 2008-08-19 23:39:32 | [diff] [blame] | 332 | SetFilePointer(log_file, 0, 0, FILE_END); |
[email protected] | 78c6dd6 | 2009-06-08 23:29:11 | [diff] [blame] | 333 | #elif defined(OS_POSIX) |
| 334 | log_file = fopen(log_file_name->c_str(), "a"); |
| 335 | if (log_file == NULL) |
| 336 | return false; |
[email protected] | f6abeba | 2008-08-08 13:27:28 | [diff] [blame] | 337 | #endif |
[email protected] | 1d8c270 | 2008-08-19 23:39:32 | [diff] [blame] | 338 | } |
| 339 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 340 | return true; |
| 341 | } |
| 342 | |
[email protected] | 064aa16 | 2011-12-03 00:30:08 | [diff] [blame] | 343 | } // namespace |
| 344 | |
| 345 | |
[email protected] | c7d5da99 | 2010-10-28 00:20:21 | [diff] [blame] | 346 | bool BaseInitLoggingImpl(const PathChar* new_log_file, |
[email protected] | ff3d0c3 | 2010-08-23 19:57:46 | [diff] [blame] | 347 | LoggingDestination logging_dest, |
| 348 | LogLockingState lock_log, |
[email protected] | 7c10f755 | 2011-01-11 01:03:36 | [diff] [blame] | 349 | OldFileDeletionState delete_old, |
| 350 | DcheckState dcheck_state) { |
[email protected] | 7c10f755 | 2011-01-11 01:03:36 | [diff] [blame] | 351 | g_dcheck_state = dcheck_state; |
[email protected] | 380d832 | 2012-02-28 21:35:44 | [diff] [blame] | 352 | // TODO(bbudge) Hook this up to NaCl logging. |
| 353 | #if !defined(OS_NACL) |
| 354 | CommandLine* command_line = CommandLine::ForCurrentProcess(); |
[email protected] | 99b7c57f | 2010-09-29 19:26:36 | [diff] [blame] | 355 | // Don't bother initializing g_vlog_info unless we use one of the |
| 356 | // vlog switches. |
| 357 | if (command_line->HasSwitch(switches::kV) || |
| 358 | command_line->HasSwitch(switches::kVModule)) { |
[email protected] | 064aa16 | 2011-12-03 00:30:08 | [diff] [blame] | 359 | // NOTE: If g_vlog_info has already been initialized, it might be in use |
| 360 | // by another thread. Don't delete the old VLogInfo, just create a second |
| 361 | // one. We keep track of both to avoid memory leak warnings. |
| 362 | CHECK(!g_vlog_info_prev); |
| 363 | g_vlog_info_prev = g_vlog_info; |
| 364 | |
[email protected] | 99b7c57f | 2010-09-29 19:26:36 | [diff] [blame] | 365 | g_vlog_info = |
| 366 | new VlogInfo(command_line->GetSwitchValueASCII(switches::kV), |
[email protected] | 162ac0f | 2010-11-04 15:50:49 | [diff] [blame] | 367 | command_line->GetSwitchValueASCII(switches::kVModule), |
| 368 | &min_log_level); |
[email protected] | 99b7c57f | 2010-09-29 19:26:36 | [diff] [blame] | 369 | } |
| 370 | |
[email protected] | 5b84fe3 | 2010-09-14 22:24:55 | [diff] [blame] | 371 | LoggingLock::Init(lock_log, new_log_file); |
| 372 | |
| 373 | LoggingLock logging_lock; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 374 | |
| 375 | if (log_file) { |
| 376 | // calling InitLogging twice or after some log call has already opened the |
| 377 | // default log file will re-initialize to the new options |
[email protected] | f6abeba | 2008-08-08 13:27:28 | [diff] [blame] | 378 | CloseFile(log_file); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 379 | log_file = NULL; |
| 380 | } |
| 381 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 382 | logging_destination = logging_dest; |
| 383 | |
| 384 | // ignore file options if logging is disabled or only to system |
| 385 | if (logging_destination == LOG_NONE || |
| 386 | logging_destination == LOG_ONLY_TO_SYSTEM_DEBUG_LOG) |
[email protected] | c7d5da99 | 2010-10-28 00:20:21 | [diff] [blame] | 387 | return true; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 388 | |
[email protected] | 614e9fa | 2008-08-11 22:52:59 | [diff] [blame] | 389 | if (!log_file_name) |
| 390 | log_file_name = new PathString(); |
| 391 | *log_file_name = new_log_file; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 392 | if (delete_old == DELETE_OLD_LOG_FILE) |
[email protected] | 614e9fa | 2008-08-11 22:52:59 | [diff] [blame] | 393 | DeleteFilePath(*log_file_name); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 394 | |
[email protected] | c7d5da99 | 2010-10-28 00:20:21 | [diff] [blame] | 395 | return InitializeLogFileHandle(); |
[email protected] | 380d832 | 2012-02-28 21:35:44 | [diff] [blame] | 396 | #else |
| 397 | return true; |
| 398 | #endif // !defined(OS_NACL) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | void SetMinLogLevel(int level) { |
[email protected] | deba0ff | 2010-11-03 05:30:14 | [diff] [blame] | 402 | min_log_level = std::min(LOG_ERROR_REPORT, level); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | int GetMinLogLevel() { |
| 406 | return min_log_level; |
| 407 | } |
| 408 | |
[email protected] | 162ac0f | 2010-11-04 15:50:49 | [diff] [blame] | 409 | int GetVlogVerbosity() { |
| 410 | return std::max(-1, LOG_INFO - GetMinLogLevel()); |
| 411 | } |
| 412 | |
[email protected] | 99b7c57f | 2010-09-29 19:26:36 | [diff] [blame] | 413 | int GetVlogLevelHelper(const char* file, size_t N) { |
| 414 | DCHECK_GT(N, 0U); |
[email protected] | 064aa16 | 2011-12-03 00:30:08 | [diff] [blame] | 415 | // Note: g_vlog_info may change on a different thread during startup |
| 416 | // (but will always be valid or NULL). |
| 417 | VlogInfo* vlog_info = g_vlog_info; |
| 418 | return vlog_info ? |
| 419 | vlog_info->GetVlogLevel(base::StringPiece(file, N - 1)) : |
[email protected] | 162ac0f | 2010-11-04 15:50:49 | [diff] [blame] | 420 | GetVlogVerbosity(); |
[email protected] | 99b7c57f | 2010-09-29 19:26:36 | [diff] [blame] | 421 | } |
| 422 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 423 | void SetLogItems(bool enable_process_id, bool enable_thread_id, |
| 424 | bool enable_timestamp, bool enable_tickcount) { |
| 425 | log_process_id = enable_process_id; |
| 426 | log_thread_id = enable_thread_id; |
| 427 | log_timestamp = enable_timestamp; |
| 428 | log_tickcount = enable_tickcount; |
| 429 | } |
| 430 | |
[email protected] | 81e0a85 | 2010-08-17 00:38:12 | [diff] [blame] | 431 | void SetShowErrorDialogs(bool enable_dialogs) { |
| 432 | show_error_dialogs = enable_dialogs; |
| 433 | } |
| 434 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 435 | void SetLogAssertHandler(LogAssertHandlerFunction handler) { |
| 436 | log_assert_handler = handler; |
| 437 | } |
| 438 | |
[email protected] | fb62a53 | 2009-02-12 01:19:05 | [diff] [blame] | 439 | void SetLogReportHandler(LogReportHandlerFunction handler) { |
| 440 | log_report_handler = handler; |
| 441 | } |
| 442 | |
[email protected] | 2b07b841 | 2009-11-25 15:26:34 | [diff] [blame] | 443 | void SetLogMessageHandler(LogMessageHandlerFunction handler) { |
| 444 | log_message_handler = handler; |
| 445 | } |
| 446 | |
[email protected] | 64e5cc0 | 2010-11-03 19:20:27 | [diff] [blame] | 447 | LogMessageHandlerFunction GetLogMessageHandler() { |
| 448 | return log_message_handler; |
| 449 | } |
| 450 | |
[email protected] | 6d445d3 | 2010-09-30 19:10:03 | [diff] [blame] | 451 | // MSVC doesn't like complex extern templates and DLLs. |
| 452 | #if !defined(COMPILER_MSVC) |
| 453 | // Explicit instantiations for commonly used comparisons. |
| 454 | template std::string* MakeCheckOpString<int, int>( |
| 455 | const int&, const int&, const char* names); |
| 456 | template std::string* MakeCheckOpString<unsigned long, unsigned long>( |
| 457 | const unsigned long&, const unsigned long&, const char* names); |
| 458 | template std::string* MakeCheckOpString<unsigned long, unsigned int>( |
| 459 | const unsigned long&, const unsigned int&, const char* names); |
| 460 | template std::string* MakeCheckOpString<unsigned int, unsigned long>( |
| 461 | const unsigned int&, const unsigned long&, const char* names); |
| 462 | template std::string* MakeCheckOpString<std::string, std::string>( |
| 463 | const std::string&, const std::string&, const char* name); |
| 464 | #endif |
[email protected] | 2b07b841 | 2009-11-25 15:26:34 | [diff] [blame] | 465 | |
[email protected] | d81baca4 | 2010-03-01 13:10:22 | [diff] [blame] | 466 | // Displays a message box to the user with the error message in it. |
| 467 | // Used for fatal messages, where we close the app simultaneously. |
[email protected] | 561513f | 2010-12-16 23:29:25 | [diff] [blame] | 468 | // This is for developers only; we don't use this in circumstances |
| 469 | // (like release builds) where users could see it, since users don't |
| 470 | // understand these messages anyway. |
[email protected] | d81baca4 | 2010-03-01 13:10:22 | [diff] [blame] | 471 | void DisplayDebugMessageInDialog(const std::string& str) { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 472 | if (str.empty()) |
| 473 | return; |
| 474 | |
[email protected] | 81e0a85 | 2010-08-17 00:38:12 | [diff] [blame] | 475 | if (!show_error_dialogs) |
[email protected] | 846ed9c3 | 2010-07-29 20:33:44 | [diff] [blame] | 476 | return; |
| 477 | |
[email protected] | f6abeba | 2008-08-08 13:27:28 | [diff] [blame] | 478 | #if defined(OS_WIN) |
[email protected] | d81baca4 | 2010-03-01 13:10:22 | [diff] [blame] | 479 | // For Windows programs, it's possible that the message loop is |
| 480 | // messed up on a fatal error, and creating a MessageBox will cause |
| 481 | // that message loop to be run. Instead, we try to spawn another |
| 482 | // process that displays its command line. We look for "Debug |
| 483 | // Message.exe" in the same directory as the application. If it |
| 484 | // exists, we use it, otherwise, we use a regular message box. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 485 | wchar_t prog_name[MAX_PATH]; |
| 486 | GetModuleFileNameW(NULL, prog_name, MAX_PATH); |
| 487 | wchar_t* backslash = wcsrchr(prog_name, '\\'); |
| 488 | if (backslash) |
| 489 | backslash[1] = 0; |
| 490 | wcscat_s(prog_name, MAX_PATH, L"debug_message.exe"); |
| 491 | |
[email protected] | 047a03f | 2009-10-07 02:10:20 | [diff] [blame] | 492 | std::wstring cmdline = UTF8ToWide(str); |
[email protected] | 3ca4214c1 | 2009-03-25 22:12:02 | [diff] [blame] | 493 | if (cmdline.empty()) |
| 494 | return; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 495 | |
| 496 | STARTUPINFO startup_info; |
| 497 | memset(&startup_info, 0, sizeof(startup_info)); |
| 498 | startup_info.cb = sizeof(startup_info); |
| 499 | |
| 500 | PROCESS_INFORMATION process_info; |
[email protected] | 3ca4214c1 | 2009-03-25 22:12:02 | [diff] [blame] | 501 | if (CreateProcessW(prog_name, &cmdline[0], NULL, NULL, false, 0, NULL, |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 502 | NULL, &startup_info, &process_info)) { |
| 503 | WaitForSingleObject(process_info.hProcess, INFINITE); |
| 504 | CloseHandle(process_info.hThread); |
| 505 | CloseHandle(process_info.hProcess); |
| 506 | } else { |
| 507 | // debug process broken, let's just do a message box |
[email protected] | 3ca4214c1 | 2009-03-25 22:12:02 | [diff] [blame] | 508 | MessageBoxW(NULL, &cmdline[0], L"Fatal error", |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 509 | MB_OK | MB_ICONHAND | MB_TOPMOST); |
| 510 | } |
[email protected] | f6abeba | 2008-08-08 13:27:28 | [diff] [blame] | 511 | #else |
[email protected] | 561513f | 2010-12-16 23:29:25 | [diff] [blame] | 512 | // We intentionally don't implement a dialog on other platforms. |
| 513 | // You can just look at stderr. |
[email protected] | f6abeba | 2008-08-08 13:27:28 | [diff] [blame] | 514 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 515 | } |
| 516 | |
[email protected] | 3f85caa | 2009-04-14 16:52:11 | [diff] [blame] | 517 | #if defined(OS_WIN) |
| 518 | LogMessage::SaveLastError::SaveLastError() : last_error_(::GetLastError()) { |
| 519 | } |
| 520 | |
| 521 | LogMessage::SaveLastError::~SaveLastError() { |
| 522 | ::SetLastError(last_error_); |
| 523 | } |
| 524 | #endif // defined(OS_WIN) |
| 525 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 526 | LogMessage::LogMessage(const char* file, int line, LogSeverity severity, |
| 527 | int ctr) |
[email protected] | 162ac0f | 2010-11-04 15:50:49 | [diff] [blame] | 528 | : severity_(severity), file_(file), line_(line) { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 529 | Init(file, line); |
| 530 | } |
| 531 | |
[email protected] | eae9c06 | 2011-01-11 00:50:59 | [diff] [blame] | 532 | LogMessage::LogMessage(const char* file, int line) |
| 533 | : severity_(LOG_INFO), file_(file), line_(line) { |
| 534 | Init(file, line); |
| 535 | } |
| 536 | |
| 537 | LogMessage::LogMessage(const char* file, int line, LogSeverity severity) |
| 538 | : severity_(severity), file_(file), line_(line) { |
| 539 | Init(file, line); |
| 540 | } |
| 541 | |
[email protected] | 9c7132e | 2011-02-08 07:39:08 | [diff] [blame] | 542 | LogMessage::LogMessage(const char* file, int line, std::string* result) |
[email protected] | 162ac0f | 2010-11-04 15:50:49 | [diff] [blame] | 543 | : severity_(LOG_FATAL), file_(file), line_(line) { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 544 | Init(file, line); |
[email protected] | 9c7132e | 2011-02-08 07:39:08 | [diff] [blame] | 545 | stream_ << "Check failed: " << *result; |
| 546 | delete result; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 547 | } |
| 548 | |
[email protected] | fb62a53 | 2009-02-12 01:19:05 | [diff] [blame] | 549 | LogMessage::LogMessage(const char* file, int line, LogSeverity severity, |
[email protected] | 9c7132e | 2011-02-08 07:39:08 | [diff] [blame] | 550 | std::string* result) |
[email protected] | 162ac0f | 2010-11-04 15:50:49 | [diff] [blame] | 551 | : severity_(severity), file_(file), line_(line) { |
[email protected] | fb62a53 | 2009-02-12 01:19:05 | [diff] [blame] | 552 | Init(file, line); |
[email protected] | 9c7132e | 2011-02-08 07:39:08 | [diff] [blame] | 553 | stream_ << "Check failed: " << *result; |
| 554 | delete result; |
[email protected] | fb62a53 | 2009-02-12 01:19:05 | [diff] [blame] | 555 | } |
| 556 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 557 | LogMessage::~LogMessage() { |
[email protected] | 3132e35c | 2011-07-07 20:46:50 | [diff] [blame] | 558 | // TODO(port): enable stacktrace generation on LOG_FATAL once backtrace are |
| 559 | // working in Android. |
[email protected] | 380d832 | 2012-02-28 21:35:44 | [diff] [blame] | 560 | #if !defined(NDEBUG) && !defined(OS_ANDROID) && !defined(OS_NACL) |
[email protected] | d1ccc35a | 2010-03-24 05:03:24 | [diff] [blame] | 561 | if (severity_ == LOG_FATAL) { |
| 562 | // Include a stack trace on a fatal. |
[email protected] | 5858035 | 2010-10-26 04:07:50 | [diff] [blame] | 563 | base::debug::StackTrace trace; |
[email protected] | d1ccc35a | 2010-03-24 05:03:24 | [diff] [blame] | 564 | stream_ << std::endl; // Newline to separate from log message. |
| 565 | trace.OutputToStream(&stream_); |
| 566 | } |
[email protected] | 1d8c270 | 2008-08-19 23:39:32 | [diff] [blame] | 567 | #endif |
[email protected] | d1ccc35a | 2010-03-24 05:03:24 | [diff] [blame] | 568 | stream_ << std::endl; |
| 569 | std::string str_newline(stream_.str()); |
| 570 | |
[email protected] | 2b07b841 | 2009-11-25 15:26:34 | [diff] [blame] | 571 | // Give any log message handler first dibs on the message. |
[email protected] | 162ac0f | 2010-11-04 15:50:49 | [diff] [blame] | 572 | if (log_message_handler && log_message_handler(severity_, file_, line_, |
| 573 | message_start_, str_newline)) { |
| 574 | // The handler took care of it, no further processing. |
[email protected] | 2b07b841 | 2009-11-25 15:26:34 | [diff] [blame] | 575 | return; |
[email protected] | 162ac0f | 2010-11-04 15:50:49 | [diff] [blame] | 576 | } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 577 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 578 | if (logging_destination == LOG_ONLY_TO_SYSTEM_DEBUG_LOG || |
[email protected] | f6abeba | 2008-08-08 13:27:28 | [diff] [blame] | 579 | logging_destination == LOG_TO_BOTH_FILE_AND_SYSTEM_DEBUG_LOG) { |
| 580 | #if defined(OS_WIN) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 581 | OutputDebugStringA(str_newline.c_str()); |
[email protected] | 3132e35c | 2011-07-07 20:46:50 | [diff] [blame] | 582 | #elif defined(OS_ANDROID) |
| 583 | android_LogPriority priority = ANDROID_LOG_UNKNOWN; |
| 584 | switch (severity_) { |
| 585 | case LOG_INFO: |
| 586 | priority = ANDROID_LOG_INFO; |
| 587 | break; |
| 588 | case LOG_WARNING: |
| 589 | priority = ANDROID_LOG_WARN; |
| 590 | break; |
| 591 | case LOG_ERROR: |
| 592 | case LOG_ERROR_REPORT: |
| 593 | priority = ANDROID_LOG_ERROR; |
| 594 | break; |
| 595 | case LOG_FATAL: |
| 596 | priority = ANDROID_LOG_FATAL; |
| 597 | break; |
| 598 | } |
| 599 | __android_log_write(priority, "chromium", str_newline.c_str()); |
[email protected] | 107bc0f1 | 2008-08-26 17:48:18 | [diff] [blame] | 600 | #endif |
[email protected] | 469006c | 2010-09-24 15:43:06 | [diff] [blame] | 601 | fprintf(stderr, "%s", str_newline.c_str()); |
| 602 | fflush(stderr); |
[email protected] | a33c989 | 2008-08-25 20:10:31 | [diff] [blame] | 603 | } else if (severity_ >= kAlwaysPrintErrorLevel) { |
| 604 | // When we're only outputting to a log file, above a certain log level, we |
| 605 | // should still output to stderr so that we can better detect and diagnose |
| 606 | // problems with unit tests, especially on the buildbots. |
| 607 | fprintf(stderr, "%s", str_newline.c_str()); |
[email protected] | 1ce4105 | 2009-12-02 00:34:02 | [diff] [blame] | 608 | fflush(stderr); |
[email protected] | f6abeba | 2008-08-08 13:27:28 | [diff] [blame] | 609 | } |
[email protected] | 52a261f | 2009-03-03 15:01:12 | [diff] [blame] | 610 | |
[email protected] | 5b84fe3 | 2010-09-14 22:24:55 | [diff] [blame] | 611 | // We can have multiple threads and/or processes, so try to prevent them |
| 612 | // from clobbering each other's writes. |
| 613 | // If the client app did not call InitLogging, and the lock has not |
| 614 | // been created do it now. We do this on demand, but if two threads try |
| 615 | // to do this at the same time, there will be a race condition to create |
| 616 | // the lock. This is why InitLogging should be called from the main |
| 617 | // thread at the beginning of execution. |
| 618 | LoggingLock::Init(LOCK_LOG_FILE, NULL); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 619 | // write to log file |
| 620 | if (logging_destination != LOG_NONE && |
[email protected] | 5b84fe3 | 2010-09-14 22:24:55 | [diff] [blame] | 621 | logging_destination != LOG_ONLY_TO_SYSTEM_DEBUG_LOG) { |
| 622 | LoggingLock logging_lock; |
| 623 | if (InitializeLogFileHandle()) { |
[email protected] | f6abeba | 2008-08-08 13:27:28 | [diff] [blame] | 624 | #if defined(OS_WIN) |
[email protected] | 5b84fe3 | 2010-09-14 22:24:55 | [diff] [blame] | 625 | SetFilePointer(log_file, 0, 0, SEEK_END); |
| 626 | DWORD num_written; |
| 627 | WriteFile(log_file, |
| 628 | static_cast<const void*>(str_newline.c_str()), |
| 629 | static_cast<DWORD>(str_newline.length()), |
| 630 | &num_written, |
| 631 | NULL); |
[email protected] | cba2196 | 2010-08-31 22:35:55 | [diff] [blame] | 632 | #else |
[email protected] | 5b84fe3 | 2010-09-14 22:24:55 | [diff] [blame] | 633 | fprintf(log_file, "%s", str_newline.c_str()); |
| 634 | fflush(log_file); |
[email protected] | cba2196 | 2010-08-31 22:35:55 | [diff] [blame] | 635 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 636 | } |
| 637 | } |
| 638 | |
| 639 | if (severity_ == LOG_FATAL) { |
[email protected] | eb4c4d03 | 2012-04-03 18:45:05 | [diff] [blame] | 640 | // Ensure the first characters of the string are on the stack so they |
| 641 | // are contained in minidumps for diagnostic purposes. |
| 642 | char str_stack[1024]; |
| 643 | str_newline.copy(str_stack, arraysize(str_stack)); |
| 644 | base::debug::Alias(str_stack); |
| 645 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 646 | // display a message or break into the debugger on a fatal error |
[email protected] | 5858035 | 2010-10-26 04:07:50 | [diff] [blame] | 647 | if (base::debug::BeingDebugged()) { |
| 648 | base::debug::BreakDebugger(); |
[email protected] | 1ffe08c1 | 2008-08-13 11:15:11 | [diff] [blame] | 649 | } else { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 650 | if (log_assert_handler) { |
| 651 | // make a copy of the string for the handler out of paranoia |
| 652 | log_assert_handler(std::string(stream_.str())); |
| 653 | } else { |
[email protected] | 4d590127 | 2008-11-06 00:33:50 | [diff] [blame] | 654 | // Don't use the string with the newline, get a fresh version to send to |
| 655 | // the debug message process. We also don't display assertions to the |
| 656 | // user in release mode. The enduser can't do anything with this |
| 657 | // information, and displaying message boxes when the application is |
| 658 | // hosed can cause additional problems. |
| 659 | #ifndef NDEBUG |
[email protected] | d81baca4 | 2010-03-01 13:10:22 | [diff] [blame] | 660 | DisplayDebugMessageInDialog(stream_.str()); |
[email protected] | 4d590127 | 2008-11-06 00:33:50 | [diff] [blame] | 661 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 662 | // Crash the process to generate a dump. |
[email protected] | 5858035 | 2010-10-26 04:07:50 | [diff] [blame] | 663 | base::debug::BreakDebugger(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 664 | } |
| 665 | } |
[email protected] | fb62a53 | 2009-02-12 01:19:05 | [diff] [blame] | 666 | } else if (severity_ == LOG_ERROR_REPORT) { |
| 667 | // We are here only if the user runs with --enable-dcheck in release mode. |
| 668 | if (log_report_handler) { |
| 669 | log_report_handler(std::string(stream_.str())); |
| 670 | } else { |
[email protected] | d81baca4 | 2010-03-01 13:10:22 | [diff] [blame] | 671 | DisplayDebugMessageInDialog(stream_.str()); |
[email protected] | fb62a53 | 2009-02-12 01:19:05 | [diff] [blame] | 672 | } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 673 | } |
| 674 | } |
| 675 | |
[email protected] | eae9c06 | 2011-01-11 00:50:59 | [diff] [blame] | 676 | // writes the common header info to the stream |
| 677 | void LogMessage::Init(const char* file, int line) { |
| 678 | base::StringPiece filename(file); |
| 679 | size_t last_slash_pos = filename.find_last_of("\\/"); |
| 680 | if (last_slash_pos != base::StringPiece::npos) |
| 681 | filename.remove_prefix(last_slash_pos + 1); |
| 682 | |
| 683 | // TODO(darin): It might be nice if the columns were fixed width. |
| 684 | |
| 685 | stream_ << '['; |
| 686 | if (log_process_id) |
| 687 | stream_ << CurrentProcessId() << ':'; |
| 688 | if (log_thread_id) |
[email protected] | 63e6680 | 2012-01-18 21:21:09 | [diff] [blame] | 689 | stream_ << base::PlatformThread::CurrentId() << ':'; |
[email protected] | eae9c06 | 2011-01-11 00:50:59 | [diff] [blame] | 690 | if (log_timestamp) { |
| 691 | time_t t = time(NULL); |
| 692 | struct tm local_time = {0}; |
| 693 | #if _MSC_VER >= 1400 |
| 694 | localtime_s(&local_time, &t); |
| 695 | #else |
| 696 | localtime_r(&t, &local_time); |
| 697 | #endif |
| 698 | struct tm* tm_time = &local_time; |
| 699 | stream_ << std::setfill('0') |
| 700 | << std::setw(2) << 1 + tm_time->tm_mon |
| 701 | << std::setw(2) << tm_time->tm_mday |
| 702 | << '/' |
| 703 | << std::setw(2) << tm_time->tm_hour |
| 704 | << std::setw(2) << tm_time->tm_min |
| 705 | << std::setw(2) << tm_time->tm_sec |
| 706 | << ':'; |
| 707 | } |
| 708 | if (log_tickcount) |
| 709 | stream_ << TickCount() << ':'; |
| 710 | if (severity_ >= 0) |
| 711 | stream_ << log_severity_names[severity_]; |
| 712 | else |
| 713 | stream_ << "VERBOSE" << -severity_; |
| 714 | |
| 715 | stream_ << ":" << filename << "(" << line << ")] "; |
| 716 | |
| 717 | message_start_ = stream_.tellp(); |
| 718 | } |
| 719 | |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 720 | #if defined(OS_WIN) |
| 721 | // This has already been defined in the header, but defining it again as DWORD |
| 722 | // ensures that the type used in the header is equivalent to DWORD. If not, |
| 723 | // the redefinition is a compile error. |
| 724 | typedef DWORD SystemErrorCode; |
| 725 | #endif |
| 726 | |
| 727 | SystemErrorCode GetLastSystemErrorCode() { |
| 728 | #if defined(OS_WIN) |
| 729 | return ::GetLastError(); |
| 730 | #elif defined(OS_POSIX) |
| 731 | return errno; |
| 732 | #else |
| 733 | #error Not implemented |
| 734 | #endif |
| 735 | } |
| 736 | |
| 737 | #if defined(OS_WIN) |
| 738 | Win32ErrorLogMessage::Win32ErrorLogMessage(const char* file, |
| 739 | int line, |
| 740 | LogSeverity severity, |
| 741 | SystemErrorCode err, |
| 742 | const char* module) |
| 743 | : err_(err), |
| 744 | module_(module), |
| 745 | log_message_(file, line, severity) { |
| 746 | } |
| 747 | |
| 748 | Win32ErrorLogMessage::Win32ErrorLogMessage(const char* file, |
| 749 | int line, |
| 750 | LogSeverity severity, |
| 751 | SystemErrorCode err) |
| 752 | : err_(err), |
| 753 | module_(NULL), |
| 754 | log_message_(file, line, severity) { |
| 755 | } |
| 756 | |
| 757 | Win32ErrorLogMessage::~Win32ErrorLogMessage() { |
| 758 | const int error_message_buffer_size = 256; |
| 759 | char msgbuf[error_message_buffer_size]; |
[email protected] | 6ac3df5 | 2010-10-20 01:33:32 | [diff] [blame] | 760 | DWORD flags = FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS; |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 761 | HMODULE hmod; |
| 762 | if (module_) { |
| 763 | hmod = GetModuleHandleA(module_); |
| 764 | if (hmod) { |
| 765 | flags |= FORMAT_MESSAGE_FROM_HMODULE; |
| 766 | } else { |
| 767 | // This makes a nested Win32ErrorLogMessage. It will have module_ of NULL |
| 768 | // so it will not call GetModuleHandle, so recursive errors are |
| 769 | // impossible. |
| 770 | DPLOG(WARNING) << "Couldn't open module " << module_ |
| 771 | << " for error message query"; |
| 772 | } |
| 773 | } else { |
| 774 | hmod = NULL; |
| 775 | } |
| 776 | DWORD len = FormatMessageA(flags, |
| 777 | hmod, |
| 778 | err_, |
[email protected] | 6ac3df5 | 2010-10-20 01:33:32 | [diff] [blame] | 779 | 0, |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 780 | msgbuf, |
| 781 | sizeof(msgbuf) / sizeof(msgbuf[0]), |
| 782 | NULL); |
| 783 | if (len) { |
| 784 | while ((len > 0) && |
| 785 | isspace(static_cast<unsigned char>(msgbuf[len - 1]))) { |
| 786 | msgbuf[--len] = 0; |
| 787 | } |
| 788 | stream() << ": " << msgbuf; |
| 789 | } else { |
| 790 | stream() << ": Error " << GetLastError() << " while retrieving error " |
| 791 | << err_; |
| 792 | } |
[email protected] | 20909e7 | 2012-04-05 16:57:06 | [diff] [blame^] | 793 | // We're about to crash (CHECK). Put |err_| on the stack (by placing it in a |
| 794 | // field) and use Alias in hopes that it makes it into crash dumps. |
| 795 | DWORD last_error = err_; |
| 796 | base::debug::Alias(&last_error); |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 797 | } |
| 798 | #elif defined(OS_POSIX) |
| 799 | ErrnoLogMessage::ErrnoLogMessage(const char* file, |
| 800 | int line, |
| 801 | LogSeverity severity, |
| 802 | SystemErrorCode err) |
| 803 | : err_(err), |
| 804 | log_message_(file, line, severity) { |
| 805 | } |
| 806 | |
| 807 | ErrnoLogMessage::~ErrnoLogMessage() { |
| 808 | stream() << ": " << safe_strerror(err_); |
| 809 | } |
| 810 | #endif // OS_WIN |
| 811 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 812 | void CloseLogFile() { |
[email protected] | 5b84fe3 | 2010-09-14 22:24:55 | [diff] [blame] | 813 | LoggingLock logging_lock; |
| 814 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 815 | if (!log_file) |
| 816 | return; |
| 817 | |
[email protected] | f6abeba | 2008-08-08 13:27:28 | [diff] [blame] | 818 | CloseFile(log_file); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 819 | log_file = NULL; |
| 820 | } |
| 821 | |
[email protected] | e36ddc8 | 2009-12-08 04:22:50 | [diff] [blame] | 822 | void RawLog(int level, const char* message) { |
| 823 | if (level >= min_log_level) { |
| 824 | size_t bytes_written = 0; |
| 825 | const size_t message_len = strlen(message); |
| 826 | int rv; |
| 827 | while (bytes_written < message_len) { |
| 828 | rv = HANDLE_EINTR( |
| 829 | write(STDERR_FILENO, message + bytes_written, |
| 830 | message_len - bytes_written)); |
| 831 | if (rv < 0) { |
| 832 | // Give up, nothing we can do now. |
| 833 | break; |
| 834 | } |
| 835 | bytes_written += rv; |
| 836 | } |
| 837 | |
| 838 | if (message_len > 0 && message[message_len - 1] != '\n') { |
| 839 | do { |
| 840 | rv = HANDLE_EINTR(write(STDERR_FILENO, "\n", 1)); |
| 841 | if (rv < 0) { |
| 842 | // Give up, nothing we can do now. |
| 843 | break; |
| 844 | } |
| 845 | } while (rv != 1); |
| 846 | } |
| 847 | } |
| 848 | |
| 849 | if (level == LOG_FATAL) |
[email protected] | 5858035 | 2010-10-26 04:07:50 | [diff] [blame] | 850 | base::debug::BreakDebugger(); |
[email protected] | e36ddc8 | 2009-12-08 04:22:50 | [diff] [blame] | 851 | } |
| 852 | |
[email protected] | 34a90773 | 2012-01-20 06:33:27 | [diff] [blame] | 853 | // This was defined at the beginning of this file. |
| 854 | #undef write |
| 855 | |
[email protected] | 96fd003 | 2009-04-24 00:13:08 | [diff] [blame] | 856 | } // namespace logging |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 857 | |
| 858 | std::ostream& operator<<(std::ostream& out, const wchar_t* wstr) { |
[email protected] | 047a03f | 2009-10-07 02:10:20 | [diff] [blame] | 859 | return out << WideToUTF8(std::wstring(wstr)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 860 | } |