Commit 7ea0aaca authored by achuith's avatar achuith Committed by Commit Bot

Move RedirectChromeLogging to src/chromeos.

BUG=chromium:724273

Review-Url: https://codereview.chromium.org/2901173002
Cr-Original-Commit-Position: refs/heads/master@{#474725}
Committed: https://chromium.googlesource.com/chromium/src/+/cb9bde9860d070b022407e3d988ba3239e00f35a
Review-Url: https://codereview.chromium.org/2901173002
Cr-Commit-Position: refs/heads/master@{#476417}
parent e449d3e6
...@@ -714,6 +714,8 @@ source_set("chromeos") { ...@@ -714,6 +714,8 @@ source_set("chromeos") {
"lock_screen_apps/state_controller.cc", "lock_screen_apps/state_controller.cc",
"lock_screen_apps/state_controller.h", "lock_screen_apps/state_controller.h",
"lock_screen_apps/state_observer.h", "lock_screen_apps/state_observer.h",
"logging.cc",
"logging.h",
"login/app_launch_controller.cc", "login/app_launch_controller.cc",
"login/app_launch_controller.h", "login/app_launch_controller.h",
"login/app_launch_signin_screen.cc", "login/app_launch_signin_screen.cc",
......
...@@ -56,6 +56,7 @@ ...@@ -56,6 +56,7 @@
#include "chrome/browser/chromeos/input_method/input_method_configuration.h" #include "chrome/browser/chromeos/input_method/input_method_configuration.h"
#include "chrome/browser/chromeos/language_preferences.h" #include "chrome/browser/chromeos/language_preferences.h"
#include "chrome/browser/chromeos/lock_screen_apps/state_controller.h" #include "chrome/browser/chromeos/lock_screen_apps/state_controller.h"
#include "chrome/browser/chromeos/logging.h"
#include "chrome/browser/chromeos/login/helper.h" #include "chrome/browser/chromeos/login/helper.h"
#include "chrome/browser/chromeos/login/lock/screen_locker.h" #include "chrome/browser/chromeos/login/lock/screen_locker.h"
#include "chrome/browser/chromeos/login/login_wizard.h" #include "chrome/browser/chromeos/login/login_wizard.h"
......
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/chromeos/logging.h"
#include "base/bind.h"
#include "base/command_line.h"
#include "base/files/file_path.h"
#include "base/logging.h"
#include "base/task_scheduler/post_task.h"
#include "chrome/common/logging_chrome.h"
#include "content/public/browser/browser_thread.h"
namespace logging {
namespace {
// This should be true for exactly the period between the end of
// InitChromeLogging() and the beginning of CleanupChromeLogging().
bool chrome_logging_redirected_ = false;
void SymlinkSetUp(const base::CommandLine& command_line,
const base::FilePath& log_path,
const base::FilePath& target_path) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
// ChromeOS always logs through the symlink, so it shouldn't be
// deleted if it already exists.
logging::LoggingSettings settings;
settings.logging_dest = DetermineLoggingDestination(command_line);
settings.log_file = log_path.value().c_str();
if (!logging::InitLogging(settings)) {
DLOG(ERROR) << "Unable to initialize logging to " << log_path.value();
base::PostTaskWithTraits(
FROM_HERE, {base::MayBlock()},
base::Bind(&RemoveSymlinkAndLog, log_path, target_path));
} else {
chrome_logging_redirected_ = true;
}
}
} // namespace
void RedirectChromeLogging(const base::CommandLine& command_line) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
if (chrome_logging_redirected_) {
// TODO: Support multiple active users. http://crbug.com/230345
LOG(WARNING) << "NOT redirecting logging for multi-profiles case.";
return;
}
DCHECK(!chrome_logging_redirected_)
<< "Attempted to redirect logging when it was already initialized.";
// Redirect logs to the session log directory, if set. Otherwise
// defaults to the profile dir.
const base::FilePath log_path = GetSessionLogFile(command_line);
// Always force a new symlink when redirecting.
base::PostTaskWithTraitsAndReplyWithResult(
FROM_HERE, {base::MayBlock()},
base::BindOnce(&SetUpSymlinkIfNeeded, log_path, true),
base::BindOnce(&SymlinkSetUp, command_line, log_path));
}
} // namespace logging
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_CHROMEOS_LOGGING_H_
#define CHROME_BROWSER_CHROMEOS_LOGGING_H_
namespace base {
class CommandLine;
}
namespace logging {
// Redirects chrome logging to the appropriate session log dir.
void RedirectChromeLogging(const base::CommandLine& command_line);
} // namespace logging
#endif // CHROME_BROWSER_CHROMEOS_LOGGING_H_
...@@ -37,6 +37,7 @@ ...@@ -37,6 +37,7 @@
#include "chrome/browser/chromeos/boot_times_recorder.h" #include "chrome/browser/chromeos/boot_times_recorder.h"
#include "chrome/browser/chromeos/first_run/first_run.h" #include "chrome/browser/chromeos/first_run/first_run.h"
#include "chrome/browser/chromeos/first_run/goodies_displayer.h" #include "chrome/browser/chromeos/first_run/goodies_displayer.h"
#include "chrome/browser/chromeos/logging.h"
#include "chrome/browser/chromeos/login/auth/chrome_cryptohome_authenticator.h" #include "chrome/browser/chromeos/login/auth/chrome_cryptohome_authenticator.h"
#include "chrome/browser/chromeos/login/chrome_restart_request.h" #include "chrome/browser/chromeos/login/chrome_restart_request.h"
#include "chrome/browser/chromeos/login/demo_mode/demo_app_launcher.h" #include "chrome/browser/chromeos/login/demo_mode/demo_app_launcher.h"
...@@ -980,7 +981,7 @@ void UserSessionManager::CreateUserSession(const UserContext& user_context, ...@@ -980,7 +981,7 @@ void UserSessionManager::CreateUserSession(const UserContext& user_context,
void UserSessionManager::PreStartSession() { void UserSessionManager::PreStartSession() {
// Switch log file as soon as possible. // Switch log file as soon as possible.
if (base::SysInfo::IsRunningOnChromeOS()) if (base::SysInfo::IsRunningOnChromeOS())
logging::RedirectChromeLogging(*(base::CommandLine::ForCurrentProcess())); logging::RedirectChromeLogging(*base::CommandLine::ForCurrentProcess());
} }
void UserSessionManager::StoreUserContextDataBeforeProfileIsCreated() { void UserSessionManager::StoreUserContextDataBeforeProfileIsCreated() {
......
...@@ -47,7 +47,6 @@ ...@@ -47,7 +47,6 @@
#include "base/strings/string_util.h" #include "base/strings/string_util.h"
#include "base/strings/stringprintf.h" #include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h" #include "base/strings/utf_string_conversions.h"
#include "base/threading/thread_restrictions.h"
#include "chrome/common/chrome_constants.h" #include "chrome/common/chrome_constants.h"
#include "chrome/common/chrome_paths.h" #include "chrome/common/chrome_paths.h"
#include "chrome/common/chrome_switches.h" #include "chrome/common/chrome_switches.h"
...@@ -66,11 +65,12 @@ ...@@ -66,11 +65,12 @@
#include "chrome/install_static/install_details.h" #include "chrome/install_static/install_details.h"
#endif #endif
namespace logging {
namespace { namespace {
// When true, this means that error dialogs should not be shown. // When true, this means that error dialogs should not be shown.
bool dialogs_are_suppressed_ = false; bool dialogs_are_suppressed_ = false;
logging::ScopedLogAssertHandler* assert_handler_ = nullptr; ScopedLogAssertHandler* assert_handler_ = nullptr;
// This should be true for exactly the period between the end of // This should be true for exactly the period between the end of
// InitChromeLogging() and the beginning of CleanupChromeLogging(). // InitChromeLogging() and the beginning of CleanupChromeLogging().
...@@ -108,8 +108,8 @@ void SuppressDialogs() { ...@@ -108,8 +108,8 @@ void SuppressDialogs() {
if (dialogs_are_suppressed_) if (dialogs_are_suppressed_)
return; return;
assert_handler_ = new logging::ScopedLogAssertHandler( assert_handler_ =
base::Bind(SilentRuntimeAssertHandler)); new ScopedLogAssertHandler(base::Bind(SilentRuntimeAssertHandler));
#if defined(OS_WIN) #if defined(OS_WIN)
UINT new_flags = SEM_FAILCRITICALERRORS | UINT new_flags = SEM_FAILCRITICALERRORS |
...@@ -126,39 +126,37 @@ void SuppressDialogs() { ...@@ -126,39 +126,37 @@ void SuppressDialogs() {
} // anonymous namespace } // anonymous namespace
namespace logging { LoggingDestination DetermineLoggingDestination(
const base::CommandLine& command_line) {
LoggingDestination DetermineLogMode(const base::CommandLine& command_line) { // only use OutputDebugString in debug mode
// only use OutputDebugString in debug mode
#ifdef NDEBUG #ifdef NDEBUG
bool enable_logging = false; bool enable_logging = false;
const char *kInvertLoggingSwitch = switches::kEnableLogging; const char *kInvertLoggingSwitch = switches::kEnableLogging;
const logging::LoggingDestination kDefaultLoggingMode = logging::LOG_TO_FILE; const LoggingDestination kDefaultLoggingMode = LOG_TO_FILE;
#else #else
bool enable_logging = true; bool enable_logging = true;
const char *kInvertLoggingSwitch = switches::kDisableLogging; const char *kInvertLoggingSwitch = switches::kDisableLogging;
const logging::LoggingDestination kDefaultLoggingMode = logging::LOG_TO_ALL; const LoggingDestination kDefaultLoggingMode = LOG_TO_ALL;
#endif #endif
if (command_line.HasSwitch(kInvertLoggingSwitch)) if (command_line.HasSwitch(kInvertLoggingSwitch))
enable_logging = !enable_logging; enable_logging = !enable_logging;
logging::LoggingDestination log_mode; LoggingDestination log_mode;
if (enable_logging) { if (enable_logging) {
// Let --enable-logging=stderr force only stderr, particularly useful for // Let --enable-logging=stderr force only stderr, particularly useful for
// non-debug builds where otherwise you can't get logs to stderr at all. // non-debug builds where otherwise you can't get logs to stderr at all.
if (command_line.GetSwitchValueASCII(switches::kEnableLogging) == "stderr") if (command_line.GetSwitchValueASCII(switches::kEnableLogging) == "stderr")
log_mode = logging::LOG_TO_SYSTEM_DEBUG_LOG; log_mode = LOG_TO_SYSTEM_DEBUG_LOG;
else else
log_mode = kDefaultLoggingMode; log_mode = kDefaultLoggingMode;
} else { } else {
log_mode = logging::LOG_NONE; log_mode = LOG_NONE;
} }
return log_mode; return log_mode;
} }
#if defined(OS_CHROMEOS) #if defined(OS_CHROMEOS)
namespace {
base::FilePath SetUpSymlinkIfNeeded(const base::FilePath& symlink_path, base::FilePath SetUpSymlinkIfNeeded(const base::FilePath& symlink_path,
bool new_log) { bool new_log) {
DCHECK(!symlink_path.empty()); DCHECK(!symlink_path.empty());
...@@ -196,8 +194,6 @@ void RemoveSymlinkAndLog(const base::FilePath& link_path, ...@@ -196,8 +194,6 @@ void RemoveSymlinkAndLog(const base::FilePath& link_path,
DPLOG(WARNING) << "Unable to unlink log file " << target_path.value(); DPLOG(WARNING) << "Unable to unlink log file " << target_path.value();
} }
} // anonymous namespace
base::FilePath GetSessionLogDir(const base::CommandLine& command_line) { base::FilePath GetSessionLogDir(const base::CommandLine& command_line) {
base::FilePath log_dir; base::FilePath log_dir;
std::string log_dir_str; std::string log_dir_str;
...@@ -228,39 +224,6 @@ base::FilePath GetSessionLogFile(const base::CommandLine& command_line) { ...@@ -228,39 +224,6 @@ base::FilePath GetSessionLogFile(const base::CommandLine& command_line) {
return GetSessionLogDir(command_line).Append(GetLogFileName().BaseName()); return GetSessionLogDir(command_line).Append(GetLogFileName().BaseName());
} }
void RedirectChromeLogging(const base::CommandLine& command_line) {
if (chrome_logging_redirected_) {
// TODO(nkostylev): Support multiple active users. http://crbug.com/230345
LOG(WARNING) << "NOT redirecting logging for multi-profiles case.";
return;
}
DCHECK(!chrome_logging_redirected_) <<
"Attempted to redirect logging when it was already initialized.";
// Redirect logs to the session log directory, if set. Otherwise
// defaults to the profile dir.
base::FilePath log_path = GetSessionLogFile(command_line);
// Creating symlink causes us to do blocking IO on UI thread.
// Temporarily allow it until we fix http://crbug.com/61143
base::ThreadRestrictions::ScopedAllowIO allow_io;
// Always force a new symlink when redirecting.
base::FilePath target_path = SetUpSymlinkIfNeeded(log_path, true);
// ChromeOS always logs through the symlink, so it shouldn't be
// deleted if it already exists.
logging::LoggingSettings settings;
settings.logging_dest = DetermineLogMode(command_line);
settings.log_file = log_path.value().c_str();
if (!logging::InitLogging(settings)) {
DLOG(ERROR) << "Unable to initialize logging to " << log_path.value();
RemoveSymlinkAndLog(log_path, target_path);
} else {
chrome_logging_redirected_ = true;
}
}
#endif // OS_CHROMEOS #endif // OS_CHROMEOS
void InitChromeLogging(const base::CommandLine& command_line, void InitChromeLogging(const base::CommandLine& command_line,
...@@ -268,7 +231,7 @@ void InitChromeLogging(const base::CommandLine& command_line, ...@@ -268,7 +231,7 @@ void InitChromeLogging(const base::CommandLine& command_line,
DCHECK(!chrome_logging_initialized_) << DCHECK(!chrome_logging_initialized_) <<
"Attempted to initialize logging when it was already initialized."; "Attempted to initialize logging when it was already initialized.";
LoggingDestination logging_dest = DetermineLogMode(command_line); LoggingDestination logging_dest = DetermineLoggingDestination(command_line);
LogLockingState log_locking_state = LOCK_LOG_FILE; LogLockingState log_locking_state = LOCK_LOG_FILE;
base::FilePath log_path; base::FilePath log_path;
#if defined(OS_CHROMEOS) #if defined(OS_CHROMEOS)
...@@ -291,23 +254,23 @@ void InitChromeLogging(const base::CommandLine& command_line, ...@@ -291,23 +254,23 @@ void InitChromeLogging(const base::CommandLine& command_line,
// symlink if we've been asked to delete the old log, since that // symlink if we've been asked to delete the old log, since that
// indicates the start of a new session. // indicates the start of a new session.
target_path = SetUpSymlinkIfNeeded( target_path = SetUpSymlinkIfNeeded(
log_path, delete_old_log_file == logging::DELETE_OLD_LOG_FILE); log_path, delete_old_log_file == DELETE_OLD_LOG_FILE);
// Because ChromeOS manages the move to a new session by redirecting // Because ChromeOS manages the move to a new session by redirecting
// the link, it shouldn't remove the old file in the logging code, // the link, it shouldn't remove the old file in the logging code,
// since that will remove the newly created link instead. // since that will remove the newly created link instead.
delete_old_log_file = logging::APPEND_TO_OLD_LOG_FILE; delete_old_log_file = APPEND_TO_OLD_LOG_FILE;
#endif #endif
} else { } else {
log_locking_state = DONT_LOCK_LOG_FILE; log_locking_state = DONT_LOCK_LOG_FILE;
} }
logging::LoggingSettings settings; LoggingSettings settings;
settings.logging_dest = logging_dest; settings.logging_dest = logging_dest;
settings.log_file = log_path.value().c_str(); settings.log_file = log_path.value().c_str();
settings.lock_log = log_locking_state; settings.lock_log = log_locking_state;
settings.delete_old = delete_old_log_file; settings.delete_old = delete_old_log_file;
bool success = logging::InitLogging(settings); bool success = InitLogging(settings);
#if defined(OS_CHROMEOS) #if defined(OS_CHROMEOS)
if (!success) { if (!success) {
...@@ -328,13 +291,13 @@ void InitChromeLogging(const base::CommandLine& command_line, ...@@ -328,13 +291,13 @@ void InitChromeLogging(const base::CommandLine& command_line,
// Default to showing error dialogs. // Default to showing error dialogs.
if (!base::CommandLine::ForCurrentProcess()->HasSwitch( if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
switches::kNoErrorDialogs)) switches::kNoErrorDialogs))
logging::SetShowErrorDialogs(true); SetShowErrorDialogs(true);
// we want process and thread IDs because we have a lot of things running // we want process and thread IDs because we have a lot of things running
logging::SetLogItems(true, // enable_process_id SetLogItems(true, // enable_process_id
true, // enable_thread_id true, // enable_thread_id
true, // enable_timestamp true, // enable_timestamp
false); // enable_tickcount false); // enable_tickcount
// We call running in unattended mode "headless", and allow // We call running in unattended mode "headless", and allow
// headless mode to be configured either by the Environment // headless mode to be configured either by the Environment
...@@ -348,17 +311,17 @@ void InitChromeLogging(const base::CommandLine& command_line, ...@@ -348,17 +311,17 @@ void InitChromeLogging(const base::CommandLine& command_line,
// Use a minimum log level if the command line asks for one. Ignore this // Use a minimum log level if the command line asks for one. Ignore this
// switch if there's vlog level switch present too (as both of these switches // switch if there's vlog level switch present too (as both of these switches
// refer to the same underlying log level, and the vlog level switch has // refer to the same underlying log level, and the vlog level switch has
// already been processed inside logging::InitLogging). If there is neither // already been processed inside InitLogging). If there is neither
// log level nor vlog level specified, then just leave the default level // log level nor vlog level specified, then just leave the default level
// (INFO). // (INFO).
if (command_line.HasSwitch(switches::kLoggingLevel) && if (command_line.HasSwitch(switches::kLoggingLevel) &&
logging::GetMinLogLevel() >= 0) { GetMinLogLevel() >= 0) {
std::string log_level = std::string log_level =
command_line.GetSwitchValueASCII(switches::kLoggingLevel); command_line.GetSwitchValueASCII(switches::kLoggingLevel);
int level = 0; int level = 0;
if (base::StringToInt(log_level, &level) && level >= 0 && if (base::StringToInt(log_level, &level) && level >= 0 &&
level < LOG_NUM_SEVERITIES) { level < LOG_NUM_SEVERITIES) {
logging::SetMinLogLevel(level); SetMinLogLevel(level);
} else { } else {
DLOG(WARNING) << "Bad log level: " << log_level; DLOG(WARNING) << "Bad log level: " << log_level;
} }
...@@ -366,10 +329,10 @@ void InitChromeLogging(const base::CommandLine& command_line, ...@@ -366,10 +329,10 @@ void InitChromeLogging(const base::CommandLine& command_line,
#if defined(OS_WIN) #if defined(OS_WIN)
// Enable trace control and transport through event tracing for Windows. // Enable trace control and transport through event tracing for Windows.
logging::LogEventProvider::Initialize(kChromeTraceProviderName); LogEventProvider::Initialize(kChromeTraceProviderName);
// Enable logging to the Windows Event Log. // Enable logging to the Windows Event Log.
logging::SetEventSourceName(base::UTF16ToASCII( SetEventSourceName(base::UTF16ToASCII(
install_static::InstallDetails::Get().install_full_name())); install_static::InstallDetails::Get().install_full_name()));
#endif #endif
......
...@@ -32,15 +32,23 @@ namespace logging { ...@@ -32,15 +32,23 @@ namespace logging {
void InitChromeLogging(const base::CommandLine& command_line, void InitChromeLogging(const base::CommandLine& command_line,
OldFileDeletionState delete_old_log_file); OldFileDeletionState delete_old_log_file);
LoggingDestination DetermineLoggingDestination(
const base::CommandLine& command_line);
#if defined(OS_CHROMEOS) #if defined(OS_CHROMEOS)
// Point the logging symlink to the system log or the user session log.
base::FilePath SetUpSymlinkIfNeeded(const base::FilePath& symlink_path,
bool new_log);
// Remove the logging symlink.
void RemoveSymlinkAndLog(const base::FilePath& link_path,
const base::FilePath& target_path);
// Get the log file directory path. // Get the log file directory path.
base::FilePath GetSessionLogDir(const base::CommandLine& command_line); base::FilePath GetSessionLogDir(const base::CommandLine& command_line);
// Get the log file location. // Get the log file location.
base::FilePath GetSessionLogFile(const base::CommandLine& command_line); base::FilePath GetSessionLogFile(const base::CommandLine& command_line);
// Redirects chrome logging to the appropriate session log dir.
void RedirectChromeLogging(const base::CommandLine& command_line);
#endif #endif
// Call when done using logging for Chrome. // Call when done using logging for Chrome.
......
...@@ -220,7 +220,7 @@ void InProcessBrowserTest::SetUp() { ...@@ -220,7 +220,7 @@ void InProcessBrowserTest::SetUp() {
#if defined(OS_CHROMEOS) #if defined(OS_CHROMEOS)
// Make sure that the log directory exists. // Make sure that the log directory exists.
base::FilePath log_dir = logging::GetSessionLogFile(*command_line).DirName(); base::FilePath log_dir = logging::GetSessionLogDir(*command_line);
base::CreateDirectory(log_dir); base::CreateDirectory(log_dir);
// Disable IME extension loading to avoid many browser tests failures. // Disable IME extension loading to avoid many browser tests failures.
chromeos::input_method::DisableExtensionLoading(); chromeos::input_method::DisableExtensionLoading();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment