[email protected] | 0f65b52d | 2012-02-09 21:02:47 | [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 | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
[email protected] | ec65780 | 2008-11-07 20:05:39 | [diff] [blame] | 5 | #include "build/build_config.h" |
| 6 | |
[email protected] | d55aaa13 | 2009-09-28 21:08:04 | [diff] [blame] | 7 | // Need to include this before most other files because it defines |
| 8 | // IPC_MESSAGE_LOG_ENABLED. We need to use it to define |
| 9 | // IPC_MESSAGE_MACROS_LOG_ENABLED so render_messages.h will generate the |
| 10 | // ViewMsgLog et al. functions. |
| 11 | #include "ipc/ipc_message.h" |
| 12 | |
| 13 | // On Windows, the about:ipc dialog shows IPCs; on POSIX, we hook up a |
| 14 | // logger in this file. (We implement about:ipc on Mac but implement |
| 15 | // the loggers here anyway). We need to do this real early to be sure |
| 16 | // IPC_MESSAGE_MACROS_LOG_ENABLED doesn't get undefined. |
| 17 | #if defined(OS_POSIX) && defined(IPC_MESSAGE_LOG_ENABLED) |
| 18 | #define IPC_MESSAGE_MACROS_LOG_ENABLED |
[email protected] | 4a6d7415 | 2011-09-26 20:42:30 | [diff] [blame] | 19 | #include "chrome/common/all_messages.h" |
[email protected] | d55aaa13 | 2009-09-28 21:08:04 | [diff] [blame] | 20 | #endif |
| 21 | |
[email protected] | ec65780 | 2008-11-07 20:05:39 | [diff] [blame] | 22 | #if defined(OS_WIN) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 23 | #include <windows.h> |
[email protected] | ec65780 | 2008-11-07 20:05:39 | [diff] [blame] | 24 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 25 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 26 | #include <fstream> |
| 27 | |
| 28 | #include "chrome/common/logging_chrome.h" |
| 29 | |
| 30 | #include "base/command_line.h" |
[email protected] | 0eb9f434 | 2008-11-21 18:48:52 | [diff] [blame] | 31 | #include "base/compiler_specific.h" |
[email protected] | 5858035 | 2010-10-26 04:07:50 | [diff] [blame] | 32 | #include "base/debug/debugger.h" |
[email protected] | 76b90d31 | 2010-08-03 03:00:50 | [diff] [blame] | 33 | #include "base/environment.h" |
[email protected] | 73e2348 | 2009-08-13 00:26:58 | [diff] [blame] | 34 | #include "base/file_path.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 35 | #include "base/file_util.h" |
| 36 | #include "base/logging.h" |
| 37 | #include "base/path_service.h" |
[email protected] | 528c56d | 2010-07-30 19:28:44 | [diff] [blame] | 38 | #include "base/string_number_conversions.h" |
[email protected] | 5d91c9e | 2010-07-28 17:25:28 | [diff] [blame] | 39 | #include "base/string_util.h" |
[email protected] | 92e43a04 | 2011-08-17 00:36:40 | [diff] [blame] | 40 | #include "base/stringprintf.h" |
[email protected] | 61f9321 | 2011-01-24 14:37:08 | [diff] [blame] | 41 | #include "base/threading/thread_restrictions.h" |
[email protected] | 77ee1b1 | 2010-06-14 16:21:30 | [diff] [blame] | 42 | #include "base/time.h" |
[email protected] | 4197e09 | 2010-03-10 00:35:05 | [diff] [blame] | 43 | #include "base/utf_string_conversions.h" |
[email protected] | ab7ee5b | 2011-03-01 22:24:14 | [diff] [blame] | 44 | #include "chrome/common/chrome_constants.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 45 | #include "chrome/common/chrome_paths.h" |
| 46 | #include "chrome/common/chrome_switches.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 47 | #include "chrome/common/env_vars.h" |
[email protected] | 51d5e16 | 2009-07-27 19:23:54 | [diff] [blame] | 48 | #include "ipc/ipc_logging.h" |
[email protected] | ab7ee5b | 2011-03-01 22:24:14 | [diff] [blame] | 49 | |
[email protected] | 2b07b841 | 2009-11-25 15:26:34 | [diff] [blame] | 50 | #if defined(OS_WIN) |
| 51 | #include "base/logging_win.h" |
| 52 | #include <initguid.h> |
| 53 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 54 | |
[email protected] | ab7ee5b | 2011-03-01 22:24:14 | [diff] [blame] | 55 | namespace { |
| 56 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 57 | // When true, this means that error dialogs should not be shown. |
[email protected] | ab7ee5b | 2011-03-01 22:24:14 | [diff] [blame] | 58 | bool dialogs_are_suppressed_ = false; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 59 | |
| 60 | // This should be true for exactly the period between the end of |
| 61 | // InitChromeLogging() and the beginning of CleanupChromeLogging(). |
[email protected] | ab7ee5b | 2011-03-01 22:24:14 | [diff] [blame] | 62 | bool chrome_logging_initialized_ = false; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 63 | |
[email protected] | 4a6d7415 | 2011-09-26 20:42:30 | [diff] [blame] | 64 | // Set if we called InitChromeLogging() but failed to initialize. |
[email protected] | ab7ee5b | 2011-03-01 22:24:14 | [diff] [blame] | 65 | bool chrome_logging_failed_ = false; |
[email protected] | 7109dc3 | 2011-02-02 22:58:29 | [diff] [blame] | 66 | |
[email protected] | 30f547c | 2010-08-04 01:00:01 | [diff] [blame] | 67 | // This should be true for exactly the period between the end of |
| 68 | // InitChromeLogging() and the beginning of CleanupChromeLogging(). |
[email protected] | ab7ee5b | 2011-03-01 22:24:14 | [diff] [blame] | 69 | bool chrome_logging_redirected_ = false; |
[email protected] | 30f547c | 2010-08-04 01:00:01 | [diff] [blame] | 70 | |
[email protected] | 2b07b841 | 2009-11-25 15:26:34 | [diff] [blame] | 71 | #if defined(OS_WIN) |
| 72 | // {7FE69228-633E-4f06-80C1-527FEA23E3A7} |
[email protected] | ab7ee5b | 2011-03-01 22:24:14 | [diff] [blame] | 73 | const GUID kChromeTraceProviderName = { |
[email protected] | 162ac0f | 2010-11-04 15:50:49 | [diff] [blame] | 74 | 0x7fe69228, 0x633e, 0x4f06, |
| 75 | { 0x80, 0xc1, 0x52, 0x7f, 0xea, 0x23, 0xe3, 0xa7 } }; |
[email protected] | 2b07b841 | 2009-11-25 15:26:34 | [diff] [blame] | 76 | #endif |
| 77 | |
[email protected] | 0f65b52d | 2012-02-09 21:02:47 | [diff] [blame^] | 78 | #if defined(USE_LINUX_BREAKPAD) |
| 79 | // Pointer to the function that's called by DumpWithoutCrashing() to dump the |
| 80 | // process's memory. |
| 81 | void (*dump_without_crashing_function_)() = NULL; |
| 82 | #endif |
| 83 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 84 | // Assertion handler for logging errors that occur when dialogs are |
| 85 | // silenced. To record a new error, pass the log string associated |
| 86 | // with that error in the str parameter. |
[email protected] | 0eb9f434 | 2008-11-21 18:48:52 | [diff] [blame] | 87 | MSVC_DISABLE_OPTIMIZE(); |
[email protected] | ab7ee5b | 2011-03-01 22:24:14 | [diff] [blame] | 88 | void SilentRuntimeAssertHandler(const std::string& str) { |
[email protected] | 5858035 | 2010-10-26 04:07:50 | [diff] [blame] | 89 | base::debug::BreakDebugger(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 90 | } |
[email protected] | ab7ee5b | 2011-03-01 22:24:14 | [diff] [blame] | 91 | void SilentRuntimeReportHandler(const std::string& str) { |
[email protected] | 6cc67d7f | 2009-02-12 06:48:24 | [diff] [blame] | 92 | } |
[email protected] | ab7ee5b | 2011-03-01 22:24:14 | [diff] [blame] | 93 | #if defined(OS_WIN) |
| 94 | // Handler to silently dump the current process when there is an assert in |
| 95 | // chrome. |
| 96 | void DumpProcessAssertHandler(const std::string& str) { |
| 97 | // Get the breakpad pointer from chrome.exe |
| 98 | typedef void (__cdecl *DumpProcessFunction)(); |
| 99 | DumpProcessFunction DumpProcess = reinterpret_cast<DumpProcessFunction>( |
| 100 | ::GetProcAddress(::GetModuleHandle(chrome::kBrowserProcessExecutableName), |
| 101 | "DumpProcess")); |
| 102 | if (DumpProcess) |
| 103 | DumpProcess(); |
| 104 | } |
| 105 | #endif // OS_WIN |
[email protected] | 0eb9f434 | 2008-11-21 18:48:52 | [diff] [blame] | 106 | MSVC_ENABLE_OPTIMIZE(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 107 | |
| 108 | // Suppresses error/assertion dialogs and enables the logging of |
| 109 | // those errors into silenced_errors_. |
[email protected] | ab7ee5b | 2011-03-01 22:24:14 | [diff] [blame] | 110 | void SuppressDialogs() { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 111 | if (dialogs_are_suppressed_) |
| 112 | return; |
| 113 | |
| 114 | logging::SetLogAssertHandler(SilentRuntimeAssertHandler); |
[email protected] | 6cc67d7f | 2009-02-12 06:48:24 | [diff] [blame] | 115 | logging::SetLogReportHandler(SilentRuntimeReportHandler); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 116 | |
[email protected] | ec65780 | 2008-11-07 20:05:39 | [diff] [blame] | 117 | #if defined(OS_WIN) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 118 | UINT new_flags = SEM_FAILCRITICALERRORS | |
| 119 | SEM_NOGPFAULTERRORBOX | |
| 120 | SEM_NOOPENFILEERRORBOX; |
| 121 | |
| 122 | // Preserve existing error mode, as discussed at http://t/dmea |
| 123 | UINT existing_flags = SetErrorMode(new_flags); |
| 124 | SetErrorMode(existing_flags | new_flags); |
[email protected] | ec65780 | 2008-11-07 20:05:39 | [diff] [blame] | 125 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 126 | |
| 127 | dialogs_are_suppressed_ = true; |
| 128 | } |
| 129 | |
[email protected] | ab7ee5b | 2011-03-01 22:24:14 | [diff] [blame] | 130 | } // anonymous namespace |
| 131 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 132 | namespace logging { |
| 133 | |
[email protected] | 77ee1b1 | 2010-06-14 16:21:30 | [diff] [blame] | 134 | LoggingDestination DetermineLogMode(const CommandLine& command_line) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 135 | // only use OutputDebugString in debug mode |
| 136 | #ifdef NDEBUG |
| 137 | bool enable_logging = false; |
[email protected] | b7e0a2a | 2009-10-13 02:07:25 | [diff] [blame] | 138 | const char *kInvertLoggingSwitch = switches::kEnableLogging; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 139 | const logging::LoggingDestination kDefaultLoggingMode = |
| 140 | logging::LOG_ONLY_TO_FILE; |
| 141 | #else |
| 142 | bool enable_logging = true; |
[email protected] | b7e0a2a | 2009-10-13 02:07:25 | [diff] [blame] | 143 | const char *kInvertLoggingSwitch = switches::kDisableLogging; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 144 | const logging::LoggingDestination kDefaultLoggingMode = |
| 145 | logging::LOG_TO_BOTH_FILE_AND_SYSTEM_DEBUG_LOG; |
| 146 | #endif |
| 147 | |
| 148 | if (command_line.HasSwitch(kInvertLoggingSwitch)) |
| 149 | enable_logging = !enable_logging; |
| 150 | |
| 151 | logging::LoggingDestination log_mode; |
| 152 | if (enable_logging) { |
[email protected] | 3bba958 | 2009-08-05 01:29:12 | [diff] [blame] | 153 | // Let --enable-logging=stderr force only stderr, particularly useful for |
| 154 | // non-debug builds where otherwise you can't get logs to stderr at all. |
[email protected] | c4e52f0d | 2009-11-06 19:55:16 | [diff] [blame] | 155 | if (command_line.GetSwitchValueASCII(switches::kEnableLogging) == "stderr") |
[email protected] | 3bba958 | 2009-08-05 01:29:12 | [diff] [blame] | 156 | log_mode = logging::LOG_ONLY_TO_SYSTEM_DEBUG_LOG; |
| 157 | else |
| 158 | log_mode = kDefaultLoggingMode; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 159 | } else { |
| 160 | log_mode = logging::LOG_NONE; |
| 161 | } |
[email protected] | 77ee1b1 | 2010-06-14 16:21:30 | [diff] [blame] | 162 | return log_mode; |
| 163 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 164 | |
[email protected] | 77ee1b1 | 2010-06-14 16:21:30 | [diff] [blame] | 165 | #if defined(OS_CHROMEOS) |
[email protected] | c7d5da99 | 2010-10-28 00:20:21 | [diff] [blame] | 166 | namespace { |
| 167 | FilePath GenerateTimestampedName(const FilePath& base_path, |
| 168 | base::Time timestamp) { |
[email protected] | 77ee1b1 | 2010-06-14 16:21:30 | [diff] [blame] | 169 | base::Time::Exploded time_deets; |
| 170 | timestamp.LocalExplode(&time_deets); |
[email protected] | 93f1052 | 2010-10-31 16:27:48 | [diff] [blame] | 171 | std::string suffix = base::StringPrintf("_%02d%02d%02d-%02d%02d%02d", |
| 172 | time_deets.year, |
| 173 | time_deets.month, |
| 174 | time_deets.day_of_month, |
| 175 | time_deets.hour, |
| 176 | time_deets.minute, |
| 177 | time_deets.second); |
[email protected] | c7d5da99 | 2010-10-28 00:20:21 | [diff] [blame] | 178 | return base_path.InsertBeforeExtension(suffix); |
[email protected] | 77ee1b1 | 2010-06-14 16:21:30 | [diff] [blame] | 179 | } |
| 180 | |
[email protected] | c7d5da99 | 2010-10-28 00:20:21 | [diff] [blame] | 181 | FilePath SetUpSymlinkIfNeeded(const FilePath& symlink_path, bool new_log) { |
[email protected] | f25b33a | 2010-10-28 20:15:51 | [diff] [blame] | 182 | DCHECK(!symlink_path.empty()); |
| 183 | |
[email protected] | c7d5da99 | 2010-10-28 00:20:21 | [diff] [blame] | 184 | // If not starting a new log, then just log through the existing |
| 185 | // symlink, but if the symlink doesn't exist, create it. If |
| 186 | // starting a new log, then delete the old symlink and make a new |
| 187 | // one to a fresh log file. |
| 188 | FilePath target_path; |
[email protected] | 0750b144 | 2010-11-02 21:59:37 | [diff] [blame] | 189 | bool symlink_exists = file_util::PathExists(symlink_path); |
| 190 | if (new_log || !symlink_exists) { |
[email protected] | c7d5da99 | 2010-10-28 00:20:21 | [diff] [blame] | 191 | target_path = GenerateTimestampedName(symlink_path, base::Time::Now()); |
| 192 | |
| 193 | // We don't care if the unlink fails; we're going to continue anyway. |
[email protected] | 723571a | 2010-12-03 17:37:54 | [diff] [blame] | 194 | if (::unlink(symlink_path.value().c_str()) == -1) { |
[email protected] | 0750b144 | 2010-11-02 21:59:37 | [diff] [blame] | 195 | if (symlink_exists) // only warn if we might expect it to succeed. |
[email protected] | 26f025e | 2011-10-28 22:49:27 | [diff] [blame] | 196 | DPLOG(WARNING) << "Unable to unlink " << symlink_path.value(); |
[email protected] | c7d5da99 | 2010-10-28 00:20:21 | [diff] [blame] | 197 | } |
[email protected] | 723571a | 2010-12-03 17:37:54 | [diff] [blame] | 198 | if (!file_util::CreateSymbolicLink(target_path, symlink_path)) { |
[email protected] | 26f025e | 2011-10-28 22:49:27 | [diff] [blame] | 199 | DPLOG(ERROR) << "Unable to create symlink " << symlink_path.value() |
| 200 | << " pointing at " << target_path.value(); |
[email protected] | c7d5da99 | 2010-10-28 00:20:21 | [diff] [blame] | 201 | } |
| 202 | } else { |
[email protected] | 723571a | 2010-12-03 17:37:54 | [diff] [blame] | 203 | if (!file_util::ReadSymbolicLink(symlink_path, &target_path)) |
[email protected] | 26f025e | 2011-10-28 22:49:27 | [diff] [blame] | 204 | DPLOG(ERROR) << "Unable to read symlink " << symlink_path.value(); |
[email protected] | c7d5da99 | 2010-10-28 00:20:21 | [diff] [blame] | 205 | } |
| 206 | return target_path; |
| 207 | } |
| 208 | |
| 209 | void RemoveSymlinkAndLog(const FilePath& link_path, |
| 210 | const FilePath& target_path) { |
| 211 | if (::unlink(link_path.value().c_str()) == -1) |
[email protected] | 26f025e | 2011-10-28 22:49:27 | [diff] [blame] | 212 | DPLOG(WARNING) << "Unable to unlink symlink " << link_path.value(); |
[email protected] | c7d5da99 | 2010-10-28 00:20:21 | [diff] [blame] | 213 | if (::unlink(target_path.value().c_str()) == -1) |
[email protected] | 26f025e | 2011-10-28 22:49:27 | [diff] [blame] | 214 | DPLOG(WARNING) << "Unable to unlink log file " << target_path.value(); |
[email protected] | c7d5da99 | 2010-10-28 00:20:21 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | } // anonymous namespace |
| 218 | |
[email protected] | 0750b144 | 2010-11-02 21:59:37 | [diff] [blame] | 219 | FilePath GetSessionLogFile(const CommandLine& command_line) { |
| 220 | FilePath log_dir; |
| 221 | std::string log_dir_str; |
| 222 | scoped_ptr<base::Environment> env(base::Environment::Create()); |
| 223 | if (env->GetVar(env_vars::kSessionLogDir, &log_dir_str) && |
| 224 | !log_dir_str.empty()) { |
| 225 | log_dir = FilePath(log_dir_str); |
| 226 | } else { |
| 227 | PathService::Get(chrome::DIR_USER_DATA, &log_dir); |
| 228 | FilePath login_profile = |
| 229 | command_line.GetSwitchValuePath(switches::kLoginProfile); |
| 230 | log_dir = log_dir.Append(login_profile); |
| 231 | } |
| 232 | return log_dir.Append(GetLogFileName().BaseName()); |
| 233 | } |
| 234 | |
| 235 | void RedirectChromeLogging(const CommandLine& command_line) { |
[email protected] | 30f547c | 2010-08-04 01:00:01 | [diff] [blame] | 236 | DCHECK(!chrome_logging_redirected_) << |
| 237 | "Attempted to redirect logging when it was already initialized."; |
[email protected] | 0750b144 | 2010-11-02 21:59:37 | [diff] [blame] | 238 | |
| 239 | // Redirect logs to the session log directory, if set. Otherwise |
| 240 | // defaults to the profile dir. |
| 241 | FilePath log_path = GetSessionLogFile(command_line); |
[email protected] | c7d5da99 | 2010-10-28 00:20:21 | [diff] [blame] | 242 | |
[email protected] | 61f9321 | 2011-01-24 14:37:08 | [diff] [blame] | 243 | // Creating symlink causes us to do blocking IO on UI thread. |
| 244 | // Temporarily allow it until we fix http://crbug.com/61143 |
| 245 | base::ThreadRestrictions::ScopedAllowIO allow_io; |
[email protected] | c7d5da99 | 2010-10-28 00:20:21 | [diff] [blame] | 246 | // Always force a new symlink when redirecting. |
| 247 | FilePath target_path = SetUpSymlinkIfNeeded(log_path, true); |
| 248 | |
[email protected] | 7c10f755 | 2011-01-11 01:03:36 | [diff] [blame] | 249 | logging::DcheckState dcheck_state = |
| 250 | command_line.HasSwitch(switches::kEnableDCHECK) ? |
| 251 | logging::ENABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS : |
| 252 | logging::DISABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS; |
| 253 | |
[email protected] | c7d5da99 | 2010-10-28 00:20:21 | [diff] [blame] | 254 | // ChromeOS always logs through the symlink, so it shouldn't be |
| 255 | // deleted if it already exists. |
| 256 | if (!InitLogging(log_path.value().c_str(), |
| 257 | DetermineLogMode(command_line), |
| 258 | logging::LOCK_LOG_FILE, |
[email protected] | 7c10f755 | 2011-01-11 01:03:36 | [diff] [blame] | 259 | logging::APPEND_TO_OLD_LOG_FILE, |
| 260 | dcheck_state)) { |
[email protected] | 26f025e | 2011-10-28 22:49:27 | [diff] [blame] | 261 | DLOG(ERROR) << "Unable to initialize logging to " << log_path.value(); |
[email protected] | c7d5da99 | 2010-10-28 00:20:21 | [diff] [blame] | 262 | RemoveSymlinkAndLog(log_path, target_path); |
| 263 | } else { |
| 264 | chrome_logging_redirected_ = true; |
| 265 | } |
[email protected] | 77ee1b1 | 2010-06-14 16:21:30 | [diff] [blame] | 266 | } |
[email protected] | 0750b144 | 2010-11-02 21:59:37 | [diff] [blame] | 267 | |
[email protected] | 8527119 | 2011-05-10 20:38:25 | [diff] [blame] | 268 | #endif // OS_CHROMEOS |
[email protected] | 77ee1b1 | 2010-06-14 16:21:30 | [diff] [blame] | 269 | |
| 270 | void InitChromeLogging(const CommandLine& command_line, |
| 271 | OldFileDeletionState delete_old_log_file) { |
| 272 | DCHECK(!chrome_logging_initialized_) << |
| 273 | "Attempted to initialize logging when it was already initialized."; |
| 274 | |
| 275 | #if defined(OS_POSIX) && defined(IPC_MESSAGE_LOG_ENABLED) |
[email protected] | 21fa3a1 | 2010-12-08 23:34:16 | [diff] [blame] | 276 | IPC::Logging::set_log_function_map(&g_log_function_mapping); |
[email protected] | 77ee1b1 | 2010-06-14 16:21:30 | [diff] [blame] | 277 | #endif |
[email protected] | 4a10099 | 2011-06-14 18:22:46 | [diff] [blame] | 278 | LoggingDestination logging_dest = DetermineLogMode(command_line); |
| 279 | FilePath log_path; |
| 280 | #if defined(OS_CHROMEOS) |
| 281 | FilePath target_path; |
| 282 | #endif |
[email protected] | 77ee1b1 | 2010-06-14 16:21:30 | [diff] [blame] | 283 | |
[email protected] | 4a10099 | 2011-06-14 18:22:46 | [diff] [blame] | 284 | // Don't resolve the log path unless we need to. Otherwise we leave an open |
| 285 | // ALPC handle after sandbox lockdown on Windows. |
| 286 | if (logging_dest == LOG_ONLY_TO_FILE || |
| 287 | logging_dest == LOG_TO_BOTH_FILE_AND_SYSTEM_DEBUG_LOG) { |
| 288 | log_path = GetLogFileName(); |
[email protected] | c7d5da99 | 2010-10-28 00:20:21 | [diff] [blame] | 289 | |
[email protected] | 77ee1b1 | 2010-06-14 16:21:30 | [diff] [blame] | 290 | #if defined(OS_CHROMEOS) |
[email protected] | 4a10099 | 2011-06-14 18:22:46 | [diff] [blame] | 291 | // For BWSI (Incognito) logins, we want to put the logs in the user |
| 292 | // profile directory that is created for the temporary session instead |
| 293 | // of in the system log directory, for privacy reasons. |
| 294 | if (command_line.HasSwitch(switches::kGuestSession)) |
| 295 | log_path = GetSessionLogFile(command_line); |
[email protected] | 0750b144 | 2010-11-02 21:59:37 | [diff] [blame] | 296 | |
[email protected] | 4a10099 | 2011-06-14 18:22:46 | [diff] [blame] | 297 | // On ChromeOS we log to the symlink. We force creation of a new |
| 298 | // symlink if we've been asked to delete the old log, since that |
| 299 | // indicates the start of a new session. |
| 300 | target_path = SetUpSymlinkIfNeeded( |
| 301 | log_path, delete_old_log_file == logging::DELETE_OLD_LOG_FILE); |
[email protected] | c7d5da99 | 2010-10-28 00:20:21 | [diff] [blame] | 302 | |
[email protected] | 4a10099 | 2011-06-14 18:22:46 | [diff] [blame] | 303 | // Because ChromeOS manages the move to a new session by redirecting |
| 304 | // the link, it shouldn't remove the old file in the logging code, |
| 305 | // since that will remove the newly created link instead. |
| 306 | delete_old_log_file = logging::APPEND_TO_OLD_LOG_FILE; |
[email protected] | 77ee1b1 | 2010-06-14 16:21:30 | [diff] [blame] | 307 | #endif |
[email protected] | 4a10099 | 2011-06-14 18:22:46 | [diff] [blame] | 308 | } |
[email protected] | 77ee1b1 | 2010-06-14 16:21:30 | [diff] [blame] | 309 | |
[email protected] | 7c10f755 | 2011-01-11 01:03:36 | [diff] [blame] | 310 | logging::DcheckState dcheck_state = |
| 311 | command_line.HasSwitch(switches::kEnableDCHECK) ? |
| 312 | logging::ENABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS : |
| 313 | logging::DISABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS; |
| 314 | |
[email protected] | c7d5da99 | 2010-10-28 00:20:21 | [diff] [blame] | 315 | bool success = InitLogging(log_path.value().c_str(), |
[email protected] | 4a10099 | 2011-06-14 18:22:46 | [diff] [blame] | 316 | logging_dest, |
[email protected] | c7d5da99 | 2010-10-28 00:20:21 | [diff] [blame] | 317 | logging::LOCK_LOG_FILE, |
[email protected] | 7c10f755 | 2011-01-11 01:03:36 | [diff] [blame] | 318 | delete_old_log_file, |
| 319 | dcheck_state); |
[email protected] | c7d5da99 | 2010-10-28 00:20:21 | [diff] [blame] | 320 | |
| 321 | #if defined(OS_CHROMEOS) |
| 322 | if (!success) { |
[email protected] | 26f025e | 2011-10-28 22:49:27 | [diff] [blame] | 323 | DPLOG(ERROR) << "Unable to initialize logging to " << log_path.value() |
[email protected] | c7d5da99 | 2010-10-28 00:20:21 | [diff] [blame] | 324 | << " (which should be a link to " << target_path.value() << ")"; |
| 325 | RemoveSymlinkAndLog(log_path, target_path); |
[email protected] | 7109dc3 | 2011-02-02 22:58:29 | [diff] [blame] | 326 | chrome_logging_failed_ = true; |
[email protected] | c7d5da99 | 2010-10-28 00:20:21 | [diff] [blame] | 327 | return; |
| 328 | } |
| 329 | #else |
| 330 | if (!success) { |
[email protected] | 26f025e | 2011-10-28 22:49:27 | [diff] [blame] | 331 | DPLOG(ERROR) << "Unable to initialize logging to " << log_path.value(); |
[email protected] | 7109dc3 | 2011-02-02 22:58:29 | [diff] [blame] | 332 | chrome_logging_failed_ = true; |
[email protected] | c7d5da99 | 2010-10-28 00:20:21 | [diff] [blame] | 333 | return; |
| 334 | } |
| 335 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 336 | |
[email protected] | 81e0a85 | 2010-08-17 00:38:12 | [diff] [blame] | 337 | // Default to showing error dialogs. |
| 338 | if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kNoErrorDialogs)) |
| 339 | logging::SetShowErrorDialogs(true); |
| 340 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 341 | // we want process and thread IDs because we have a lot of things running |
[email protected] | c7d5da99 | 2010-10-28 00:20:21 | [diff] [blame] | 342 | logging::SetLogItems(true, // enable_process_id |
| 343 | true, // enable_thread_id |
| 344 | false, // enable_timestamp |
| 345 | true); // enable_tickcount |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 346 | |
| 347 | // We call running in unattended mode "headless", and allow |
| 348 | // headless mode to be configured either by the Environment |
| 349 | // Variable or by the Command Line Switch. This is for |
| 350 | // automated test purposes. |
[email protected] | 76b90d31 | 2010-08-03 03:00:50 | [diff] [blame] | 351 | scoped_ptr<base::Environment> env(base::Environment::Create()); |
[email protected] | 9432ade | 2010-08-04 23:43:20 | [diff] [blame] | 352 | if (env->HasVar(env_vars::kHeadless) || |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 353 | command_line.HasSwitch(switches::kNoErrorDialogs)) |
| 354 | SuppressDialogs(); |
| 355 | |
[email protected] | 3deeb6b3 | 2010-10-05 21:53:52 | [diff] [blame] | 356 | // Use a minimum log level if the command line asks for one, |
| 357 | // otherwise leave it at the default level (INFO). |
| 358 | if (command_line.HasSwitch(switches::kLoggingLevel)) { |
| 359 | std::string log_level = command_line.GetSwitchValueASCII( |
| 360 | switches::kLoggingLevel); |
| 361 | int level = 0; |
| 362 | if (base::StringToInt(log_level, &level) && |
| 363 | level >= 0 && level < LOG_NUM_SEVERITIES) { |
[email protected] | bb5185c5 | 2008-08-29 19:51:06 | [diff] [blame] | 364 | logging::SetMinLogLevel(level); |
[email protected] | 3deeb6b3 | 2010-10-05 21:53:52 | [diff] [blame] | 365 | } else { |
[email protected] | 26f025e | 2011-10-28 22:49:27 | [diff] [blame] | 366 | DLOG(WARNING) << "Bad log level: " << log_level; |
[email protected] | 3deeb6b3 | 2010-10-05 21:53:52 | [diff] [blame] | 367 | } |
[email protected] | bb5185c5 | 2008-08-29 19:51:06 | [diff] [blame] | 368 | } |
| 369 | |
[email protected] | 2b07b841 | 2009-11-25 15:26:34 | [diff] [blame] | 370 | #if defined(OS_WIN) |
| 371 | // Enable trace control and transport through event tracing for Windows. |
[email protected] | 9432ade | 2010-08-04 23:43:20 | [diff] [blame] | 372 | if (env->HasVar(env_vars::kEtwLogging)) |
[email protected] | 2b07b841 | 2009-11-25 15:26:34 | [diff] [blame] | 373 | logging::LogEventProvider::Initialize(kChromeTraceProviderName); |
| 374 | #endif |
| 375 | |
[email protected] | ab7ee5b | 2011-03-01 22:24:14 | [diff] [blame] | 376 | #ifdef NDEBUG |
| 377 | if (command_line.HasSwitch(switches::kSilentDumpOnDCHECK) && |
| 378 | command_line.HasSwitch(switches::kEnableDCHECK)) { |
| 379 | #if defined(OS_WIN) |
| 380 | logging::SetLogReportHandler(DumpProcessAssertHandler); |
| 381 | #endif |
| 382 | } |
| 383 | #endif // NDEBUG |
| 384 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 385 | chrome_logging_initialized_ = true; |
| 386 | } |
| 387 | |
| 388 | // This is a no-op, but we'll keep it around in case |
| 389 | // we need to do more cleanup in the future. |
| 390 | void CleanupChromeLogging() { |
[email protected] | 7109dc3 | 2011-02-02 22:58:29 | [diff] [blame] | 391 | if (chrome_logging_failed_) |
| 392 | return; // We failed to initiailize logging, no cleanup. |
| 393 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 394 | DCHECK(chrome_logging_initialized_) << |
| 395 | "Attempted to clean up logging when it wasn't initialized."; |
| 396 | |
| 397 | CloseLogFile(); |
| 398 | |
| 399 | chrome_logging_initialized_ = false; |
[email protected] | 30f547c | 2010-08-04 01:00:01 | [diff] [blame] | 400 | chrome_logging_redirected_ = false; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 401 | } |
| 402 | |
[email protected] | 73e2348 | 2009-08-13 00:26:58 | [diff] [blame] | 403 | FilePath GetLogFileName() { |
[email protected] | c83dd91 | 2010-04-06 18:50:51 | [diff] [blame] | 404 | std::string filename; |
[email protected] | 76b90d31 | 2010-08-03 03:00:50 | [diff] [blame] | 405 | scoped_ptr<base::Environment> env(base::Environment::Create()); |
[email protected] | 3ba7e08 | 2010-08-07 02:57:59 | [diff] [blame] | 406 | if (env->GetVar(env_vars::kLogFileName, &filename) && !filename.empty()) { |
[email protected] | c83dd91 | 2010-04-06 18:50:51 | [diff] [blame] | 407 | #if defined(OS_WIN) |
[email protected] | 8f3a836 | 2011-08-02 01:33:53 | [diff] [blame] | 408 | return FilePath(UTF8ToWide(filename)); |
[email protected] | c83dd91 | 2010-04-06 18:50:51 | [diff] [blame] | 409 | #elif defined(OS_POSIX) |
[email protected] | 8f3a836 | 2011-08-02 01:33:53 | [diff] [blame] | 410 | return FilePath(filename); |
[email protected] | c83dd91 | 2010-04-06 18:50:51 | [diff] [blame] | 411 | #endif |
| 412 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 413 | |
[email protected] | 73e2348 | 2009-08-13 00:26:58 | [diff] [blame] | 414 | const FilePath log_filename(FILE_PATH_LITERAL("chrome_debug.log")); |
| 415 | FilePath log_path; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 416 | |
| 417 | if (PathService::Get(chrome::DIR_LOGS, &log_path)) { |
[email protected] | 73e2348 | 2009-08-13 00:26:58 | [diff] [blame] | 418 | log_path = log_path.Append(log_filename); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 419 | return log_path; |
| 420 | } else { |
| 421 | // error with path service, just use some default file somewhere |
| 422 | return log_filename; |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | bool DialogsAreSuppressed() { |
| 427 | return dialogs_are_suppressed_; |
| 428 | } |
| 429 | |
| 430 | size_t GetFatalAssertions(AssertionList* assertions) { |
| 431 | // In this function, we don't assume that assertions is non-null, so |
| 432 | // that if you just want an assertion count, you can pass in NULL. |
| 433 | if (assertions) |
| 434 | assertions->clear(); |
| 435 | size_t assertion_count = 0; |
| 436 | |
| 437 | std::ifstream log_file; |
[email protected] | 73e2348 | 2009-08-13 00:26:58 | [diff] [blame] | 438 | log_file.open(GetLogFileName().value().c_str()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 439 | if (!log_file.is_open()) |
| 440 | return 0; |
| 441 | |
| 442 | std::string utf8_line; |
| 443 | std::wstring wide_line; |
[email protected] | 4a3dab2 | 2009-11-11 17:36:50 | [diff] [blame] | 444 | while (!log_file.eof()) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 445 | getline(log_file, utf8_line); |
| 446 | if (utf8_line.find(":FATAL:") != std::string::npos) { |
| 447 | wide_line = UTF8ToWide(utf8_line); |
| 448 | if (assertions) |
| 449 | assertions->push_back(wide_line); |
| 450 | ++assertion_count; |
| 451 | } |
| 452 | } |
| 453 | log_file.close(); |
| 454 | |
| 455 | return assertion_count; |
| 456 | } |
| 457 | |
[email protected] | b6994152 | 2011-10-08 03:17:37 | [diff] [blame] | 458 | void DumpWithoutCrashing() { |
| 459 | #if defined(OS_WIN) |
| 460 | std::string str; |
| 461 | DumpProcessAssertHandler(str); |
[email protected] | 0f65b52d | 2012-02-09 21:02:47 | [diff] [blame^] | 462 | #elif defined(USE_LINUX_BREAKPAD) |
| 463 | if (dump_without_crashing_function_) |
| 464 | (*dump_without_crashing_function_)(); |
[email protected] | b6994152 | 2011-10-08 03:17:37 | [diff] [blame] | 465 | #else |
| 466 | NOTIMPLEMENTED(); |
[email protected] | 0f65b52d | 2012-02-09 21:02:47 | [diff] [blame^] | 467 | #endif |
[email protected] | b6994152 | 2011-10-08 03:17:37 | [diff] [blame] | 468 | } |
| 469 | |
[email protected] | 0f65b52d | 2012-02-09 21:02:47 | [diff] [blame^] | 470 | #if defined(USE_LINUX_BREAKPAD) |
| 471 | void SetDumpWithoutCrashingFunction(void (*function)()) { |
| 472 | dump_without_crashing_function_ = function; |
| 473 | } |
| 474 | #endif |
| 475 | |
[email protected] | c83dd91 | 2010-04-06 18:50:51 | [diff] [blame] | 476 | } // namespace logging |