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