blob: 5b0987bf22d7f459234c43d68027d0122d690813 [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]ec657802008-11-07 20:05:3935#include "base/debug_util.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]ec657802008-11-07 20:05:3976 DebugUtil::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)
138void SetUpSymlink(const FilePath& symlink_path, const FilePath& new_log_path) {
[email protected]dcc56de2010-06-14 21:24:04139 // We don't care if the unlink fails; we're going to continue anyway.
140 if (unlink(symlink_path.value().c_str()) == -1)
141 PLOG(WARNING) << "Unable to unlink " << symlink_path.value();
[email protected]77ee1b12010-06-14 16:21:30142 if (symlink(new_log_path.value().c_str(),
143 symlink_path.value().c_str()) == -1) {
144 PLOG(ERROR) << "Unable to create symlink " << symlink_path.value()
145 << " pointing at " << new_log_path.value();
146 }
147}
148
149FilePath TimestampLog(const FilePath& new_log_file, base::Time timestamp) {
150 base::Time::Exploded time_deets;
151 timestamp.LocalExplode(&time_deets);
152 std::string suffix = StringPrintf("_%02d%02d%02d-%02d%02d%02d",
153 time_deets.year,
154 time_deets.month,
155 time_deets.day_of_month,
156 time_deets.hour,
157 time_deets.minute,
158 time_deets.second);
159 FilePath new_log_path = new_log_file.InsertBeforeExtension(suffix);
160 SetUpSymlink(new_log_file, new_log_path);
161
162 return new_log_path;
163}
164
165void RedirectChromeLogging(const FilePath& new_log_dir,
166 const CommandLine& command_line,
167 OldFileDeletionState delete_old_log_file) {
[email protected]30f547c2010-08-04 01:00:01168 DCHECK(!chrome_logging_redirected_) <<
169 "Attempted to redirect logging when it was already initialized.";
[email protected]77ee1b12010-06-14 16:21:30170 FilePath log_file_name = GetLogFileName().BaseName();
171 FilePath new_log_path =
172 TimestampLog(new_log_dir.Append(log_file_name), base::Time::Now());
173 InitLogging(new_log_path.value().c_str(),
174 DetermineLogMode(command_line),
175 logging::LOCK_LOG_FILE,
176 delete_old_log_file);
[email protected]30f547c2010-08-04 01:00:01177 chrome_logging_redirected_ = true;
[email protected]77ee1b12010-06-14 16:21:30178}
179#endif
180
181void InitChromeLogging(const CommandLine& command_line,
182 OldFileDeletionState delete_old_log_file) {
183 DCHECK(!chrome_logging_initialized_) <<
184 "Attempted to initialize logging when it was already initialized.";
185
186#if defined(OS_POSIX) && defined(IPC_MESSAGE_LOG_ENABLED)
187 IPC::Logging::SetLoggerFunctions(g_log_function_mapping);
188#endif
189
190 FilePath log_path = GetLogFileName();
191#if defined(OS_CHROMEOS)
192 log_path = TimestampLog(log_path, base::Time::Now());
193#endif
194
195 logging::InitLogging(log_path.value().c_str(),
196 DetermineLogMode(command_line),
initial.commit09911bf2008-07-26 23:55:29197 logging::LOCK_LOG_FILE,
198 delete_old_log_file);
199
[email protected]81e0a852010-08-17 00:38:12200 // Default to showing error dialogs.
201 if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kNoErrorDialogs))
202 logging::SetShowErrorDialogs(true);
203
initial.commit09911bf2008-07-26 23:55:29204 // we want process and thread IDs because we have a lot of things running
205 logging::SetLogItems(true, true, false, true);
206
207 // We call running in unattended mode "headless", and allow
208 // headless mode to be configured either by the Environment
209 // Variable or by the Command Line Switch. This is for
210 // automated test purposes.
[email protected]76b90d312010-08-03 03:00:50211 scoped_ptr<base::Environment> env(base::Environment::Create());
[email protected]9432ade2010-08-04 23:43:20212 if (env->HasVar(env_vars::kHeadless) ||
initial.commit09911bf2008-07-26 23:55:29213 command_line.HasSwitch(switches::kNoErrorDialogs))
214 SuppressDialogs();
215
[email protected]c4e52f0d2009-11-06 19:55:16216 std::string log_filter_prefix =
217 command_line.GetSwitchValueASCII(switches::kLogFilterPrefix);
218 logging::SetLogFilterPrefix(log_filter_prefix.c_str());
initial.commit09911bf2008-07-26 23:55:29219
[email protected]bb5185c52008-08-29 19:51:06220 // Use a minimum log level if the command line has one, otherwise set the
221 // default to LOG_WARNING.
[email protected]c4e52f0d2009-11-06 19:55:16222 std::string log_level = command_line.GetSwitchValueASCII(
223 switches::kLoggingLevel);
[email protected]bb5185c52008-08-29 19:51:06224 int level = 0;
[email protected]528c56d2010-07-30 19:28:44225 if (base::StringToInt(log_level, &level)) {
[email protected]bb5185c52008-08-29 19:51:06226 if ((level >= 0) && (level < LOG_NUM_SEVERITIES))
227 logging::SetMinLogLevel(level);
228 } else {
229 logging::SetMinLogLevel(LOG_WARNING);
230 }
231
[email protected]2b07b8412009-11-25 15:26:34232#if defined(OS_WIN)
233 // Enable trace control and transport through event tracing for Windows.
[email protected]9432ade2010-08-04 23:43:20234 if (env->HasVar(env_vars::kEtwLogging))
[email protected]2b07b8412009-11-25 15:26:34235 logging::LogEventProvider::Initialize(kChromeTraceProviderName);
236#endif
237
initial.commit09911bf2008-07-26 23:55:29238 chrome_logging_initialized_ = true;
239}
240
241// This is a no-op, but we'll keep it around in case
242// we need to do more cleanup in the future.
243void CleanupChromeLogging() {
244 DCHECK(chrome_logging_initialized_) <<
245 "Attempted to clean up logging when it wasn't initialized.";
246
247 CloseLogFile();
248
249 chrome_logging_initialized_ = false;
[email protected]30f547c2010-08-04 01:00:01250 chrome_logging_redirected_ = false;
initial.commit09911bf2008-07-26 23:55:29251}
252
[email protected]73e23482009-08-13 00:26:58253FilePath GetLogFileName() {
[email protected]c83dd912010-04-06 18:50:51254 std::string filename;
[email protected]76b90d312010-08-03 03:00:50255 scoped_ptr<base::Environment> env(base::Environment::Create());
[email protected]3ba7e082010-08-07 02:57:59256 if (env->GetVar(env_vars::kLogFileName, &filename) && !filename.empty()) {
[email protected]c83dd912010-04-06 18:50:51257#if defined(OS_WIN)
258 return FilePath(UTF8ToWide(filename).c_str());
259#elif defined(OS_POSIX)
260 return FilePath(filename.c_str());
261#endif
262 }
initial.commit09911bf2008-07-26 23:55:29263
[email protected]73e23482009-08-13 00:26:58264 const FilePath log_filename(FILE_PATH_LITERAL("chrome_debug.log"));
265 FilePath log_path;
initial.commit09911bf2008-07-26 23:55:29266
267 if (PathService::Get(chrome::DIR_LOGS, &log_path)) {
[email protected]73e23482009-08-13 00:26:58268 log_path = log_path.Append(log_filename);
initial.commit09911bf2008-07-26 23:55:29269 return log_path;
270 } else {
271 // error with path service, just use some default file somewhere
272 return log_filename;
273 }
274}
275
276bool DialogsAreSuppressed() {
277 return dialogs_are_suppressed_;
278}
279
280size_t GetFatalAssertions(AssertionList* assertions) {
281 // In this function, we don't assume that assertions is non-null, so
282 // that if you just want an assertion count, you can pass in NULL.
283 if (assertions)
284 assertions->clear();
285 size_t assertion_count = 0;
286
287 std::ifstream log_file;
[email protected]73e23482009-08-13 00:26:58288 log_file.open(GetLogFileName().value().c_str());
initial.commit09911bf2008-07-26 23:55:29289 if (!log_file.is_open())
290 return 0;
291
292 std::string utf8_line;
293 std::wstring wide_line;
[email protected]4a3dab22009-11-11 17:36:50294 while (!log_file.eof()) {
initial.commit09911bf2008-07-26 23:55:29295 getline(log_file, utf8_line);
296 if (utf8_line.find(":FATAL:") != std::string::npos) {
297 wide_line = UTF8ToWide(utf8_line);
298 if (assertions)
299 assertions->push_back(wide_line);
300 ++assertion_count;
301 }
302 }
303 log_file.close();
304
305 return assertion_count;
306}
307
[email protected]c83dd912010-04-06 18:50:51308} // namespace logging