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