blob: 896cc7bc7f1b53b114ec9a6df8891f8503a7550f [file] [log] [blame]
[email protected]0f65b52d2012-02-09 21:02:471// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
[email protected]ec657802008-11-07 20:05:395#include "build/build_config.h"
6
[email protected]d55aaa132009-09-28 21:08:047// 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]4f68ed72012-10-30 05:24:0719#include "content/public/common/content_ipc_logging.h"
20#define IPC_LOG_TABLE_ADD_ENTRY(msg_id, logger) \
21 content::RegisterIPCLogger(msg_id, logger)
[email protected]4a6d74152011-09-26 20:42:3022#include "chrome/common/all_messages.h"
[email protected]d55aaa132009-09-28 21:08:0423#endif
24
[email protected]ec657802008-11-07 20:05:3925#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:2926#include <windows.h>
[email protected]ec657802008-11-07 20:05:3927#endif
initial.commit09911bf2008-07-26 23:55:2928
initial.commit09911bf2008-07-26 23:55:2929#include <fstream>
30
31#include "chrome/common/logging_chrome.h"
32
33#include "base/command_line.h"
[email protected]0eb9f4342008-11-21 18:48:5234#include "base/compiler_specific.h"
[email protected]58580352010-10-26 04:07:5035#include "base/debug/debugger.h"
[email protected]76b90d312010-08-03 03:00:5036#include "base/environment.h"
initial.commit09911bf2008-07-26 23:55:2937#include "base/file_util.h"
[email protected]57999812013-02-24 05:40:5238#include "base/files/file_path.h"
initial.commit09911bf2008-07-26 23:55:2939#include "base/logging.h"
40#include "base/path_service.h"
[email protected]5d91c9e2010-07-28 17:25:2841#include "base/string_util.h"
[email protected]92e43a042011-08-17 00:36:4042#include "base/stringprintf.h"
[email protected]3ea1b182013-02-08 22:38:4143#include "base/strings/string_number_conversions.h"
[email protected]61f93212011-01-24 14:37:0844#include "base/threading/thread_restrictions.h"
[email protected]77ee1b12010-06-14 16:21:3045#include "base/time.h"
[email protected]4197e092010-03-10 00:35:0546#include "base/utf_string_conversions.h"
[email protected]ab7ee5b2011-03-01 22:24:1447#include "chrome/common/chrome_constants.h"
initial.commit09911bf2008-07-26 23:55:2948#include "chrome/common/chrome_paths.h"
49#include "chrome/common/chrome_switches.h"
[email protected]fe69aa12013-02-01 03:37:2750#include "chrome/common/dump_without_crashing.h"
initial.commit09911bf2008-07-26 23:55:2951#include "chrome/common/env_vars.h"
[email protected]51d5e162009-07-27 19:23:5452#include "ipc/ipc_logging.h"
[email protected]ab7ee5b2011-03-01 22:24:1453
[email protected]2b07b8412009-11-25 15:26:3454#if defined(OS_WIN)
[email protected]2b07b8412009-11-25 15:26:3455#include <initguid.h>
[email protected]3ea1b182013-02-08 22:38:4156#include "base/logging_win.h"
[email protected]2b07b8412009-11-25 15:26:3457#endif
initial.commit09911bf2008-07-26 23:55:2958
[email protected]ab7ee5b2011-03-01 22:24:1459namespace {
60
initial.commit09911bf2008-07-26 23:55:2961// When true, this means that error dialogs should not be shown.
[email protected]ab7ee5b2011-03-01 22:24:1462bool dialogs_are_suppressed_ = false;
initial.commit09911bf2008-07-26 23:55:2963
64// This should be true for exactly the period between the end of
65// InitChromeLogging() and the beginning of CleanupChromeLogging().
[email protected]ab7ee5b2011-03-01 22:24:1466bool chrome_logging_initialized_ = false;
initial.commit09911bf2008-07-26 23:55:2967
[email protected]4a6d74152011-09-26 20:42:3068// Set if we called InitChromeLogging() but failed to initialize.
[email protected]ab7ee5b2011-03-01 22:24:1469bool chrome_logging_failed_ = false;
[email protected]7109dc32011-02-02 22:58:2970
[email protected]30f547c2010-08-04 01:00:0171// This should be true for exactly the period between the end of
72// InitChromeLogging() and the beginning of CleanupChromeLogging().
[email protected]ab7ee5b2011-03-01 22:24:1473bool chrome_logging_redirected_ = false;
[email protected]30f547c2010-08-04 01:00:0174
[email protected]2b07b8412009-11-25 15:26:3475#if defined(OS_WIN)
76// {7FE69228-633E-4f06-80C1-527FEA23E3A7}
[email protected]ab7ee5b2011-03-01 22:24:1477const GUID kChromeTraceProviderName = {
[email protected]162ac0f2010-11-04 15:50:4978 0x7fe69228, 0x633e, 0x4f06,
79 { 0x80, 0xc1, 0x52, 0x7f, 0xea, 0x23, 0xe3, 0xa7 } };
[email protected]2b07b8412009-11-25 15:26:3480#endif
81
initial.commit09911bf2008-07-26 23:55:2982// Assertion handler for logging errors that occur when dialogs are
83// silenced. To record a new error, pass the log string associated
84// with that error in the str parameter.
[email protected]0eb9f4342008-11-21 18:48:5285MSVC_DISABLE_OPTIMIZE();
[email protected]ab7ee5b2011-03-01 22:24:1486void SilentRuntimeAssertHandler(const std::string& str) {
[email protected]58580352010-10-26 04:07:5087 base::debug::BreakDebugger();
initial.commit09911bf2008-07-26 23:55:2988}
[email protected]ab7ee5b2011-03-01 22:24:1489void SilentRuntimeReportHandler(const std::string& str) {
[email protected]6cc67d7f2009-02-12 06:48:2490}
[email protected]ab7ee5b2011-03-01 22:24:1491#if defined(OS_WIN)
92// Handler to silently dump the current process when there is an assert in
93// chrome.
94void DumpProcessAssertHandler(const std::string& str) {
[email protected]fe69aa12013-02-01 03:37:2795 logging::DumpWithoutCrashing();
[email protected]ab7ee5b2011-03-01 22:24:1496}
97#endif // OS_WIN
[email protected]0eb9f4342008-11-21 18:48:5298MSVC_ENABLE_OPTIMIZE();
initial.commit09911bf2008-07-26 23:55:2999
100// Suppresses error/assertion dialogs and enables the logging of
101// those errors into silenced_errors_.
[email protected]ab7ee5b2011-03-01 22:24:14102void SuppressDialogs() {
initial.commit09911bf2008-07-26 23:55:29103 if (dialogs_are_suppressed_)
104 return;
105
106 logging::SetLogAssertHandler(SilentRuntimeAssertHandler);
[email protected]6cc67d7f2009-02-12 06:48:24107 logging::SetLogReportHandler(SilentRuntimeReportHandler);
initial.commit09911bf2008-07-26 23:55:29108
[email protected]ec657802008-11-07 20:05:39109#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:29110 UINT new_flags = SEM_FAILCRITICALERRORS |
111 SEM_NOGPFAULTERRORBOX |
112 SEM_NOOPENFILEERRORBOX;
113
114 // Preserve existing error mode, as discussed at http://t/dmea
115 UINT existing_flags = SetErrorMode(new_flags);
116 SetErrorMode(existing_flags | new_flags);
[email protected]ec657802008-11-07 20:05:39117#endif
initial.commit09911bf2008-07-26 23:55:29118
119 dialogs_are_suppressed_ = true;
120}
121
[email protected]ab7ee5b2011-03-01 22:24:14122} // anonymous namespace
123
initial.commit09911bf2008-07-26 23:55:29124namespace logging {
125
[email protected]77ee1b12010-06-14 16:21:30126LoggingDestination DetermineLogMode(const CommandLine& command_line) {
initial.commit09911bf2008-07-26 23:55:29127 // only use OutputDebugString in debug mode
128#ifdef NDEBUG
129 bool enable_logging = false;
[email protected]b7e0a2a2009-10-13 02:07:25130 const char *kInvertLoggingSwitch = switches::kEnableLogging;
initial.commit09911bf2008-07-26 23:55:29131 const logging::LoggingDestination kDefaultLoggingMode =
132 logging::LOG_ONLY_TO_FILE;
133#else
134 bool enable_logging = true;
[email protected]b7e0a2a2009-10-13 02:07:25135 const char *kInvertLoggingSwitch = switches::kDisableLogging;
initial.commit09911bf2008-07-26 23:55:29136 const logging::LoggingDestination kDefaultLoggingMode =
137 logging::LOG_TO_BOTH_FILE_AND_SYSTEM_DEBUG_LOG;
138#endif
139
140 if (command_line.HasSwitch(kInvertLoggingSwitch))
141 enable_logging = !enable_logging;
142
143 logging::LoggingDestination log_mode;
144 if (enable_logging) {
[email protected]3bba9582009-08-05 01:29:12145 // Let --enable-logging=stderr force only stderr, particularly useful for
146 // non-debug builds where otherwise you can't get logs to stderr at all.
[email protected]c4e52f0d2009-11-06 19:55:16147 if (command_line.GetSwitchValueASCII(switches::kEnableLogging) == "stderr")
[email protected]3bba9582009-08-05 01:29:12148 log_mode = logging::LOG_ONLY_TO_SYSTEM_DEBUG_LOG;
149 else
150 log_mode = kDefaultLoggingMode;
initial.commit09911bf2008-07-26 23:55:29151 } else {
152 log_mode = logging::LOG_NONE;
153 }
[email protected]77ee1b12010-06-14 16:21:30154 return log_mode;
155}
initial.commit09911bf2008-07-26 23:55:29156
[email protected]77ee1b12010-06-14 16:21:30157#if defined(OS_CHROMEOS)
[email protected]c7d5da992010-10-28 00:20:21158namespace {
[email protected]a7329162013-02-07 19:21:48159base::FilePath SetUpSymlinkIfNeeded(const base::FilePath& symlink_path,
160 bool new_log) {
[email protected]f25b33a2010-10-28 20:15:51161 DCHECK(!symlink_path.empty());
162
[email protected]c7d5da992010-10-28 00:20:21163 // If not starting a new log, then just log through the existing
164 // symlink, but if the symlink doesn't exist, create it. If
165 // starting a new log, then delete the old symlink and make a new
166 // one to a fresh log file.
[email protected]a7329162013-02-07 19:21:48167 base::FilePath target_path;
[email protected]0750b1442010-11-02 21:59:37168 bool symlink_exists = file_util::PathExists(symlink_path);
169 if (new_log || !symlink_exists) {
[email protected]c7d5da992010-10-28 00:20:21170 target_path = GenerateTimestampedName(symlink_path, base::Time::Now());
171
172 // We don't care if the unlink fails; we're going to continue anyway.
[email protected]723571a2010-12-03 17:37:54173 if (::unlink(symlink_path.value().c_str()) == -1) {
[email protected]0750b1442010-11-02 21:59:37174 if (symlink_exists) // only warn if we might expect it to succeed.
[email protected]26f025e2011-10-28 22:49:27175 DPLOG(WARNING) << "Unable to unlink " << symlink_path.value();
[email protected]c7d5da992010-10-28 00:20:21176 }
[email protected]723571a2010-12-03 17:37:54177 if (!file_util::CreateSymbolicLink(target_path, symlink_path)) {
[email protected]26f025e2011-10-28 22:49:27178 DPLOG(ERROR) << "Unable to create symlink " << symlink_path.value()
179 << " pointing at " << target_path.value();
[email protected]c7d5da992010-10-28 00:20:21180 }
181 } else {
[email protected]723571a2010-12-03 17:37:54182 if (!file_util::ReadSymbolicLink(symlink_path, &target_path))
[email protected]26f025e2011-10-28 22:49:27183 DPLOG(ERROR) << "Unable to read symlink " << symlink_path.value();
[email protected]c7d5da992010-10-28 00:20:21184 }
185 return target_path;
186}
187
[email protected]a7329162013-02-07 19:21:48188void RemoveSymlinkAndLog(const base::FilePath& link_path,
189 const base::FilePath& target_path) {
[email protected]c7d5da992010-10-28 00:20:21190 if (::unlink(link_path.value().c_str()) == -1)
[email protected]26f025e2011-10-28 22:49:27191 DPLOG(WARNING) << "Unable to unlink symlink " << link_path.value();
[email protected]c7d5da992010-10-28 00:20:21192 if (::unlink(target_path.value().c_str()) == -1)
[email protected]26f025e2011-10-28 22:49:27193 DPLOG(WARNING) << "Unable to unlink log file " << target_path.value();
[email protected]c7d5da992010-10-28 00:20:21194}
195
196} // anonymous namespace
197
[email protected]a7329162013-02-07 19:21:48198base::FilePath GetSessionLogFile(const CommandLine& command_line) {
199 base::FilePath log_dir;
[email protected]0750b1442010-11-02 21:59:37200 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()) {
[email protected]a7329162013-02-07 19:21:48204 log_dir = base::FilePath(log_dir_str);
[email protected]0750b1442010-11-02 21:59:37205 } else {
206 PathService::Get(chrome::DIR_USER_DATA, &log_dir);
[email protected]a7329162013-02-07 19:21:48207 base::FilePath login_profile =
[email protected]0750b1442010-11-02 21:59:37208 command_line.GetSwitchValuePath(switches::kLoginProfile);
209 log_dir = log_dir.Append(login_profile);
210 }
211 return log_dir.Append(GetLogFileName().BaseName());
212}
213
214void RedirectChromeLogging(const CommandLine& command_line) {
[email protected]30f547c2010-08-04 01:00:01215 DCHECK(!chrome_logging_redirected_) <<
216 "Attempted to redirect logging when it was already initialized.";
[email protected]0750b1442010-11-02 21:59:37217
218 // Redirect logs to the session log directory, if set. Otherwise
219 // defaults to the profile dir.
[email protected]a7329162013-02-07 19:21:48220 base::FilePath log_path = GetSessionLogFile(command_line);
[email protected]c7d5da992010-10-28 00:20:21221
[email protected]61f93212011-01-24 14:37:08222 // Creating symlink causes us to do blocking IO on UI thread.
223 // Temporarily allow it until we fix http://crbug.com/61143
224 base::ThreadRestrictions::ScopedAllowIO allow_io;
[email protected]c7d5da992010-10-28 00:20:21225 // Always force a new symlink when redirecting.
[email protected]a7329162013-02-07 19:21:48226 base::FilePath target_path = SetUpSymlinkIfNeeded(log_path, true);
[email protected]c7d5da992010-10-28 00:20:21227
[email protected]7c10f7552011-01-11 01:03:36228 logging::DcheckState dcheck_state =
229 command_line.HasSwitch(switches::kEnableDCHECK) ?
230 logging::ENABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS :
231 logging::DISABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS;
232
[email protected]c7d5da992010-10-28 00:20:21233 // ChromeOS always logs through the symlink, so it shouldn't be
234 // deleted if it already exists.
235 if (!InitLogging(log_path.value().c_str(),
236 DetermineLogMode(command_line),
237 logging::LOCK_LOG_FILE,
[email protected]7c10f7552011-01-11 01:03:36238 logging::APPEND_TO_OLD_LOG_FILE,
239 dcheck_state)) {
[email protected]26f025e2011-10-28 22:49:27240 DLOG(ERROR) << "Unable to initialize logging to " << log_path.value();
[email protected]c7d5da992010-10-28 00:20:21241 RemoveSymlinkAndLog(log_path, target_path);
242 } else {
243 chrome_logging_redirected_ = true;
244 }
[email protected]77ee1b12010-06-14 16:21:30245}
[email protected]0750b1442010-11-02 21:59:37246
[email protected]85271192011-05-10 20:38:25247#endif // OS_CHROMEOS
[email protected]77ee1b12010-06-14 16:21:30248
249void InitChromeLogging(const CommandLine& command_line,
250 OldFileDeletionState delete_old_log_file) {
251 DCHECK(!chrome_logging_initialized_) <<
252 "Attempted to initialize logging when it was already initialized.";
253
[email protected]4a100992011-06-14 18:22:46254 LoggingDestination logging_dest = DetermineLogMode(command_line);
[email protected]d64ea42172012-09-14 05:43:59255 LogLockingState log_locking_state = LOCK_LOG_FILE;
[email protected]a7329162013-02-07 19:21:48256 base::FilePath log_path;
[email protected]4a100992011-06-14 18:22:46257#if defined(OS_CHROMEOS)
[email protected]a7329162013-02-07 19:21:48258 base::FilePath target_path;
[email protected]4a100992011-06-14 18:22:46259#endif
[email protected]77ee1b12010-06-14 16:21:30260
[email protected]4a100992011-06-14 18:22:46261 // Don't resolve the log path unless we need to. Otherwise we leave an open
262 // ALPC handle after sandbox lockdown on Windows.
263 if (logging_dest == LOG_ONLY_TO_FILE ||
264 logging_dest == LOG_TO_BOTH_FILE_AND_SYSTEM_DEBUG_LOG) {
265 log_path = GetLogFileName();
[email protected]c7d5da992010-10-28 00:20:21266
[email protected]77ee1b12010-06-14 16:21:30267#if defined(OS_CHROMEOS)
[email protected]4a100992011-06-14 18:22:46268 // For BWSI (Incognito) logins, we want to put the logs in the user
269 // profile directory that is created for the temporary session instead
270 // of in the system log directory, for privacy reasons.
271 if (command_line.HasSwitch(switches::kGuestSession))
272 log_path = GetSessionLogFile(command_line);
[email protected]0750b1442010-11-02 21:59:37273
[email protected]4a100992011-06-14 18:22:46274 // On ChromeOS we log to the symlink. We force creation of a new
275 // symlink if we've been asked to delete the old log, since that
276 // indicates the start of a new session.
277 target_path = SetUpSymlinkIfNeeded(
278 log_path, delete_old_log_file == logging::DELETE_OLD_LOG_FILE);
[email protected]c7d5da992010-10-28 00:20:21279
[email protected]4a100992011-06-14 18:22:46280 // Because ChromeOS manages the move to a new session by redirecting
281 // the link, it shouldn't remove the old file in the logging code,
282 // since that will remove the newly created link instead.
283 delete_old_log_file = logging::APPEND_TO_OLD_LOG_FILE;
[email protected]77ee1b12010-06-14 16:21:30284#endif
[email protected]d64ea42172012-09-14 05:43:59285 } else {
286 log_locking_state = DONT_LOCK_LOG_FILE;
[email protected]4a100992011-06-14 18:22:46287 }
[email protected]77ee1b12010-06-14 16:21:30288
[email protected]7c10f7552011-01-11 01:03:36289 logging::DcheckState dcheck_state =
290 command_line.HasSwitch(switches::kEnableDCHECK) ?
291 logging::ENABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS :
292 logging::DISABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS;
293
[email protected]c7d5da992010-10-28 00:20:21294 bool success = InitLogging(log_path.value().c_str(),
[email protected]4a100992011-06-14 18:22:46295 logging_dest,
[email protected]d64ea42172012-09-14 05:43:59296 log_locking_state,
[email protected]7c10f7552011-01-11 01:03:36297 delete_old_log_file,
298 dcheck_state);
[email protected]c7d5da992010-10-28 00:20:21299
300#if defined(OS_CHROMEOS)
301 if (!success) {
[email protected]26f025e2011-10-28 22:49:27302 DPLOG(ERROR) << "Unable to initialize logging to " << log_path.value()
[email protected]c7d5da992010-10-28 00:20:21303 << " (which should be a link to " << target_path.value() << ")";
304 RemoveSymlinkAndLog(log_path, target_path);
[email protected]7109dc32011-02-02 22:58:29305 chrome_logging_failed_ = true;
[email protected]c7d5da992010-10-28 00:20:21306 return;
307 }
308#else
309 if (!success) {
[email protected]26f025e2011-10-28 22:49:27310 DPLOG(ERROR) << "Unable to initialize logging to " << log_path.value();
[email protected]7109dc32011-02-02 22:58:29311 chrome_logging_failed_ = true;
[email protected]c7d5da992010-10-28 00:20:21312 return;
313 }
314#endif
initial.commit09911bf2008-07-26 23:55:29315
[email protected]81e0a852010-08-17 00:38:12316 // Default to showing error dialogs.
317 if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kNoErrorDialogs))
318 logging::SetShowErrorDialogs(true);
319
initial.commit09911bf2008-07-26 23:55:29320 // we want process and thread IDs because we have a lot of things running
[email protected]c7d5da992010-10-28 00:20:21321 logging::SetLogItems(true, // enable_process_id
322 true, // enable_thread_id
[email protected]51f5a60c2012-09-01 06:07:55323 true, // enable_timestamp
324 false); // enable_tickcount
initial.commit09911bf2008-07-26 23:55:29325
326 // We call running in unattended mode "headless", and allow
327 // headless mode to be configured either by the Environment
328 // Variable or by the Command Line Switch. This is for
329 // automated test purposes.
[email protected]76b90d312010-08-03 03:00:50330 scoped_ptr<base::Environment> env(base::Environment::Create());
[email protected]9432ade2010-08-04 23:43:20331 if (env->HasVar(env_vars::kHeadless) ||
initial.commit09911bf2008-07-26 23:55:29332 command_line.HasSwitch(switches::kNoErrorDialogs))
333 SuppressDialogs();
334
[email protected]3deeb6b32010-10-05 21:53:52335 // Use a minimum log level if the command line asks for one,
336 // otherwise leave it at the default level (INFO).
337 if (command_line.HasSwitch(switches::kLoggingLevel)) {
338 std::string log_level = command_line.GetSwitchValueASCII(
339 switches::kLoggingLevel);
340 int level = 0;
341 if (base::StringToInt(log_level, &level) &&
342 level >= 0 && level < LOG_NUM_SEVERITIES) {
[email protected]bb5185c52008-08-29 19:51:06343 logging::SetMinLogLevel(level);
[email protected]3deeb6b32010-10-05 21:53:52344 } else {
[email protected]26f025e2011-10-28 22:49:27345 DLOG(WARNING) << "Bad log level: " << log_level;
[email protected]3deeb6b32010-10-05 21:53:52346 }
[email protected]bb5185c52008-08-29 19:51:06347 }
348
[email protected]2b07b8412009-11-25 15:26:34349#if defined(OS_WIN)
350 // Enable trace control and transport through event tracing for Windows.
[email protected]b650bf42012-03-15 01:02:24351 logging::LogEventProvider::Initialize(kChromeTraceProviderName);
[email protected]2b07b8412009-11-25 15:26:34352#endif
353
[email protected]ab7ee5b2011-03-01 22:24:14354#ifdef NDEBUG
355 if (command_line.HasSwitch(switches::kSilentDumpOnDCHECK) &&
356 command_line.HasSwitch(switches::kEnableDCHECK)) {
357#if defined(OS_WIN)
358 logging::SetLogReportHandler(DumpProcessAssertHandler);
359#endif
360 }
361#endif // NDEBUG
362
initial.commit09911bf2008-07-26 23:55:29363 chrome_logging_initialized_ = true;
364}
365
366// This is a no-op, but we'll keep it around in case
367// we need to do more cleanup in the future.
368void CleanupChromeLogging() {
[email protected]7109dc32011-02-02 22:58:29369 if (chrome_logging_failed_)
370 return; // We failed to initiailize logging, no cleanup.
371
initial.commit09911bf2008-07-26 23:55:29372 DCHECK(chrome_logging_initialized_) <<
373 "Attempted to clean up logging when it wasn't initialized.";
374
375 CloseLogFile();
376
377 chrome_logging_initialized_ = false;
[email protected]30f547c2010-08-04 01:00:01378 chrome_logging_redirected_ = false;
initial.commit09911bf2008-07-26 23:55:29379}
380
[email protected]a7329162013-02-07 19:21:48381base::FilePath GetLogFileName() {
[email protected]c83dd912010-04-06 18:50:51382 std::string filename;
[email protected]76b90d312010-08-03 03:00:50383 scoped_ptr<base::Environment> env(base::Environment::Create());
[email protected]3ba7e082010-08-07 02:57:59384 if (env->GetVar(env_vars::kLogFileName, &filename) && !filename.empty()) {
[email protected]c83dd912010-04-06 18:50:51385#if defined(OS_WIN)
[email protected]a7329162013-02-07 19:21:48386 return base::FilePath(UTF8ToWide(filename));
[email protected]c83dd912010-04-06 18:50:51387#elif defined(OS_POSIX)
[email protected]a7329162013-02-07 19:21:48388 return base::FilePath(filename);
[email protected]c83dd912010-04-06 18:50:51389#endif
390 }
initial.commit09911bf2008-07-26 23:55:29391
[email protected]a7329162013-02-07 19:21:48392 const base::FilePath log_filename(FILE_PATH_LITERAL("chrome_debug.log"));
393 base::FilePath log_path;
initial.commit09911bf2008-07-26 23:55:29394
395 if (PathService::Get(chrome::DIR_LOGS, &log_path)) {
[email protected]73e23482009-08-13 00:26:58396 log_path = log_path.Append(log_filename);
initial.commit09911bf2008-07-26 23:55:29397 return log_path;
398 } else {
399 // error with path service, just use some default file somewhere
400 return log_filename;
401 }
402}
403
404bool DialogsAreSuppressed() {
405 return dialogs_are_suppressed_;
406}
407
408size_t GetFatalAssertions(AssertionList* assertions) {
409 // In this function, we don't assume that assertions is non-null, so
410 // that if you just want an assertion count, you can pass in NULL.
411 if (assertions)
412 assertions->clear();
413 size_t assertion_count = 0;
414
415 std::ifstream log_file;
[email protected]73e23482009-08-13 00:26:58416 log_file.open(GetLogFileName().value().c_str());
initial.commit09911bf2008-07-26 23:55:29417 if (!log_file.is_open())
418 return 0;
419
420 std::string utf8_line;
421 std::wstring wide_line;
[email protected]4a3dab22009-11-11 17:36:50422 while (!log_file.eof()) {
initial.commit09911bf2008-07-26 23:55:29423 getline(log_file, utf8_line);
424 if (utf8_line.find(":FATAL:") != std::string::npos) {
425 wide_line = UTF8ToWide(utf8_line);
426 if (assertions)
427 assertions->push_back(wide_line);
428 ++assertion_count;
429 }
430 }
431 log_file.close();
432
433 return assertion_count;
434}
435
[email protected]a7329162013-02-07 19:21:48436base::FilePath GenerateTimestampedName(const base::FilePath& base_path,
437 base::Time timestamp) {
[email protected]395a0392012-04-12 15:35:03438 base::Time::Exploded time_deets;
439 timestamp.LocalExplode(&time_deets);
440 std::string suffix = base::StringPrintf("_%02d%02d%02d-%02d%02d%02d",
441 time_deets.year,
442 time_deets.month,
443 time_deets.day_of_month,
444 time_deets.hour,
445 time_deets.minute,
446 time_deets.second);
447 return base_path.InsertBeforeExtensionASCII(suffix);
448}
449
[email protected]c83dd912010-04-06 18:50:51450} // namespace logging