[email protected] | 0f65b52d | 2012-02-09 21:02:47 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
[email protected] | ec65780 | 2008-11-07 20:05:39 | [diff] [blame] | 5 | #include "build/build_config.h" |
| 6 | |
[email protected] | d55aaa13 | 2009-09-28 21:08:04 | [diff] [blame] | 7 | // 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 Violet | a35f9a4 | 2018-03-22 22:00:44 | [diff] [blame] | 11 | #include "ipc/ipc_buildflags.h" |
[email protected] | d55aaa13 | 2009-09-28 21:08:04 | [diff] [blame] | 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. |
davidsz | 041528a | 2017-05-12 09:19:23 | [diff] [blame] | 17 | #if defined(OS_POSIX) && BUILDFLAG(IPC_MESSAGE_LOG_ENABLED) |
[email protected] | d55aaa13 | 2009-09-28 21:08:04 | [diff] [blame] | 18 | #define IPC_MESSAGE_MACROS_LOG_ENABLED |
[email protected] | 4f68ed7 | 2012-10-30 05:24:07 | [diff] [blame] | 19 | #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] | 4a6d7415 | 2011-09-26 20:42:30 | [diff] [blame] | 22 | #include "chrome/common/all_messages.h" |
[email protected] | d55aaa13 | 2009-09-28 21:08:04 | [diff] [blame] | 23 | #endif |
| 24 | |
[email protected] | ec65780 | 2008-11-07 20:05:39 | [diff] [blame] | 25 | #if defined(OS_WIN) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 26 | #include <windows.h> |
[email protected] | ec65780 | 2008-11-07 20:05:39 | [diff] [blame] | 27 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 28 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 29 | #include "chrome/common/logging_chrome.h" |
| 30 | |
erikwright | dfe2976 | 2015-03-16 16:06:46 | [diff] [blame] | 31 | #include <fstream> // NOLINT |
thakis | 8b51c0a | 2016-04-12 19:48:18 | [diff] [blame] | 32 | #include <memory> // NOLINT |
erikwright | dfe2976 | 2015-03-16 16:06:46 | [diff] [blame] | 33 | #include <string> // NOLINT |
| 34 | |
brettw | f195891 | 2015-10-07 19:43:12 | [diff] [blame] | 35 | #include "base/base_switches.h" |
davidsz | 041528a | 2017-05-12 09:19:23 | [diff] [blame] | 36 | #include "base/bind.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 37 | #include "base/command_line.h" |
[email protected] | 0eb9f434 | 2008-11-21 18:48:52 | [diff] [blame] | 38 | #include "base/compiler_specific.h" |
[email protected] | 5858035 | 2010-10-26 04:07:50 | [diff] [blame] | 39 | #include "base/debug/debugger.h" |
[email protected] | 5a8d4ce | 2013-12-18 17:42:27 | [diff] [blame] | 40 | #include "base/debug/dump_without_crashing.h" |
[email protected] | 76b90d31 | 2010-08-03 03:00:50 | [diff] [blame] | 41 | #include "base/environment.h" |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 42 | #include "base/files/file_path.h" |
thestig | 18dfb7a5 | 2014-08-26 10:44:04 | [diff] [blame] | 43 | #include "base/files/file_util.h" |
boriay | 648b3732 | 2016-07-05 08:15:29 | [diff] [blame] | 44 | #include "base/metrics/statistics_recorder.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 45 | #include "base/path_service.h" |
[email protected] | 3ea1b18 | 2013-02-08 22:38:41 | [diff] [blame] | 46 | #include "base/strings/string_number_conversions.h" |
[email protected] | 3c8a6b0 | 2013-06-11 00:49:49 | [diff] [blame] | 47 | #include "base/strings/string_util.h" |
| 48 | #include "base/strings/stringprintf.h" |
[email protected] | 12bfb61 | 2013-06-07 19:54:02 | [diff] [blame] | 49 | #include "base/strings/utf_string_conversions.h" |
[email protected] | ab7ee5b | 2011-03-01 22:24:14 | [diff] [blame] | 50 | #include "chrome/common/chrome_constants.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 51 | #include "chrome/common/chrome_paths.h" |
| 52 | #include "chrome/common/chrome_switches.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 53 | #include "chrome/common/env_vars.h" |
brettw | 90e9260 | 2015-10-10 00:12:40 | [diff] [blame] | 54 | #include "content/public/common/content_switches.h" |
[email protected] | 51d5e16 | 2009-07-27 19:23:54 | [diff] [blame] | 55 | #include "ipc/ipc_logging.h" |
[email protected] | ab7ee5b | 2011-03-01 22:24:14 | [diff] [blame] | 56 | |
[email protected] | 931d104 | 2013-04-05 17:50:44 | [diff] [blame] | 57 | #if defined(OS_CHROMEOS) |
Steven Bennetts | 38e9bde2 | 2019-01-03 17:31:10 | [diff] [blame] | 58 | #include "chromeos/constants/chromeos_switches.h" |
[email protected] | 931d104 | 2013-04-05 17:50:44 | [diff] [blame] | 59 | #endif |
| 60 | |
[email protected] | 2b07b841 | 2009-11-25 15:26:34 | [diff] [blame] | 61 | #if defined(OS_WIN) |
[email protected] | 2b07b841 | 2009-11-25 15:26:34 | [diff] [blame] | 62 | #include <initguid.h> |
[email protected] | 3ea1b18 | 2013-02-08 22:38:41 | [diff] [blame] | 63 | #include "base/logging_win.h" |
pastarmovj | a3eac43 | 2016-12-07 17:38:52 | [diff] [blame] | 64 | #include "base/syslog_logging.h" |
proberge | fe47600 | 2017-06-23 14:29:01 | [diff] [blame] | 65 | #include "chrome/common/win/eventlog_messages.h" |
pastarmovj | a3eac43 | 2016-12-07 17:38:52 | [diff] [blame] | 66 | #include "chrome/install_static/install_details.h" |
[email protected] | 2b07b841 | 2009-11-25 15:26:34 | [diff] [blame] | 67 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 68 | |
achuith | 7ea0aaca6 | 2017-06-01 20:31:42 | [diff] [blame] | 69 | namespace logging { |
[email protected] | ab7ee5b | 2011-03-01 22:24:14 | [diff] [blame] | 70 | namespace { |
| 71 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 72 | // When true, this means that error dialogs should not be shown. |
[email protected] | ab7ee5b | 2011-03-01 22:24:14 | [diff] [blame] | 73 | bool dialogs_are_suppressed_ = false; |
achuith | 7ea0aaca6 | 2017-06-01 20:31:42 | [diff] [blame] | 74 | ScopedLogAssertHandler* assert_handler_ = nullptr; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 75 | |
| 76 | // This should be true for exactly the period between the end of |
| 77 | // InitChromeLogging() and the beginning of CleanupChromeLogging(). |
[email protected] | ab7ee5b | 2011-03-01 22:24:14 | [diff] [blame] | 78 | bool chrome_logging_initialized_ = false; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 79 | |
[email protected] | 4a6d7415 | 2011-09-26 20:42:30 | [diff] [blame] | 80 | // Set if we called InitChromeLogging() but failed to initialize. |
[email protected] | ab7ee5b | 2011-03-01 22:24:14 | [diff] [blame] | 81 | bool chrome_logging_failed_ = false; |
[email protected] | 7109dc3 | 2011-02-02 22:58:29 | [diff] [blame] | 82 | |
[email protected] | 30f547c | 2010-08-04 01:00:01 | [diff] [blame] | 83 | // This should be true for exactly the period between the end of |
| 84 | // InitChromeLogging() and the beginning of CleanupChromeLogging(). |
[email protected] | ab7ee5b | 2011-03-01 22:24:14 | [diff] [blame] | 85 | bool chrome_logging_redirected_ = false; |
[email protected] | 30f547c | 2010-08-04 01:00:01 | [diff] [blame] | 86 | |
[email protected] | 2b07b841 | 2009-11-25 15:26:34 | [diff] [blame] | 87 | #if defined(OS_WIN) |
| 88 | // {7FE69228-633E-4f06-80C1-527FEA23E3A7} |
[email protected] | ab7ee5b | 2011-03-01 22:24:14 | [diff] [blame] | 89 | const GUID kChromeTraceProviderName = { |
[email protected] | 162ac0f | 2010-11-04 15:50:49 | [diff] [blame] | 90 | 0x7fe69228, 0x633e, 0x4f06, |
| 91 | { 0x80, 0xc1, 0x52, 0x7f, 0xea, 0x23, 0xe3, 0xa7 } }; |
[email protected] | 2b07b841 | 2009-11-25 15:26:34 | [diff] [blame] | 92 | #endif |
| 93 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 94 | // Assertion handler for logging errors that occur when dialogs are |
| 95 | // silenced. To record a new error, pass the log string associated |
| 96 | // with that error in the str parameter. |
David Bienvenu | a86573a | 2018-08-20 19:34:00 | [diff] [blame] | 97 | NOINLINE void SilentRuntimeAssertHandler(const char* file, |
| 98 | int line, |
| 99 | const base::StringPiece message, |
| 100 | const base::StringPiece stack_trace) { |
[email protected] | 5858035 | 2010-10-26 04:07:50 | [diff] [blame] | 101 | base::debug::BreakDebugger(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 102 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 103 | |
| 104 | // Suppresses error/assertion dialogs and enables the logging of |
| 105 | // those errors into silenced_errors_. |
[email protected] | ab7ee5b | 2011-03-01 22:24:14 | [diff] [blame] | 106 | void SuppressDialogs() { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 107 | if (dialogs_are_suppressed_) |
| 108 | return; |
| 109 | |
Ella Ge | df89f8ad9 | 2020-06-29 16:29:22 | [diff] [blame] | 110 | assert_handler_ = new ScopedLogAssertHandler( |
| 111 | base::BindRepeating(SilentRuntimeAssertHandler)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 112 | |
[email protected] | ec65780 | 2008-11-07 20:05:39 | [diff] [blame] | 113 | #if defined(OS_WIN) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 114 | UINT new_flags = SEM_FAILCRITICALERRORS | |
| 115 | SEM_NOGPFAULTERRORBOX | |
| 116 | SEM_NOOPENFILEERRORBOX; |
| 117 | |
| 118 | // Preserve existing error mode, as discussed at http://t/dmea |
| 119 | UINT existing_flags = SetErrorMode(new_flags); |
| 120 | SetErrorMode(existing_flags | new_flags); |
[email protected] | ec65780 | 2008-11-07 20:05:39 | [diff] [blame] | 121 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 122 | |
| 123 | dialogs_are_suppressed_ = true; |
| 124 | } |
| 125 | |
[email protected] | ab7ee5b | 2011-03-01 22:24:14 | [diff] [blame] | 126 | } // anonymous namespace |
| 127 | |
achuith | 7ea0aaca6 | 2017-06-01 20:31:42 | [diff] [blame] | 128 | LoggingDestination DetermineLoggingDestination( |
| 129 | const base::CommandLine& command_line) { |
| 130 | // only use OutputDebugString in debug mode |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 131 | #ifdef NDEBUG |
| 132 | bool enable_logging = false; |
[email protected] | b7e0a2a | 2009-10-13 02:07:25 | [diff] [blame] | 133 | const char *kInvertLoggingSwitch = switches::kEnableLogging; |
achuith | 7ea0aaca6 | 2017-06-01 20:31:42 | [diff] [blame] | 134 | const LoggingDestination kDefaultLoggingMode = LOG_TO_FILE; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 135 | #else |
| 136 | bool enable_logging = true; |
[email protected] | b7e0a2a | 2009-10-13 02:07:25 | [diff] [blame] | 137 | const char *kInvertLoggingSwitch = switches::kDisableLogging; |
achuith | 7ea0aaca6 | 2017-06-01 20:31:42 | [diff] [blame] | 138 | const LoggingDestination kDefaultLoggingMode = LOG_TO_ALL; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 139 | #endif |
| 140 | |
| 141 | if (command_line.HasSwitch(kInvertLoggingSwitch)) |
| 142 | enable_logging = !enable_logging; |
| 143 | |
Camillo Bruni | 213d59a2 | 2020-04-01 11:21:52 | [diff] [blame] | 144 | if (!enable_logging) |
| 145 | return LOG_NONE; |
| 146 | if (command_line.HasSwitch(switches::kEnableLogging)) { |
[email protected] | 3bba958 | 2009-08-05 01:29:12 | [diff] [blame] | 147 | // Let --enable-logging=stderr force only stderr, particularly useful for |
| 148 | // non-debug builds where otherwise you can't get logs to stderr at all. |
Camillo Bruni | 213d59a2 | 2020-04-01 11:21:52 | [diff] [blame] | 149 | std::string logging_destination = |
| 150 | command_line.GetSwitchValueASCII(switches::kEnableLogging); |
| 151 | if (logging_destination == "stderr") { |
| 152 | return LOG_TO_SYSTEM_DEBUG_LOG | LOG_TO_STDERR; |
| 153 | } else if (logging_destination != "") { |
| 154 | PLOG(ERROR) << "Invalid logging destination: " << logging_destination; |
| 155 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 156 | } |
Camillo Bruni | 213d59a2 | 2020-04-01 11:21:52 | [diff] [blame] | 157 | return kDefaultLoggingMode; |
[email protected] | 77ee1b1 | 2010-06-14 16:21:30 | [diff] [blame] | 158 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 159 | |
[email protected] | 77ee1b1 | 2010-06-14 16:21:30 | [diff] [blame] | 160 | #if defined(OS_CHROMEOS) |
[email protected] | a732916 | 2013-02-07 19:21:48 | [diff] [blame] | 161 | base::FilePath SetUpSymlinkIfNeeded(const base::FilePath& symlink_path, |
| 162 | bool new_log) { |
[email protected] | f25b33a | 2010-10-28 20:15:51 | [diff] [blame] | 163 | DCHECK(!symlink_path.empty()); |
Luigi Semenzato | 5238430 | 2017-08-10 20:22:59 | [diff] [blame] | 164 | // For backward compatibility, set up a .../chrome symlink to |
| 165 | // .../chrome.LATEST as needed. This code needs to run only |
| 166 | // after the migration (i.e. the addition of chrome.LATEST). |
| 167 | if (symlink_path.Extension() == ".LATEST") { |
| 168 | base::FilePath extensionless_path = symlink_path.ReplaceExtension(""); |
| 169 | base::FilePath target_path; |
Luigi Semenzato | 214cd28 | 2018-01-27 01:15:59 | [diff] [blame] | 170 | bool extensionless_symlink_exists = |
| 171 | base::ReadSymbolicLink(extensionless_path, &target_path); |
[email protected] | f25b33a | 2010-10-28 20:15:51 | [diff] [blame] | 172 | |
Luigi Semenzato | 5238430 | 2017-08-10 20:22:59 | [diff] [blame] | 173 | if (target_path != symlink_path) { |
| 174 | // No link, or wrong link. Clean up. This should happen only once in |
| 175 | // each log directory after the OS version update, but some of those |
| 176 | // directories may not be accessed for a long time, so this code needs to |
| 177 | // stay in forever :/ |
Luigi Semenzato | 214cd28 | 2018-01-27 01:15:59 | [diff] [blame] | 178 | if (extensionless_symlink_exists && |
Lei Zhang | 3c16723 | 2020-07-08 00:33:53 | [diff] [blame^] | 179 | !base::DeleteFile(extensionless_path)) { |
Luigi Semenzato | 5238430 | 2017-08-10 20:22:59 | [diff] [blame] | 180 | DPLOG(WARNING) << "Cannot delete " << extensionless_path.value(); |
| 181 | } |
| 182 | // After cleaning up, create the symlink. |
| 183 | if (!base::CreateSymbolicLink(symlink_path, extensionless_path)) { |
| 184 | DPLOG(ERROR) << "Cannot create " << extensionless_path.value(); |
| 185 | } |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | // If not starting a new log, then just log through the existing symlink, but |
| 190 | // if the symlink doesn't exist, create it. |
| 191 | // |
| 192 | // If starting a new log, then rename the old symlink as |
| 193 | // symlink_path.PREVIOUS and make a new symlink to a fresh log file. |
[email protected] | c7d5da99 | 2010-10-28 00:20:21 | [diff] [blame] | 194 | |
Luigi Semenzato | 214cd28 | 2018-01-27 01:15:59 | [diff] [blame] | 195 | // Check for existence of the symlink. |
| 196 | base::FilePath target_path; |
| 197 | bool symlink_exists = base::ReadSymbolicLink(symlink_path, &target_path); |
| 198 | |
| 199 | if (symlink_exists && !new_log) |
| 200 | return target_path; |
| 201 | |
| 202 | // Remove any extension before time-stamping. |
| 203 | target_path = GenerateTimestampedName(symlink_path.RemoveExtension(), |
| 204 | base::Time::Now()); |
| 205 | |
| 206 | if (symlink_exists) { |
| 207 | base::FilePath previous_symlink_path = |
| 208 | symlink_path.ReplaceExtension(".PREVIOUS"); |
| 209 | // Rename symlink to .PREVIOUS. This nukes an existing symlink just like |
| 210 | // the rename(2) syscall does. |
| 211 | if (!base::ReplaceFile(symlink_path, previous_symlink_path, nullptr)) { |
| 212 | DPLOG(WARNING) << "Cannot rename " << symlink_path.value() << " to " |
| 213 | << previous_symlink_path.value(); |
[email protected] | c7d5da99 | 2010-10-28 00:20:21 | [diff] [blame] | 214 | } |
Luigi Semenzato | 214cd28 | 2018-01-27 01:15:59 | [diff] [blame] | 215 | } |
| 216 | // If all went well, the symlink no longer exists. Recreate it. |
Vladislav Kuzkokov | 5c7ff5a | 2019-06-26 16:52:34 | [diff] [blame] | 217 | base::FilePath relative_target_path = target_path.BaseName(); |
| 218 | if (!base::CreateSymbolicLink(relative_target_path, symlink_path)) { |
Luigi Semenzato | 214cd28 | 2018-01-27 01:15:59 | [diff] [blame] | 219 | DPLOG(ERROR) << "Unable to create symlink " << symlink_path.value() |
Vladislav Kuzkokov | 5c7ff5a | 2019-06-26 16:52:34 | [diff] [blame] | 220 | << " pointing at " << relative_target_path.value(); |
[email protected] | c7d5da99 | 2010-10-28 00:20:21 | [diff] [blame] | 221 | } |
| 222 | return target_path; |
| 223 | } |
| 224 | |
[email protected] | a732916 | 2013-02-07 19:21:48 | [diff] [blame] | 225 | void RemoveSymlinkAndLog(const base::FilePath& link_path, |
| 226 | const base::FilePath& target_path) { |
[email protected] | c7d5da99 | 2010-10-28 00:20:21 | [diff] [blame] | 227 | if (::unlink(link_path.value().c_str()) == -1) |
[email protected] | 26f025e | 2011-10-28 22:49:27 | [diff] [blame] | 228 | DPLOG(WARNING) << "Unable to unlink symlink " << link_path.value(); |
[email protected] | c7d5da99 | 2010-10-28 00:20:21 | [diff] [blame] | 229 | if (::unlink(target_path.value().c_str()) == -1) |
[email protected] | 26f025e | 2011-10-28 22:49:27 | [diff] [blame] | 230 | DPLOG(WARNING) << "Unable to unlink log file " << target_path.value(); |
[email protected] | c7d5da99 | 2010-10-28 00:20:21 | [diff] [blame] | 231 | } |
| 232 | |
avi | 79bf913 | 2014-12-25 17:48:05 | [diff] [blame] | 233 | base::FilePath GetSessionLogDir(const base::CommandLine& command_line) { |
[email protected] | a732916 | 2013-02-07 19:21:48 | [diff] [blame] | 234 | base::FilePath log_dir; |
[email protected] | 0750b144 | 2010-11-02 21:59:37 | [diff] [blame] | 235 | std::string log_dir_str; |
thakis | 8b51c0a | 2016-04-12 19:48:18 | [diff] [blame] | 236 | std::unique_ptr<base::Environment> env(base::Environment::Create()); |
[email protected] | 0750b144 | 2010-11-02 21:59:37 | [diff] [blame] | 237 | if (env->GetVar(env_vars::kSessionLogDir, &log_dir_str) && |
| 238 | !log_dir_str.empty()) { |
[email protected] | a732916 | 2013-02-07 19:21:48 | [diff] [blame] | 239 | log_dir = base::FilePath(log_dir_str); |
[email protected] | 68e3169 | 2013-05-20 13:08:27 | [diff] [blame] | 240 | } else if (command_line.HasSwitch(chromeos::switches::kLoginProfile)) { |
Avi Drissman | 7d43b4b | 2018-05-01 16:14:06 | [diff] [blame] | 241 | base::PathService::Get(chrome::DIR_USER_DATA, &log_dir); |
[email protected] | 68e3169 | 2013-05-20 13:08:27 | [diff] [blame] | 242 | base::FilePath profile_dir; |
[email protected] | 9c8b1f9 | 2013-09-19 14:39:59 | [diff] [blame] | 243 | std::string login_profile_value = |
| 244 | command_line.GetSwitchValueASCII(chromeos::switches::kLoginProfile); |
[email protected] | 64146ec | 2014-03-19 21:52:25 | [diff] [blame] | 245 | if (login_profile_value == chrome::kLegacyProfileDir || |
| 246 | login_profile_value == chrome::kTestUserProfileDir) { |
[email protected] | 9c8b1f9 | 2013-09-19 14:39:59 | [diff] [blame] | 247 | profile_dir = base::FilePath(login_profile_value); |
| 248 | } else { |
[email protected] | 68e3169 | 2013-05-20 13:08:27 | [diff] [blame] | 249 | // We could not use g_browser_process > profile_helper() here. |
| 250 | std::string profile_dir_str = chrome::kProfileDirPrefix; |
[email protected] | 9c8b1f9 | 2013-09-19 14:39:59 | [diff] [blame] | 251 | profile_dir_str.append(login_profile_value); |
[email protected] | 68e3169 | 2013-05-20 13:08:27 | [diff] [blame] | 252 | profile_dir = base::FilePath(profile_dir_str); |
[email protected] | 68e3169 | 2013-05-20 13:08:27 | [diff] [blame] | 253 | } |
| 254 | log_dir = log_dir.Append(profile_dir); |
[email protected] | 0750b144 | 2010-11-02 21:59:37 | [diff] [blame] | 255 | } |
[email protected] | cc5ff985 | 2014-07-23 22:29:29 | [diff] [blame] | 256 | return log_dir; |
| 257 | } |
| 258 | |
avi | 79bf913 | 2014-12-25 17:48:05 | [diff] [blame] | 259 | base::FilePath GetSessionLogFile(const base::CommandLine& command_line) { |
Michael Giuffrida | 6802ffe | 2017-09-29 03:40:25 | [diff] [blame] | 260 | return GetSessionLogDir(command_line) |
| 261 | .Append(GetLogFileName(command_line).BaseName()); |
[email protected] | 0750b144 | 2010-11-02 21:59:37 | [diff] [blame] | 262 | } |
| 263 | |
Luigi Semenzato | 214cd28 | 2018-01-27 01:15:59 | [diff] [blame] | 264 | #endif // defined(OS_CHROMEOS) |
[email protected] | 77ee1b1 | 2010-06-14 16:21:30 | [diff] [blame] | 265 | |
avi | 79bf913 | 2014-12-25 17:48:05 | [diff] [blame] | 266 | void InitChromeLogging(const base::CommandLine& command_line, |
[email protected] | 77ee1b1 | 2010-06-14 16:21:30 | [diff] [blame] | 267 | OldFileDeletionState delete_old_log_file) { |
| 268 | DCHECK(!chrome_logging_initialized_) << |
| 269 | "Attempted to initialize logging when it was already initialized."; |
achuith | 7ea0aaca6 | 2017-06-01 20:31:42 | [diff] [blame] | 270 | LoggingDestination logging_dest = DetermineLoggingDestination(command_line); |
[email protected] | d64ea4217 | 2012-09-14 05:43:59 | [diff] [blame] | 271 | LogLockingState log_locking_state = LOCK_LOG_FILE; |
[email protected] | a732916 | 2013-02-07 19:21:48 | [diff] [blame] | 272 | base::FilePath log_path; |
[email protected] | 4a10099 | 2011-06-14 18:22:46 | [diff] [blame] | 273 | #if defined(OS_CHROMEOS) |
[email protected] | a732916 | 2013-02-07 19:21:48 | [diff] [blame] | 274 | base::FilePath target_path; |
[email protected] | 4a10099 | 2011-06-14 18:22:46 | [diff] [blame] | 275 | #endif |
[email protected] | 77ee1b1 | 2010-06-14 16:21:30 | [diff] [blame] | 276 | |
[email protected] | 4a10099 | 2011-06-14 18:22:46 | [diff] [blame] | 277 | // Don't resolve the log path unless we need to. Otherwise we leave an open |
| 278 | // ALPC handle after sandbox lockdown on Windows. |
[email protected] | 5e3f7c2 | 2013-06-21 21:15:33 | [diff] [blame] | 279 | if ((logging_dest & LOG_TO_FILE) != 0) { |
Michael Giuffrida | 6802ffe | 2017-09-29 03:40:25 | [diff] [blame] | 280 | log_path = GetLogFileName(command_line); |
[email protected] | c7d5da99 | 2010-10-28 00:20:21 | [diff] [blame] | 281 | |
[email protected] | 77ee1b1 | 2010-06-14 16:21:30 | [diff] [blame] | 282 | #if defined(OS_CHROMEOS) |
[email protected] | 4a10099 | 2011-06-14 18:22:46 | [diff] [blame] | 283 | // For BWSI (Incognito) logins, we want to put the logs in the user |
| 284 | // profile directory that is created for the temporary session instead |
| 285 | // of in the system log directory, for privacy reasons. |
[email protected] | 931d104 | 2013-04-05 17:50:44 | [diff] [blame] | 286 | if (command_line.HasSwitch(chromeos::switches::kGuestSession)) |
[email protected] | 4a10099 | 2011-06-14 18:22:46 | [diff] [blame] | 287 | log_path = GetSessionLogFile(command_line); |
[email protected] | 0750b144 | 2010-11-02 21:59:37 | [diff] [blame] | 288 | |
[email protected] | 4a10099 | 2011-06-14 18:22:46 | [diff] [blame] | 289 | // On ChromeOS we log to the symlink. We force creation of a new |
| 290 | // symlink if we've been asked to delete the old log, since that |
| 291 | // indicates the start of a new session. |
| 292 | target_path = SetUpSymlinkIfNeeded( |
achuith | 7ea0aaca6 | 2017-06-01 20:31:42 | [diff] [blame] | 293 | log_path, delete_old_log_file == DELETE_OLD_LOG_FILE); |
[email protected] | c7d5da99 | 2010-10-28 00:20:21 | [diff] [blame] | 294 | |
[email protected] | 4a10099 | 2011-06-14 18:22:46 | [diff] [blame] | 295 | // Because ChromeOS manages the move to a new session by redirecting |
| 296 | // the link, it shouldn't remove the old file in the logging code, |
| 297 | // since that will remove the newly created link instead. |
achuith | 7ea0aaca6 | 2017-06-01 20:31:42 | [diff] [blame] | 298 | delete_old_log_file = APPEND_TO_OLD_LOG_FILE; |
Luigi Semenzato | 214cd28 | 2018-01-27 01:15:59 | [diff] [blame] | 299 | #endif // defined(OS_CHROMEOS) |
[email protected] | d64ea4217 | 2012-09-14 05:43:59 | [diff] [blame] | 300 | } else { |
| 301 | log_locking_state = DONT_LOCK_LOG_FILE; |
[email protected] | 4a10099 | 2011-06-14 18:22:46 | [diff] [blame] | 302 | } |
[email protected] | 77ee1b1 | 2010-06-14 16:21:30 | [diff] [blame] | 303 | |
achuith | 7ea0aaca6 | 2017-06-01 20:31:42 | [diff] [blame] | 304 | LoggingSettings settings; |
[email protected] | 5e3f7c2 | 2013-06-21 21:15:33 | [diff] [blame] | 305 | settings.logging_dest = logging_dest; |
Robbie McElrath | 8bf4984 | 2019-08-20 22:22:53 | [diff] [blame] | 306 | settings.log_file_path = log_path.value().c_str(); |
[email protected] | 5e3f7c2 | 2013-06-21 21:15:33 | [diff] [blame] | 307 | settings.lock_log = log_locking_state; |
| 308 | settings.delete_old = delete_old_log_file; |
achuith | 7ea0aaca6 | 2017-06-01 20:31:42 | [diff] [blame] | 309 | bool success = InitLogging(settings); |
[email protected] | c7d5da99 | 2010-10-28 00:20:21 | [diff] [blame] | 310 | |
| 311 | #if defined(OS_CHROMEOS) |
| 312 | if (!success) { |
[email protected] | 26f025e | 2011-10-28 22:49:27 | [diff] [blame] | 313 | DPLOG(ERROR) << "Unable to initialize logging to " << log_path.value() |
[email protected] | c7d5da99 | 2010-10-28 00:20:21 | [diff] [blame] | 314 | << " (which should be a link to " << target_path.value() << ")"; |
| 315 | RemoveSymlinkAndLog(log_path, target_path); |
[email protected] | 7109dc3 | 2011-02-02 22:58:29 | [diff] [blame] | 316 | chrome_logging_failed_ = true; |
[email protected] | c7d5da99 | 2010-10-28 00:20:21 | [diff] [blame] | 317 | return; |
| 318 | } |
Luigi Semenzato | 214cd28 | 2018-01-27 01:15:59 | [diff] [blame] | 319 | #else // defined(OS_CHROMEOS) |
[email protected] | c7d5da99 | 2010-10-28 00:20:21 | [diff] [blame] | 320 | if (!success) { |
[email protected] | 26f025e | 2011-10-28 22:49:27 | [diff] [blame] | 321 | DPLOG(ERROR) << "Unable to initialize logging to " << log_path.value(); |
[email protected] | 7109dc3 | 2011-02-02 22:58:29 | [diff] [blame] | 322 | chrome_logging_failed_ = true; |
[email protected] | c7d5da99 | 2010-10-28 00:20:21 | [diff] [blame] | 323 | return; |
| 324 | } |
Luigi Semenzato | 214cd28 | 2018-01-27 01:15:59 | [diff] [blame] | 325 | #endif // defined(OS_CHROMEOS) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 326 | |
Greg Thompson | d548ffa | 2017-10-31 08:25:05 | [diff] [blame] | 327 | // We call running in unattended mode "headless", and allow headless mode to |
| 328 | // be configured either by the Environment Variable or by the Command Line |
| 329 | // Switch. This is for automated test purposes. |
| 330 | std::unique_ptr<base::Environment> env(base::Environment::Create()); |
| 331 | const bool is_headless = env->HasVar(env_vars::kHeadless) || |
| 332 | command_line.HasSwitch(switches::kNoErrorDialogs); |
| 333 | |
| 334 | // Show fatal log messages in a dialog in debug builds when not headless. |
| 335 | if (!is_headless) |
achuith | 7ea0aaca6 | 2017-06-01 20:31:42 | [diff] [blame] | 336 | SetShowErrorDialogs(true); |
[email protected] | 81e0a85 | 2010-08-17 00:38:12 | [diff] [blame] | 337 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 338 | // we want process and thread IDs because we have a lot of things running |
achuith | 7ea0aaca6 | 2017-06-01 20:31:42 | [diff] [blame] | 339 | SetLogItems(true, // enable_process_id |
| 340 | true, // enable_thread_id |
| 341 | true, // enable_timestamp |
| 342 | false); // enable_tickcount |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 343 | |
Greg Thompson | d548ffa | 2017-10-31 08:25:05 | [diff] [blame] | 344 | // Suppress system error dialogs when headless. |
| 345 | if (is_headless) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 346 | SuppressDialogs(); |
| 347 | |
emaxx | 0bcc7aa66 | 2015-05-15 22:25:26 | [diff] [blame] | 348 | // Use a minimum log level if the command line asks for one. Ignore this |
| 349 | // switch if there's vlog level switch present too (as both of these switches |
| 350 | // refer to the same underlying log level, and the vlog level switch has |
achuith | 7ea0aaca6 | 2017-06-01 20:31:42 | [diff] [blame] | 351 | // already been processed inside InitLogging). If there is neither |
emaxx | 0bcc7aa66 | 2015-05-15 22:25:26 | [diff] [blame] | 352 | // log level nor vlog level specified, then just leave the default level |
| 353 | // (INFO). |
| 354 | if (command_line.HasSwitch(switches::kLoggingLevel) && |
achuith | 7ea0aaca6 | 2017-06-01 20:31:42 | [diff] [blame] | 355 | GetMinLogLevel() >= 0) { |
emaxx | 0bcc7aa66 | 2015-05-15 22:25:26 | [diff] [blame] | 356 | std::string log_level = |
| 357 | command_line.GetSwitchValueASCII(switches::kLoggingLevel); |
[email protected] | 3deeb6b3 | 2010-10-05 21:53:52 | [diff] [blame] | 358 | int level = 0; |
emaxx | 0bcc7aa66 | 2015-05-15 22:25:26 | [diff] [blame] | 359 | if (base::StringToInt(log_level, &level) && level >= 0 && |
| 360 | level < LOG_NUM_SEVERITIES) { |
achuith | 7ea0aaca6 | 2017-06-01 20:31:42 | [diff] [blame] | 361 | SetMinLogLevel(level); |
[email protected] | 3deeb6b3 | 2010-10-05 21:53:52 | [diff] [blame] | 362 | } else { |
[email protected] | 26f025e | 2011-10-28 22:49:27 | [diff] [blame] | 363 | DLOG(WARNING) << "Bad log level: " << log_level; |
[email protected] | 3deeb6b3 | 2010-10-05 21:53:52 | [diff] [blame] | 364 | } |
[email protected] | bb5185c5 | 2008-08-29 19:51:06 | [diff] [blame] | 365 | } |
| 366 | |
[email protected] | 2b07b841 | 2009-11-25 15:26:34 | [diff] [blame] | 367 | #if defined(OS_WIN) |
| 368 | // Enable trace control and transport through event tracing for Windows. |
achuith | 7ea0aaca6 | 2017-06-01 20:31:42 | [diff] [blame] | 369 | LogEventProvider::Initialize(kChromeTraceProviderName); |
pastarmovj | a3eac43 | 2016-12-07 17:38:52 | [diff] [blame] | 370 | |
| 371 | // Enable logging to the Windows Event Log. |
proberge | fe47600 | 2017-06-23 14:29:01 | [diff] [blame] | 372 | SetEventSource(base::UTF16ToASCII( |
| 373 | install_static::InstallDetails::Get().install_full_name()), |
| 374 | BROWSER_CATEGORY, MSG_LOG_MESSAGE); |
[email protected] | 2b07b841 | 2009-11-25 15:26:34 | [diff] [blame] | 375 | #endif |
| 376 | |
boriay | 648b3732 | 2016-07-05 08:15:29 | [diff] [blame] | 377 | base::StatisticsRecorder::InitLogOnShutdown(); |
| 378 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 379 | chrome_logging_initialized_ = true; |
| 380 | } |
| 381 | |
| 382 | // This is a no-op, but we'll keep it around in case |
| 383 | // we need to do more cleanup in the future. |
| 384 | void CleanupChromeLogging() { |
[email protected] | 7109dc3 | 2011-02-02 22:58:29 | [diff] [blame] | 385 | if (chrome_logging_failed_) |
| 386 | return; // We failed to initiailize logging, no cleanup. |
| 387 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 388 | DCHECK(chrome_logging_initialized_) << |
| 389 | "Attempted to clean up logging when it wasn't initialized."; |
| 390 | |
| 391 | CloseLogFile(); |
| 392 | |
| 393 | chrome_logging_initialized_ = false; |
[email protected] | 30f547c | 2010-08-04 01:00:01 | [diff] [blame] | 394 | chrome_logging_redirected_ = false; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 395 | } |
| 396 | |
Michael Giuffrida | 6802ffe | 2017-09-29 03:40:25 | [diff] [blame] | 397 | base::FilePath GetLogFileName(const base::CommandLine& command_line) { |
| 398 | std::string filename = command_line.GetSwitchValueASCII(switches::kLogFile); |
| 399 | if (filename.empty()) |
| 400 | base::Environment::Create()->GetVar(env_vars::kLogFileName, &filename); |
| 401 | if (!filename.empty()) |
[email protected] | 0b58a81 | 2013-05-31 01:15:17 | [diff] [blame] | 402 | return base::FilePath::FromUTF8Unsafe(filename); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 403 | |
[email protected] | a732916 | 2013-02-07 19:21:48 | [diff] [blame] | 404 | const base::FilePath log_filename(FILE_PATH_LITERAL("chrome_debug.log")); |
| 405 | base::FilePath log_path; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 406 | |
Avi Drissman | 7d43b4b | 2018-05-01 16:14:06 | [diff] [blame] | 407 | if (base::PathService::Get(chrome::DIR_LOGS, &log_path)) { |
[email protected] | 73e2348 | 2009-08-13 00:26:58 | [diff] [blame] | 408 | log_path = log_path.Append(log_filename); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 409 | return log_path; |
| 410 | } else { |
| 411 | // error with path service, just use some default file somewhere |
| 412 | return log_filename; |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | bool DialogsAreSuppressed() { |
| 417 | return dialogs_are_suppressed_; |
| 418 | } |
Luigi Semenzato | 5238430 | 2017-08-10 20:22:59 | [diff] [blame] | 419 | |
| 420 | #if defined(OS_CHROMEOS) |
[email protected] | a732916 | 2013-02-07 19:21:48 | [diff] [blame] | 421 | base::FilePath GenerateTimestampedName(const base::FilePath& base_path, |
| 422 | base::Time timestamp) { |
[email protected] | 395a039 | 2012-04-12 15:35:03 | [diff] [blame] | 423 | base::Time::Exploded time_deets; |
| 424 | timestamp.LocalExplode(&time_deets); |
| 425 | std::string suffix = base::StringPrintf("_%02d%02d%02d-%02d%02d%02d", |
| 426 | time_deets.year, |
| 427 | time_deets.month, |
| 428 | time_deets.day_of_month, |
| 429 | time_deets.hour, |
| 430 | time_deets.minute, |
| 431 | time_deets.second); |
Luigi Semenzato | 214cd28 | 2018-01-27 01:15:59 | [diff] [blame] | 432 | return base_path.InsertBeforeExtensionASCII(suffix); |
[email protected] | 395a039 | 2012-04-12 15:35:03 | [diff] [blame] | 433 | } |
Luigi Semenzato | 5238430 | 2017-08-10 20:22:59 | [diff] [blame] | 434 | #endif // defined(OS_CHROMEOS) |
[email protected] | 395a039 | 2012-04-12 15:35:03 | [diff] [blame] | 435 | |
[email protected] | c83dd91 | 2010-04-06 18:50:51 | [diff] [blame] | 436 | } // namespace logging |