[email protected] | c83dd91 | 2010-04-06 18:50:51 | [diff] [blame] | 1 | // Copyright (c) 2010 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 |
| 19 | #include "chrome/common/devtools_messages.h" |
| 20 | #include "chrome/common/plugin_messages.h" |
| 21 | #include "chrome/common/render_messages.h" |
| 22 | #include "chrome/common/worker_messages.h" |
| 23 | #endif |
| 24 | |
[email protected] | ec65780 | 2008-11-07 20:05:39 | [diff] [blame] | 25 | #if defined(OS_WIN) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 26 | #include <windows.h> |
[email protected] | ec65780 | 2008-11-07 20:05:39 | [diff] [blame] | 27 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 28 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 29 | #include <fstream> |
| 30 | |
| 31 | #include "chrome/common/logging_chrome.h" |
| 32 | |
| 33 | #include "base/command_line.h" |
[email protected] | 0eb9f434 | 2008-11-21 18:48:52 | [diff] [blame] | 34 | #include "base/compiler_specific.h" |
[email protected] | 5858035 | 2010-10-26 04:07:50 | [diff] [blame] | 35 | #include "base/debug/debugger.h" |
[email protected] | 76b90d31 | 2010-08-03 03:00:50 | [diff] [blame] | 36 | #include "base/environment.h" |
[email protected] | 73e2348 | 2009-08-13 00:26:58 | [diff] [blame] | 37 | #include "base/file_path.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 38 | #include "base/file_util.h" |
| 39 | #include "base/logging.h" |
| 40 | #include "base/path_service.h" |
[email protected] | 528c56d | 2010-07-30 19:28:44 | [diff] [blame] | 41 | #include "base/string_number_conversions.h" |
[email protected] | 5d91c9e | 2010-07-28 17:25:28 | [diff] [blame] | 42 | #include "base/string_util.h" |
[email protected] | 77ee1b1 | 2010-06-14 16:21:30 | [diff] [blame] | 43 | #include "base/time.h" |
[email protected] | 4197e09 | 2010-03-10 00:35:05 | [diff] [blame] | 44 | #include "base/utf_string_conversions.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] | 2b07b841 | 2009-11-25 15:26:34 | [diff] [blame] | 49 | #if defined(OS_WIN) |
| 50 | #include "base/logging_win.h" |
| 51 | #include <initguid.h> |
| 52 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 53 | |
| 54 | // When true, this means that error dialogs should not be shown. |
| 55 | static bool dialogs_are_suppressed_ = false; |
| 56 | |
| 57 | // This should be true for exactly the period between the end of |
| 58 | // InitChromeLogging() and the beginning of CleanupChromeLogging(). |
| 59 | static bool chrome_logging_initialized_ = false; |
| 60 | |
[email protected] | 30f547c | 2010-08-04 01:00:01 | [diff] [blame] | 61 | // This should be true for exactly the period between the end of |
| 62 | // InitChromeLogging() and the beginning of CleanupChromeLogging(). |
| 63 | static bool chrome_logging_redirected_ = false; |
| 64 | |
[email protected] | 2b07b841 | 2009-11-25 15:26:34 | [diff] [blame] | 65 | #if defined(OS_WIN) |
| 66 | // {7FE69228-633E-4f06-80C1-527FEA23E3A7} |
[email protected] | 162ac0f | 2010-11-04 15:50:49 | [diff] [blame^] | 67 | static const GUID kChromeTraceProviderName = { |
| 68 | 0x7fe69228, 0x633e, 0x4f06, |
| 69 | { 0x80, 0xc1, 0x52, 0x7f, 0xea, 0x23, 0xe3, 0xa7 } }; |
[email protected] | 2b07b841 | 2009-11-25 15:26:34 | [diff] [blame] | 70 | #endif |
| 71 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 72 | // Assertion handler for logging errors that occur when dialogs are |
| 73 | // silenced. To record a new error, pass the log string associated |
| 74 | // with that error in the str parameter. |
[email protected] | 0eb9f434 | 2008-11-21 18:48:52 | [diff] [blame] | 75 | MSVC_DISABLE_OPTIMIZE(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 76 | static void SilentRuntimeAssertHandler(const std::string& str) { |
[email protected] | 5858035 | 2010-10-26 04:07:50 | [diff] [blame] | 77 | base::debug::BreakDebugger(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 78 | } |
[email protected] | 6cc67d7f | 2009-02-12 06:48:24 | [diff] [blame] | 79 | static void SilentRuntimeReportHandler(const std::string& str) { |
| 80 | } |
[email protected] | 0eb9f434 | 2008-11-21 18:48:52 | [diff] [blame] | 81 | MSVC_ENABLE_OPTIMIZE(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 82 | |
| 83 | // Suppresses error/assertion dialogs and enables the logging of |
| 84 | // those errors into silenced_errors_. |
| 85 | static void SuppressDialogs() { |
| 86 | if (dialogs_are_suppressed_) |
| 87 | return; |
| 88 | |
| 89 | logging::SetLogAssertHandler(SilentRuntimeAssertHandler); |
[email protected] | 6cc67d7f | 2009-02-12 06:48:24 | [diff] [blame] | 90 | logging::SetLogReportHandler(SilentRuntimeReportHandler); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 91 | |
[email protected] | ec65780 | 2008-11-07 20:05:39 | [diff] [blame] | 92 | #if defined(OS_WIN) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 93 | UINT new_flags = SEM_FAILCRITICALERRORS | |
| 94 | SEM_NOGPFAULTERRORBOX | |
| 95 | SEM_NOOPENFILEERRORBOX; |
| 96 | |
| 97 | // Preserve existing error mode, as discussed at http://t/dmea |
| 98 | UINT existing_flags = SetErrorMode(new_flags); |
| 99 | SetErrorMode(existing_flags | new_flags); |
[email protected] | ec65780 | 2008-11-07 20:05:39 | [diff] [blame] | 100 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 101 | |
| 102 | dialogs_are_suppressed_ = true; |
| 103 | } |
| 104 | |
| 105 | namespace logging { |
| 106 | |
[email protected] | 77ee1b1 | 2010-06-14 16:21:30 | [diff] [blame] | 107 | LoggingDestination DetermineLogMode(const CommandLine& command_line) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 108 | // only use OutputDebugString in debug mode |
| 109 | #ifdef NDEBUG |
| 110 | bool enable_logging = false; |
[email protected] | b7e0a2a | 2009-10-13 02:07:25 | [diff] [blame] | 111 | const char *kInvertLoggingSwitch = switches::kEnableLogging; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 112 | const logging::LoggingDestination kDefaultLoggingMode = |
| 113 | logging::LOG_ONLY_TO_FILE; |
| 114 | #else |
| 115 | bool enable_logging = true; |
[email protected] | b7e0a2a | 2009-10-13 02:07:25 | [diff] [blame] | 116 | const char *kInvertLoggingSwitch = switches::kDisableLogging; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 117 | const logging::LoggingDestination kDefaultLoggingMode = |
| 118 | logging::LOG_TO_BOTH_FILE_AND_SYSTEM_DEBUG_LOG; |
| 119 | #endif |
| 120 | |
| 121 | if (command_line.HasSwitch(kInvertLoggingSwitch)) |
| 122 | enable_logging = !enable_logging; |
| 123 | |
| 124 | logging::LoggingDestination log_mode; |
| 125 | if (enable_logging) { |
[email protected] | 3bba958 | 2009-08-05 01:29:12 | [diff] [blame] | 126 | // Let --enable-logging=stderr force only stderr, particularly useful for |
| 127 | // 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] | 128 | if (command_line.GetSwitchValueASCII(switches::kEnableLogging) == "stderr") |
[email protected] | 3bba958 | 2009-08-05 01:29:12 | [diff] [blame] | 129 | log_mode = logging::LOG_ONLY_TO_SYSTEM_DEBUG_LOG; |
| 130 | else |
| 131 | log_mode = kDefaultLoggingMode; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 132 | } else { |
| 133 | log_mode = logging::LOG_NONE; |
| 134 | } |
[email protected] | 77ee1b1 | 2010-06-14 16:21:30 | [diff] [blame] | 135 | return log_mode; |
| 136 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 137 | |
[email protected] | 77ee1b1 | 2010-06-14 16:21:30 | [diff] [blame] | 138 | #if defined(OS_CHROMEOS) |
[email protected] | c7d5da99 | 2010-10-28 00:20:21 | [diff] [blame] | 139 | namespace { |
| 140 | FilePath GenerateTimestampedName(const FilePath& base_path, |
| 141 | base::Time timestamp) { |
[email protected] | 77ee1b1 | 2010-06-14 16:21:30 | [diff] [blame] | 142 | base::Time::Exploded time_deets; |
| 143 | timestamp.LocalExplode(&time_deets); |
[email protected] | 93f1052 | 2010-10-31 16:27:48 | [diff] [blame] | 144 | std::string suffix = base::StringPrintf("_%02d%02d%02d-%02d%02d%02d", |
| 145 | time_deets.year, |
| 146 | time_deets.month, |
| 147 | time_deets.day_of_month, |
| 148 | time_deets.hour, |
| 149 | time_deets.minute, |
| 150 | time_deets.second); |
[email protected] | c7d5da99 | 2010-10-28 00:20:21 | [diff] [blame] | 151 | return base_path.InsertBeforeExtension(suffix); |
[email protected] | 77ee1b1 | 2010-06-14 16:21:30 | [diff] [blame] | 152 | } |
| 153 | |
[email protected] | c7d5da99 | 2010-10-28 00:20:21 | [diff] [blame] | 154 | FilePath SetUpSymlinkIfNeeded(const FilePath& symlink_path, bool new_log) { |
[email protected] | f25b33a | 2010-10-28 20:15:51 | [diff] [blame] | 155 | DCHECK(!symlink_path.empty()); |
| 156 | |
[email protected] | c7d5da99 | 2010-10-28 00:20:21 | [diff] [blame] | 157 | // If not starting a new log, then just log through the existing |
| 158 | // symlink, but if the symlink doesn't exist, create it. If |
| 159 | // starting a new log, then delete the old symlink and make a new |
| 160 | // one to a fresh log file. |
| 161 | FilePath target_path; |
[email protected] | 0750b144 | 2010-11-02 21:59:37 | [diff] [blame] | 162 | bool symlink_exists = file_util::PathExists(symlink_path); |
| 163 | if (new_log || !symlink_exists) { |
[email protected] | c7d5da99 | 2010-10-28 00:20:21 | [diff] [blame] | 164 | target_path = GenerateTimestampedName(symlink_path, base::Time::Now()); |
| 165 | |
| 166 | // We don't care if the unlink fails; we're going to continue anyway. |
| 167 | if (unlink(symlink_path.value().c_str()) == -1) { |
[email protected] | 0750b144 | 2010-11-02 21:59:37 | [diff] [blame] | 168 | if (symlink_exists) // only warn if we might expect it to succeed. |
[email protected] | c7d5da99 | 2010-10-28 00:20:21 | [diff] [blame] | 169 | PLOG(WARNING) << "Unable to unlink " << symlink_path.value(); |
| 170 | } |
| 171 | if (symlink(target_path.value().c_str(), |
| 172 | symlink_path.value().c_str()) == -1) { |
| 173 | PLOG(ERROR) << "Unable to create symlink " << symlink_path.value() |
| 174 | << " pointing at " << target_path.value(); |
| 175 | } |
| 176 | } else { |
[email protected] | f25b33a | 2010-10-28 20:15:51 | [diff] [blame] | 177 | char buf[PATH_MAX]; |
| 178 | ssize_t count = readlink(symlink_path.value().c_str(), buf, arraysize(buf)); |
[email protected] | c7d5da99 | 2010-10-28 00:20:21 | [diff] [blame] | 179 | if (count > 0) { |
[email protected] | f25b33a | 2010-10-28 20:15:51 | [diff] [blame] | 180 | target_path = FilePath(FilePath::StringType(buf, count)); |
[email protected] | c7d5da99 | 2010-10-28 00:20:21 | [diff] [blame] | 181 | } else { |
| 182 | PLOG(ERROR) << "Unable to read symlink " << symlink_path.value(); |
| 183 | } |
| 184 | } |
| 185 | return target_path; |
| 186 | } |
| 187 | |
| 188 | void RemoveSymlinkAndLog(const FilePath& link_path, |
| 189 | const FilePath& target_path) { |
| 190 | if (::unlink(link_path.value().c_str()) == -1) |
| 191 | PLOG(WARNING) << "Unable to unlink symlink " << link_path.value(); |
| 192 | if (::unlink(target_path.value().c_str()) == -1) |
| 193 | PLOG(WARNING) << "Unable to unlink log file " << target_path.value(); |
| 194 | } |
| 195 | |
| 196 | } // anonymous namespace |
| 197 | |
[email protected] | 0750b144 | 2010-11-02 21:59:37 | [diff] [blame] | 198 | FilePath GetSessionLogFile(const CommandLine& command_line) { |
| 199 | FilePath log_dir; |
| 200 | std::string log_dir_str; |
| 201 | scoped_ptr<base::Environment> env(base::Environment::Create()); |
| 202 | if (env->GetVar(env_vars::kSessionLogDir, &log_dir_str) && |
| 203 | !log_dir_str.empty()) { |
| 204 | log_dir = FilePath(log_dir_str); |
| 205 | } else { |
| 206 | PathService::Get(chrome::DIR_USER_DATA, &log_dir); |
| 207 | FilePath login_profile = |
| 208 | command_line.GetSwitchValuePath(switches::kLoginProfile); |
| 209 | log_dir = log_dir.Append(login_profile); |
| 210 | } |
| 211 | return log_dir.Append(GetLogFileName().BaseName()); |
| 212 | } |
| 213 | |
| 214 | void RedirectChromeLogging(const CommandLine& command_line) { |
[email protected] | 30f547c | 2010-08-04 01:00:01 | [diff] [blame] | 215 | DCHECK(!chrome_logging_redirected_) << |
| 216 | "Attempted to redirect logging when it was already initialized."; |
[email protected] | 0750b144 | 2010-11-02 21:59:37 | [diff] [blame] | 217 | |
| 218 | // Redirect logs to the session log directory, if set. Otherwise |
| 219 | // defaults to the profile dir. |
| 220 | FilePath log_path = GetSessionLogFile(command_line); |
[email protected] | c7d5da99 | 2010-10-28 00:20:21 | [diff] [blame] | 221 | |
| 222 | // Always force a new symlink when redirecting. |
| 223 | FilePath target_path = SetUpSymlinkIfNeeded(log_path, true); |
| 224 | |
| 225 | // ChromeOS always logs through the symlink, so it shouldn't be |
| 226 | // deleted if it already exists. |
| 227 | if (!InitLogging(log_path.value().c_str(), |
| 228 | DetermineLogMode(command_line), |
| 229 | logging::LOCK_LOG_FILE, |
| 230 | logging::APPEND_TO_OLD_LOG_FILE)) { |
| 231 | LOG(ERROR) << "Unable to initialize logging to " << log_path.value(); |
| 232 | RemoveSymlinkAndLog(log_path, target_path); |
| 233 | } else { |
| 234 | chrome_logging_redirected_ = true; |
| 235 | } |
[email protected] | 77ee1b1 | 2010-06-14 16:21:30 | [diff] [blame] | 236 | } |
[email protected] | 0750b144 | 2010-11-02 21:59:37 | [diff] [blame] | 237 | |
| 238 | |
[email protected] | 77ee1b1 | 2010-06-14 16:21:30 | [diff] [blame] | 239 | #endif |
| 240 | |
| 241 | void InitChromeLogging(const CommandLine& command_line, |
| 242 | OldFileDeletionState delete_old_log_file) { |
| 243 | DCHECK(!chrome_logging_initialized_) << |
| 244 | "Attempted to initialize logging when it was already initialized."; |
| 245 | |
| 246 | #if defined(OS_POSIX) && defined(IPC_MESSAGE_LOG_ENABLED) |
| 247 | IPC::Logging::SetLoggerFunctions(g_log_function_mapping); |
| 248 | #endif |
| 249 | |
| 250 | FilePath log_path = GetLogFileName(); |
[email protected] | c7d5da99 | 2010-10-28 00:20:21 | [diff] [blame] | 251 | |
[email protected] | 77ee1b1 | 2010-06-14 16:21:30 | [diff] [blame] | 252 | #if defined(OS_CHROMEOS) |
[email protected] | 0750b144 | 2010-11-02 21:59:37 | [diff] [blame] | 253 | // For BWSI (Incognito) logins, we want to put the logs in the user |
| 254 | // profile directory that is created for the temporary session instead |
| 255 | // of in the system log directory, for privacy reasons. |
| 256 | if (command_line.HasSwitch(switches::kGuestSession)) |
| 257 | log_path = GetSessionLogFile(command_line); |
| 258 | |
[email protected] | c7d5da99 | 2010-10-28 00:20:21 | [diff] [blame] | 259 | // On ChromeOS we log to the symlink. We force creation of a new |
| 260 | // symlink if we've been asked to delete the old log, since that |
| 261 | // indicates the start of a new session. |
| 262 | FilePath target_path = SetUpSymlinkIfNeeded( |
| 263 | log_path, delete_old_log_file == logging::DELETE_OLD_LOG_FILE); |
| 264 | |
| 265 | // Because ChromeOS manages the move to a new session by redirecting |
| 266 | // the link, it shouldn't remove the old file in the logging code, |
| 267 | // since that will remove the newly created link instead. |
| 268 | delete_old_log_file = logging::APPEND_TO_OLD_LOG_FILE; |
[email protected] | 77ee1b1 | 2010-06-14 16:21:30 | [diff] [blame] | 269 | #endif |
| 270 | |
[email protected] | c7d5da99 | 2010-10-28 00:20:21 | [diff] [blame] | 271 | bool success = InitLogging(log_path.value().c_str(), |
| 272 | DetermineLogMode(command_line), |
| 273 | logging::LOCK_LOG_FILE, |
| 274 | delete_old_log_file); |
| 275 | |
| 276 | #if defined(OS_CHROMEOS) |
| 277 | if (!success) { |
| 278 | PLOG(ERROR) << "Unable to initialize logging to " << log_path.value() |
| 279 | << " (which should be a link to " << target_path.value() << ")"; |
| 280 | RemoveSymlinkAndLog(log_path, target_path); |
| 281 | return; |
| 282 | } |
| 283 | #else |
| 284 | if (!success) { |
| 285 | PLOG(ERROR) << "Unable to initialize logging to " << log_path.value(); |
| 286 | return; |
| 287 | } |
| 288 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 289 | |
[email protected] | 81e0a85 | 2010-08-17 00:38:12 | [diff] [blame] | 290 | // Default to showing error dialogs. |
| 291 | if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kNoErrorDialogs)) |
| 292 | logging::SetShowErrorDialogs(true); |
| 293 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 294 | // 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] | 295 | logging::SetLogItems(true, // enable_process_id |
| 296 | true, // enable_thread_id |
| 297 | false, // enable_timestamp |
| 298 | true); // enable_tickcount |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 299 | |
| 300 | // We call running in unattended mode "headless", and allow |
| 301 | // headless mode to be configured either by the Environment |
| 302 | // Variable or by the Command Line Switch. This is for |
| 303 | // automated test purposes. |
[email protected] | 76b90d31 | 2010-08-03 03:00:50 | [diff] [blame] | 304 | scoped_ptr<base::Environment> env(base::Environment::Create()); |
[email protected] | 9432ade | 2010-08-04 23:43:20 | [diff] [blame] | 305 | if (env->HasVar(env_vars::kHeadless) || |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 306 | command_line.HasSwitch(switches::kNoErrorDialogs)) |
| 307 | SuppressDialogs(); |
| 308 | |
[email protected] | 3deeb6b3 | 2010-10-05 21:53:52 | [diff] [blame] | 309 | // Use a minimum log level if the command line asks for one, |
| 310 | // otherwise leave it at the default level (INFO). |
| 311 | if (command_line.HasSwitch(switches::kLoggingLevel)) { |
| 312 | std::string log_level = command_line.GetSwitchValueASCII( |
| 313 | switches::kLoggingLevel); |
| 314 | int level = 0; |
| 315 | if (base::StringToInt(log_level, &level) && |
| 316 | level >= 0 && level < LOG_NUM_SEVERITIES) { |
[email protected] | bb5185c5 | 2008-08-29 19:51:06 | [diff] [blame] | 317 | logging::SetMinLogLevel(level); |
[email protected] | 3deeb6b3 | 2010-10-05 21:53:52 | [diff] [blame] | 318 | } else { |
| 319 | LOG(WARNING) << "Bad log level: " << log_level; |
| 320 | } |
[email protected] | bb5185c5 | 2008-08-29 19:51:06 | [diff] [blame] | 321 | } |
| 322 | |
[email protected] | 2b07b841 | 2009-11-25 15:26:34 | [diff] [blame] | 323 | #if defined(OS_WIN) |
| 324 | // Enable trace control and transport through event tracing for Windows. |
[email protected] | 9432ade | 2010-08-04 23:43:20 | [diff] [blame] | 325 | if (env->HasVar(env_vars::kEtwLogging)) |
[email protected] | 2b07b841 | 2009-11-25 15:26:34 | [diff] [blame] | 326 | logging::LogEventProvider::Initialize(kChromeTraceProviderName); |
| 327 | #endif |
| 328 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 329 | chrome_logging_initialized_ = true; |
| 330 | } |
| 331 | |
| 332 | // This is a no-op, but we'll keep it around in case |
| 333 | // we need to do more cleanup in the future. |
| 334 | void CleanupChromeLogging() { |
| 335 | DCHECK(chrome_logging_initialized_) << |
| 336 | "Attempted to clean up logging when it wasn't initialized."; |
| 337 | |
| 338 | CloseLogFile(); |
| 339 | |
| 340 | chrome_logging_initialized_ = false; |
[email protected] | 30f547c | 2010-08-04 01:00:01 | [diff] [blame] | 341 | chrome_logging_redirected_ = false; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 342 | } |
| 343 | |
[email protected] | 73e2348 | 2009-08-13 00:26:58 | [diff] [blame] | 344 | FilePath GetLogFileName() { |
[email protected] | c83dd91 | 2010-04-06 18:50:51 | [diff] [blame] | 345 | std::string filename; |
[email protected] | 76b90d31 | 2010-08-03 03:00:50 | [diff] [blame] | 346 | scoped_ptr<base::Environment> env(base::Environment::Create()); |
[email protected] | 3ba7e08 | 2010-08-07 02:57:59 | [diff] [blame] | 347 | if (env->GetVar(env_vars::kLogFileName, &filename) && !filename.empty()) { |
[email protected] | c83dd91 | 2010-04-06 18:50:51 | [diff] [blame] | 348 | #if defined(OS_WIN) |
| 349 | return FilePath(UTF8ToWide(filename).c_str()); |
| 350 | #elif defined(OS_POSIX) |
| 351 | return FilePath(filename.c_str()); |
| 352 | #endif |
| 353 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 354 | |
[email protected] | 73e2348 | 2009-08-13 00:26:58 | [diff] [blame] | 355 | const FilePath log_filename(FILE_PATH_LITERAL("chrome_debug.log")); |
| 356 | FilePath log_path; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 357 | |
| 358 | if (PathService::Get(chrome::DIR_LOGS, &log_path)) { |
[email protected] | 73e2348 | 2009-08-13 00:26:58 | [diff] [blame] | 359 | log_path = log_path.Append(log_filename); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 360 | return log_path; |
| 361 | } else { |
| 362 | // error with path service, just use some default file somewhere |
| 363 | return log_filename; |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | bool DialogsAreSuppressed() { |
| 368 | return dialogs_are_suppressed_; |
| 369 | } |
| 370 | |
| 371 | size_t GetFatalAssertions(AssertionList* assertions) { |
| 372 | // In this function, we don't assume that assertions is non-null, so |
| 373 | // that if you just want an assertion count, you can pass in NULL. |
| 374 | if (assertions) |
| 375 | assertions->clear(); |
| 376 | size_t assertion_count = 0; |
| 377 | |
| 378 | std::ifstream log_file; |
[email protected] | 73e2348 | 2009-08-13 00:26:58 | [diff] [blame] | 379 | log_file.open(GetLogFileName().value().c_str()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 380 | if (!log_file.is_open()) |
| 381 | return 0; |
| 382 | |
| 383 | std::string utf8_line; |
| 384 | std::wstring wide_line; |
[email protected] | 4a3dab2 | 2009-11-11 17:36:50 | [diff] [blame] | 385 | while (!log_file.eof()) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 386 | getline(log_file, utf8_line); |
| 387 | if (utf8_line.find(":FATAL:") != std::string::npos) { |
| 388 | wide_line = UTF8ToWide(utf8_line); |
| 389 | if (assertions) |
| 390 | assertions->push_back(wide_line); |
| 391 | ++assertion_count; |
| 392 | } |
| 393 | } |
| 394 | log_file.close(); |
| 395 | |
| 396 | return assertion_count; |
| 397 | } |
| 398 | |
[email protected] | c83dd91 | 2010-04-06 18:50:51 | [diff] [blame] | 399 | } // namespace logging |