blob: 1194522f6c0bb45d7f4e9f4566e0a269282d6e8e [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.
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
[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"
initial.commit09911bf2008-07-26 23:55:2936#include "base/command_line.h"
[email protected]0eb9f4342008-11-21 18:48:5237#include "base/compiler_specific.h"
[email protected]58580352010-10-26 04:07:5038#include "base/debug/debugger.h"
[email protected]5a8d4ce2013-12-18 17:42:2739#include "base/debug/dump_without_crashing.h"
[email protected]76b90d312010-08-03 03:00:5040#include "base/environment.h"
[email protected]57999812013-02-24 05:40:5241#include "base/files/file_path.h"
thestig18dfb7a52014-08-26 10:44:0442#include "base/files/file_util.h"
initial.commit09911bf2008-07-26 23:55:2943#include "base/path_service.h"
[email protected]3ea1b182013-02-08 22:38:4144#include "base/strings/string_number_conversions.h"
[email protected]3c8a6b02013-06-11 00:49:4945#include "base/strings/string_util.h"
46#include "base/strings/stringprintf.h"
[email protected]12bfb612013-06-07 19:54:0247#include "base/strings/utf_string_conversions.h"
[email protected]61f93212011-01-24 14:37:0848#include "base/threading/thread_restrictions.h"
[email protected]ab7ee5b2011-03-01 22:24:1449#include "chrome/common/chrome_constants.h"
initial.commit09911bf2008-07-26 23:55:2950#include "chrome/common/chrome_paths.h"
51#include "chrome/common/chrome_switches.h"
initial.commit09911bf2008-07-26 23:55:2952#include "chrome/common/env_vars.h"
brettw90e92602015-10-10 00:12:4053#include "content/public/common/content_switches.h"
[email protected]51d5e162009-07-27 19:23:5454#include "ipc/ipc_logging.h"
[email protected]ab7ee5b2011-03-01 22:24:1455
[email protected]931d1042013-04-05 17:50:4456#if defined(OS_CHROMEOS)
57#include "chromeos/chromeos_switches.h"
58#endif
59
[email protected]2b07b8412009-11-25 15:26:3460#if defined(OS_WIN)
[email protected]2b07b8412009-11-25 15:26:3461#include <initguid.h>
[email protected]3ea1b182013-02-08 22:38:4162#include "base/logging_win.h"
[email protected]2b07b8412009-11-25 15:26:3463#endif
initial.commit09911bf2008-07-26 23:55:2964
[email protected]ab7ee5b2011-03-01 22:24:1465namespace {
66
initial.commit09911bf2008-07-26 23:55:2967// When true, this means that error dialogs should not be shown.
[email protected]ab7ee5b2011-03-01 22:24:1468bool dialogs_are_suppressed_ = false;
initial.commit09911bf2008-07-26 23:55:2969
70// This should be true for exactly the period between the end of
71// InitChromeLogging() and the beginning of CleanupChromeLogging().
[email protected]ab7ee5b2011-03-01 22:24:1472bool chrome_logging_initialized_ = false;
initial.commit09911bf2008-07-26 23:55:2973
[email protected]4a6d74152011-09-26 20:42:3074// Set if we called InitChromeLogging() but failed to initialize.
[email protected]ab7ee5b2011-03-01 22:24:1475bool chrome_logging_failed_ = false;
[email protected]7109dc32011-02-02 22:58:2976
[email protected]30f547c2010-08-04 01:00:0177// 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_redirected_ = false;
[email protected]30f547c2010-08-04 01:00:0180
[email protected]2b07b8412009-11-25 15:26:3481#if defined(OS_WIN)
82// {7FE69228-633E-4f06-80C1-527FEA23E3A7}
[email protected]ab7ee5b2011-03-01 22:24:1483const GUID kChromeTraceProviderName = {
[email protected]162ac0f2010-11-04 15:50:4984 0x7fe69228, 0x633e, 0x4f06,
85 { 0x80, 0xc1, 0x52, 0x7f, 0xea, 0x23, 0xe3, 0xa7 } };
[email protected]2b07b8412009-11-25 15:26:3486#endif
87
initial.commit09911bf2008-07-26 23:55:2988// Assertion handler for logging errors that occur when dialogs are
89// silenced. To record a new error, pass the log string associated
90// with that error in the str parameter.
[email protected]0eb9f4342008-11-21 18:48:5291MSVC_DISABLE_OPTIMIZE();
[email protected]ab7ee5b2011-03-01 22:24:1492void SilentRuntimeAssertHandler(const std::string& str) {
[email protected]58580352010-10-26 04:07:5093 base::debug::BreakDebugger();
initial.commit09911bf2008-07-26 23:55:2994}
[email protected]0eb9f4342008-11-21 18:48:5295MSVC_ENABLE_OPTIMIZE();
initial.commit09911bf2008-07-26 23:55:2996
97// Suppresses error/assertion dialogs and enables the logging of
98// those errors into silenced_errors_.
[email protected]ab7ee5b2011-03-01 22:24:1499void SuppressDialogs() {
initial.commit09911bf2008-07-26 23:55:29100 if (dialogs_are_suppressed_)
101 return;
102
103 logging::SetLogAssertHandler(SilentRuntimeAssertHandler);
104
[email protected]ec657802008-11-07 20:05:39105#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:29106 UINT new_flags = SEM_FAILCRITICALERRORS |
107 SEM_NOGPFAULTERRORBOX |
108 SEM_NOOPENFILEERRORBOX;
109
110 // Preserve existing error mode, as discussed at http://t/dmea
111 UINT existing_flags = SetErrorMode(new_flags);
112 SetErrorMode(existing_flags | new_flags);
[email protected]ec657802008-11-07 20:05:39113#endif
initial.commit09911bf2008-07-26 23:55:29114
115 dialogs_are_suppressed_ = true;
116}
117
[email protected]ab7ee5b2011-03-01 22:24:14118} // anonymous namespace
119
initial.commit09911bf2008-07-26 23:55:29120namespace logging {
121
avi79bf9132014-12-25 17:48:05122LoggingDestination DetermineLogMode(const base::CommandLine& command_line) {
initial.commit09911bf2008-07-26 23:55:29123 // only use OutputDebugString in debug mode
124#ifdef NDEBUG
125 bool enable_logging = false;
[email protected]b7e0a2a2009-10-13 02:07:25126 const char *kInvertLoggingSwitch = switches::kEnableLogging;
[email protected]5e3f7c22013-06-21 21:15:33127 const logging::LoggingDestination kDefaultLoggingMode = logging::LOG_TO_FILE;
initial.commit09911bf2008-07-26 23:55:29128#else
129 bool enable_logging = true;
[email protected]b7e0a2a2009-10-13 02:07:25130 const char *kInvertLoggingSwitch = switches::kDisableLogging;
[email protected]5e3f7c22013-06-21 21:15:33131 const logging::LoggingDestination kDefaultLoggingMode = logging::LOG_TO_ALL;
initial.commit09911bf2008-07-26 23:55:29132#endif
133
134 if (command_line.HasSwitch(kInvertLoggingSwitch))
135 enable_logging = !enable_logging;
136
137 logging::LoggingDestination log_mode;
138 if (enable_logging) {
[email protected]3bba9582009-08-05 01:29:12139 // Let --enable-logging=stderr force only stderr, particularly useful for
140 // non-debug builds where otherwise you can't get logs to stderr at all.
[email protected]c4e52f0d2009-11-06 19:55:16141 if (command_line.GetSwitchValueASCII(switches::kEnableLogging) == "stderr")
[email protected]5e3f7c22013-06-21 21:15:33142 log_mode = logging::LOG_TO_SYSTEM_DEBUG_LOG;
[email protected]3bba9582009-08-05 01:29:12143 else
144 log_mode = kDefaultLoggingMode;
initial.commit09911bf2008-07-26 23:55:29145 } else {
146 log_mode = logging::LOG_NONE;
147 }
[email protected]77ee1b12010-06-14 16:21:30148 return log_mode;
149}
initial.commit09911bf2008-07-26 23:55:29150
[email protected]77ee1b12010-06-14 16:21:30151#if defined(OS_CHROMEOS)
[email protected]c7d5da992010-10-28 00:20:21152namespace {
[email protected]a7329162013-02-07 19:21:48153base::FilePath SetUpSymlinkIfNeeded(const base::FilePath& symlink_path,
154 bool new_log) {
[email protected]f25b33a2010-10-28 20:15:51155 DCHECK(!symlink_path.empty());
156
[email protected]c7d5da992010-10-28 00:20:21157 // If not starting a new log, then just log through the existing
158 // symlink, but if the symlink doesn't exist, create it. If
159 // starting a new log, then delete the old symlink and make a new
160 // one to a fresh log file.
[email protected]a7329162013-02-07 19:21:48161 base::FilePath target_path;
[email protected]7567484142013-07-11 17:36:07162 bool symlink_exists = base::PathExists(symlink_path);
[email protected]0750b1442010-11-02 21:59:37163 if (new_log || !symlink_exists) {
[email protected]c7d5da992010-10-28 00:20:21164 target_path = GenerateTimestampedName(symlink_path, base::Time::Now());
165
166 // We don't care if the unlink fails; we're going to continue anyway.
[email protected]723571a2010-12-03 17:37:54167 if (::unlink(symlink_path.value().c_str()) == -1) {
[email protected]0750b1442010-11-02 21:59:37168 if (symlink_exists) // only warn if we might expect it to succeed.
[email protected]26f025e2011-10-28 22:49:27169 DPLOG(WARNING) << "Unable to unlink " << symlink_path.value();
[email protected]c7d5da992010-10-28 00:20:21170 }
[email protected]b264eab2013-11-27 23:22:08171 if (!base::CreateSymbolicLink(target_path, symlink_path)) {
[email protected]26f025e2011-10-28 22:49:27172 DPLOG(ERROR) << "Unable to create symlink " << symlink_path.value()
173 << " pointing at " << target_path.value();
[email protected]c7d5da992010-10-28 00:20:21174 }
175 } else {
[email protected]b264eab2013-11-27 23:22:08176 if (!base::ReadSymbolicLink(symlink_path, &target_path))
[email protected]26f025e2011-10-28 22:49:27177 DPLOG(ERROR) << "Unable to read symlink " << symlink_path.value();
[email protected]c7d5da992010-10-28 00:20:21178 }
179 return target_path;
180}
181
[email protected]a7329162013-02-07 19:21:48182void RemoveSymlinkAndLog(const base::FilePath& link_path,
183 const base::FilePath& target_path) {
[email protected]c7d5da992010-10-28 00:20:21184 if (::unlink(link_path.value().c_str()) == -1)
[email protected]26f025e2011-10-28 22:49:27185 DPLOG(WARNING) << "Unable to unlink symlink " << link_path.value();
[email protected]c7d5da992010-10-28 00:20:21186 if (::unlink(target_path.value().c_str()) == -1)
[email protected]26f025e2011-10-28 22:49:27187 DPLOG(WARNING) << "Unable to unlink log file " << target_path.value();
[email protected]c7d5da992010-10-28 00:20:21188}
189
190} // anonymous namespace
191
avi79bf9132014-12-25 17:48:05192base::FilePath GetSessionLogDir(const base::CommandLine& command_line) {
[email protected]a7329162013-02-07 19:21:48193 base::FilePath log_dir;
[email protected]0750b1442010-11-02 21:59:37194 std::string log_dir_str;
thakis8b51c0a2016-04-12 19:48:18195 std::unique_ptr<base::Environment> env(base::Environment::Create());
[email protected]0750b1442010-11-02 21:59:37196 if (env->GetVar(env_vars::kSessionLogDir, &log_dir_str) &&
197 !log_dir_str.empty()) {
[email protected]a7329162013-02-07 19:21:48198 log_dir = base::FilePath(log_dir_str);
[email protected]68e31692013-05-20 13:08:27199 } else if (command_line.HasSwitch(chromeos::switches::kLoginProfile)) {
[email protected]0750b1442010-11-02 21:59:37200 PathService::Get(chrome::DIR_USER_DATA, &log_dir);
[email protected]68e31692013-05-20 13:08:27201 base::FilePath profile_dir;
[email protected]9c8b1f92013-09-19 14:39:59202 std::string login_profile_value =
203 command_line.GetSwitchValueASCII(chromeos::switches::kLoginProfile);
[email protected]64146ec2014-03-19 21:52:25204 if (login_profile_value == chrome::kLegacyProfileDir ||
205 login_profile_value == chrome::kTestUserProfileDir) {
[email protected]9c8b1f92013-09-19 14:39:59206 profile_dir = base::FilePath(login_profile_value);
207 } else {
[email protected]68e31692013-05-20 13:08:27208 // We could not use g_browser_process > profile_helper() here.
209 std::string profile_dir_str = chrome::kProfileDirPrefix;
[email protected]9c8b1f92013-09-19 14:39:59210 profile_dir_str.append(login_profile_value);
[email protected]68e31692013-05-20 13:08:27211 profile_dir = base::FilePath(profile_dir_str);
[email protected]68e31692013-05-20 13:08:27212 }
213 log_dir = log_dir.Append(profile_dir);
[email protected]0750b1442010-11-02 21:59:37214 }
[email protected]cc5ff9852014-07-23 22:29:29215 return log_dir;
216}
217
avi79bf9132014-12-25 17:48:05218base::FilePath GetSessionLogFile(const base::CommandLine& command_line) {
[email protected]cc5ff9852014-07-23 22:29:29219 return GetSessionLogDir(command_line).Append(GetLogFileName().BaseName());
[email protected]0750b1442010-11-02 21:59:37220}
221
avi79bf9132014-12-25 17:48:05222void RedirectChromeLogging(const base::CommandLine& command_line) {
[email protected]f9357a442014-05-15 18:44:07223 if (chrome_logging_redirected_) {
[email protected]e718e6f2013-04-15 16:01:59224 // TODO(nkostylev): Support multiple active users. http://crbug.com/230345
[email protected]f9357a442014-05-15 18:44:07225 LOG(WARNING) << "NOT redirecting logging for multi-profiles case.";
[email protected]e718e6f2013-04-15 16:01:59226 return;
227 }
228
[email protected]30f547c2010-08-04 01:00:01229 DCHECK(!chrome_logging_redirected_) <<
230 "Attempted to redirect logging when it was already initialized.";
[email protected]0750b1442010-11-02 21:59:37231
232 // Redirect logs to the session log directory, if set. Otherwise
233 // defaults to the profile dir.
[email protected]a7329162013-02-07 19:21:48234 base::FilePath log_path = GetSessionLogFile(command_line);
[email protected]c7d5da992010-10-28 00:20:21235
[email protected]61f93212011-01-24 14:37:08236 // Creating symlink causes us to do blocking IO on UI thread.
237 // Temporarily allow it until we fix http://crbug.com/61143
238 base::ThreadRestrictions::ScopedAllowIO allow_io;
[email protected]c7d5da992010-10-28 00:20:21239 // Always force a new symlink when redirecting.
[email protected]a7329162013-02-07 19:21:48240 base::FilePath target_path = SetUpSymlinkIfNeeded(log_path, true);
[email protected]c7d5da992010-10-28 00:20:21241
242 // ChromeOS always logs through the symlink, so it shouldn't be
243 // deleted if it already exists.
[email protected]5e3f7c22013-06-21 21:15:33244 logging::LoggingSettings settings;
245 settings.logging_dest = DetermineLogMode(command_line);
246 settings.log_file = log_path.value().c_str();
[email protected]5e3f7c22013-06-21 21:15:33247 if (!logging::InitLogging(settings)) {
[email protected]26f025e2011-10-28 22:49:27248 DLOG(ERROR) << "Unable to initialize logging to " << log_path.value();
[email protected]c7d5da992010-10-28 00:20:21249 RemoveSymlinkAndLog(log_path, target_path);
250 } else {
251 chrome_logging_redirected_ = true;
252 }
[email protected]77ee1b12010-06-14 16:21:30253}
[email protected]0750b1442010-11-02 21:59:37254
[email protected]85271192011-05-10 20:38:25255#endif // OS_CHROMEOS
[email protected]77ee1b12010-06-14 16:21:30256
avi79bf9132014-12-25 17:48:05257void InitChromeLogging(const base::CommandLine& command_line,
[email protected]77ee1b12010-06-14 16:21:30258 OldFileDeletionState delete_old_log_file) {
259 DCHECK(!chrome_logging_initialized_) <<
260 "Attempted to initialize logging when it was already initialized.";
261
[email protected]4a100992011-06-14 18:22:46262 LoggingDestination logging_dest = DetermineLogMode(command_line);
[email protected]d64ea42172012-09-14 05:43:59263 LogLockingState log_locking_state = LOCK_LOG_FILE;
[email protected]a7329162013-02-07 19:21:48264 base::FilePath log_path;
[email protected]4a100992011-06-14 18:22:46265#if defined(OS_CHROMEOS)
[email protected]a7329162013-02-07 19:21:48266 base::FilePath target_path;
[email protected]4a100992011-06-14 18:22:46267#endif
[email protected]77ee1b12010-06-14 16:21:30268
[email protected]4a100992011-06-14 18:22:46269 // Don't resolve the log path unless we need to. Otherwise we leave an open
270 // ALPC handle after sandbox lockdown on Windows.
[email protected]5e3f7c22013-06-21 21:15:33271 if ((logging_dest & LOG_TO_FILE) != 0) {
[email protected]4a100992011-06-14 18:22:46272 log_path = GetLogFileName();
[email protected]c7d5da992010-10-28 00:20:21273
[email protected]77ee1b12010-06-14 16:21:30274#if defined(OS_CHROMEOS)
[email protected]4a100992011-06-14 18:22:46275 // For BWSI (Incognito) logins, we want to put the logs in the user
276 // profile directory that is created for the temporary session instead
277 // of in the system log directory, for privacy reasons.
[email protected]931d1042013-04-05 17:50:44278 if (command_line.HasSwitch(chromeos::switches::kGuestSession))
[email protected]4a100992011-06-14 18:22:46279 log_path = GetSessionLogFile(command_line);
[email protected]0750b1442010-11-02 21:59:37280
[email protected]4a100992011-06-14 18:22:46281 // On ChromeOS we log to the symlink. We force creation of a new
282 // symlink if we've been asked to delete the old log, since that
283 // indicates the start of a new session.
284 target_path = SetUpSymlinkIfNeeded(
285 log_path, delete_old_log_file == logging::DELETE_OLD_LOG_FILE);
[email protected]c7d5da992010-10-28 00:20:21286
[email protected]4a100992011-06-14 18:22:46287 // Because ChromeOS manages the move to a new session by redirecting
288 // the link, it shouldn't remove the old file in the logging code,
289 // since that will remove the newly created link instead.
290 delete_old_log_file = logging::APPEND_TO_OLD_LOG_FILE;
[email protected]77ee1b12010-06-14 16:21:30291#endif
[email protected]d64ea42172012-09-14 05:43:59292 } else {
293 log_locking_state = DONT_LOCK_LOG_FILE;
[email protected]4a100992011-06-14 18:22:46294 }
[email protected]77ee1b12010-06-14 16:21:30295
[email protected]5e3f7c22013-06-21 21:15:33296 logging::LoggingSettings settings;
297 settings.logging_dest = logging_dest;
298 settings.log_file = log_path.value().c_str();
299 settings.lock_log = log_locking_state;
300 settings.delete_old = delete_old_log_file;
[email protected]5e3f7c22013-06-21 21:15:33301 bool success = logging::InitLogging(settings);
[email protected]c7d5da992010-10-28 00:20:21302
303#if defined(OS_CHROMEOS)
304 if (!success) {
[email protected]26f025e2011-10-28 22:49:27305 DPLOG(ERROR) << "Unable to initialize logging to " << log_path.value()
[email protected]c7d5da992010-10-28 00:20:21306 << " (which should be a link to " << target_path.value() << ")";
307 RemoveSymlinkAndLog(log_path, target_path);
[email protected]7109dc32011-02-02 22:58:29308 chrome_logging_failed_ = true;
[email protected]c7d5da992010-10-28 00:20:21309 return;
310 }
311#else
312 if (!success) {
[email protected]26f025e2011-10-28 22:49:27313 DPLOG(ERROR) << "Unable to initialize logging to " << log_path.value();
[email protected]7109dc32011-02-02 22:58:29314 chrome_logging_failed_ = true;
[email protected]c7d5da992010-10-28 00:20:21315 return;
316 }
317#endif
initial.commit09911bf2008-07-26 23:55:29318
[email protected]81e0a852010-08-17 00:38:12319 // Default to showing error dialogs.
avi79bf9132014-12-25 17:48:05320 if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
321 switches::kNoErrorDialogs))
[email protected]81e0a852010-08-17 00:38:12322 logging::SetShowErrorDialogs(true);
323
initial.commit09911bf2008-07-26 23:55:29324 // we want process and thread IDs because we have a lot of things running
[email protected]c7d5da992010-10-28 00:20:21325 logging::SetLogItems(true, // enable_process_id
326 true, // enable_thread_id
[email protected]51f5a60c2012-09-01 06:07:55327 true, // enable_timestamp
328 false); // enable_tickcount
initial.commit09911bf2008-07-26 23:55:29329
330 // We call running in unattended mode "headless", and allow
331 // headless mode to be configured either by the Environment
332 // Variable or by the Command Line Switch. This is for
333 // automated test purposes.
thakis8b51c0a2016-04-12 19:48:18334 std::unique_ptr<base::Environment> env(base::Environment::Create());
[email protected]9432ade2010-08-04 23:43:20335 if (env->HasVar(env_vars::kHeadless) ||
initial.commit09911bf2008-07-26 23:55:29336 command_line.HasSwitch(switches::kNoErrorDialogs))
337 SuppressDialogs();
338
emaxx0bcc7aa662015-05-15 22:25:26339 // Use a minimum log level if the command line asks for one. Ignore this
340 // switch if there's vlog level switch present too (as both of these switches
341 // refer to the same underlying log level, and the vlog level switch has
342 // already been processed inside logging::InitLogging). If there is neither
343 // log level nor vlog level specified, then just leave the default level
344 // (INFO).
345 if (command_line.HasSwitch(switches::kLoggingLevel) &&
346 logging::GetMinLogLevel() >= 0) {
347 std::string log_level =
348 command_line.GetSwitchValueASCII(switches::kLoggingLevel);
[email protected]3deeb6b32010-10-05 21:53:52349 int level = 0;
emaxx0bcc7aa662015-05-15 22:25:26350 if (base::StringToInt(log_level, &level) && level >= 0 &&
351 level < LOG_NUM_SEVERITIES) {
[email protected]bb5185c52008-08-29 19:51:06352 logging::SetMinLogLevel(level);
[email protected]3deeb6b32010-10-05 21:53:52353 } else {
[email protected]26f025e2011-10-28 22:49:27354 DLOG(WARNING) << "Bad log level: " << log_level;
[email protected]3deeb6b32010-10-05 21:53:52355 }
[email protected]bb5185c52008-08-29 19:51:06356 }
357
[email protected]2b07b8412009-11-25 15:26:34358#if defined(OS_WIN)
359 // Enable trace control and transport through event tracing for Windows.
[email protected]b650bf42012-03-15 01:02:24360 logging::LogEventProvider::Initialize(kChromeTraceProviderName);
[email protected]2b07b8412009-11-25 15:26:34361#endif
362
initial.commit09911bf2008-07-26 23:55:29363 chrome_logging_initialized_ = true;
364}
365
366// This is a no-op, but we'll keep it around in case
367// we need to do more cleanup in the future.
368void CleanupChromeLogging() {
[email protected]7109dc32011-02-02 22:58:29369 if (chrome_logging_failed_)
370 return; // We failed to initiailize logging, no cleanup.
371
initial.commit09911bf2008-07-26 23:55:29372 DCHECK(chrome_logging_initialized_) <<
373 "Attempted to clean up logging when it wasn't initialized.";
374
375 CloseLogFile();
376
377 chrome_logging_initialized_ = false;
[email protected]30f547c2010-08-04 01:00:01378 chrome_logging_redirected_ = false;
initial.commit09911bf2008-07-26 23:55:29379}
380
[email protected]a7329162013-02-07 19:21:48381base::FilePath GetLogFileName() {
[email protected]c83dd912010-04-06 18:50:51382 std::string filename;
thakis8b51c0a2016-04-12 19:48:18383 std::unique_ptr<base::Environment> env(base::Environment::Create());
[email protected]0b58a812013-05-31 01:15:17384 if (env->GetVar(env_vars::kLogFileName, &filename) && !filename.empty())
385 return base::FilePath::FromUTF8Unsafe(filename);
initial.commit09911bf2008-07-26 23:55:29386
[email protected]a7329162013-02-07 19:21:48387 const base::FilePath log_filename(FILE_PATH_LITERAL("chrome_debug.log"));
388 base::FilePath log_path;
initial.commit09911bf2008-07-26 23:55:29389
390 if (PathService::Get(chrome::DIR_LOGS, &log_path)) {
[email protected]73e23482009-08-13 00:26:58391 log_path = log_path.Append(log_filename);
initial.commit09911bf2008-07-26 23:55:29392 return log_path;
393 } else {
394 // error with path service, just use some default file somewhere
395 return log_filename;
396 }
397}
398
399bool DialogsAreSuppressed() {
400 return dialogs_are_suppressed_;
401}
[email protected]a7329162013-02-07 19:21:48402base::FilePath GenerateTimestampedName(const base::FilePath& base_path,
403 base::Time timestamp) {
[email protected]395a0392012-04-12 15:35:03404 base::Time::Exploded time_deets;
405 timestamp.LocalExplode(&time_deets);
406 std::string suffix = base::StringPrintf("_%02d%02d%02d-%02d%02d%02d",
407 time_deets.year,
408 time_deets.month,
409 time_deets.day_of_month,
410 time_deets.hour,
411 time_deets.minute,
412 time_deets.second);
413 return base_path.InsertBeforeExtensionASCII(suffix);
414}
415
[email protected]c83dd912010-04-06 18:50:51416} // namespace logging