blob: 2bf9783eee168a77f059ca48d550864f68ed0576 [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.
Xiaohan Wang4d5c5042022-01-18 21:54:3717#if BUILDFLAG(IS_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
Xiaohan Wang4d5c5042022-01-18 21:54:3725#if BUILDFLAG(IS_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"
Yuta Hijikata2bac1282020-11-24 03:51:2750#include "build/chromeos_buildflags.h"
[email protected]ab7ee5b2011-03-01 22:24:1451#include "chrome/common/chrome_constants.h"
initial.commit09911bf2008-07-26 23:55:2952#include "chrome/common/chrome_paths.h"
53#include "chrome/common/chrome_switches.h"
initial.commit09911bf2008-07-26 23:55:2954#include "chrome/common/env_vars.h"
brettw90e92602015-10-10 00:12:4055#include "content/public/common/content_switches.h"
[email protected]51d5e162009-07-27 19:23:5456#include "ipc/ipc_logging.h"
[email protected]ab7ee5b2011-03-01 22:24:1457
Yuta Hijikata2bac1282020-11-24 03:51:2758#if BUILDFLAG(IS_CHROMEOS_ASH)
Henrique Ferreirof3fbcea22021-02-05 23:12:1959#include "ash/constants/ash_switches.h"
[email protected]931d1042013-04-05 17:50:4460#endif
61
Xiaohan Wang4d5c5042022-01-18 21:54:3762#if BUILDFLAG(IS_WIN)
[email protected]2b07b8412009-11-25 15:26:3463#include <initguid.h>
[email protected]3ea1b182013-02-08 22:38:4164#include "base/logging_win.h"
pastarmovja3eac432016-12-07 17:38:5265#include "base/syslog_logging.h"
probergefe476002017-06-23 14:29:0166#include "chrome/common/win/eventlog_messages.h"
pastarmovja3eac432016-12-07 17:38:5267#include "chrome/install_static/install_details.h"
[email protected]2b07b8412009-11-25 15:26:3468#endif
initial.commit09911bf2008-07-26 23:55:2969
achuith7ea0aaca62017-06-01 20:31:4270namespace logging {
[email protected]ab7ee5b2011-03-01 22:24:1471namespace {
72
initial.commit09911bf2008-07-26 23:55:2973// When true, this means that error dialogs should not be shown.
[email protected]ab7ee5b2011-03-01 22:24:1474bool dialogs_are_suppressed_ = false;
achuith7ea0aaca62017-06-01 20:31:4275ScopedLogAssertHandler* assert_handler_ = nullptr;
initial.commit09911bf2008-07-26 23:55:2976
77// This should be true for exactly the period between the end of
78// InitChromeLogging() and the beginning of CleanupChromeLogging().
[email protected]ab7ee5b2011-03-01 22:24:1479bool chrome_logging_initialized_ = false;
initial.commit09911bf2008-07-26 23:55:2980
[email protected]4a6d74152011-09-26 20:42:3081// Set if we called InitChromeLogging() but failed to initialize.
[email protected]ab7ee5b2011-03-01 22:24:1482bool chrome_logging_failed_ = false;
[email protected]7109dc32011-02-02 22:58:2983
[email protected]30f547c2010-08-04 01:00:0184// This should be true for exactly the period between the end of
85// InitChromeLogging() and the beginning of CleanupChromeLogging().
[email protected]ab7ee5b2011-03-01 22:24:1486bool chrome_logging_redirected_ = false;
[email protected]30f547c2010-08-04 01:00:0187
yoshiki iguchibf61e0f2022-05-17 17:57:5088// The directory on which we do rotation of log files instead of switching
89// with symlink. Because this directory doesn't support symlinks and the logic
90// doesn't work correctly.
91#if BUILDFLAG(IS_CHROMEOS_ASH)
92constexpr char kChronosHomeDir[] = "/home/chronos/user/";
93#endif // BUILDFLAG(IS_CHROMEOS_ASH)
94
Xiaohan Wang4d5c5042022-01-18 21:54:3795#if BUILDFLAG(IS_WIN)
[email protected]2b07b8412009-11-25 15:26:3496// {7FE69228-633E-4f06-80C1-527FEA23E3A7}
[email protected]ab7ee5b2011-03-01 22:24:1497const GUID kChromeTraceProviderName = {
[email protected]162ac0f2010-11-04 15:50:4998 0x7fe69228, 0x633e, 0x4f06,
99 { 0x80, 0xc1, 0x52, 0x7f, 0xea, 0x23, 0xe3, 0xa7 } };
[email protected]2b07b8412009-11-25 15:26:34100#endif
101
initial.commit09911bf2008-07-26 23:55:29102// Assertion handler for logging errors that occur when dialogs are
103// silenced. To record a new error, pass the log string associated
104// with that error in the str parameter.
David Bienvenua86573a2018-08-20 19:34:00105NOINLINE void SilentRuntimeAssertHandler(const char* file,
106 int line,
107 const base::StringPiece message,
108 const base::StringPiece stack_trace) {
[email protected]58580352010-10-26 04:07:50109 base::debug::BreakDebugger();
initial.commit09911bf2008-07-26 23:55:29110}
initial.commit09911bf2008-07-26 23:55:29111
112// Suppresses error/assertion dialogs and enables the logging of
113// those errors into silenced_errors_.
[email protected]ab7ee5b2011-03-01 22:24:14114void SuppressDialogs() {
initial.commit09911bf2008-07-26 23:55:29115 if (dialogs_are_suppressed_)
116 return;
117
Ella Gedf89f8ad92020-06-29 16:29:22118 assert_handler_ = new ScopedLogAssertHandler(
119 base::BindRepeating(SilentRuntimeAssertHandler));
initial.commit09911bf2008-07-26 23:55:29120
Xiaohan Wang4d5c5042022-01-18 21:54:37121#if BUILDFLAG(IS_WIN)
yoshiki iguchibf61e0f2022-05-17 17:57:50122 UINT new_flags =
123 SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX | SEM_NOOPENFILEERRORBOX;
initial.commit09911bf2008-07-26 23:55:29124 // Preserve existing error mode, as discussed at http://t/dmea
125 UINT existing_flags = SetErrorMode(new_flags);
126 SetErrorMode(existing_flags | new_flags);
[email protected]ec657802008-11-07 20:05:39127#endif
initial.commit09911bf2008-07-26 23:55:29128
129 dialogs_are_suppressed_ = true;
130}
131
[email protected]ab7ee5b2011-03-01 22:24:14132} // anonymous namespace
133
achuith7ea0aaca62017-06-01 20:31:42134LoggingDestination DetermineLoggingDestination(
135 const base::CommandLine& command_line) {
Benjamin Lermand823d682021-10-25 11:31:01136#if BUILDFLAG(IS_FUCHSIA)
137 // On Fuchsia, the default logging mode is the system log in both debug and
138 // release mode.
139 const LoggingDestination kDefaultLoggingMode = LOG_TO_SYSTEM_DEBUG_LOG;
140#elif NDEBUG
achuith7ea0aaca62017-06-01 20:31:42141 const LoggingDestination kDefaultLoggingMode = LOG_TO_FILE;
initial.commit09911bf2008-07-26 23:55:29142#else
achuith7ea0aaca62017-06-01 20:31:42143 const LoggingDestination kDefaultLoggingMode = LOG_TO_ALL;
Benjamin Lermand823d682021-10-25 11:31:01144#endif // BUILDFLAG(IS_FUCHSIA)
145
146#ifdef NDEBUG
147 bool enable_logging = false;
148 const char* kInvertLoggingSwitch = switches::kEnableLogging;
149#else
150 bool enable_logging = true;
151 const char* kInvertLoggingSwitch = switches::kDisableLogging;
initial.commit09911bf2008-07-26 23:55:29152#endif
153
154 if (command_line.HasSwitch(kInvertLoggingSwitch))
155 enable_logging = !enable_logging;
156
Camillo Bruni213d59a22020-04-01 11:21:52157 if (!enable_logging)
158 return LOG_NONE;
159 if (command_line.HasSwitch(switches::kEnableLogging)) {
[email protected]3bba9582009-08-05 01:29:12160 // Let --enable-logging=stderr force only stderr, particularly useful for
161 // non-debug builds where otherwise you can't get logs to stderr at all.
Camillo Bruni213d59a22020-04-01 11:21:52162 std::string logging_destination =
163 command_line.GetSwitchValueASCII(switches::kEnableLogging);
164 if (logging_destination == "stderr") {
165 return LOG_TO_SYSTEM_DEBUG_LOG | LOG_TO_STDERR;
166 } else if (logging_destination != "") {
167 PLOG(ERROR) << "Invalid logging destination: " << logging_destination;
168 }
initial.commit09911bf2008-07-26 23:55:29169 }
Camillo Bruni213d59a22020-04-01 11:21:52170 return kDefaultLoggingMode;
[email protected]77ee1b12010-06-14 16:21:30171}
initial.commit09911bf2008-07-26 23:55:29172
Yuta Hijikata2bac1282020-11-24 03:51:27173#if BUILDFLAG(IS_CHROMEOS_ASH)
yoshiki iguchibf61e0f2022-05-17 17:57:50174bool RotateLogFile(const base::FilePath& target_path) {
175 DCHECK(!target_path.empty());
176 // If the old log file doesn't exist, do nothing.
177 if (!base::PathExists(target_path)) {
178 return true;
179 }
180
181 // Retrieve the creation time of the old log file.
182 base::File::Info info;
183 {
184 // Opens a file, only if it exists.
185 base::File fp(target_path, base::File::FLAG_OPEN | base::File::FLAG_READ);
186 if (!fp.GetInfo(&info)) {
187 // On failure, keep using the same file.
188 return false;
189 }
190 }
191
192 // Generate the rotated log path name from the creation time.
193 // (eg. "/home/chrome/user/log/chrome_220102-030405")
194 base::Time timestamp = info.creation_time;
195 base::FilePath rotated_path = GenerateTimestampedName(target_path, timestamp);
196
197 // Rare case: if the target path already exists, generate the alternative by
198 // incrementing the timestamp. This may happen when the Chrome restarts
199 // multiple times in a second.
200 while (base::PathExists(rotated_path)) {
201 timestamp += base::Seconds(1);
202 rotated_path = GenerateTimestampedName(target_path, timestamp);
203 }
204
205 // Rename the old log file: |target_path| => |rotated_path|.
206 // We don't use |base::Move|, since we don't consider the inter-filesystem
207 // move in this logic. The current logic depends on the fact that the ctime
208 // won't be changed after rotation, but ctime may be changed on
209 // inter-filesystem move.
210 if (!base::ReplaceFile(target_path, rotated_path, nullptr)) {
211 PLOG(ERROR) << "Failed to rotate the log files: " << target_path << " => "
212 << rotated_path;
213 return false;
214 }
215
216 return true;
217}
218
[email protected]a7329162013-02-07 19:21:48219base::FilePath SetUpSymlinkIfNeeded(const base::FilePath& symlink_path,
220 bool new_log) {
[email protected]f25b33a2010-10-28 20:15:51221 DCHECK(!symlink_path.empty());
Luigi Semenzato52384302017-08-10 20:22:59222 // For backward compatibility, set up a .../chrome symlink to
223 // .../chrome.LATEST as needed. This code needs to run only
224 // after the migration (i.e. the addition of chrome.LATEST).
225 if (symlink_path.Extension() == ".LATEST") {
226 base::FilePath extensionless_path = symlink_path.ReplaceExtension("");
227 base::FilePath target_path;
Luigi Semenzato214cd282018-01-27 01:15:59228 bool extensionless_symlink_exists =
229 base::ReadSymbolicLink(extensionless_path, &target_path);
[email protected]f25b33a2010-10-28 20:15:51230
Luigi Semenzato52384302017-08-10 20:22:59231 if (target_path != symlink_path) {
232 // No link, or wrong link. Clean up. This should happen only once in
233 // each log directory after the OS version update, but some of those
234 // directories may not be accessed for a long time, so this code needs to
235 // stay in forever :/
Luigi Semenzato214cd282018-01-27 01:15:59236 if (extensionless_symlink_exists &&
Lei Zhang3c167232020-07-08 00:33:53237 !base::DeleteFile(extensionless_path)) {
Luigi Semenzato52384302017-08-10 20:22:59238 DPLOG(WARNING) << "Cannot delete " << extensionless_path.value();
239 }
240 // After cleaning up, create the symlink.
241 if (!base::CreateSymbolicLink(symlink_path, extensionless_path)) {
242 DPLOG(ERROR) << "Cannot create " << extensionless_path.value();
243 }
244 }
245 }
246
247 // If not starting a new log, then just log through the existing symlink, but
248 // if the symlink doesn't exist, create it.
249 //
250 // If starting a new log, then rename the old symlink as
251 // symlink_path.PREVIOUS and make a new symlink to a fresh log file.
[email protected]c7d5da992010-10-28 00:20:21252
Luigi Semenzato214cd282018-01-27 01:15:59253 // Check for existence of the symlink.
254 base::FilePath target_path;
255 bool symlink_exists = base::ReadSymbolicLink(symlink_path, &target_path);
256
257 if (symlink_exists && !new_log)
258 return target_path;
259
260 // Remove any extension before time-stamping.
261 target_path = GenerateTimestampedName(symlink_path.RemoveExtension(),
262 base::Time::Now());
263
264 if (symlink_exists) {
265 base::FilePath previous_symlink_path =
266 symlink_path.ReplaceExtension(".PREVIOUS");
267 // Rename symlink to .PREVIOUS. This nukes an existing symlink just like
268 // the rename(2) syscall does.
269 if (!base::ReplaceFile(symlink_path, previous_symlink_path, nullptr)) {
270 DPLOG(WARNING) << "Cannot rename " << symlink_path.value() << " to "
271 << previous_symlink_path.value();
[email protected]c7d5da992010-10-28 00:20:21272 }
Luigi Semenzato214cd282018-01-27 01:15:59273 }
274 // If all went well, the symlink no longer exists. Recreate it.
Vladislav Kuzkokov5c7ff5a2019-06-26 16:52:34275 base::FilePath relative_target_path = target_path.BaseName();
276 if (!base::CreateSymbolicLink(relative_target_path, symlink_path)) {
Luigi Semenzato214cd282018-01-27 01:15:59277 DPLOG(ERROR) << "Unable to create symlink " << symlink_path.value()
Vladislav Kuzkokov5c7ff5a2019-06-26 16:52:34278 << " pointing at " << relative_target_path.value();
[email protected]c7d5da992010-10-28 00:20:21279 }
280 return target_path;
281}
282
[email protected]a7329162013-02-07 19:21:48283void RemoveSymlinkAndLog(const base::FilePath& link_path,
284 const base::FilePath& target_path) {
[email protected]c7d5da992010-10-28 00:20:21285 if (::unlink(link_path.value().c_str()) == -1)
[email protected]26f025e2011-10-28 22:49:27286 DPLOG(WARNING) << "Unable to unlink symlink " << link_path.value();
yoshiki iguchibf61e0f2022-05-17 17:57:50287 if (target_path != link_path && ::unlink(target_path.value().c_str()) == -1)
[email protected]26f025e2011-10-28 22:49:27288 DPLOG(WARNING) << "Unable to unlink log file " << target_path.value();
[email protected]c7d5da992010-10-28 00:20:21289}
290
avi79bf9132014-12-25 17:48:05291base::FilePath GetSessionLogDir(const base::CommandLine& command_line) {
Hidehiko Abedcb0774a2022-07-01 19:30:37292 std::string log_dir;
thakis8b51c0a2016-04-12 19:48:18293 std::unique_ptr<base::Environment> env(base::Environment::Create());
Hidehiko Abedcb0774a2022-07-01 19:30:37294 if (!env->GetVar(env_vars::kSessionLogDir, &log_dir))
295 NOTREACHED();
296 return base::FilePath(log_dir);
[email protected]cc5ff9852014-07-23 22:29:29297}
298
avi79bf9132014-12-25 17:48:05299base::FilePath GetSessionLogFile(const base::CommandLine& command_line) {
Michael Giuffrida6802ffe2017-09-29 03:40:25300 return GetSessionLogDir(command_line)
301 .Append(GetLogFileName(command_line).BaseName());
[email protected]0750b1442010-11-02 21:59:37302}
303
yoshiki iguchibf61e0f2022-05-17 17:57:50304base::FilePath SetUpLogFile(const base::FilePath& target_path, bool new_log) {
305 const bool supports_symlinks =
306 !(target_path.IsAbsolute() &&
307 base::StartsWith(target_path.value(), kChronosHomeDir));
308
309 // TODO(crbug.com/1326369): Remove the old symlink logic.
310 if (supports_symlinks) {
311 // As for now, we keep the original log rotation logic on the file system
312 // which supports symlinks.
313 return SetUpSymlinkIfNeeded(target_path, new_log);
314 }
315
316 // Chrome OS doesn't support symlinks on this file system, so that it uses
317 // the rotation logic which doesn't use symlinks.
318 if (!new_log) {
319 // Keep using the same log file without doing anything.
320 return target_path;
321 }
322
323 // For backward compatibility, ignore a ".LATEST" extension the way
324 // |SetUpSymlinkIfNeeded()| does.
325 base::FilePath bare_path = target_path;
326 if (target_path.Extension() == ".LATEST") {
327 bare_path = target_path.ReplaceExtension("");
328 }
329
330 // Try to rotate the log.
331 if (!RotateLogFile(bare_path)) {
332 DPLOG(ERROR) << "Failed to rotate the log file: " << bare_path.value()
333 << ". Keeping using the same log file without rotating.";
334 }
335
336 return bare_path;
337}
338
Yuta Hijikata2bac1282020-11-24 03:51:27339#endif // BUILDFLAG(IS_CHROMEOS_ASH)
[email protected]77ee1b12010-06-14 16:21:30340
avi79bf9132014-12-25 17:48:05341void InitChromeLogging(const base::CommandLine& command_line,
[email protected]77ee1b12010-06-14 16:21:30342 OldFileDeletionState delete_old_log_file) {
343 DCHECK(!chrome_logging_initialized_) <<
344 "Attempted to initialize logging when it was already initialized.";
achuith7ea0aaca62017-06-01 20:31:42345 LoggingDestination logging_dest = DetermineLoggingDestination(command_line);
[email protected]d64ea42172012-09-14 05:43:59346 LogLockingState log_locking_state = LOCK_LOG_FILE;
[email protected]a7329162013-02-07 19:21:48347 base::FilePath log_path;
Yuta Hijikata2bac1282020-11-24 03:51:27348#if BUILDFLAG(IS_CHROMEOS_ASH)
[email protected]a7329162013-02-07 19:21:48349 base::FilePath target_path;
[email protected]4a100992011-06-14 18:22:46350#endif
[email protected]77ee1b12010-06-14 16:21:30351
[email protected]4a100992011-06-14 18:22:46352 // Don't resolve the log path unless we need to. Otherwise we leave an open
353 // ALPC handle after sandbox lockdown on Windows.
[email protected]5e3f7c22013-06-21 21:15:33354 if ((logging_dest & LOG_TO_FILE) != 0) {
Michael Giuffrida6802ffe2017-09-29 03:40:25355 log_path = GetLogFileName(command_line);
[email protected]c7d5da992010-10-28 00:20:21356
Yuta Hijikata2bac1282020-11-24 03:51:27357#if BUILDFLAG(IS_CHROMEOS_ASH)
[email protected]4a100992011-06-14 18:22:46358 // For BWSI (Incognito) logins, we want to put the logs in the user
359 // profile directory that is created for the temporary session instead
360 // of in the system log directory, for privacy reasons.
Henrique Ferreiro93dd33b2022-01-18 16:06:45361 if (command_line.HasSwitch(ash::switches::kGuestSession))
[email protected]4a100992011-06-14 18:22:46362 log_path = GetSessionLogFile(command_line);
[email protected]0750b1442010-11-02 21:59:37363
yoshiki iguchibf61e0f2022-05-17 17:57:50364 // Prepares a log file. We rotate the previous log file and prepare a new
365 // log file if we've been asked to delete the old log, since that
[email protected]4a100992011-06-14 18:22:46366 // indicates the start of a new session.
yoshiki iguchibf61e0f2022-05-17 17:57:50367 target_path =
368 SetUpLogFile(log_path, delete_old_log_file == DELETE_OLD_LOG_FILE);
[email protected]c7d5da992010-10-28 00:20:21369
[email protected]4a100992011-06-14 18:22:46370 // Because ChromeOS manages the move to a new session by redirecting
371 // the link, it shouldn't remove the old file in the logging code,
372 // since that will remove the newly created link instead.
achuith7ea0aaca62017-06-01 20:31:42373 delete_old_log_file = APPEND_TO_OLD_LOG_FILE;
Yuta Hijikata2bac1282020-11-24 03:51:27374#endif // BUILDFLAG(IS_CHROMEOS_ASH)
[email protected]d64ea42172012-09-14 05:43:59375 } else {
376 log_locking_state = DONT_LOCK_LOG_FILE;
[email protected]4a100992011-06-14 18:22:46377 }
[email protected]77ee1b12010-06-14 16:21:30378
achuith7ea0aaca62017-06-01 20:31:42379 LoggingSettings settings;
[email protected]5e3f7c22013-06-21 21:15:33380 settings.logging_dest = logging_dest;
Robbie McElrath8bf49842019-08-20 22:22:53381 settings.log_file_path = log_path.value().c_str();
[email protected]5e3f7c22013-06-21 21:15:33382 settings.lock_log = log_locking_state;
383 settings.delete_old = delete_old_log_file;
achuith7ea0aaca62017-06-01 20:31:42384 bool success = InitLogging(settings);
[email protected]c7d5da992010-10-28 00:20:21385
Yuta Hijikata2bac1282020-11-24 03:51:27386#if BUILDFLAG(IS_CHROMEOS_ASH)
[email protected]c7d5da992010-10-28 00:20:21387 if (!success) {
[email protected]26f025e2011-10-28 22:49:27388 DPLOG(ERROR) << "Unable to initialize logging to " << log_path.value()
[email protected]c7d5da992010-10-28 00:20:21389 << " (which should be a link to " << target_path.value() << ")";
390 RemoveSymlinkAndLog(log_path, target_path);
[email protected]7109dc32011-02-02 22:58:29391 chrome_logging_failed_ = true;
[email protected]c7d5da992010-10-28 00:20:21392 return;
393 }
Yuta Hijikata2bac1282020-11-24 03:51:27394#else // BUILDFLAG(IS_CHROMEOS_ASH)
[email protected]c7d5da992010-10-28 00:20:21395 if (!success) {
[email protected]26f025e2011-10-28 22:49:27396 DPLOG(ERROR) << "Unable to initialize logging to " << log_path.value();
[email protected]7109dc32011-02-02 22:58:29397 chrome_logging_failed_ = true;
[email protected]c7d5da992010-10-28 00:20:21398 return;
399 }
Yuta Hijikata2bac1282020-11-24 03:51:27400#endif // BUILDFLAG(IS_CHROMEOS_ASH)
initial.commit09911bf2008-07-26 23:55:29401
Greg Thompsond548ffa2017-10-31 08:25:05402 // We call running in unattended mode "headless", and allow headless mode to
403 // be configured either by the Environment Variable or by the Command Line
404 // Switch. This is for automated test purposes.
405 std::unique_ptr<base::Environment> env(base::Environment::Create());
406 const bool is_headless = env->HasVar(env_vars::kHeadless) ||
407 command_line.HasSwitch(switches::kNoErrorDialogs);
408
409 // Show fatal log messages in a dialog in debug builds when not headless.
410 if (!is_headless)
achuith7ea0aaca62017-06-01 20:31:42411 SetShowErrorDialogs(true);
[email protected]81e0a852010-08-17 00:38:12412
initial.commit09911bf2008-07-26 23:55:29413 // we want process and thread IDs because we have a lot of things running
achuith7ea0aaca62017-06-01 20:31:42414 SetLogItems(true, // enable_process_id
415 true, // enable_thread_id
416 true, // enable_timestamp
417 false); // enable_tickcount
initial.commit09911bf2008-07-26 23:55:29418
Greg Thompsond548ffa2017-10-31 08:25:05419 // Suppress system error dialogs when headless.
420 if (is_headless)
initial.commit09911bf2008-07-26 23:55:29421 SuppressDialogs();
422
emaxx0bcc7aa662015-05-15 22:25:26423 // Use a minimum log level if the command line asks for one. Ignore this
424 // switch if there's vlog level switch present too (as both of these switches
425 // refer to the same underlying log level, and the vlog level switch has
achuith7ea0aaca62017-06-01 20:31:42426 // already been processed inside InitLogging). If there is neither
emaxx0bcc7aa662015-05-15 22:25:26427 // log level nor vlog level specified, then just leave the default level
428 // (INFO).
429 if (command_line.HasSwitch(switches::kLoggingLevel) &&
achuith7ea0aaca62017-06-01 20:31:42430 GetMinLogLevel() >= 0) {
emaxx0bcc7aa662015-05-15 22:25:26431 std::string log_level =
432 command_line.GetSwitchValueASCII(switches::kLoggingLevel);
[email protected]3deeb6b32010-10-05 21:53:52433 int level = 0;
emaxx0bcc7aa662015-05-15 22:25:26434 if (base::StringToInt(log_level, &level) && level >= 0 &&
Lei Zhange4722bc2020-10-23 22:39:53435 level < LOGGING_NUM_SEVERITIES) {
achuith7ea0aaca62017-06-01 20:31:42436 SetMinLogLevel(level);
[email protected]3deeb6b32010-10-05 21:53:52437 } else {
[email protected]26f025e2011-10-28 22:49:27438 DLOG(WARNING) << "Bad log level: " << log_level;
[email protected]3deeb6b32010-10-05 21:53:52439 }
[email protected]bb5185c52008-08-29 19:51:06440 }
441
Xiaohan Wang4d5c5042022-01-18 21:54:37442#if BUILDFLAG(IS_WIN)
[email protected]2b07b8412009-11-25 15:26:34443 // Enable trace control and transport through event tracing for Windows.
achuith7ea0aaca62017-06-01 20:31:42444 LogEventProvider::Initialize(kChromeTraceProviderName);
pastarmovja3eac432016-12-07 17:38:52445
446 // Enable logging to the Windows Event Log.
Peter Kasting53264c412021-02-04 18:35:08447 SetEventSource(base::WideToASCII(
probergefe476002017-06-23 14:29:01448 install_static::InstallDetails::Get().install_full_name()),
449 BROWSER_CATEGORY, MSG_LOG_MESSAGE);
[email protected]2b07b8412009-11-25 15:26:34450#endif
451
boriay648b37322016-07-05 08:15:29452 base::StatisticsRecorder::InitLogOnShutdown();
453
initial.commit09911bf2008-07-26 23:55:29454 chrome_logging_initialized_ = true;
455}
456
457// This is a no-op, but we'll keep it around in case
458// we need to do more cleanup in the future.
459void CleanupChromeLogging() {
[email protected]7109dc32011-02-02 22:58:29460 if (chrome_logging_failed_)
461 return; // We failed to initiailize logging, no cleanup.
462
yoshiki iguchibf61e0f2022-05-17 17:57:50463 DCHECK(chrome_logging_initialized_)
464 << "Attempted to clean up logging when it wasn't initialized.";
initial.commit09911bf2008-07-26 23:55:29465
466 CloseLogFile();
467
468 chrome_logging_initialized_ = false;
[email protected]30f547c2010-08-04 01:00:01469 chrome_logging_redirected_ = false;
initial.commit09911bf2008-07-26 23:55:29470}
471
Michael Giuffrida6802ffe2017-09-29 03:40:25472base::FilePath GetLogFileName(const base::CommandLine& command_line) {
Andrew Naumov2a4acd562022-02-10 07:12:34473 auto filename = command_line.GetSwitchValueNative(switches::kLogFile);
Michael Giuffrida6802ffe2017-09-29 03:40:25474 if (!filename.empty())
Andrew Naumov2a4acd562022-02-10 07:12:34475 return base::FilePath(filename);
476
477 std::string env_filename;
478 base::Environment::Create()->GetVar(env_vars::kLogFileName, &env_filename);
479 if (!env_filename.empty())
480 return base::FilePath::FromUTF8Unsafe(env_filename);
initial.commit09911bf2008-07-26 23:55:29481
[email protected]a7329162013-02-07 19:21:48482 const base::FilePath log_filename(FILE_PATH_LITERAL("chrome_debug.log"));
483 base::FilePath log_path;
initial.commit09911bf2008-07-26 23:55:29484
Avi Drissman7d43b4b2018-05-01 16:14:06485 if (base::PathService::Get(chrome::DIR_LOGS, &log_path)) {
[email protected]73e23482009-08-13 00:26:58486 log_path = log_path.Append(log_filename);
initial.commit09911bf2008-07-26 23:55:29487 return log_path;
488 } else {
489 // error with path service, just use some default file somewhere
490 return log_filename;
491 }
492}
493
494bool DialogsAreSuppressed() {
495 return dialogs_are_suppressed_;
496}
Luigi Semenzato52384302017-08-10 20:22:59497
Yuta Hijikata2bac1282020-11-24 03:51:27498#if BUILDFLAG(IS_CHROMEOS_ASH)
[email protected]a7329162013-02-07 19:21:48499base::FilePath GenerateTimestampedName(const base::FilePath& base_path,
500 base::Time timestamp) {
[email protected]395a0392012-04-12 15:35:03501 base::Time::Exploded time_deets;
Georg Neisc8193ec2022-01-20 12:27:41502 timestamp.UTCExplode(&time_deets);
[email protected]395a0392012-04-12 15:35:03503 std::string suffix = base::StringPrintf("_%02d%02d%02d-%02d%02d%02d",
504 time_deets.year,
505 time_deets.month,
506 time_deets.day_of_month,
507 time_deets.hour,
508 time_deets.minute,
509 time_deets.second);
Luigi Semenzato214cd282018-01-27 01:15:59510 return base_path.InsertBeforeExtensionASCII(suffix);
[email protected]395a0392012-04-12 15:35:03511}
Yuta Hijikata2bac1282020-11-24 03:51:27512#endif // BUILDFLAG(IS_CHROMEOS_ASH)
[email protected]395a0392012-04-12 15:35:03513
[email protected]c83dd912010-04-06 18:50:51514} // namespace logging