Index: chrome/common/logging_chrome.cc |
diff --git a/chrome/common/logging_chrome.cc b/chrome/common/logging_chrome.cc |
index 8f676714335e5ad2481bdccc82fea7ac4eca6048..4e2df8c8efdf47d8b3ab16953696495eaf4d7c70 100644 |
--- a/chrome/common/logging_chrome.cc |
+++ b/chrome/common/logging_chrome.cc |
@@ -47,6 +47,7 @@ |
#include "base/strings/string_util.h" |
#include "base/strings/stringprintf.h" |
#include "base/strings/utf_string_conversions.h" |
+#include "base/threading/thread_restrictions.h" |
#include "chrome/common/chrome_constants.h" |
#include "chrome/common/chrome_paths.h" |
#include "chrome/common/chrome_switches.h" |
@@ -65,12 +66,11 @@ |
#include "chrome/install_static/install_details.h" |
#endif |
-namespace logging { |
namespace { |
// When true, this means that error dialogs should not be shown. |
bool dialogs_are_suppressed_ = false; |
-ScopedLogAssertHandler* assert_handler_ = nullptr; |
+logging::ScopedLogAssertHandler* assert_handler_ = nullptr; |
// This should be true for exactly the period between the end of |
// InitChromeLogging() and the beginning of CleanupChromeLogging(). |
@@ -108,8 +108,8 @@ |
if (dialogs_are_suppressed_) |
return; |
- assert_handler_ = |
- new ScopedLogAssertHandler(base::Bind(SilentRuntimeAssertHandler)); |
+ assert_handler_ = new logging::ScopedLogAssertHandler( |
+ base::Bind(SilentRuntimeAssertHandler)); |
#if defined(OS_WIN) |
UINT new_flags = SEM_FAILCRITICALERRORS | |
@@ -126,37 +126,39 @@ |
} // anonymous namespace |
-LoggingDestination DetermineLoggingDestination( |
- const base::CommandLine& command_line) { |
-// only use OutputDebugString in debug mode |
+namespace logging { |
+ |
+LoggingDestination DetermineLogMode(const base::CommandLine& command_line) { |
+ // only use OutputDebugString in debug mode |
#ifdef NDEBUG |
bool enable_logging = false; |
const char *kInvertLoggingSwitch = switches::kEnableLogging; |
- const LoggingDestination kDefaultLoggingMode = LOG_TO_FILE; |
+ const logging::LoggingDestination kDefaultLoggingMode = logging::LOG_TO_FILE; |
#else |
bool enable_logging = true; |
const char *kInvertLoggingSwitch = switches::kDisableLogging; |
- const LoggingDestination kDefaultLoggingMode = LOG_TO_ALL; |
+ const logging::LoggingDestination kDefaultLoggingMode = logging::LOG_TO_ALL; |
#endif |
if (command_line.HasSwitch(kInvertLoggingSwitch)) |
enable_logging = !enable_logging; |
- LoggingDestination log_mode; |
+ logging::LoggingDestination log_mode; |
if (enable_logging) { |
// Let --enable-logging=stderr force only stderr, particularly useful for |
// non-debug builds where otherwise you can't get logs to stderr at all. |
if (command_line.GetSwitchValueASCII(switches::kEnableLogging) == "stderr") |
- log_mode = LOG_TO_SYSTEM_DEBUG_LOG; |
+ log_mode = logging::LOG_TO_SYSTEM_DEBUG_LOG; |
else |
log_mode = kDefaultLoggingMode; |
} else { |
- log_mode = LOG_NONE; |
+ log_mode = logging::LOG_NONE; |
} |
return log_mode; |
} |
#if defined(OS_CHROMEOS) |
+namespace { |
base::FilePath SetUpSymlinkIfNeeded(const base::FilePath& symlink_path, |
bool new_log) { |
DCHECK(!symlink_path.empty()); |
@@ -193,6 +195,8 @@ |
if (::unlink(target_path.value().c_str()) == -1) |
DPLOG(WARNING) << "Unable to unlink log file " << target_path.value(); |
} |
+ |
+} // anonymous namespace |
base::FilePath GetSessionLogDir(const base::CommandLine& command_line) { |
base::FilePath log_dir; |
@@ -224,6 +228,39 @@ |
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 |
void InitChromeLogging(const base::CommandLine& command_line, |
@@ -231,7 +268,7 @@ |
DCHECK(!chrome_logging_initialized_) << |
"Attempted to initialize logging when it was already initialized."; |
- LoggingDestination logging_dest = DetermineLoggingDestination(command_line); |
+ LoggingDestination logging_dest = DetermineLogMode(command_line); |
LogLockingState log_locking_state = LOCK_LOG_FILE; |
base::FilePath log_path; |
#if defined(OS_CHROMEOS) |
@@ -254,23 +291,23 @@ |
// symlink if we've been asked to delete the old log, since that |
// indicates the start of a new session. |
target_path = SetUpSymlinkIfNeeded( |
- log_path, delete_old_log_file == DELETE_OLD_LOG_FILE); |
+ log_path, delete_old_log_file == logging::DELETE_OLD_LOG_FILE); |
// Because ChromeOS manages the move to a new session by redirecting |
// the link, it shouldn't remove the old file in the logging code, |
// since that will remove the newly created link instead. |
- delete_old_log_file = APPEND_TO_OLD_LOG_FILE; |
+ delete_old_log_file = logging::APPEND_TO_OLD_LOG_FILE; |
#endif |
} else { |
log_locking_state = DONT_LOCK_LOG_FILE; |
} |
- LoggingSettings settings; |
+ logging::LoggingSettings settings; |
settings.logging_dest = logging_dest; |
settings.log_file = log_path.value().c_str(); |
settings.lock_log = log_locking_state; |
settings.delete_old = delete_old_log_file; |
- bool success = InitLogging(settings); |
+ bool success = logging::InitLogging(settings); |
#if defined(OS_CHROMEOS) |
if (!success) { |
@@ -291,13 +328,13 @@ |
// Default to showing error dialogs. |
if (!base::CommandLine::ForCurrentProcess()->HasSwitch( |
switches::kNoErrorDialogs)) |
- SetShowErrorDialogs(true); |
+ logging::SetShowErrorDialogs(true); |
// we want process and thread IDs because we have a lot of things running |
- SetLogItems(true, // enable_process_id |
- true, // enable_thread_id |
- true, // enable_timestamp |
- false); // enable_tickcount |
+ logging::SetLogItems(true, // enable_process_id |
+ true, // enable_thread_id |
+ true, // enable_timestamp |
+ false); // enable_tickcount |
// We call running in unattended mode "headless", and allow |
// headless mode to be configured either by the Environment |
@@ -311,17 +348,17 @@ |
// 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 |
// refer to the same underlying log level, and the vlog level switch has |
- // already been processed inside InitLogging). If there is neither |
+ // already been processed inside logging::InitLogging). If there is neither |
// log level nor vlog level specified, then just leave the default level |
// (INFO). |
if (command_line.HasSwitch(switches::kLoggingLevel) && |
- GetMinLogLevel() >= 0) { |
+ logging::GetMinLogLevel() >= 0) { |
std::string log_level = |
command_line.GetSwitchValueASCII(switches::kLoggingLevel); |
int level = 0; |
if (base::StringToInt(log_level, &level) && level >= 0 && |
level < LOG_NUM_SEVERITIES) { |
- SetMinLogLevel(level); |
+ logging::SetMinLogLevel(level); |
} else { |
DLOG(WARNING) << "Bad log level: " << log_level; |
} |
@@ -329,10 +366,10 @@ |
#if defined(OS_WIN) |
// Enable trace control and transport through event tracing for Windows. |
- LogEventProvider::Initialize(kChromeTraceProviderName); |
+ logging::LogEventProvider::Initialize(kChromeTraceProviderName); |
// Enable logging to the Windows Event Log. |
- SetEventSourceName(base::UTF16ToASCII( |
+ logging::SetEventSourceName(base::UTF16ToASCII( |
install_static::InstallDetails::Get().install_full_name())); |
#endif |