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