blob: b1333a22b1b251944ca44fd7b12d9bd90d2de2b0 [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
29#include <iostream>
30#include <fstream>
31
32#include "chrome/common/logging_chrome.h"
33
34#include "base/command_line.h"
[email protected]0eb9f4342008-11-21 18:48:5235#include "base/compiler_specific.h"
[email protected]ec657802008-11-07 20:05:3936#include "base/debug_util.h"
[email protected]c83dd912010-04-06 18:50:5137#include "base/env_var.h"
[email protected]73e23482009-08-13 00:26:5838#include "base/file_path.h"
initial.commit09911bf2008-07-26 23:55:2939#include "base/file_util.h"
40#include "base/logging.h"
41#include "base/path_service.h"
[email protected]77ee1b12010-06-14 16:21:3042#include "base/time.h"
[email protected]4197e092010-03-10 00:35:0543#include "base/utf_string_conversions.h"
initial.commit09911bf2008-07-26 23:55:2944#include "chrome/common/chrome_paths.h"
45#include "chrome/common/chrome_switches.h"
initial.commit09911bf2008-07-26 23:55:2946#include "chrome/common/env_vars.h"
[email protected]51d5e162009-07-27 19:23:5447#include "ipc/ipc_logging.h"
[email protected]2b07b8412009-11-25 15:26:3448#if defined(OS_WIN)
49#include "base/logging_win.h"
50#include <initguid.h>
51#endif
initial.commit09911bf2008-07-26 23:55:2952
53// When true, this means that error dialogs should not be shown.
54static bool dialogs_are_suppressed_ = false;
55
56// This should be true for exactly the period between the end of
57// InitChromeLogging() and the beginning of CleanupChromeLogging().
58static bool chrome_logging_initialized_ = false;
59
[email protected]2b07b8412009-11-25 15:26:3460#if defined(OS_WIN)
61// {7FE69228-633E-4f06-80C1-527FEA23E3A7}
62DEFINE_GUID(kChromeTraceProviderName,
63 0x7fe69228, 0x633e, 0x4f06, 0x80, 0xc1, 0x52, 0x7f, 0xea, 0x23, 0xe3, 0xa7);
64#endif
65
initial.commit09911bf2008-07-26 23:55:2966// Assertion handler for logging errors that occur when dialogs are
67// silenced. To record a new error, pass the log string associated
68// with that error in the str parameter.
[email protected]0eb9f4342008-11-21 18:48:5269MSVC_DISABLE_OPTIMIZE();
initial.commit09911bf2008-07-26 23:55:2970static void SilentRuntimeAssertHandler(const std::string& str) {
[email protected]ec657802008-11-07 20:05:3971 DebugUtil::BreakDebugger();
initial.commit09911bf2008-07-26 23:55:2972}
[email protected]6cc67d7f2009-02-12 06:48:2473static void SilentRuntimeReportHandler(const std::string& str) {
74}
[email protected]0eb9f4342008-11-21 18:48:5275MSVC_ENABLE_OPTIMIZE();
initial.commit09911bf2008-07-26 23:55:2976
77// Suppresses error/assertion dialogs and enables the logging of
78// those errors into silenced_errors_.
79static void SuppressDialogs() {
80 if (dialogs_are_suppressed_)
81 return;
82
83 logging::SetLogAssertHandler(SilentRuntimeAssertHandler);
[email protected]6cc67d7f2009-02-12 06:48:2484 logging::SetLogReportHandler(SilentRuntimeReportHandler);
initial.commit09911bf2008-07-26 23:55:2985
[email protected]ec657802008-11-07 20:05:3986#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:2987 UINT new_flags = SEM_FAILCRITICALERRORS |
88 SEM_NOGPFAULTERRORBOX |
89 SEM_NOOPENFILEERRORBOX;
90
91 // Preserve existing error mode, as discussed at http://t/dmea
92 UINT existing_flags = SetErrorMode(new_flags);
93 SetErrorMode(existing_flags | new_flags);
[email protected]ec657802008-11-07 20:05:3994#endif
initial.commit09911bf2008-07-26 23:55:2995
96 dialogs_are_suppressed_ = true;
97}
98
99namespace logging {
100
[email protected]77ee1b12010-06-14 16:21:30101LoggingDestination DetermineLogMode(const CommandLine& command_line) {
initial.commit09911bf2008-07-26 23:55:29102 // only use OutputDebugString in debug mode
103#ifdef NDEBUG
104 bool enable_logging = false;
[email protected]b7e0a2a2009-10-13 02:07:25105 const char *kInvertLoggingSwitch = switches::kEnableLogging;
initial.commit09911bf2008-07-26 23:55:29106 const logging::LoggingDestination kDefaultLoggingMode =
107 logging::LOG_ONLY_TO_FILE;
108#else
109 bool enable_logging = true;
[email protected]b7e0a2a2009-10-13 02:07:25110 const char *kInvertLoggingSwitch = switches::kDisableLogging;
initial.commit09911bf2008-07-26 23:55:29111 const logging::LoggingDestination kDefaultLoggingMode =
112 logging::LOG_TO_BOTH_FILE_AND_SYSTEM_DEBUG_LOG;
113#endif
114
115 if (command_line.HasSwitch(kInvertLoggingSwitch))
116 enable_logging = !enable_logging;
117
118 logging::LoggingDestination log_mode;
119 if (enable_logging) {
[email protected]3bba9582009-08-05 01:29:12120 // Let --enable-logging=stderr force only stderr, particularly useful for
121 // non-debug builds where otherwise you can't get logs to stderr at all.
[email protected]c4e52f0d2009-11-06 19:55:16122 if (command_line.GetSwitchValueASCII(switches::kEnableLogging) == "stderr")
[email protected]3bba9582009-08-05 01:29:12123 log_mode = logging::LOG_ONLY_TO_SYSTEM_DEBUG_LOG;
124 else
125 log_mode = kDefaultLoggingMode;
initial.commit09911bf2008-07-26 23:55:29126 } else {
127 log_mode = logging::LOG_NONE;
128 }
[email protected]77ee1b12010-06-14 16:21:30129 return log_mode;
130}
initial.commit09911bf2008-07-26 23:55:29131
[email protected]77ee1b12010-06-14 16:21:30132#if defined(OS_CHROMEOS)
133void SetUpSymlink(const FilePath& symlink_path, const FilePath& new_log_path) {
[email protected]dcc56de2010-06-14 21:24:04134 // We don't care if the unlink fails; we're going to continue anyway.
135 if (unlink(symlink_path.value().c_str()) == -1)
136 PLOG(WARNING) << "Unable to unlink " << symlink_path.value();
[email protected]77ee1b12010-06-14 16:21:30137 if (symlink(new_log_path.value().c_str(),
138 symlink_path.value().c_str()) == -1) {
139 PLOG(ERROR) << "Unable to create symlink " << symlink_path.value()
140 << " pointing at " << new_log_path.value();
141 }
142}
143
144FilePath TimestampLog(const FilePath& new_log_file, base::Time timestamp) {
145 base::Time::Exploded time_deets;
146 timestamp.LocalExplode(&time_deets);
147 std::string suffix = StringPrintf("_%02d%02d%02d-%02d%02d%02d",
148 time_deets.year,
149 time_deets.month,
150 time_deets.day_of_month,
151 time_deets.hour,
152 time_deets.minute,
153 time_deets.second);
154 FilePath new_log_path = new_log_file.InsertBeforeExtension(suffix);
155 SetUpSymlink(new_log_file, new_log_path);
156
157 return new_log_path;
158}
159
160void RedirectChromeLogging(const FilePath& new_log_dir,
161 const CommandLine& command_line,
162 OldFileDeletionState delete_old_log_file) {
163 FilePath log_file_name = GetLogFileName().BaseName();
164 FilePath new_log_path =
165 TimestampLog(new_log_dir.Append(log_file_name), base::Time::Now());
166 InitLogging(new_log_path.value().c_str(),
167 DetermineLogMode(command_line),
168 logging::LOCK_LOG_FILE,
169 delete_old_log_file);
170}
171#endif
172
173void InitChromeLogging(const CommandLine& command_line,
174 OldFileDeletionState delete_old_log_file) {
175 DCHECK(!chrome_logging_initialized_) <<
176 "Attempted to initialize logging when it was already initialized.";
177
178#if defined(OS_POSIX) && defined(IPC_MESSAGE_LOG_ENABLED)
179 IPC::Logging::SetLoggerFunctions(g_log_function_mapping);
180#endif
181
182 FilePath log_path = GetLogFileName();
183#if defined(OS_CHROMEOS)
184 log_path = TimestampLog(log_path, base::Time::Now());
185#endif
186
187 logging::InitLogging(log_path.value().c_str(),
188 DetermineLogMode(command_line),
initial.commit09911bf2008-07-26 23:55:29189 logging::LOCK_LOG_FILE,
190 delete_old_log_file);
191
192 // we want process and thread IDs because we have a lot of things running
193 logging::SetLogItems(true, true, false, true);
194
195 // We call running in unattended mode "headless", and allow
196 // headless mode to be configured either by the Environment
197 // Variable or by the Command Line Switch. This is for
198 // automated test purposes.
[email protected]c83dd912010-04-06 18:50:51199 scoped_ptr<base::EnvVarGetter> env(base::EnvVarGetter::Create());
200 if (env->HasEnv(env_vars::kHeadless) ||
initial.commit09911bf2008-07-26 23:55:29201 command_line.HasSwitch(switches::kNoErrorDialogs))
202 SuppressDialogs();
203
[email protected]c4e52f0d2009-11-06 19:55:16204 std::string log_filter_prefix =
205 command_line.GetSwitchValueASCII(switches::kLogFilterPrefix);
206 logging::SetLogFilterPrefix(log_filter_prefix.c_str());
initial.commit09911bf2008-07-26 23:55:29207
[email protected]bb5185c52008-08-29 19:51:06208 // Use a minimum log level if the command line has one, otherwise set the
209 // default to LOG_WARNING.
[email protected]c4e52f0d2009-11-06 19:55:16210 std::string log_level = command_line.GetSwitchValueASCII(
211 switches::kLoggingLevel);
[email protected]bb5185c52008-08-29 19:51:06212 int level = 0;
[email protected]c4e52f0d2009-11-06 19:55:16213 if (StringToInt(log_level, &level)) {
[email protected]bb5185c52008-08-29 19:51:06214 if ((level >= 0) && (level < LOG_NUM_SEVERITIES))
215 logging::SetMinLogLevel(level);
216 } else {
217 logging::SetMinLogLevel(LOG_WARNING);
218 }
219
[email protected]2b07b8412009-11-25 15:26:34220#if defined(OS_WIN)
221 // Enable trace control and transport through event tracing for Windows.
[email protected]c83dd912010-04-06 18:50:51222 if (env->HasEnv(env_vars::kEtwLogging))
[email protected]2b07b8412009-11-25 15:26:34223 logging::LogEventProvider::Initialize(kChromeTraceProviderName);
224#endif
225
initial.commit09911bf2008-07-26 23:55:29226 chrome_logging_initialized_ = true;
227}
228
229// This is a no-op, but we'll keep it around in case
230// we need to do more cleanup in the future.
231void CleanupChromeLogging() {
232 DCHECK(chrome_logging_initialized_) <<
233 "Attempted to clean up logging when it wasn't initialized.";
234
235 CloseLogFile();
236
237 chrome_logging_initialized_ = false;
238}
239
[email protected]73e23482009-08-13 00:26:58240FilePath GetLogFileName() {
[email protected]c83dd912010-04-06 18:50:51241 std::string filename;
242 scoped_ptr<base::EnvVarGetter> env(base::EnvVarGetter::Create());
243 if (env->GetEnv(env_vars::kLogFileName, &filename) && !filename.empty()) {
244#if defined(OS_WIN)
245 return FilePath(UTF8ToWide(filename).c_str());
246#elif defined(OS_POSIX)
247 return FilePath(filename.c_str());
248#endif
249 }
initial.commit09911bf2008-07-26 23:55:29250
[email protected]73e23482009-08-13 00:26:58251 const FilePath log_filename(FILE_PATH_LITERAL("chrome_debug.log"));
252 FilePath log_path;
initial.commit09911bf2008-07-26 23:55:29253
254 if (PathService::Get(chrome::DIR_LOGS, &log_path)) {
[email protected]73e23482009-08-13 00:26:58255 log_path = log_path.Append(log_filename);
initial.commit09911bf2008-07-26 23:55:29256 return log_path;
257 } else {
258 // error with path service, just use some default file somewhere
259 return log_filename;
260 }
261}
262
263bool DialogsAreSuppressed() {
264 return dialogs_are_suppressed_;
265}
266
267size_t GetFatalAssertions(AssertionList* assertions) {
268 // In this function, we don't assume that assertions is non-null, so
269 // that if you just want an assertion count, you can pass in NULL.
270 if (assertions)
271 assertions->clear();
272 size_t assertion_count = 0;
273
274 std::ifstream log_file;
[email protected]73e23482009-08-13 00:26:58275 log_file.open(GetLogFileName().value().c_str());
initial.commit09911bf2008-07-26 23:55:29276 if (!log_file.is_open())
277 return 0;
278
279 std::string utf8_line;
280 std::wstring wide_line;
[email protected]4a3dab22009-11-11 17:36:50281 while (!log_file.eof()) {
initial.commit09911bf2008-07-26 23:55:29282 getline(log_file, utf8_line);
283 if (utf8_line.find(":FATAL:") != std::string::npos) {
284 wide_line = UTF8ToWide(utf8_line);
285 if (assertions)
286 assertions->push_back(wide_line);
287 ++assertion_count;
288 }
289 }
290 log_file.close();
291
292 return assertion_count;
293}
294
[email protected]c83dd912010-04-06 18:50:51295} // namespace logging