blob: 0098433e3bb98f7567d1db6e9d19c4d380e06345 [file] [log] [blame]
[email protected]4a100992011-06-14 18:22:461// Copyright (c) 2011 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]d55aaa132009-09-28 21:08:0419#include "chrome/common/render_messages.h"
[email protected]bf7cc5382011-05-27 08:26:0620#include "content/common/devtools_messages.h"
[email protected]105303e2011-03-14 22:16:1021#include "content/common/plugin_messages.h"
[email protected]4d223d32011-03-12 06:38:5622#include "content/common/worker_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"
[email protected]73e23482009-08-13 00:26:5837#include "base/file_path.h"
initial.commit09911bf2008-07-26 23:55:2938#include "base/file_util.h"
39#include "base/logging.h"
40#include "base/path_service.h"
[email protected]528c56d2010-07-30 19:28:4441#include "base/string_number_conversions.h"
[email protected]5d91c9e2010-07-28 17:25:2842#include "base/string_util.h"
[email protected]92e43a042011-08-17 00:36:4043#include "base/stringprintf.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"
initial.commit09911bf2008-07-26 23:55:2950#include "chrome/common/env_vars.h"
[email protected]51d5e162009-07-27 19:23:5451#include "ipc/ipc_logging.h"
[email protected]ab7ee5b2011-03-01 22:24:1452
[email protected]2b07b8412009-11-25 15:26:3453#if defined(OS_WIN)
54#include "base/logging_win.h"
55#include <initguid.h>
56#endif
initial.commit09911bf2008-07-26 23:55:2957
[email protected]ab7ee5b2011-03-01 22:24:1458namespace {
59
initial.commit09911bf2008-07-26 23:55:2960// When true, this means that error dialogs should not be shown.
[email protected]ab7ee5b2011-03-01 22:24:1461bool dialogs_are_suppressed_ = false;
initial.commit09911bf2008-07-26 23:55:2962
63// This should be true for exactly the period between the end of
64// InitChromeLogging() and the beginning of CleanupChromeLogging().
[email protected]ab7ee5b2011-03-01 22:24:1465bool chrome_logging_initialized_ = false;
initial.commit09911bf2008-07-26 23:55:2966
[email protected]7109dc32011-02-02 22:58:2967// Set if we caled InitChromeLogging() but failed to initialize.
[email protected]ab7ee5b2011-03-01 22:24:1468bool chrome_logging_failed_ = false;
[email protected]7109dc32011-02-02 22:58:2969
[email protected]30f547c2010-08-04 01:00:0170// This should be true for exactly the period between the end of
71// InitChromeLogging() and the beginning of CleanupChromeLogging().
[email protected]ab7ee5b2011-03-01 22:24:1472bool chrome_logging_redirected_ = false;
[email protected]30f547c2010-08-04 01:00:0173
[email protected]2b07b8412009-11-25 15:26:3474#if defined(OS_WIN)
75// {7FE69228-633E-4f06-80C1-527FEA23E3A7}
[email protected]ab7ee5b2011-03-01 22:24:1476const GUID kChromeTraceProviderName = {
[email protected]162ac0f2010-11-04 15:50:4977 0x7fe69228, 0x633e, 0x4f06,
78 { 0x80, 0xc1, 0x52, 0x7f, 0xea, 0x23, 0xe3, 0xa7 } };
[email protected]2b07b8412009-11-25 15:26:3479#endif
80
initial.commit09911bf2008-07-26 23:55:2981// Assertion handler for logging errors that occur when dialogs are
82// silenced. To record a new error, pass the log string associated
83// with that error in the str parameter.
[email protected]0eb9f4342008-11-21 18:48:5284MSVC_DISABLE_OPTIMIZE();
[email protected]ab7ee5b2011-03-01 22:24:1485void SilentRuntimeAssertHandler(const std::string& str) {
[email protected]58580352010-10-26 04:07:5086 base::debug::BreakDebugger();
initial.commit09911bf2008-07-26 23:55:2987}
[email protected]ab7ee5b2011-03-01 22:24:1488void SilentRuntimeReportHandler(const std::string& str) {
[email protected]6cc67d7f2009-02-12 06:48:2489}
[email protected]ab7ee5b2011-03-01 22:24:1490#if defined(OS_WIN)
91// Handler to silently dump the current process when there is an assert in
92// chrome.
93void DumpProcessAssertHandler(const std::string& str) {
94 // Get the breakpad pointer from chrome.exe
95 typedef void (__cdecl *DumpProcessFunction)();
96 DumpProcessFunction DumpProcess = reinterpret_cast<DumpProcessFunction>(
97 ::GetProcAddress(::GetModuleHandle(chrome::kBrowserProcessExecutableName),
98 "DumpProcess"));
99 if (DumpProcess)
100 DumpProcess();
101}
102#endif // OS_WIN
[email protected]0eb9f4342008-11-21 18:48:52103MSVC_ENABLE_OPTIMIZE();
initial.commit09911bf2008-07-26 23:55:29104
105// Suppresses error/assertion dialogs and enables the logging of
106// those errors into silenced_errors_.
[email protected]ab7ee5b2011-03-01 22:24:14107void SuppressDialogs() {
initial.commit09911bf2008-07-26 23:55:29108 if (dialogs_are_suppressed_)
109 return;
110
111 logging::SetLogAssertHandler(SilentRuntimeAssertHandler);
[email protected]6cc67d7f2009-02-12 06:48:24112 logging::SetLogReportHandler(SilentRuntimeReportHandler);
initial.commit09911bf2008-07-26 23:55:29113
[email protected]ec657802008-11-07 20:05:39114#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:29115 UINT new_flags = SEM_FAILCRITICALERRORS |
116 SEM_NOGPFAULTERRORBOX |
117 SEM_NOOPENFILEERRORBOX;
118
119 // Preserve existing error mode, as discussed at http://t/dmea
120 UINT existing_flags = SetErrorMode(new_flags);
121 SetErrorMode(existing_flags | new_flags);
[email protected]ec657802008-11-07 20:05:39122#endif
initial.commit09911bf2008-07-26 23:55:29123
124 dialogs_are_suppressed_ = true;
125}
126
[email protected]ab7ee5b2011-03-01 22:24:14127} // anonymous namespace
128
initial.commit09911bf2008-07-26 23:55:29129namespace logging {
130
[email protected]77ee1b12010-06-14 16:21:30131LoggingDestination DetermineLogMode(const CommandLine& command_line) {
initial.commit09911bf2008-07-26 23:55:29132 // only use OutputDebugString in debug mode
133#ifdef NDEBUG
134 bool enable_logging = false;
[email protected]b7e0a2a2009-10-13 02:07:25135 const char *kInvertLoggingSwitch = switches::kEnableLogging;
initial.commit09911bf2008-07-26 23:55:29136 const logging::LoggingDestination kDefaultLoggingMode =
137 logging::LOG_ONLY_TO_FILE;
138#else
139 bool enable_logging = true;
[email protected]b7e0a2a2009-10-13 02:07:25140 const char *kInvertLoggingSwitch = switches::kDisableLogging;
initial.commit09911bf2008-07-26 23:55:29141 const logging::LoggingDestination kDefaultLoggingMode =
142 logging::LOG_TO_BOTH_FILE_AND_SYSTEM_DEBUG_LOG;
143#endif
144
145 if (command_line.HasSwitch(kInvertLoggingSwitch))
146 enable_logging = !enable_logging;
147
148 logging::LoggingDestination log_mode;
149 if (enable_logging) {
[email protected]3bba9582009-08-05 01:29:12150 // Let --enable-logging=stderr force only stderr, particularly useful for
151 // non-debug builds where otherwise you can't get logs to stderr at all.
[email protected]c4e52f0d2009-11-06 19:55:16152 if (command_line.GetSwitchValueASCII(switches::kEnableLogging) == "stderr")
[email protected]3bba9582009-08-05 01:29:12153 log_mode = logging::LOG_ONLY_TO_SYSTEM_DEBUG_LOG;
154 else
155 log_mode = kDefaultLoggingMode;
initial.commit09911bf2008-07-26 23:55:29156 } else {
157 log_mode = logging::LOG_NONE;
158 }
[email protected]77ee1b12010-06-14 16:21:30159 return log_mode;
160}
initial.commit09911bf2008-07-26 23:55:29161
[email protected]77ee1b12010-06-14 16:21:30162#if defined(OS_CHROMEOS)
[email protected]c7d5da992010-10-28 00:20:21163namespace {
164FilePath GenerateTimestampedName(const FilePath& base_path,
165 base::Time timestamp) {
[email protected]77ee1b12010-06-14 16:21:30166 base::Time::Exploded time_deets;
167 timestamp.LocalExplode(&time_deets);
[email protected]93f10522010-10-31 16:27:48168 std::string suffix = base::StringPrintf("_%02d%02d%02d-%02d%02d%02d",
169 time_deets.year,
170 time_deets.month,
171 time_deets.day_of_month,
172 time_deets.hour,
173 time_deets.minute,
174 time_deets.second);
[email protected]c7d5da992010-10-28 00:20:21175 return base_path.InsertBeforeExtension(suffix);
[email protected]77ee1b12010-06-14 16:21:30176}
177
[email protected]c7d5da992010-10-28 00:20:21178FilePath SetUpSymlinkIfNeeded(const FilePath& symlink_path, bool new_log) {
[email protected]f25b33a2010-10-28 20:15:51179 DCHECK(!symlink_path.empty());
180
[email protected]c7d5da992010-10-28 00:20:21181 // If not starting a new log, then just log through the existing
182 // symlink, but if the symlink doesn't exist, create it. If
183 // starting a new log, then delete the old symlink and make a new
184 // one to a fresh log file.
185 FilePath target_path;
[email protected]0750b1442010-11-02 21:59:37186 bool symlink_exists = file_util::PathExists(symlink_path);
187 if (new_log || !symlink_exists) {
[email protected]c7d5da992010-10-28 00:20:21188 target_path = GenerateTimestampedName(symlink_path, base::Time::Now());
189
190 // We don't care if the unlink fails; we're going to continue anyway.
[email protected]723571a2010-12-03 17:37:54191 if (::unlink(symlink_path.value().c_str()) == -1) {
[email protected]0750b1442010-11-02 21:59:37192 if (symlink_exists) // only warn if we might expect it to succeed.
[email protected]c7d5da992010-10-28 00:20:21193 PLOG(WARNING) << "Unable to unlink " << symlink_path.value();
194 }
[email protected]723571a2010-12-03 17:37:54195 if (!file_util::CreateSymbolicLink(target_path, symlink_path)) {
[email protected]c7d5da992010-10-28 00:20:21196 PLOG(ERROR) << "Unable to create symlink " << symlink_path.value()
197 << " pointing at " << target_path.value();
198 }
199 } else {
[email protected]723571a2010-12-03 17:37:54200 if (!file_util::ReadSymbolicLink(symlink_path, &target_path))
[email protected]c7d5da992010-10-28 00:20:21201 PLOG(ERROR) << "Unable to read symlink " << symlink_path.value();
[email protected]c7d5da992010-10-28 00:20:21202 }
203 return target_path;
204}
205
206void RemoveSymlinkAndLog(const FilePath& link_path,
207 const FilePath& target_path) {
208 if (::unlink(link_path.value().c_str()) == -1)
209 PLOG(WARNING) << "Unable to unlink symlink " << link_path.value();
210 if (::unlink(target_path.value().c_str()) == -1)
211 PLOG(WARNING) << "Unable to unlink log file " << target_path.value();
212}
213
214} // anonymous namespace
215
[email protected]0750b1442010-11-02 21:59:37216FilePath GetSessionLogFile(const CommandLine& command_line) {
217 FilePath log_dir;
218 std::string log_dir_str;
219 scoped_ptr<base::Environment> env(base::Environment::Create());
220 if (env->GetVar(env_vars::kSessionLogDir, &log_dir_str) &&
221 !log_dir_str.empty()) {
222 log_dir = FilePath(log_dir_str);
223 } else {
224 PathService::Get(chrome::DIR_USER_DATA, &log_dir);
225 FilePath login_profile =
226 command_line.GetSwitchValuePath(switches::kLoginProfile);
227 log_dir = log_dir.Append(login_profile);
228 }
229 return log_dir.Append(GetLogFileName().BaseName());
230}
231
232void RedirectChromeLogging(const CommandLine& command_line) {
[email protected]30f547c2010-08-04 01:00:01233 DCHECK(!chrome_logging_redirected_) <<
234 "Attempted to redirect logging when it was already initialized.";
[email protected]0750b1442010-11-02 21:59:37235
236 // Redirect logs to the session log directory, if set. Otherwise
237 // defaults to the profile dir.
238 FilePath log_path = GetSessionLogFile(command_line);
[email protected]c7d5da992010-10-28 00:20:21239
[email protected]61f93212011-01-24 14:37:08240 // Creating symlink causes us to do blocking IO on UI thread.
241 // Temporarily allow it until we fix http://crbug.com/61143
242 base::ThreadRestrictions::ScopedAllowIO allow_io;
[email protected]c7d5da992010-10-28 00:20:21243 // Always force a new symlink when redirecting.
244 FilePath target_path = SetUpSymlinkIfNeeded(log_path, true);
245
[email protected]7c10f7552011-01-11 01:03:36246 logging::DcheckState dcheck_state =
247 command_line.HasSwitch(switches::kEnableDCHECK) ?
248 logging::ENABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS :
249 logging::DISABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS;
250
[email protected]c7d5da992010-10-28 00:20:21251 // ChromeOS always logs through the symlink, so it shouldn't be
252 // deleted if it already exists.
253 if (!InitLogging(log_path.value().c_str(),
254 DetermineLogMode(command_line),
255 logging::LOCK_LOG_FILE,
[email protected]7c10f7552011-01-11 01:03:36256 logging::APPEND_TO_OLD_LOG_FILE,
257 dcheck_state)) {
[email protected]c7d5da992010-10-28 00:20:21258 LOG(ERROR) << "Unable to initialize logging to " << log_path.value();
259 RemoveSymlinkAndLog(log_path, target_path);
260 } else {
261 chrome_logging_redirected_ = true;
262 }
[email protected]77ee1b12010-06-14 16:21:30263}
[email protected]0750b1442010-11-02 21:59:37264
[email protected]85271192011-05-10 20:38:25265#endif // OS_CHROMEOS
[email protected]77ee1b12010-06-14 16:21:30266
267void InitChromeLogging(const CommandLine& command_line,
268 OldFileDeletionState delete_old_log_file) {
269 DCHECK(!chrome_logging_initialized_) <<
270 "Attempted to initialize logging when it was already initialized.";
271
272#if defined(OS_POSIX) && defined(IPC_MESSAGE_LOG_ENABLED)
[email protected]21fa3a12010-12-08 23:34:16273 IPC::Logging::set_log_function_map(&g_log_function_mapping);
[email protected]77ee1b12010-06-14 16:21:30274#endif
[email protected]4a100992011-06-14 18:22:46275 LoggingDestination logging_dest = DetermineLogMode(command_line);
276 FilePath log_path;
277#if defined(OS_CHROMEOS)
278 FilePath target_path;
279#endif
[email protected]77ee1b12010-06-14 16:21:30280
[email protected]4a100992011-06-14 18:22:46281 // Don't resolve the log path unless we need to. Otherwise we leave an open
282 // ALPC handle after sandbox lockdown on Windows.
283 if (logging_dest == LOG_ONLY_TO_FILE ||
284 logging_dest == LOG_TO_BOTH_FILE_AND_SYSTEM_DEBUG_LOG) {
285 log_path = GetLogFileName();
[email protected]c7d5da992010-10-28 00:20:21286
[email protected]77ee1b12010-06-14 16:21:30287#if defined(OS_CHROMEOS)
[email protected]4a100992011-06-14 18:22:46288 // For BWSI (Incognito) logins, we want to put the logs in the user
289 // profile directory that is created for the temporary session instead
290 // of in the system log directory, for privacy reasons.
291 if (command_line.HasSwitch(switches::kGuestSession))
292 log_path = GetSessionLogFile(command_line);
[email protected]0750b1442010-11-02 21:59:37293
[email protected]4a100992011-06-14 18:22:46294 // On ChromeOS we log to the symlink. We force creation of a new
295 // symlink if we've been asked to delete the old log, since that
296 // indicates the start of a new session.
297 target_path = SetUpSymlinkIfNeeded(
298 log_path, delete_old_log_file == logging::DELETE_OLD_LOG_FILE);
[email protected]c7d5da992010-10-28 00:20:21299
[email protected]4a100992011-06-14 18:22:46300 // Because ChromeOS manages the move to a new session by redirecting
301 // the link, it shouldn't remove the old file in the logging code,
302 // since that will remove the newly created link instead.
303 delete_old_log_file = logging::APPEND_TO_OLD_LOG_FILE;
[email protected]77ee1b12010-06-14 16:21:30304#endif
[email protected]4a100992011-06-14 18:22:46305 }
[email protected]77ee1b12010-06-14 16:21:30306
[email protected]7c10f7552011-01-11 01:03:36307 logging::DcheckState dcheck_state =
308 command_line.HasSwitch(switches::kEnableDCHECK) ?
309 logging::ENABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS :
310 logging::DISABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS;
311
[email protected]c7d5da992010-10-28 00:20:21312 bool success = InitLogging(log_path.value().c_str(),
[email protected]4a100992011-06-14 18:22:46313 logging_dest,
[email protected]c7d5da992010-10-28 00:20:21314 logging::LOCK_LOG_FILE,
[email protected]7c10f7552011-01-11 01:03:36315 delete_old_log_file,
316 dcheck_state);
[email protected]c7d5da992010-10-28 00:20:21317
318#if defined(OS_CHROMEOS)
319 if (!success) {
320 PLOG(ERROR) << "Unable to initialize logging to " << log_path.value()
321 << " (which should be a link to " << target_path.value() << ")";
322 RemoveSymlinkAndLog(log_path, target_path);
[email protected]7109dc32011-02-02 22:58:29323 chrome_logging_failed_ = true;
[email protected]c7d5da992010-10-28 00:20:21324 return;
325 }
326#else
327 if (!success) {
328 PLOG(ERROR) << "Unable to initialize logging to " << log_path.value();
[email protected]7109dc32011-02-02 22:58:29329 chrome_logging_failed_ = true;
[email protected]c7d5da992010-10-28 00:20:21330 return;
331 }
332#endif
initial.commit09911bf2008-07-26 23:55:29333
[email protected]81e0a852010-08-17 00:38:12334 // Default to showing error dialogs.
335 if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kNoErrorDialogs))
336 logging::SetShowErrorDialogs(true);
337
initial.commit09911bf2008-07-26 23:55:29338 // we want process and thread IDs because we have a lot of things running
[email protected]c7d5da992010-10-28 00:20:21339 logging::SetLogItems(true, // enable_process_id
340 true, // enable_thread_id
341 false, // enable_timestamp
342 true); // enable_tickcount
initial.commit09911bf2008-07-26 23:55:29343
344 // We call running in unattended mode "headless", and allow
345 // headless mode to be configured either by the Environment
346 // Variable or by the Command Line Switch. This is for
347 // automated test purposes.
[email protected]76b90d312010-08-03 03:00:50348 scoped_ptr<base::Environment> env(base::Environment::Create());
[email protected]9432ade2010-08-04 23:43:20349 if (env->HasVar(env_vars::kHeadless) ||
initial.commit09911bf2008-07-26 23:55:29350 command_line.HasSwitch(switches::kNoErrorDialogs))
351 SuppressDialogs();
352
[email protected]3deeb6b32010-10-05 21:53:52353 // Use a minimum log level if the command line asks for one,
354 // otherwise leave it at the default level (INFO).
355 if (command_line.HasSwitch(switches::kLoggingLevel)) {
356 std::string log_level = command_line.GetSwitchValueASCII(
357 switches::kLoggingLevel);
358 int level = 0;
359 if (base::StringToInt(log_level, &level) &&
360 level >= 0 && level < LOG_NUM_SEVERITIES) {
[email protected]bb5185c52008-08-29 19:51:06361 logging::SetMinLogLevel(level);
[email protected]3deeb6b32010-10-05 21:53:52362 } else {
363 LOG(WARNING) << "Bad log level: " << log_level;
364 }
[email protected]bb5185c52008-08-29 19:51:06365 }
366
[email protected]2b07b8412009-11-25 15:26:34367#if defined(OS_WIN)
368 // Enable trace control and transport through event tracing for Windows.
[email protected]9432ade2010-08-04 23:43:20369 if (env->HasVar(env_vars::kEtwLogging))
[email protected]2b07b8412009-11-25 15:26:34370 logging::LogEventProvider::Initialize(kChromeTraceProviderName);
371#endif
372
[email protected]ab7ee5b2011-03-01 22:24:14373#ifdef NDEBUG
374 if (command_line.HasSwitch(switches::kSilentDumpOnDCHECK) &&
375 command_line.HasSwitch(switches::kEnableDCHECK)) {
376#if defined(OS_WIN)
377 logging::SetLogReportHandler(DumpProcessAssertHandler);
378#endif
379 }
380#endif // NDEBUG
381
initial.commit09911bf2008-07-26 23:55:29382 chrome_logging_initialized_ = true;
383}
384
385// This is a no-op, but we'll keep it around in case
386// we need to do more cleanup in the future.
387void CleanupChromeLogging() {
[email protected]7109dc32011-02-02 22:58:29388 if (chrome_logging_failed_)
389 return; // We failed to initiailize logging, no cleanup.
390
initial.commit09911bf2008-07-26 23:55:29391 DCHECK(chrome_logging_initialized_) <<
392 "Attempted to clean up logging when it wasn't initialized.";
393
394 CloseLogFile();
395
396 chrome_logging_initialized_ = false;
[email protected]30f547c2010-08-04 01:00:01397 chrome_logging_redirected_ = false;
initial.commit09911bf2008-07-26 23:55:29398}
399
[email protected]73e23482009-08-13 00:26:58400FilePath GetLogFileName() {
[email protected]c83dd912010-04-06 18:50:51401 std::string filename;
[email protected]76b90d312010-08-03 03:00:50402 scoped_ptr<base::Environment> env(base::Environment::Create());
[email protected]3ba7e082010-08-07 02:57:59403 if (env->GetVar(env_vars::kLogFileName, &filename) && !filename.empty()) {
[email protected]c83dd912010-04-06 18:50:51404#if defined(OS_WIN)
[email protected]8f3a8362011-08-02 01:33:53405 return FilePath(UTF8ToWide(filename));
[email protected]c83dd912010-04-06 18:50:51406#elif defined(OS_POSIX)
[email protected]8f3a8362011-08-02 01:33:53407 return FilePath(filename);
[email protected]c83dd912010-04-06 18:50:51408#endif
409 }
initial.commit09911bf2008-07-26 23:55:29410
[email protected]73e23482009-08-13 00:26:58411 const FilePath log_filename(FILE_PATH_LITERAL("chrome_debug.log"));
412 FilePath log_path;
initial.commit09911bf2008-07-26 23:55:29413
414 if (PathService::Get(chrome::DIR_LOGS, &log_path)) {
[email protected]73e23482009-08-13 00:26:58415 log_path = log_path.Append(log_filename);
initial.commit09911bf2008-07-26 23:55:29416 return log_path;
417 } else {
418 // error with path service, just use some default file somewhere
419 return log_filename;
420 }
421}
422
423bool DialogsAreSuppressed() {
424 return dialogs_are_suppressed_;
425}
426
427size_t GetFatalAssertions(AssertionList* assertions) {
428 // In this function, we don't assume that assertions is non-null, so
429 // that if you just want an assertion count, you can pass in NULL.
430 if (assertions)
431 assertions->clear();
432 size_t assertion_count = 0;
433
434 std::ifstream log_file;
[email protected]73e23482009-08-13 00:26:58435 log_file.open(GetLogFileName().value().c_str());
initial.commit09911bf2008-07-26 23:55:29436 if (!log_file.is_open())
437 return 0;
438
439 std::string utf8_line;
440 std::wstring wide_line;
[email protected]4a3dab22009-11-11 17:36:50441 while (!log_file.eof()) {
initial.commit09911bf2008-07-26 23:55:29442 getline(log_file, utf8_line);
443 if (utf8_line.find(":FATAL:") != std::string::npos) {
444 wide_line = UTF8ToWide(utf8_line);
445 if (assertions)
446 assertions->push_back(wide_line);
447 ++assertion_count;
448 }
449 }
450 log_file.close();
451
452 return assertion_count;
453}
454
[email protected]c83dd912010-04-06 18:50:51455} // namespace logging