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