From cf20046bba6070546c2e4e8ba9ea6841f9ff947e Mon Sep 17 00:00:00 2001 From: Marshall Greenblatt Date: Fri, 23 Oct 2015 12:58:33 -0400 Subject: [PATCH] Use a reasonable default log file location if none is specified (issue #1757) --- include/internal/cef_types.h | 10 ++++--- libcef/common/main_delegate.cc | 53 ++++++++++++++++++++++++++-------- 2 files changed, 47 insertions(+), 16 deletions(-) diff --git a/include/internal/cef_types.h b/include/internal/cef_types.h index 19aaf4c41..4428973dd 100644 --- a/include/internal/cef_types.h +++ b/include/internal/cef_types.h @@ -262,10 +262,12 @@ typedef struct _cef_settings_t { cef_string_t locale; /// - // The directory and file name to use for the debug log. If empty, the - // default name of "debug.log" will be used and the file will be written - // to the application directory. Also configurable using the "log-file" - // command-line switch. + // The directory and file name to use for the debug log. If empty a default + // log file name and location will be used. On Windows and Linux a "debug.log" + // file will be written in the main executable directory. On Mac OS X a + // "~/Library/Logs/_debug.log" file will be written where + // is the name of the main app executable. Also configurable using the + // "log-file" command-line switch. /// cef_string_t log_file; diff --git a/libcef/common/main_delegate.cc b/libcef/common/main_delegate.cc index e3fe5fe8e..6a5f1c28e 100644 --- a/libcef/common/main_delegate.cc +++ b/libcef/common/main_delegate.cc @@ -106,22 +106,32 @@ base::FilePath GetResourcesFilePath() { return GetFrameworkBundlePath().Append(FILE_PATH_LITERAL("Resources")); } +// Retrieve the name of the running executable. +std::string GetExecutableName() { + base::FilePath path; + PathService::Get(base::FILE_EXE, &path); + return path.BaseName().value(); +} + +// Use a "~/Library/Logs/_debug.log" file where is the name +// of the running executable. +base::FilePath GetDefaultLogFile() { + return base::mac::GetUserLibraryPath() + .Append(FILE_PATH_LITERAL("Logs")) + .Append(FILE_PATH_LITERAL(GetExecutableName() + "_debug.log")); +} + void OverrideFrameworkBundlePath() { base::mac::SetOverrideFrameworkBundlePath(GetFrameworkBundlePath()); } void OverrideChildProcessPath() { - // Retrieve the name of the running executable. - base::FilePath path; - PathService::Get(base::FILE_EXE, &path); - - std::string name = path.BaseName().value(); - + const std::string& exe_name = GetExecutableName(); base::FilePath helper_path = GetFrameworksPath() - .Append(FILE_PATH_LITERAL(name+" Helper.app")) + .Append(FILE_PATH_LITERAL(exe_name + " Helper.app")) .Append(FILE_PATH_LITERAL("Contents")) .Append(FILE_PATH_LITERAL("MacOS")) - .Append(FILE_PATH_LITERAL(name+" Helper")); + .Append(FILE_PATH_LITERAL(exe_name + " Helper")); PathService::Override(content::CHILD_PROCESS_EXE, helper_path); } @@ -134,6 +144,13 @@ base::FilePath GetResourcesFilePath() { return pak_dir; } +// Use a "debug.log" file in the running executable's directory. +base::FilePath GetDefaultLogFile() { + base::FilePath log_path; + PathService::Get(base::DIR_EXE, &log_path); + return log_path.Append(FILE_PATH_LITERAL("debug.log")); +} + #endif // !defined(OS_MACOSX) #if defined(OS_WIN) @@ -360,11 +377,20 @@ bool CefMainDelegate::BasicStartupComplete(int* exit_code) { command_line->AppendSwitchASCII(switches::kLang, "en-US"); } - if (settings.log_file.length > 0) { - base::FilePath file_path = base::FilePath(CefString(&settings.log_file)); - if (!file_path.empty()) - command_line->AppendSwitchPath(switches::kLogFile, file_path); + base::FilePath log_file; + bool has_log_file_cmdline = false; + if (settings.log_file.length > 0) + log_file = base::FilePath(CefString(&settings.log_file)); + if (log_file.empty() && command_line->HasSwitch(switches::kLogFile)) { + log_file = command_line->GetSwitchValuePath(switches::kLogFile); + if (!log_file.empty()) + has_log_file_cmdline = true; } + if (log_file.empty()) + log_file = GetDefaultLogFile(); + DCHECK(!log_file.empty()); + if (!has_log_file_cmdline) + command_line->AppendSwitchPath(switches::kLogFile, log_file); if (settings.log_severity != LOGSEVERITY_DEFAULT) { std::string log_severity; @@ -444,9 +470,12 @@ bool CefMainDelegate::BasicStartupComplete(int* exit_code) { // Initialize logging. logging::LoggingSettings log_settings; + const base::FilePath& log_file = command_line->GetSwitchValuePath(switches::kLogFile); + DCHECK(!log_file.empty()); log_settings.log_file = log_file.value().c_str(); + log_settings.lock_log = logging::DONT_LOCK_LOG_FILE; log_settings.delete_old = logging::APPEND_TO_OLD_LOG_FILE;