blob: 56c9d572c3ede6e78e775708d855df02b43e2a19 [file] [log] [blame]
[email protected]0f65b52d2012-02-09 21:02:471// Copyright (c) 2012 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.
Scott Violeta35f9a42018-03-22 22:00:4411#include "ipc/ipc_buildflags.h"
[email protected]d55aaa132009-09-28 21:08:0412
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.
davidsz041528a2017-05-12 09:19:2317#if defined(OS_POSIX) && BUILDFLAG(IPC_MESSAGE_LOG_ENABLED)
[email protected]d55aaa132009-09-28 21:08:0418#define IPC_MESSAGE_MACROS_LOG_ENABLED
[email protected]4f68ed72012-10-30 05:24:0719#include "content/public/common/content_ipc_logging.h"
20#define IPC_LOG_TABLE_ADD_ENTRY(msg_id, logger) \
21 content::RegisterIPCLogger(msg_id, logger)
[email protected]4a6d74152011-09-26 20:42:3022#include "chrome/common/all_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 "chrome/common/logging_chrome.h"
30
erikwrightdfe29762015-03-16 16:06:4631#include <fstream> // NOLINT
thakis8b51c0a2016-04-12 19:48:1832#include <memory> // NOLINT
erikwrightdfe29762015-03-16 16:06:4633#include <string> // NOLINT
34
brettwf1958912015-10-07 19:43:1235#include "base/base_switches.h"
davidsz041528a2017-05-12 09:19:2336#include "base/bind.h"
initial.commit09911bf2008-07-26 23:55:2937#include "base/command_line.h"
[email protected]0eb9f4342008-11-21 18:48:5238#include "base/compiler_specific.h"
[email protected]58580352010-10-26 04:07:5039#include "base/debug/debugger.h"
[email protected]5a8d4ce2013-12-18 17:42:2740#include "base/debug/dump_without_crashing.h"
[email protected]76b90d312010-08-03 03:00:5041#include "base/environment.h"
[email protected]57999812013-02-24 05:40:5242#include "base/files/file_path.h"
thestig18dfb7a52014-08-26 10:44:0443#include "base/files/file_util.h"
boriay648b37322016-07-05 08:15:2944#include "base/metrics/statistics_recorder.h"
initial.commit09911bf2008-07-26 23:55:2945#include "base/path_service.h"
[email protected]3ea1b182013-02-08 22:38:4146#include "base/strings/string_number_conversions.h"
[email protected]3c8a6b02013-06-11 00:49:4947#include "base/strings/string_util.h"
48#include "base/strings/stringprintf.h"
[email protected]12bfb612013-06-07 19:54:0249#include "base/strings/utf_string_conversions.h"
[email protected]ab7ee5b2011-03-01 22:24:1450#include "chrome/common/chrome_constants.h"
initial.commit09911bf2008-07-26 23:55:2951#include "chrome/common/chrome_paths.h"
52#include "chrome/common/chrome_switches.h"
initial.commit09911bf2008-07-26 23:55:2953#include "chrome/common/env_vars.h"
brettw90e92602015-10-10 00:12:4054#include "content/public/common/content_switches.h"
[email protected]51d5e162009-07-27 19:23:5455#include "ipc/ipc_logging.h"
[email protected]ab7ee5b2011-03-01 22:24:1456
[email protected]931d1042013-04-05 17:50:4457#if defined(OS_CHROMEOS)
Steven Bennetts38e9bde22019-01-03 17:31:1058#include "chromeos/constants/chromeos_switches.h"
[email protected]931d1042013-04-05 17:50:4459#endif
60
[email protected]2b07b8412009-11-25 15:26:3461#if defined(OS_WIN)
[email protected]2b07b8412009-11-25 15:26:3462#include <initguid.h>
[email protected]3ea1b182013-02-08 22:38:4163#include "base/logging_win.h"
pastarmovja3eac432016-12-07 17:38:5264#include "base/syslog_logging.h"
probergefe476002017-06-23 14:29:0165#include "chrome/common/win/eventlog_messages.h"
pastarmovja3eac432016-12-07 17:38:5266#include "chrome/install_static/install_details.h"
[email protected]2b07b8412009-11-25 15:26:3467#endif
initial.commit09911bf2008-07-26 23:55:2968
achuith7ea0aaca62017-06-01 20:31:4269namespace logging {
[email protected]ab7ee5b2011-03-01 22:24:1470namespace {
71
initial.commit09911bf2008-07-26 23:55:2972// When true, this means that error dialogs should not be shown.
[email protected]ab7ee5b2011-03-01 22:24:1473bool dialogs_are_suppressed_ = false;
achuith7ea0aaca62017-06-01 20:31:4274ScopedLogAssertHandler* assert_handler_ = nullptr;
initial.commit09911bf2008-07-26 23:55:2975
76// This should be true for exactly the period between the end of
77// InitChromeLogging() and the beginning of CleanupChromeLogging().
[email protected]ab7ee5b2011-03-01 22:24:1478bool chrome_logging_initialized_ = false;
initial.commit09911bf2008-07-26 23:55:2979
[email protected]4a6d74152011-09-26 20:42:3080// Set if we called InitChromeLogging() but failed to initialize.
[email protected]ab7ee5b2011-03-01 22:24:1481bool chrome_logging_failed_ = false;
[email protected]7109dc32011-02-02 22:58:2982
[email protected]30f547c2010-08-04 01:00:0183// This should be true for exactly the period between the end of
84// InitChromeLogging() and the beginning of CleanupChromeLogging().
[email protected]ab7ee5b2011-03-01 22:24:1485bool chrome_logging_redirected_ = false;
[email protected]30f547c2010-08-04 01:00:0186
[email protected]2b07b8412009-11-25 15:26:3487#if defined(OS_WIN)
88// {7FE69228-633E-4f06-80C1-527FEA23E3A7}
[email protected]ab7ee5b2011-03-01 22:24:1489const GUID kChromeTraceProviderName = {
[email protected]162ac0f2010-11-04 15:50:4990 0x7fe69228, 0x633e, 0x4f06,
91 { 0x80, 0xc1, 0x52, 0x7f, 0xea, 0x23, 0xe3, 0xa7 } };
[email protected]2b07b8412009-11-25 15:26:3492#endif
93
initial.commit09911bf2008-07-26 23:55:2994// Assertion handler for logging errors that occur when dialogs are
95// silenced. To record a new error, pass the log string associated
96// with that error in the str parameter.
David Bienvenua86573a2018-08-20 19:34:0097NOINLINE void SilentRuntimeAssertHandler(const char* file,
98 int line,
99 const base::StringPiece message,
100 const base::StringPiece stack_trace) {
[email protected]58580352010-10-26 04:07:50101 base::debug::BreakDebugger();
initial.commit09911bf2008-07-26 23:55:29102}
initial.commit09911bf2008-07-26 23:55:29103
104// Suppresses error/assertion dialogs and enables the logging of
105// those errors into silenced_errors_.
[email protected]ab7ee5b2011-03-01 22:24:14106void SuppressDialogs() {
initial.commit09911bf2008-07-26 23:55:29107 if (dialogs_are_suppressed_)
108 return;
109
achuith7ea0aaca62017-06-01 20:31:42110 assert_handler_ =
111 new ScopedLogAssertHandler(base::Bind(SilentRuntimeAssertHandler));
initial.commit09911bf2008-07-26 23:55:29112
[email protected]ec657802008-11-07 20:05:39113#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:29114 UINT new_flags = SEM_FAILCRITICALERRORS |
115 SEM_NOGPFAULTERRORBOX |
116 SEM_NOOPENFILEERRORBOX;
117
118 // Preserve existing error mode, as discussed at http://t/dmea
119 UINT existing_flags = SetErrorMode(new_flags);
120 SetErrorMode(existing_flags | new_flags);
[email protected]ec657802008-11-07 20:05:39121#endif
initial.commit09911bf2008-07-26 23:55:29122
123 dialogs_are_suppressed_ = true;
124}
125
[email protected]ab7ee5b2011-03-01 22:24:14126} // anonymous namespace
127
achuith7ea0aaca62017-06-01 20:31:42128LoggingDestination DetermineLoggingDestination(
129 const base::CommandLine& command_line) {
130// only use OutputDebugString in debug mode
initial.commit09911bf2008-07-26 23:55:29131#ifdef NDEBUG
132 bool enable_logging = false;
[email protected]b7e0a2a2009-10-13 02:07:25133 const char *kInvertLoggingSwitch = switches::kEnableLogging;
achuith7ea0aaca62017-06-01 20:31:42134 const LoggingDestination kDefaultLoggingMode = LOG_TO_FILE;
initial.commit09911bf2008-07-26 23:55:29135#else
136 bool enable_logging = true;
[email protected]b7e0a2a2009-10-13 02:07:25137 const char *kInvertLoggingSwitch = switches::kDisableLogging;
achuith7ea0aaca62017-06-01 20:31:42138 const LoggingDestination kDefaultLoggingMode = LOG_TO_ALL;
initial.commit09911bf2008-07-26 23:55:29139#endif
140
141 if (command_line.HasSwitch(kInvertLoggingSwitch))
142 enable_logging = !enable_logging;
143
achuith7ea0aaca62017-06-01 20:31:42144 LoggingDestination log_mode;
initial.commit09911bf2008-07-26 23:55:29145 if (enable_logging) {
[email protected]3bba9582009-08-05 01:29:12146 // Let --enable-logging=stderr force only stderr, particularly useful for
147 // non-debug builds where otherwise you can't get logs to stderr at all.
[email protected]c4e52f0d2009-11-06 19:55:16148 if (command_line.GetSwitchValueASCII(switches::kEnableLogging) == "stderr")
Sharon Yang7cb919a2019-05-20 20:27:15149 log_mode = LOG_TO_SYSTEM_DEBUG_LOG | LOG_TO_STDERR;
[email protected]3bba9582009-08-05 01:29:12150 else
151 log_mode = kDefaultLoggingMode;
initial.commit09911bf2008-07-26 23:55:29152 } else {
achuith7ea0aaca62017-06-01 20:31:42153 log_mode = LOG_NONE;
initial.commit09911bf2008-07-26 23:55:29154 }
[email protected]77ee1b12010-06-14 16:21:30155 return log_mode;
156}
initial.commit09911bf2008-07-26 23:55:29157
[email protected]77ee1b12010-06-14 16:21:30158#if defined(OS_CHROMEOS)
[email protected]a7329162013-02-07 19:21:48159base::FilePath SetUpSymlinkIfNeeded(const base::FilePath& symlink_path,
160 bool new_log) {
[email protected]f25b33a2010-10-28 20:15:51161 DCHECK(!symlink_path.empty());
Luigi Semenzato52384302017-08-10 20:22:59162 // For backward compatibility, set up a .../chrome symlink to
163 // .../chrome.LATEST as needed. This code needs to run only
164 // after the migration (i.e. the addition of chrome.LATEST).
165 if (symlink_path.Extension() == ".LATEST") {
166 base::FilePath extensionless_path = symlink_path.ReplaceExtension("");
167 base::FilePath target_path;
Luigi Semenzato214cd282018-01-27 01:15:59168 bool extensionless_symlink_exists =
169 base::ReadSymbolicLink(extensionless_path, &target_path);
[email protected]f25b33a2010-10-28 20:15:51170
Luigi Semenzato52384302017-08-10 20:22:59171 if (target_path != symlink_path) {
172 // No link, or wrong link. Clean up. This should happen only once in
173 // each log directory after the OS version update, but some of those
174 // directories may not be accessed for a long time, so this code needs to
175 // stay in forever :/
Luigi Semenzato214cd282018-01-27 01:15:59176 if (extensionless_symlink_exists &&
Luigi Semenzato52384302017-08-10 20:22:59177 !base::DeleteFile(extensionless_path, false)) {
178 DPLOG(WARNING) << "Cannot delete " << extensionless_path.value();
179 }
180 // After cleaning up, create the symlink.
181 if (!base::CreateSymbolicLink(symlink_path, extensionless_path)) {
182 DPLOG(ERROR) << "Cannot create " << extensionless_path.value();
183 }
184 }
185 }
186
187 // If not starting a new log, then just log through the existing symlink, but
188 // if the symlink doesn't exist, create it.
189 //
190 // If starting a new log, then rename the old symlink as
191 // symlink_path.PREVIOUS and make a new symlink to a fresh log file.
[email protected]c7d5da992010-10-28 00:20:21192
Luigi Semenzato214cd282018-01-27 01:15:59193 // Check for existence of the symlink.
194 base::FilePath target_path;
195 bool symlink_exists = base::ReadSymbolicLink(symlink_path, &target_path);
196
197 if (symlink_exists && !new_log)
198 return target_path;
199
200 // Remove any extension before time-stamping.
201 target_path = GenerateTimestampedName(symlink_path.RemoveExtension(),
202 base::Time::Now());
203
204 if (symlink_exists) {
205 base::FilePath previous_symlink_path =
206 symlink_path.ReplaceExtension(".PREVIOUS");
207 // Rename symlink to .PREVIOUS. This nukes an existing symlink just like
208 // the rename(2) syscall does.
209 if (!base::ReplaceFile(symlink_path, previous_symlink_path, nullptr)) {
210 DPLOG(WARNING) << "Cannot rename " << symlink_path.value() << " to "
211 << previous_symlink_path.value();
[email protected]c7d5da992010-10-28 00:20:21212 }
Luigi Semenzato214cd282018-01-27 01:15:59213 }
214 // If all went well, the symlink no longer exists. Recreate it.
Vladislav Kuzkokov5c7ff5a2019-06-26 16:52:34215 base::FilePath relative_target_path = target_path.BaseName();
216 if (!base::CreateSymbolicLink(relative_target_path, symlink_path)) {
Luigi Semenzato214cd282018-01-27 01:15:59217 DPLOG(ERROR) << "Unable to create symlink " << symlink_path.value()
Vladislav Kuzkokov5c7ff5a2019-06-26 16:52:34218 << " pointing at " << relative_target_path.value();
[email protected]c7d5da992010-10-28 00:20:21219 }
220 return target_path;
221}
222
[email protected]a7329162013-02-07 19:21:48223void RemoveSymlinkAndLog(const base::FilePath& link_path,
224 const base::FilePath& target_path) {
[email protected]c7d5da992010-10-28 00:20:21225 if (::unlink(link_path.value().c_str()) == -1)
[email protected]26f025e2011-10-28 22:49:27226 DPLOG(WARNING) << "Unable to unlink symlink " << link_path.value();
[email protected]c7d5da992010-10-28 00:20:21227 if (::unlink(target_path.value().c_str()) == -1)
[email protected]26f025e2011-10-28 22:49:27228 DPLOG(WARNING) << "Unable to unlink log file " << target_path.value();
[email protected]c7d5da992010-10-28 00:20:21229}
230
avi79bf9132014-12-25 17:48:05231base::FilePath GetSessionLogDir(const base::CommandLine& command_line) {
[email protected]a7329162013-02-07 19:21:48232 base::FilePath log_dir;
[email protected]0750b1442010-11-02 21:59:37233 std::string log_dir_str;
thakis8b51c0a2016-04-12 19:48:18234 std::unique_ptr<base::Environment> env(base::Environment::Create());
[email protected]0750b1442010-11-02 21:59:37235 if (env->GetVar(env_vars::kSessionLogDir, &log_dir_str) &&
236 !log_dir_str.empty()) {
[email protected]a7329162013-02-07 19:21:48237 log_dir = base::FilePath(log_dir_str);
[email protected]68e31692013-05-20 13:08:27238 } else if (command_line.HasSwitch(chromeos::switches::kLoginProfile)) {
Avi Drissman7d43b4b2018-05-01 16:14:06239 base::PathService::Get(chrome::DIR_USER_DATA, &log_dir);
[email protected]68e31692013-05-20 13:08:27240 base::FilePath profile_dir;
[email protected]9c8b1f92013-09-19 14:39:59241 std::string login_profile_value =
242 command_line.GetSwitchValueASCII(chromeos::switches::kLoginProfile);
[email protected]64146ec2014-03-19 21:52:25243 if (login_profile_value == chrome::kLegacyProfileDir ||
244 login_profile_value == chrome::kTestUserProfileDir) {
[email protected]9c8b1f92013-09-19 14:39:59245 profile_dir = base::FilePath(login_profile_value);
246 } else {
[email protected]68e31692013-05-20 13:08:27247 // We could not use g_browser_process > profile_helper() here.
248 std::string profile_dir_str = chrome::kProfileDirPrefix;
[email protected]9c8b1f92013-09-19 14:39:59249 profile_dir_str.append(login_profile_value);
[email protected]68e31692013-05-20 13:08:27250 profile_dir = base::FilePath(profile_dir_str);
[email protected]68e31692013-05-20 13:08:27251 }
252 log_dir = log_dir.Append(profile_dir);
[email protected]0750b1442010-11-02 21:59:37253 }
[email protected]cc5ff9852014-07-23 22:29:29254 return log_dir;
255}
256
avi79bf9132014-12-25 17:48:05257base::FilePath GetSessionLogFile(const base::CommandLine& command_line) {
Michael Giuffrida6802ffe2017-09-29 03:40:25258 return GetSessionLogDir(command_line)
259 .Append(GetLogFileName(command_line).BaseName());
[email protected]0750b1442010-11-02 21:59:37260}
261
Luigi Semenzato214cd282018-01-27 01:15:59262#endif // defined(OS_CHROMEOS)
[email protected]77ee1b12010-06-14 16:21:30263
avi79bf9132014-12-25 17:48:05264void InitChromeLogging(const base::CommandLine& command_line,
[email protected]77ee1b12010-06-14 16:21:30265 OldFileDeletionState delete_old_log_file) {
266 DCHECK(!chrome_logging_initialized_) <<
267 "Attempted to initialize logging when it was already initialized.";
achuith7ea0aaca62017-06-01 20:31:42268 LoggingDestination logging_dest = DetermineLoggingDestination(command_line);
[email protected]d64ea42172012-09-14 05:43:59269 LogLockingState log_locking_state = LOCK_LOG_FILE;
[email protected]a7329162013-02-07 19:21:48270 base::FilePath log_path;
[email protected]4a100992011-06-14 18:22:46271#if defined(OS_CHROMEOS)
[email protected]a7329162013-02-07 19:21:48272 base::FilePath target_path;
[email protected]4a100992011-06-14 18:22:46273#endif
[email protected]77ee1b12010-06-14 16:21:30274
[email protected]4a100992011-06-14 18:22:46275 // Don't resolve the log path unless we need to. Otherwise we leave an open
276 // ALPC handle after sandbox lockdown on Windows.
[email protected]5e3f7c22013-06-21 21:15:33277 if ((logging_dest & LOG_TO_FILE) != 0) {
Michael Giuffrida6802ffe2017-09-29 03:40:25278 log_path = GetLogFileName(command_line);
[email protected]c7d5da992010-10-28 00:20:21279
[email protected]77ee1b12010-06-14 16:21:30280#if defined(OS_CHROMEOS)
[email protected]4a100992011-06-14 18:22:46281 // For BWSI (Incognito) logins, we want to put the logs in the user
282 // profile directory that is created for the temporary session instead
283 // of in the system log directory, for privacy reasons.
[email protected]931d1042013-04-05 17:50:44284 if (command_line.HasSwitch(chromeos::switches::kGuestSession))
[email protected]4a100992011-06-14 18:22:46285 log_path = GetSessionLogFile(command_line);
[email protected]0750b1442010-11-02 21:59:37286
[email protected]4a100992011-06-14 18:22:46287 // On ChromeOS we log to the symlink. We force creation of a new
288 // symlink if we've been asked to delete the old log, since that
289 // indicates the start of a new session.
290 target_path = SetUpSymlinkIfNeeded(
achuith7ea0aaca62017-06-01 20:31:42291 log_path, delete_old_log_file == DELETE_OLD_LOG_FILE);
[email protected]c7d5da992010-10-28 00:20:21292
[email protected]4a100992011-06-14 18:22:46293 // Because ChromeOS manages the move to a new session by redirecting
294 // the link, it shouldn't remove the old file in the logging code,
295 // since that will remove the newly created link instead.
achuith7ea0aaca62017-06-01 20:31:42296 delete_old_log_file = APPEND_TO_OLD_LOG_FILE;
Luigi Semenzato214cd282018-01-27 01:15:59297#endif // defined(OS_CHROMEOS)
[email protected]d64ea42172012-09-14 05:43:59298 } else {
299 log_locking_state = DONT_LOCK_LOG_FILE;
[email protected]4a100992011-06-14 18:22:46300 }
[email protected]77ee1b12010-06-14 16:21:30301
achuith7ea0aaca62017-06-01 20:31:42302 LoggingSettings settings;
[email protected]5e3f7c22013-06-21 21:15:33303 settings.logging_dest = logging_dest;
Robbie McElrath8bf49842019-08-20 22:22:53304 settings.log_file_path = log_path.value().c_str();
[email protected]5e3f7c22013-06-21 21:15:33305 settings.lock_log = log_locking_state;
306 settings.delete_old = delete_old_log_file;
achuith7ea0aaca62017-06-01 20:31:42307 bool success = InitLogging(settings);
[email protected]c7d5da992010-10-28 00:20:21308
309#if defined(OS_CHROMEOS)
310 if (!success) {
[email protected]26f025e2011-10-28 22:49:27311 DPLOG(ERROR) << "Unable to initialize logging to " << log_path.value()
[email protected]c7d5da992010-10-28 00:20:21312 << " (which should be a link to " << target_path.value() << ")";
313 RemoveSymlinkAndLog(log_path, target_path);
[email protected]7109dc32011-02-02 22:58:29314 chrome_logging_failed_ = true;
[email protected]c7d5da992010-10-28 00:20:21315 return;
316 }
Luigi Semenzato214cd282018-01-27 01:15:59317#else // defined(OS_CHROMEOS)
[email protected]c7d5da992010-10-28 00:20:21318 if (!success) {
[email protected]26f025e2011-10-28 22:49:27319 DPLOG(ERROR) << "Unable to initialize logging to " << log_path.value();
[email protected]7109dc32011-02-02 22:58:29320 chrome_logging_failed_ = true;
[email protected]c7d5da992010-10-28 00:20:21321 return;
322 }
Luigi Semenzato214cd282018-01-27 01:15:59323#endif // defined(OS_CHROMEOS)
initial.commit09911bf2008-07-26 23:55:29324
Greg Thompsond548ffa2017-10-31 08:25:05325 // We call running in unattended mode "headless", and allow headless mode to
326 // be configured either by the Environment Variable or by the Command Line
327 // Switch. This is for automated test purposes.
328 std::unique_ptr<base::Environment> env(base::Environment::Create());
329 const bool is_headless = env->HasVar(env_vars::kHeadless) ||
330 command_line.HasSwitch(switches::kNoErrorDialogs);
331
332 // Show fatal log messages in a dialog in debug builds when not headless.
333 if (!is_headless)
achuith7ea0aaca62017-06-01 20:31:42334 SetShowErrorDialogs(true);
[email protected]81e0a852010-08-17 00:38:12335
initial.commit09911bf2008-07-26 23:55:29336 // we want process and thread IDs because we have a lot of things running
achuith7ea0aaca62017-06-01 20:31:42337 SetLogItems(true, // enable_process_id
338 true, // enable_thread_id
339 true, // enable_timestamp
340 false); // enable_tickcount
initial.commit09911bf2008-07-26 23:55:29341
Greg Thompsond548ffa2017-10-31 08:25:05342 // Suppress system error dialogs when headless.
343 if (is_headless)
initial.commit09911bf2008-07-26 23:55:29344 SuppressDialogs();
345
emaxx0bcc7aa662015-05-15 22:25:26346 // Use a minimum log level if the command line asks for one. Ignore this
347 // switch if there's vlog level switch present too (as both of these switches
348 // refer to the same underlying log level, and the vlog level switch has
achuith7ea0aaca62017-06-01 20:31:42349 // already been processed inside InitLogging). If there is neither
emaxx0bcc7aa662015-05-15 22:25:26350 // log level nor vlog level specified, then just leave the default level
351 // (INFO).
352 if (command_line.HasSwitch(switches::kLoggingLevel) &&
achuith7ea0aaca62017-06-01 20:31:42353 GetMinLogLevel() >= 0) {
emaxx0bcc7aa662015-05-15 22:25:26354 std::string log_level =
355 command_line.GetSwitchValueASCII(switches::kLoggingLevel);
[email protected]3deeb6b32010-10-05 21:53:52356 int level = 0;
emaxx0bcc7aa662015-05-15 22:25:26357 if (base::StringToInt(log_level, &level) && level >= 0 &&
358 level < LOG_NUM_SEVERITIES) {
achuith7ea0aaca62017-06-01 20:31:42359 SetMinLogLevel(level);
[email protected]3deeb6b32010-10-05 21:53:52360 } else {
[email protected]26f025e2011-10-28 22:49:27361 DLOG(WARNING) << "Bad log level: " << log_level;
[email protected]3deeb6b32010-10-05 21:53:52362 }
[email protected]bb5185c52008-08-29 19:51:06363 }
364
[email protected]2b07b8412009-11-25 15:26:34365#if defined(OS_WIN)
366 // Enable trace control and transport through event tracing for Windows.
achuith7ea0aaca62017-06-01 20:31:42367 LogEventProvider::Initialize(kChromeTraceProviderName);
pastarmovja3eac432016-12-07 17:38:52368
369 // Enable logging to the Windows Event Log.
probergefe476002017-06-23 14:29:01370 SetEventSource(base::UTF16ToASCII(
371 install_static::InstallDetails::Get().install_full_name()),
372 BROWSER_CATEGORY, MSG_LOG_MESSAGE);
[email protected]2b07b8412009-11-25 15:26:34373#endif
374
boriay648b37322016-07-05 08:15:29375 base::StatisticsRecorder::InitLogOnShutdown();
376
initial.commit09911bf2008-07-26 23:55:29377 chrome_logging_initialized_ = true;
378}
379
380// This is a no-op, but we'll keep it around in case
381// we need to do more cleanup in the future.
382void CleanupChromeLogging() {
[email protected]7109dc32011-02-02 22:58:29383 if (chrome_logging_failed_)
384 return; // We failed to initiailize logging, no cleanup.
385
initial.commit09911bf2008-07-26 23:55:29386 DCHECK(chrome_logging_initialized_) <<
387 "Attempted to clean up logging when it wasn't initialized.";
388
389 CloseLogFile();
390
391 chrome_logging_initialized_ = false;
[email protected]30f547c2010-08-04 01:00:01392 chrome_logging_redirected_ = false;
initial.commit09911bf2008-07-26 23:55:29393}
394
Michael Giuffrida6802ffe2017-09-29 03:40:25395base::FilePath GetLogFileName(const base::CommandLine& command_line) {
396 std::string filename = command_line.GetSwitchValueASCII(switches::kLogFile);
397 if (filename.empty())
398 base::Environment::Create()->GetVar(env_vars::kLogFileName, &filename);
399 if (!filename.empty())
[email protected]0b58a812013-05-31 01:15:17400 return base::FilePath::FromUTF8Unsafe(filename);
initial.commit09911bf2008-07-26 23:55:29401
[email protected]a7329162013-02-07 19:21:48402 const base::FilePath log_filename(FILE_PATH_LITERAL("chrome_debug.log"));
403 base::FilePath log_path;
initial.commit09911bf2008-07-26 23:55:29404
Avi Drissman7d43b4b2018-05-01 16:14:06405 if (base::PathService::Get(chrome::DIR_LOGS, &log_path)) {
[email protected]73e23482009-08-13 00:26:58406 log_path = log_path.Append(log_filename);
initial.commit09911bf2008-07-26 23:55:29407 return log_path;
408 } else {
409 // error with path service, just use some default file somewhere
410 return log_filename;
411 }
412}
413
414bool DialogsAreSuppressed() {
415 return dialogs_are_suppressed_;
416}
Luigi Semenzato52384302017-08-10 20:22:59417
418#if defined(OS_CHROMEOS)
[email protected]a7329162013-02-07 19:21:48419base::FilePath GenerateTimestampedName(const base::FilePath& base_path,
420 base::Time timestamp) {
[email protected]395a0392012-04-12 15:35:03421 base::Time::Exploded time_deets;
422 timestamp.LocalExplode(&time_deets);
423 std::string suffix = base::StringPrintf("_%02d%02d%02d-%02d%02d%02d",
424 time_deets.year,
425 time_deets.month,
426 time_deets.day_of_month,
427 time_deets.hour,
428 time_deets.minute,
429 time_deets.second);
Luigi Semenzato214cd282018-01-27 01:15:59430 return base_path.InsertBeforeExtensionASCII(suffix);
[email protected]395a0392012-04-12 15:35:03431}
Luigi Semenzato52384302017-08-10 20:22:59432#endif // defined(OS_CHROMEOS)
[email protected]395a0392012-04-12 15:35:03433
[email protected]c83dd912010-04-06 18:50:51434} // namespace logging