blob: 116b37a64414ac1d7514965ee3a3d5ceb27471f8 [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]77ee1b12010-06-14 16:21:3043#include "base/time.h"
[email protected]4197e092010-03-10 00:35:0544#include "base/utf_string_conversions.h"
initial.commit09911bf2008-07-26 23:55:2945#include "chrome/common/chrome_paths.h"
46#include "chrome/common/chrome_switches.h"
initial.commit09911bf2008-07-26 23:55:2947#include "chrome/common/env_vars.h"
[email protected]51d5e162009-07-27 19:23:5448#include "ipc/ipc_logging.h"
[email protected]2b07b8412009-11-25 15:26:3449#if defined(OS_WIN)
50#include "base/logging_win.h"
51#include <initguid.h>
52#endif
initial.commit09911bf2008-07-26 23:55:2953
54// When true, this means that error dialogs should not be shown.
55static 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().
59static bool chrome_logging_initialized_ = false;
60
[email protected]30f547c2010-08-04 01:00:0161// This should be true for exactly the period between the end of
62// InitChromeLogging() and the beginning of CleanupChromeLogging().
63static bool chrome_logging_redirected_ = false;
64
[email protected]2b07b8412009-11-25 15:26:3465#if defined(OS_WIN)
66// {7FE69228-633E-4f06-80C1-527FEA23E3A7}
67DEFINE_GUID(kChromeTraceProviderName,
68 0x7fe69228, 0x633e, 0x4f06, 0x80, 0xc1, 0x52, 0x7f, 0xea, 0x23, 0xe3, 0xa7);
69#endif
70
initial.commit09911bf2008-07-26 23:55:2971// Assertion handler for logging errors that occur when dialogs are
72// silenced. To record a new error, pass the log string associated
73// with that error in the str parameter.
[email protected]0eb9f4342008-11-21 18:48:5274MSVC_DISABLE_OPTIMIZE();
initial.commit09911bf2008-07-26 23:55:2975static void SilentRuntimeAssertHandler(const std::string& str) {
[email protected]58580352010-10-26 04:07:5076 base::debug::BreakDebugger();
initial.commit09911bf2008-07-26 23:55:2977}
[email protected]6cc67d7f2009-02-12 06:48:2478static void SilentRuntimeReportHandler(const std::string& str) {
79}
[email protected]0eb9f4342008-11-21 18:48:5280MSVC_ENABLE_OPTIMIZE();
initial.commit09911bf2008-07-26 23:55:2981
82// Suppresses error/assertion dialogs and enables the logging of
83// those errors into silenced_errors_.
84static void SuppressDialogs() {
85 if (dialogs_are_suppressed_)
86 return;
87
88 logging::SetLogAssertHandler(SilentRuntimeAssertHandler);
[email protected]6cc67d7f2009-02-12 06:48:2489 logging::SetLogReportHandler(SilentRuntimeReportHandler);
initial.commit09911bf2008-07-26 23:55:2990
[email protected]ec657802008-11-07 20:05:3991#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:2992 UINT new_flags = SEM_FAILCRITICALERRORS |
93 SEM_NOGPFAULTERRORBOX |
94 SEM_NOOPENFILEERRORBOX;
95
96 // Preserve existing error mode, as discussed at http://t/dmea
97 UINT existing_flags = SetErrorMode(new_flags);
98 SetErrorMode(existing_flags | new_flags);
[email protected]ec657802008-11-07 20:05:3999#endif
initial.commit09911bf2008-07-26 23:55:29100
101 dialogs_are_suppressed_ = true;
102}
103
104namespace logging {
105
[email protected]77ee1b12010-06-14 16:21:30106LoggingDestination DetermineLogMode(const CommandLine& command_line) {
initial.commit09911bf2008-07-26 23:55:29107 // only use OutputDebugString in debug mode
108#ifdef NDEBUG
109 bool enable_logging = false;
[email protected]b7e0a2a2009-10-13 02:07:25110 const char *kInvertLoggingSwitch = switches::kEnableLogging;
initial.commit09911bf2008-07-26 23:55:29111 const logging::LoggingDestination kDefaultLoggingMode =
112 logging::LOG_ONLY_TO_FILE;
113#else
114 bool enable_logging = true;
[email protected]b7e0a2a2009-10-13 02:07:25115 const char *kInvertLoggingSwitch = switches::kDisableLogging;
initial.commit09911bf2008-07-26 23:55:29116 const logging::LoggingDestination kDefaultLoggingMode =
117 logging::LOG_TO_BOTH_FILE_AND_SYSTEM_DEBUG_LOG;
118#endif
119
120 if (command_line.HasSwitch(kInvertLoggingSwitch))
121 enable_logging = !enable_logging;
122
123 logging::LoggingDestination log_mode;
124 if (enable_logging) {
[email protected]3bba9582009-08-05 01:29:12125 // Let --enable-logging=stderr force only stderr, particularly useful for
126 // non-debug builds where otherwise you can't get logs to stderr at all.
[email protected]c4e52f0d2009-11-06 19:55:16127 if (command_line.GetSwitchValueASCII(switches::kEnableLogging) == "stderr")
[email protected]3bba9582009-08-05 01:29:12128 log_mode = logging::LOG_ONLY_TO_SYSTEM_DEBUG_LOG;
129 else
130 log_mode = kDefaultLoggingMode;
initial.commit09911bf2008-07-26 23:55:29131 } else {
132 log_mode = logging::LOG_NONE;
133 }
[email protected]77ee1b12010-06-14 16:21:30134 return log_mode;
135}
initial.commit09911bf2008-07-26 23:55:29136
[email protected]77ee1b12010-06-14 16:21:30137#if defined(OS_CHROMEOS)
[email protected]c7d5da992010-10-28 00:20:21138namespace {
139FilePath GenerateTimestampedName(const FilePath& base_path,
140 base::Time timestamp) {
[email protected]77ee1b12010-06-14 16:21:30141 base::Time::Exploded time_deets;
142 timestamp.LocalExplode(&time_deets);
143 std::string suffix = StringPrintf("_%02d%02d%02d-%02d%02d%02d",
144 time_deets.year,
145 time_deets.month,
146 time_deets.day_of_month,
147 time_deets.hour,
148 time_deets.minute,
149 time_deets.second);
[email protected]c7d5da992010-10-28 00:20:21150 return base_path.InsertBeforeExtension(suffix);
[email protected]77ee1b12010-06-14 16:21:30151}
152
[email protected]c7d5da992010-10-28 00:20:21153FilePath SetUpSymlinkIfNeeded(const FilePath& symlink_path, bool new_log) {
[email protected]f25b33a2010-10-28 20:15:51154 DCHECK(!symlink_path.empty());
155
[email protected]c7d5da992010-10-28 00:20:21156 // If not starting a new log, then just log through the existing
157 // symlink, but if the symlink doesn't exist, create it. If
158 // starting a new log, then delete the old symlink and make a new
159 // one to a fresh log file.
160 FilePath target_path;
161 if (new_log || !file_util::PathExists(symlink_path)) {
162 target_path = GenerateTimestampedName(symlink_path, base::Time::Now());
163
164 // We don't care if the unlink fails; we're going to continue anyway.
165 if (unlink(symlink_path.value().c_str()) == -1) {
166 if (new_log) // only warn if we might expect it to succeed.
167 PLOG(WARNING) << "Unable to unlink " << symlink_path.value();
168 }
169 if (symlink(target_path.value().c_str(),
170 symlink_path.value().c_str()) == -1) {
171 PLOG(ERROR) << "Unable to create symlink " << symlink_path.value()
172 << " pointing at " << target_path.value();
173 }
174 } else {
[email protected]f25b33a2010-10-28 20:15:51175 char buf[PATH_MAX];
176 ssize_t count = readlink(symlink_path.value().c_str(), buf, arraysize(buf));
[email protected]c7d5da992010-10-28 00:20:21177 if (count > 0) {
[email protected]f25b33a2010-10-28 20:15:51178 target_path = FilePath(FilePath::StringType(buf, count));
[email protected]c7d5da992010-10-28 00:20:21179 } else {
180 PLOG(ERROR) << "Unable to read symlink " << symlink_path.value();
181 }
182 }
183 return target_path;
184}
185
186void RemoveSymlinkAndLog(const FilePath& link_path,
187 const FilePath& target_path) {
188 if (::unlink(link_path.value().c_str()) == -1)
189 PLOG(WARNING) << "Unable to unlink symlink " << link_path.value();
190 if (::unlink(target_path.value().c_str()) == -1)
191 PLOG(WARNING) << "Unable to unlink log file " << target_path.value();
192}
193
194} // anonymous namespace
195
[email protected]77ee1b12010-06-14 16:21:30196void RedirectChromeLogging(const FilePath& new_log_dir,
[email protected]c7d5da992010-10-28 00:20:21197 const CommandLine& command_line) {
[email protected]30f547c2010-08-04 01:00:01198 DCHECK(!chrome_logging_redirected_) <<
199 "Attempted to redirect logging when it was already initialized.";
[email protected]c7d5da992010-10-28 00:20:21200 FilePath orig_log_path = GetLogFileName();
201 FilePath log_path = new_log_dir.Append(orig_log_path.BaseName());
202
203 // Always force a new symlink when redirecting.
204 FilePath target_path = SetUpSymlinkIfNeeded(log_path, true);
205
206 // ChromeOS always logs through the symlink, so it shouldn't be
207 // deleted if it already exists.
208 if (!InitLogging(log_path.value().c_str(),
209 DetermineLogMode(command_line),
210 logging::LOCK_LOG_FILE,
211 logging::APPEND_TO_OLD_LOG_FILE)) {
212 LOG(ERROR) << "Unable to initialize logging to " << log_path.value();
213 RemoveSymlinkAndLog(log_path, target_path);
214 } else {
215 chrome_logging_redirected_ = true;
216 }
[email protected]77ee1b12010-06-14 16:21:30217}
218#endif
219
220void InitChromeLogging(const CommandLine& command_line,
221 OldFileDeletionState delete_old_log_file) {
222 DCHECK(!chrome_logging_initialized_) <<
223 "Attempted to initialize logging when it was already initialized.";
224
225#if defined(OS_POSIX) && defined(IPC_MESSAGE_LOG_ENABLED)
226 IPC::Logging::SetLoggerFunctions(g_log_function_mapping);
227#endif
228
229 FilePath log_path = GetLogFileName();
[email protected]c7d5da992010-10-28 00:20:21230
[email protected]77ee1b12010-06-14 16:21:30231#if defined(OS_CHROMEOS)
[email protected]c7d5da992010-10-28 00:20:21232 // On ChromeOS we log to the symlink. We force creation of a new
233 // symlink if we've been asked to delete the old log, since that
234 // indicates the start of a new session.
235 FilePath target_path = SetUpSymlinkIfNeeded(
236 log_path, delete_old_log_file == logging::DELETE_OLD_LOG_FILE);
237
238 // Because ChromeOS manages the move to a new session by redirecting
239 // the link, it shouldn't remove the old file in the logging code,
240 // since that will remove the newly created link instead.
241 delete_old_log_file = logging::APPEND_TO_OLD_LOG_FILE;
[email protected]77ee1b12010-06-14 16:21:30242#endif
243
[email protected]c7d5da992010-10-28 00:20:21244 bool success = InitLogging(log_path.value().c_str(),
245 DetermineLogMode(command_line),
246 logging::LOCK_LOG_FILE,
247 delete_old_log_file);
248
249#if defined(OS_CHROMEOS)
250 if (!success) {
251 PLOG(ERROR) << "Unable to initialize logging to " << log_path.value()
252 << " (which should be a link to " << target_path.value() << ")";
253 RemoveSymlinkAndLog(log_path, target_path);
254 return;
255 }
256#else
257 if (!success) {
258 PLOG(ERROR) << "Unable to initialize logging to " << log_path.value();
259 return;
260 }
261#endif
initial.commit09911bf2008-07-26 23:55:29262
[email protected]81e0a852010-08-17 00:38:12263 // Default to showing error dialogs.
264 if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kNoErrorDialogs))
265 logging::SetShowErrorDialogs(true);
266
initial.commit09911bf2008-07-26 23:55:29267 // we want process and thread IDs because we have a lot of things running
[email protected]c7d5da992010-10-28 00:20:21268 logging::SetLogItems(true, // enable_process_id
269 true, // enable_thread_id
270 false, // enable_timestamp
271 true); // enable_tickcount
initial.commit09911bf2008-07-26 23:55:29272
273 // We call running in unattended mode "headless", and allow
274 // headless mode to be configured either by the Environment
275 // Variable or by the Command Line Switch. This is for
276 // automated test purposes.
[email protected]76b90d312010-08-03 03:00:50277 scoped_ptr<base::Environment> env(base::Environment::Create());
[email protected]9432ade2010-08-04 23:43:20278 if (env->HasVar(env_vars::kHeadless) ||
initial.commit09911bf2008-07-26 23:55:29279 command_line.HasSwitch(switches::kNoErrorDialogs))
280 SuppressDialogs();
281
[email protected]3deeb6b32010-10-05 21:53:52282 // Use a minimum log level if the command line asks for one,
283 // otherwise leave it at the default level (INFO).
284 if (command_line.HasSwitch(switches::kLoggingLevel)) {
285 std::string log_level = command_line.GetSwitchValueASCII(
286 switches::kLoggingLevel);
287 int level = 0;
288 if (base::StringToInt(log_level, &level) &&
289 level >= 0 && level < LOG_NUM_SEVERITIES) {
[email protected]bb5185c52008-08-29 19:51:06290 logging::SetMinLogLevel(level);
[email protected]3deeb6b32010-10-05 21:53:52291 } else {
292 LOG(WARNING) << "Bad log level: " << log_level;
293 }
[email protected]bb5185c52008-08-29 19:51:06294 }
295
[email protected]2b07b8412009-11-25 15:26:34296#if defined(OS_WIN)
297 // Enable trace control and transport through event tracing for Windows.
[email protected]9432ade2010-08-04 23:43:20298 if (env->HasVar(env_vars::kEtwLogging))
[email protected]2b07b8412009-11-25 15:26:34299 logging::LogEventProvider::Initialize(kChromeTraceProviderName);
300#endif
301
initial.commit09911bf2008-07-26 23:55:29302 chrome_logging_initialized_ = true;
303}
304
305// This is a no-op, but we'll keep it around in case
306// we need to do more cleanup in the future.
307void CleanupChromeLogging() {
308 DCHECK(chrome_logging_initialized_) <<
309 "Attempted to clean up logging when it wasn't initialized.";
310
311 CloseLogFile();
312
313 chrome_logging_initialized_ = false;
[email protected]30f547c2010-08-04 01:00:01314 chrome_logging_redirected_ = false;
initial.commit09911bf2008-07-26 23:55:29315}
316
[email protected]73e23482009-08-13 00:26:58317FilePath GetLogFileName() {
[email protected]c83dd912010-04-06 18:50:51318 std::string filename;
[email protected]76b90d312010-08-03 03:00:50319 scoped_ptr<base::Environment> env(base::Environment::Create());
[email protected]3ba7e082010-08-07 02:57:59320 if (env->GetVar(env_vars::kLogFileName, &filename) && !filename.empty()) {
[email protected]c83dd912010-04-06 18:50:51321#if defined(OS_WIN)
322 return FilePath(UTF8ToWide(filename).c_str());
323#elif defined(OS_POSIX)
324 return FilePath(filename.c_str());
325#endif
326 }
initial.commit09911bf2008-07-26 23:55:29327
[email protected]73e23482009-08-13 00:26:58328 const FilePath log_filename(FILE_PATH_LITERAL("chrome_debug.log"));
329 FilePath log_path;
initial.commit09911bf2008-07-26 23:55:29330
331 if (PathService::Get(chrome::DIR_LOGS, &log_path)) {
[email protected]73e23482009-08-13 00:26:58332 log_path = log_path.Append(log_filename);
initial.commit09911bf2008-07-26 23:55:29333 return log_path;
334 } else {
335 // error with path service, just use some default file somewhere
336 return log_filename;
337 }
338}
339
340bool DialogsAreSuppressed() {
341 return dialogs_are_suppressed_;
342}
343
344size_t GetFatalAssertions(AssertionList* assertions) {
345 // In this function, we don't assume that assertions is non-null, so
346 // that if you just want an assertion count, you can pass in NULL.
347 if (assertions)
348 assertions->clear();
349 size_t assertion_count = 0;
350
351 std::ifstream log_file;
[email protected]73e23482009-08-13 00:26:58352 log_file.open(GetLogFileName().value().c_str());
initial.commit09911bf2008-07-26 23:55:29353 if (!log_file.is_open())
354 return 0;
355
356 std::string utf8_line;
357 std::wstring wide_line;
[email protected]4a3dab22009-11-11 17:36:50358 while (!log_file.eof()) {
initial.commit09911bf2008-07-26 23:55:29359 getline(log_file, utf8_line);
360 if (utf8_line.find(":FATAL:") != std::string::npos) {
361 wide_line = UTF8ToWide(utf8_line);
362 if (assertions)
363 assertions->push_back(wide_line);
364 ++assertion_count;
365 }
366 }
367 log_file.close();
368
369 return assertion_count;
370}
371
[email protected]c83dd912010-04-06 18:50:51372} // namespace logging