blob: 5ea8012317bb99d6774eb34103c28c503c6c837d [file] [log] [blame]
[email protected]c83dd912010-04-06 18:50:511// Copyright (c) 2010 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
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]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]61f93212011-01-24 14:37:0843#include "base/threading/thread_restrictions.h"
[email protected]77ee1b12010-06-14 16:21:3044#include "base/time.h"
[email protected]4197e092010-03-10 00:35:0545#include "base/utf_string_conversions.h"
initial.commit09911bf2008-07-26 23:55:2946#include "chrome/common/chrome_paths.h"
47#include "chrome/common/chrome_switches.h"
initial.commit09911bf2008-07-26 23:55:2948#include "chrome/common/env_vars.h"
[email protected]51d5e162009-07-27 19:23:5449#include "ipc/ipc_logging.h"
[email protected]2b07b8412009-11-25 15:26:3450#if defined(OS_WIN)
51#include "base/logging_win.h"
52#include <initguid.h>
53#endif
initial.commit09911bf2008-07-26 23:55:2954
55// When true, this means that error dialogs should not be shown.
56static bool dialogs_are_suppressed_ = false;
57
58// This should be true for exactly the period between the end of
59// InitChromeLogging() and the beginning of CleanupChromeLogging().
60static bool chrome_logging_initialized_ = false;
61
[email protected]30f547c2010-08-04 01:00:0162// This should be true for exactly the period between the end of
63// InitChromeLogging() and the beginning of CleanupChromeLogging().
64static bool chrome_logging_redirected_ = false;
65
[email protected]2b07b8412009-11-25 15:26:3466#if defined(OS_WIN)
67// {7FE69228-633E-4f06-80C1-527FEA23E3A7}
[email protected]162ac0f2010-11-04 15:50:4968static const GUID kChromeTraceProviderName = {
69 0x7fe69228, 0x633e, 0x4f06,
70 { 0x80, 0xc1, 0x52, 0x7f, 0xea, 0x23, 0xe3, 0xa7 } };
[email protected]2b07b8412009-11-25 15:26:3471#endif
72
initial.commit09911bf2008-07-26 23:55:2973// Assertion handler for logging errors that occur when dialogs are
74// silenced. To record a new error, pass the log string associated
75// with that error in the str parameter.
[email protected]0eb9f4342008-11-21 18:48:5276MSVC_DISABLE_OPTIMIZE();
initial.commit09911bf2008-07-26 23:55:2977static void SilentRuntimeAssertHandler(const std::string& str) {
[email protected]58580352010-10-26 04:07:5078 base::debug::BreakDebugger();
initial.commit09911bf2008-07-26 23:55:2979}
[email protected]6cc67d7f2009-02-12 06:48:2480static void SilentRuntimeReportHandler(const std::string& str) {
81}
[email protected]0eb9f4342008-11-21 18:48:5282MSVC_ENABLE_OPTIMIZE();
initial.commit09911bf2008-07-26 23:55:2983
84// Suppresses error/assertion dialogs and enables the logging of
85// those errors into silenced_errors_.
86static void SuppressDialogs() {
87 if (dialogs_are_suppressed_)
88 return;
89
90 logging::SetLogAssertHandler(SilentRuntimeAssertHandler);
[email protected]6cc67d7f2009-02-12 06:48:2491 logging::SetLogReportHandler(SilentRuntimeReportHandler);
initial.commit09911bf2008-07-26 23:55:2992
[email protected]ec657802008-11-07 20:05:3993#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:2994 UINT new_flags = SEM_FAILCRITICALERRORS |
95 SEM_NOGPFAULTERRORBOX |
96 SEM_NOOPENFILEERRORBOX;
97
98 // Preserve existing error mode, as discussed at http://t/dmea
99 UINT existing_flags = SetErrorMode(new_flags);
100 SetErrorMode(existing_flags | new_flags);
[email protected]ec657802008-11-07 20:05:39101#endif
initial.commit09911bf2008-07-26 23:55:29102
103 dialogs_are_suppressed_ = true;
104}
105
106namespace logging {
107
[email protected]77ee1b12010-06-14 16:21:30108LoggingDestination DetermineLogMode(const CommandLine& command_line) {
initial.commit09911bf2008-07-26 23:55:29109 // only use OutputDebugString in debug mode
110#ifdef NDEBUG
111 bool enable_logging = false;
[email protected]b7e0a2a2009-10-13 02:07:25112 const char *kInvertLoggingSwitch = switches::kEnableLogging;
initial.commit09911bf2008-07-26 23:55:29113 const logging::LoggingDestination kDefaultLoggingMode =
114 logging::LOG_ONLY_TO_FILE;
115#else
116 bool enable_logging = true;
[email protected]b7e0a2a2009-10-13 02:07:25117 const char *kInvertLoggingSwitch = switches::kDisableLogging;
initial.commit09911bf2008-07-26 23:55:29118 const logging::LoggingDestination kDefaultLoggingMode =
119 logging::LOG_TO_BOTH_FILE_AND_SYSTEM_DEBUG_LOG;
120#endif
121
122 if (command_line.HasSwitch(kInvertLoggingSwitch))
123 enable_logging = !enable_logging;
124
125 logging::LoggingDestination log_mode;
126 if (enable_logging) {
[email protected]3bba9582009-08-05 01:29:12127 // Let --enable-logging=stderr force only stderr, particularly useful for
128 // non-debug builds where otherwise you can't get logs to stderr at all.
[email protected]c4e52f0d2009-11-06 19:55:16129 if (command_line.GetSwitchValueASCII(switches::kEnableLogging) == "stderr")
[email protected]3bba9582009-08-05 01:29:12130 log_mode = logging::LOG_ONLY_TO_SYSTEM_DEBUG_LOG;
131 else
132 log_mode = kDefaultLoggingMode;
initial.commit09911bf2008-07-26 23:55:29133 } else {
134 log_mode = logging::LOG_NONE;
135 }
[email protected]77ee1b12010-06-14 16:21:30136 return log_mode;
137}
initial.commit09911bf2008-07-26 23:55:29138
[email protected]77ee1b12010-06-14 16:21:30139#if defined(OS_CHROMEOS)
[email protected]c7d5da992010-10-28 00:20:21140namespace {
141FilePath GenerateTimestampedName(const FilePath& base_path,
142 base::Time timestamp) {
[email protected]77ee1b12010-06-14 16:21:30143 base::Time::Exploded time_deets;
144 timestamp.LocalExplode(&time_deets);
[email protected]93f10522010-10-31 16:27:48145 std::string suffix = base::StringPrintf("_%02d%02d%02d-%02d%02d%02d",
146 time_deets.year,
147 time_deets.month,
148 time_deets.day_of_month,
149 time_deets.hour,
150 time_deets.minute,
151 time_deets.second);
[email protected]c7d5da992010-10-28 00:20:21152 return base_path.InsertBeforeExtension(suffix);
[email protected]77ee1b12010-06-14 16:21:30153}
154
[email protected]c7d5da992010-10-28 00:20:21155FilePath SetUpSymlinkIfNeeded(const FilePath& symlink_path, bool new_log) {
[email protected]f25b33a2010-10-28 20:15:51156 DCHECK(!symlink_path.empty());
157
[email protected]c7d5da992010-10-28 00:20:21158 // If not starting a new log, then just log through the existing
159 // symlink, but if the symlink doesn't exist, create it. If
160 // starting a new log, then delete the old symlink and make a new
161 // one to a fresh log file.
162 FilePath target_path;
[email protected]0750b1442010-11-02 21:59:37163 bool symlink_exists = file_util::PathExists(symlink_path);
164 if (new_log || !symlink_exists) {
[email protected]c7d5da992010-10-28 00:20:21165 target_path = GenerateTimestampedName(symlink_path, base::Time::Now());
166
167 // We don't care if the unlink fails; we're going to continue anyway.
[email protected]723571a2010-12-03 17:37:54168 if (::unlink(symlink_path.value().c_str()) == -1) {
[email protected]0750b1442010-11-02 21:59:37169 if (symlink_exists) // only warn if we might expect it to succeed.
[email protected]c7d5da992010-10-28 00:20:21170 PLOG(WARNING) << "Unable to unlink " << symlink_path.value();
171 }
[email protected]723571a2010-12-03 17:37:54172 if (!file_util::CreateSymbolicLink(target_path, symlink_path)) {
[email protected]c7d5da992010-10-28 00:20:21173 PLOG(ERROR) << "Unable to create symlink " << symlink_path.value()
174 << " pointing at " << target_path.value();
175 }
176 } else {
[email protected]723571a2010-12-03 17:37:54177 if (!file_util::ReadSymbolicLink(symlink_path, &target_path))
[email protected]c7d5da992010-10-28 00:20:21178 PLOG(ERROR) << "Unable to read symlink " << symlink_path.value();
[email protected]c7d5da992010-10-28 00:20:21179 }
180 return target_path;
181}
182
183void RemoveSymlinkAndLog(const FilePath& link_path,
184 const FilePath& target_path) {
185 if (::unlink(link_path.value().c_str()) == -1)
186 PLOG(WARNING) << "Unable to unlink symlink " << link_path.value();
187 if (::unlink(target_path.value().c_str()) == -1)
188 PLOG(WARNING) << "Unable to unlink log file " << target_path.value();
189}
190
191} // anonymous namespace
192
[email protected]0750b1442010-11-02 21:59:37193FilePath GetSessionLogFile(const CommandLine& command_line) {
194 FilePath log_dir;
195 std::string log_dir_str;
196 scoped_ptr<base::Environment> env(base::Environment::Create());
197 if (env->GetVar(env_vars::kSessionLogDir, &log_dir_str) &&
198 !log_dir_str.empty()) {
199 log_dir = FilePath(log_dir_str);
200 } else {
201 PathService::Get(chrome::DIR_USER_DATA, &log_dir);
202 FilePath login_profile =
203 command_line.GetSwitchValuePath(switches::kLoginProfile);
204 log_dir = log_dir.Append(login_profile);
205 }
206 return log_dir.Append(GetLogFileName().BaseName());
207}
208
209void RedirectChromeLogging(const CommandLine& command_line) {
[email protected]30f547c2010-08-04 01:00:01210 DCHECK(!chrome_logging_redirected_) <<
211 "Attempted to redirect logging when it was already initialized.";
[email protected]0750b1442010-11-02 21:59:37212
213 // Redirect logs to the session log directory, if set. Otherwise
214 // defaults to the profile dir.
215 FilePath log_path = GetSessionLogFile(command_line);
[email protected]c7d5da992010-10-28 00:20:21216
[email protected]61f93212011-01-24 14:37:08217 // Creating symlink causes us to do blocking IO on UI thread.
218 // Temporarily allow it until we fix http://crbug.com/61143
219 base::ThreadRestrictions::ScopedAllowIO allow_io;
[email protected]c7d5da992010-10-28 00:20:21220 // Always force a new symlink when redirecting.
221 FilePath target_path = SetUpSymlinkIfNeeded(log_path, true);
222
[email protected]7c10f7552011-01-11 01:03:36223 logging::DcheckState dcheck_state =
224 command_line.HasSwitch(switches::kEnableDCHECK) ?
225 logging::ENABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS :
226 logging::DISABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS;
227
[email protected]c7d5da992010-10-28 00:20:21228 // ChromeOS always logs through the symlink, so it shouldn't be
229 // deleted if it already exists.
230 if (!InitLogging(log_path.value().c_str(),
231 DetermineLogMode(command_line),
232 logging::LOCK_LOG_FILE,
[email protected]7c10f7552011-01-11 01:03:36233 logging::APPEND_TO_OLD_LOG_FILE,
234 dcheck_state)) {
[email protected]c7d5da992010-10-28 00:20:21235 LOG(ERROR) << "Unable to initialize logging to " << log_path.value();
236 RemoveSymlinkAndLog(log_path, target_path);
237 } else {
238 chrome_logging_redirected_ = true;
239 }
[email protected]77ee1b12010-06-14 16:21:30240}
[email protected]0750b1442010-11-02 21:59:37241
242
[email protected]77ee1b12010-06-14 16:21:30243#endif
244
245void InitChromeLogging(const CommandLine& command_line,
246 OldFileDeletionState delete_old_log_file) {
247 DCHECK(!chrome_logging_initialized_) <<
248 "Attempted to initialize logging when it was already initialized.";
249
250#if defined(OS_POSIX) && defined(IPC_MESSAGE_LOG_ENABLED)
[email protected]21fa3a12010-12-08 23:34:16251 IPC::Logging::set_log_function_map(&g_log_function_mapping);
[email protected]77ee1b12010-06-14 16:21:30252#endif
253
254 FilePath log_path = GetLogFileName();
[email protected]c7d5da992010-10-28 00:20:21255
[email protected]77ee1b12010-06-14 16:21:30256#if defined(OS_CHROMEOS)
[email protected]0750b1442010-11-02 21:59:37257 // For BWSI (Incognito) logins, we want to put the logs in the user
258 // profile directory that is created for the temporary session instead
259 // of in the system log directory, for privacy reasons.
260 if (command_line.HasSwitch(switches::kGuestSession))
261 log_path = GetSessionLogFile(command_line);
262
[email protected]c7d5da992010-10-28 00:20:21263 // On ChromeOS we log to the symlink. We force creation of a new
264 // symlink if we've been asked to delete the old log, since that
265 // indicates the start of a new session.
266 FilePath target_path = SetUpSymlinkIfNeeded(
267 log_path, delete_old_log_file == logging::DELETE_OLD_LOG_FILE);
268
269 // Because ChromeOS manages the move to a new session by redirecting
270 // the link, it shouldn't remove the old file in the logging code,
271 // since that will remove the newly created link instead.
272 delete_old_log_file = logging::APPEND_TO_OLD_LOG_FILE;
[email protected]77ee1b12010-06-14 16:21:30273#endif
274
[email protected]7c10f7552011-01-11 01:03:36275 logging::DcheckState dcheck_state =
276 command_line.HasSwitch(switches::kEnableDCHECK) ?
277 logging::ENABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS :
278 logging::DISABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS;
279
[email protected]c7d5da992010-10-28 00:20:21280 bool success = InitLogging(log_path.value().c_str(),
281 DetermineLogMode(command_line),
282 logging::LOCK_LOG_FILE,
[email protected]7c10f7552011-01-11 01:03:36283 delete_old_log_file,
284 dcheck_state);
[email protected]c7d5da992010-10-28 00:20:21285
286#if defined(OS_CHROMEOS)
287 if (!success) {
288 PLOG(ERROR) << "Unable to initialize logging to " << log_path.value()
289 << " (which should be a link to " << target_path.value() << ")";
290 RemoveSymlinkAndLog(log_path, target_path);
291 return;
292 }
293#else
294 if (!success) {
295 PLOG(ERROR) << "Unable to initialize logging to " << log_path.value();
296 return;
297 }
298#endif
initial.commit09911bf2008-07-26 23:55:29299
[email protected]81e0a852010-08-17 00:38:12300 // Default to showing error dialogs.
301 if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kNoErrorDialogs))
302 logging::SetShowErrorDialogs(true);
303
initial.commit09911bf2008-07-26 23:55:29304 // we want process and thread IDs because we have a lot of things running
[email protected]c7d5da992010-10-28 00:20:21305 logging::SetLogItems(true, // enable_process_id
306 true, // enable_thread_id
307 false, // enable_timestamp
308 true); // enable_tickcount
initial.commit09911bf2008-07-26 23:55:29309
310 // We call running in unattended mode "headless", and allow
311 // headless mode to be configured either by the Environment
312 // Variable or by the Command Line Switch. This is for
313 // automated test purposes.
[email protected]76b90d312010-08-03 03:00:50314 scoped_ptr<base::Environment> env(base::Environment::Create());
[email protected]9432ade2010-08-04 23:43:20315 if (env->HasVar(env_vars::kHeadless) ||
initial.commit09911bf2008-07-26 23:55:29316 command_line.HasSwitch(switches::kNoErrorDialogs))
317 SuppressDialogs();
318
[email protected]3deeb6b32010-10-05 21:53:52319 // Use a minimum log level if the command line asks for one,
320 // otherwise leave it at the default level (INFO).
321 if (command_line.HasSwitch(switches::kLoggingLevel)) {
322 std::string log_level = command_line.GetSwitchValueASCII(
323 switches::kLoggingLevel);
324 int level = 0;
325 if (base::StringToInt(log_level, &level) &&
326 level >= 0 && level < LOG_NUM_SEVERITIES) {
[email protected]bb5185c52008-08-29 19:51:06327 logging::SetMinLogLevel(level);
[email protected]3deeb6b32010-10-05 21:53:52328 } else {
329 LOG(WARNING) << "Bad log level: " << log_level;
330 }
[email protected]bb5185c52008-08-29 19:51:06331 }
332
[email protected]2b07b8412009-11-25 15:26:34333#if defined(OS_WIN)
334 // Enable trace control and transport through event tracing for Windows.
[email protected]9432ade2010-08-04 23:43:20335 if (env->HasVar(env_vars::kEtwLogging))
[email protected]2b07b8412009-11-25 15:26:34336 logging::LogEventProvider::Initialize(kChromeTraceProviderName);
337#endif
338
initial.commit09911bf2008-07-26 23:55:29339 chrome_logging_initialized_ = true;
340}
341
342// This is a no-op, but we'll keep it around in case
343// we need to do more cleanup in the future.
344void CleanupChromeLogging() {
345 DCHECK(chrome_logging_initialized_) <<
346 "Attempted to clean up logging when it wasn't initialized.";
347
348 CloseLogFile();
349
350 chrome_logging_initialized_ = false;
[email protected]30f547c2010-08-04 01:00:01351 chrome_logging_redirected_ = false;
initial.commit09911bf2008-07-26 23:55:29352}
353
[email protected]73e23482009-08-13 00:26:58354FilePath GetLogFileName() {
[email protected]c83dd912010-04-06 18:50:51355 std::string filename;
[email protected]76b90d312010-08-03 03:00:50356 scoped_ptr<base::Environment> env(base::Environment::Create());
[email protected]3ba7e082010-08-07 02:57:59357 if (env->GetVar(env_vars::kLogFileName, &filename) && !filename.empty()) {
[email protected]c83dd912010-04-06 18:50:51358#if defined(OS_WIN)
359 return FilePath(UTF8ToWide(filename).c_str());
360#elif defined(OS_POSIX)
361 return FilePath(filename.c_str());
362#endif
363 }
initial.commit09911bf2008-07-26 23:55:29364
[email protected]73e23482009-08-13 00:26:58365 const FilePath log_filename(FILE_PATH_LITERAL("chrome_debug.log"));
366 FilePath log_path;
initial.commit09911bf2008-07-26 23:55:29367
368 if (PathService::Get(chrome::DIR_LOGS, &log_path)) {
[email protected]73e23482009-08-13 00:26:58369 log_path = log_path.Append(log_filename);
initial.commit09911bf2008-07-26 23:55:29370 return log_path;
371 } else {
372 // error with path service, just use some default file somewhere
373 return log_filename;
374 }
375}
376
377bool DialogsAreSuppressed() {
378 return dialogs_are_suppressed_;
379}
380
381size_t GetFatalAssertions(AssertionList* assertions) {
382 // In this function, we don't assume that assertions is non-null, so
383 // that if you just want an assertion count, you can pass in NULL.
384 if (assertions)
385 assertions->clear();
386 size_t assertion_count = 0;
387
388 std::ifstream log_file;
[email protected]73e23482009-08-13 00:26:58389 log_file.open(GetLogFileName().value().c_str());
initial.commit09911bf2008-07-26 23:55:29390 if (!log_file.is_open())
391 return 0;
392
393 std::string utf8_line;
394 std::wstring wide_line;
[email protected]4a3dab22009-11-11 17:36:50395 while (!log_file.eof()) {
initial.commit09911bf2008-07-26 23:55:29396 getline(log_file, utf8_line);
397 if (utf8_line.find(":FATAL:") != std::string::npos) {
398 wide_line = UTF8ToWide(utf8_line);
399 if (assertions)
400 assertions->push_back(wide_line);
401 ++assertion_count;
402 }
403 }
404 log_file.close();
405
406 return assertion_count;
407}
408
[email protected]c83dd912010-04-06 18:50:51409} // namespace logging