[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. |
| 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] | 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 <fstream> |
| 30 | |
| 31 | #include "chrome/common/logging_chrome.h" |
| 32 | |
| 33 | #include "base/command_line.h" |
[email protected] | 0eb9f434 | 2008-11-21 18:48:52 | [diff] [blame] | 34 | #include "base/compiler_specific.h" |
[email protected] | 5858035 | 2010-10-26 04:07:50 | [diff] [blame] | 35 | #include "base/debug/debugger.h" |
[email protected] | 5a8d4ce | 2013-12-18 17:42:27 | [diff] [blame] | 36 | #include "base/debug/dump_without_crashing.h" |
[email protected] | 76b90d31 | 2010-08-03 03:00:50 | [diff] [blame] | 37 | #include "base/environment.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 38 | #include "base/file_util.h" |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 39 | #include "base/files/file_path.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 40 | #include "base/logging.h" |
| 41 | #include "base/path_service.h" |
[email protected] | 3ea1b18 | 2013-02-08 22:38:41 | [diff] [blame] | 42 | #include "base/strings/string_number_conversions.h" |
[email protected] | 3c8a6b0 | 2013-06-11 00:49:49 | [diff] [blame] | 43 | #include "base/strings/string_util.h" |
| 44 | #include "base/strings/stringprintf.h" |
[email protected] | 12bfb61 | 2013-06-07 19:54:02 | [diff] [blame] | 45 | #include "base/strings/utf_string_conversions.h" |
[email protected] | 61f9321 | 2011-01-24 14:37:08 | [diff] [blame] | 46 | #include "base/threading/thread_restrictions.h" |
[email protected] | 3164fa82 | 2013-06-28 15:32:41 | [diff] [blame] | 47 | #include "base/time/time.h" |
[email protected] | ab7ee5b | 2011-03-01 22:24:14 | [diff] [blame] | 48 | #include "chrome/common/chrome_constants.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 49 | #include "chrome/common/chrome_paths.h" |
| 50 | #include "chrome/common/chrome_switches.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 51 | #include "chrome/common/env_vars.h" |
[email protected] | 51d5e16 | 2009-07-27 19:23:54 | [diff] [blame] | 52 | #include "ipc/ipc_logging.h" |
[email protected] | ab7ee5b | 2011-03-01 22:24:14 | [diff] [blame] | 53 | |
[email protected] | 931d104 | 2013-04-05 17:50:44 | [diff] [blame] | 54 | #if defined(OS_CHROMEOS) |
| 55 | #include "chromeos/chromeos_switches.h" |
| 56 | #endif |
| 57 | |
[email protected] | 2b07b841 | 2009-11-25 15:26:34 | [diff] [blame] | 58 | #if defined(OS_WIN) |
[email protected] | 2b07b841 | 2009-11-25 15:26:34 | [diff] [blame] | 59 | #include <initguid.h> |
[email protected] | 3ea1b18 | 2013-02-08 22:38:41 | [diff] [blame] | 60 | #include "base/logging_win.h" |
[email protected] | 2b07b841 | 2009-11-25 15:26:34 | [diff] [blame] | 61 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 62 | |
[email protected] | ab7ee5b | 2011-03-01 22:24:14 | [diff] [blame] | 63 | namespace { |
| 64 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 65 | // When true, this means that error dialogs should not be shown. |
[email protected] | ab7ee5b | 2011-03-01 22:24:14 | [diff] [blame] | 66 | bool dialogs_are_suppressed_ = false; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 67 | |
| 68 | // This should be true for exactly the period between the end of |
| 69 | // InitChromeLogging() and the beginning of CleanupChromeLogging(). |
[email protected] | ab7ee5b | 2011-03-01 22:24:14 | [diff] [blame] | 70 | bool chrome_logging_initialized_ = false; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 71 | |
[email protected] | 4a6d7415 | 2011-09-26 20:42:30 | [diff] [blame] | 72 | // Set if we called InitChromeLogging() but failed to initialize. |
[email protected] | ab7ee5b | 2011-03-01 22:24:14 | [diff] [blame] | 73 | bool chrome_logging_failed_ = false; |
[email protected] | 7109dc3 | 2011-02-02 22:58:29 | [diff] [blame] | 74 | |
[email protected] | 30f547c | 2010-08-04 01:00:01 | [diff] [blame] | 75 | // This should be true for exactly the period between the end of |
| 76 | // InitChromeLogging() and the beginning of CleanupChromeLogging(). |
[email protected] | ab7ee5b | 2011-03-01 22:24:14 | [diff] [blame] | 77 | bool chrome_logging_redirected_ = false; |
[email protected] | 30f547c | 2010-08-04 01:00:01 | [diff] [blame] | 78 | |
[email protected] | 2b07b841 | 2009-11-25 15:26:34 | [diff] [blame] | 79 | #if defined(OS_WIN) |
| 80 | // {7FE69228-633E-4f06-80C1-527FEA23E3A7} |
[email protected] | ab7ee5b | 2011-03-01 22:24:14 | [diff] [blame] | 81 | const GUID kChromeTraceProviderName = { |
[email protected] | 162ac0f | 2010-11-04 15:50:49 | [diff] [blame] | 82 | 0x7fe69228, 0x633e, 0x4f06, |
| 83 | { 0x80, 0xc1, 0x52, 0x7f, 0xea, 0x23, 0xe3, 0xa7 } }; |
[email protected] | 2b07b841 | 2009-11-25 15:26:34 | [diff] [blame] | 84 | #endif |
| 85 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 86 | // Assertion handler for logging errors that occur when dialogs are |
| 87 | // silenced. To record a new error, pass the log string associated |
| 88 | // with that error in the str parameter. |
[email protected] | 0eb9f434 | 2008-11-21 18:48:52 | [diff] [blame] | 89 | MSVC_DISABLE_OPTIMIZE(); |
[email protected] | ab7ee5b | 2011-03-01 22:24:14 | [diff] [blame] | 90 | void SilentRuntimeAssertHandler(const std::string& str) { |
[email protected] | 5858035 | 2010-10-26 04:07:50 | [diff] [blame] | 91 | base::debug::BreakDebugger(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 92 | } |
[email protected] | ab7ee5b | 2011-03-01 22:24:14 | [diff] [blame] | 93 | void SilentRuntimeReportHandler(const std::string& str) { |
[email protected] | 6cc67d7f | 2009-02-12 06:48:24 | [diff] [blame] | 94 | } |
[email protected] | ab7ee5b | 2011-03-01 22:24:14 | [diff] [blame] | 95 | #if defined(OS_WIN) |
| 96 | // Handler to silently dump the current process when there is an assert in |
| 97 | // chrome. |
| 98 | void DumpProcessAssertHandler(const std::string& str) { |
[email protected] | 5a8d4ce | 2013-12-18 17:42:27 | [diff] [blame] | 99 | base::debug::DumpWithoutCrashing(); |
[email protected] | ab7ee5b | 2011-03-01 22:24:14 | [diff] [blame] | 100 | } |
| 101 | #endif // OS_WIN |
[email protected] | 0eb9f434 | 2008-11-21 18:48:52 | [diff] [blame] | 102 | MSVC_ENABLE_OPTIMIZE(); |
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 | |
| 110 | logging::SetLogAssertHandler(SilentRuntimeAssertHandler); |
[email protected] | 6cc67d7f | 2009-02-12 06:48:24 | [diff] [blame] | 111 | logging::SetLogReportHandler(SilentRuntimeReportHandler); |
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 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 128 | namespace logging { |
| 129 | |
[email protected] | 77ee1b1 | 2010-06-14 16:21:30 | [diff] [blame] | 130 | LoggingDestination DetermineLogMode(const CommandLine& command_line) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 131 | // only use OutputDebugString in debug mode |
| 132 | #ifdef NDEBUG |
| 133 | bool enable_logging = false; |
[email protected] | b7e0a2a | 2009-10-13 02:07:25 | [diff] [blame] | 134 | const char *kInvertLoggingSwitch = switches::kEnableLogging; |
[email protected] | 5e3f7c2 | 2013-06-21 21:15:33 | [diff] [blame] | 135 | const logging::LoggingDestination kDefaultLoggingMode = logging::LOG_TO_FILE; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 136 | #else |
| 137 | bool enable_logging = true; |
[email protected] | b7e0a2a | 2009-10-13 02:07:25 | [diff] [blame] | 138 | const char *kInvertLoggingSwitch = switches::kDisableLogging; |
[email protected] | 5e3f7c2 | 2013-06-21 21:15:33 | [diff] [blame] | 139 | const logging::LoggingDestination kDefaultLoggingMode = logging::LOG_TO_ALL; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 140 | #endif |
| 141 | |
| 142 | if (command_line.HasSwitch(kInvertLoggingSwitch)) |
| 143 | enable_logging = !enable_logging; |
| 144 | |
| 145 | logging::LoggingDestination log_mode; |
| 146 | if (enable_logging) { |
[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. |
[email protected] | c4e52f0d | 2009-11-06 19:55:16 | [diff] [blame] | 149 | if (command_line.GetSwitchValueASCII(switches::kEnableLogging) == "stderr") |
[email protected] | 5e3f7c2 | 2013-06-21 21:15:33 | [diff] [blame] | 150 | log_mode = logging::LOG_TO_SYSTEM_DEBUG_LOG; |
[email protected] | 3bba958 | 2009-08-05 01:29:12 | [diff] [blame] | 151 | else |
| 152 | log_mode = kDefaultLoggingMode; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 153 | } else { |
| 154 | log_mode = logging::LOG_NONE; |
| 155 | } |
[email protected] | 77ee1b1 | 2010-06-14 16:21:30 | [diff] [blame] | 156 | return log_mode; |
| 157 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 158 | |
[email protected] | 77ee1b1 | 2010-06-14 16:21:30 | [diff] [blame] | 159 | #if defined(OS_CHROMEOS) |
[email protected] | c7d5da99 | 2010-10-28 00:20:21 | [diff] [blame] | 160 | namespace { |
[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()); |
| 164 | |
[email protected] | c7d5da99 | 2010-10-28 00:20:21 | [diff] [blame] | 165 | // If not starting a new log, then just log through the existing |
| 166 | // symlink, but if the symlink doesn't exist, create it. If |
| 167 | // starting a new log, then delete the old symlink and make a new |
| 168 | // one to a fresh log file. |
[email protected] | a732916 | 2013-02-07 19:21:48 | [diff] [blame] | 169 | base::FilePath target_path; |
[email protected] | 756748414 | 2013-07-11 17:36:07 | [diff] [blame] | 170 | bool symlink_exists = base::PathExists(symlink_path); |
[email protected] | 0750b144 | 2010-11-02 21:59:37 | [diff] [blame] | 171 | if (new_log || !symlink_exists) { |
[email protected] | c7d5da99 | 2010-10-28 00:20:21 | [diff] [blame] | 172 | target_path = GenerateTimestampedName(symlink_path, base::Time::Now()); |
| 173 | |
| 174 | // We don't care if the unlink fails; we're going to continue anyway. |
[email protected] | 723571a | 2010-12-03 17:37:54 | [diff] [blame] | 175 | if (::unlink(symlink_path.value().c_str()) == -1) { |
[email protected] | 0750b144 | 2010-11-02 21:59:37 | [diff] [blame] | 176 | if (symlink_exists) // only warn if we might expect it to succeed. |
[email protected] | 26f025e | 2011-10-28 22:49:27 | [diff] [blame] | 177 | DPLOG(WARNING) << "Unable to unlink " << symlink_path.value(); |
[email protected] | c7d5da99 | 2010-10-28 00:20:21 | [diff] [blame] | 178 | } |
[email protected] | b264eab | 2013-11-27 23:22:08 | [diff] [blame] | 179 | if (!base::CreateSymbolicLink(target_path, symlink_path)) { |
[email protected] | 26f025e | 2011-10-28 22:49:27 | [diff] [blame] | 180 | DPLOG(ERROR) << "Unable to create symlink " << symlink_path.value() |
| 181 | << " pointing at " << target_path.value(); |
[email protected] | c7d5da99 | 2010-10-28 00:20:21 | [diff] [blame] | 182 | } |
| 183 | } else { |
[email protected] | b264eab | 2013-11-27 23:22:08 | [diff] [blame] | 184 | if (!base::ReadSymbolicLink(symlink_path, &target_path)) |
[email protected] | 26f025e | 2011-10-28 22:49:27 | [diff] [blame] | 185 | DPLOG(ERROR) << "Unable to read symlink " << symlink_path.value(); |
[email protected] | c7d5da99 | 2010-10-28 00:20:21 | [diff] [blame] | 186 | } |
| 187 | return target_path; |
| 188 | } |
| 189 | |
[email protected] | a732916 | 2013-02-07 19:21:48 | [diff] [blame] | 190 | void RemoveSymlinkAndLog(const base::FilePath& link_path, |
| 191 | const base::FilePath& target_path) { |
[email protected] | c7d5da99 | 2010-10-28 00:20:21 | [diff] [blame] | 192 | if (::unlink(link_path.value().c_str()) == -1) |
[email protected] | 26f025e | 2011-10-28 22:49:27 | [diff] [blame] | 193 | DPLOG(WARNING) << "Unable to unlink symlink " << link_path.value(); |
[email protected] | c7d5da99 | 2010-10-28 00:20:21 | [diff] [blame] | 194 | if (::unlink(target_path.value().c_str()) == -1) |
[email protected] | 26f025e | 2011-10-28 22:49:27 | [diff] [blame] | 195 | DPLOG(WARNING) << "Unable to unlink log file " << target_path.value(); |
[email protected] | c7d5da99 | 2010-10-28 00:20:21 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | } // anonymous namespace |
| 199 | |
[email protected] | a732916 | 2013-02-07 19:21:48 | [diff] [blame] | 200 | base::FilePath GetSessionLogFile(const CommandLine& command_line) { |
| 201 | base::FilePath log_dir; |
[email protected] | 0750b144 | 2010-11-02 21:59:37 | [diff] [blame] | 202 | std::string log_dir_str; |
| 203 | scoped_ptr<base::Environment> env(base::Environment::Create()); |
| 204 | if (env->GetVar(env_vars::kSessionLogDir, &log_dir_str) && |
| 205 | !log_dir_str.empty()) { |
[email protected] | a732916 | 2013-02-07 19:21:48 | [diff] [blame] | 206 | log_dir = base::FilePath(log_dir_str); |
[email protected] | 68e3169 | 2013-05-20 13:08:27 | [diff] [blame] | 207 | } else if (command_line.HasSwitch(chromeos::switches::kLoginProfile)) { |
[email protected] | 0750b144 | 2010-11-02 21:59:37 | [diff] [blame] | 208 | PathService::Get(chrome::DIR_USER_DATA, &log_dir); |
[email protected] | 68e3169 | 2013-05-20 13:08:27 | [diff] [blame] | 209 | base::FilePath profile_dir; |
[email protected] | 9c8b1f9 | 2013-09-19 14:39:59 | [diff] [blame] | 210 | std::string login_profile_value = |
| 211 | command_line.GetSwitchValueASCII(chromeos::switches::kLoginProfile); |
[email protected] | 64146ec | 2014-03-19 21:52:25 | [diff] [blame] | 212 | if (login_profile_value == chrome::kLegacyProfileDir || |
| 213 | login_profile_value == chrome::kTestUserProfileDir) { |
[email protected] | 9c8b1f9 | 2013-09-19 14:39:59 | [diff] [blame] | 214 | profile_dir = base::FilePath(login_profile_value); |
| 215 | } else { |
[email protected] | 68e3169 | 2013-05-20 13:08:27 | [diff] [blame] | 216 | // We could not use g_browser_process > profile_helper() here. |
| 217 | std::string profile_dir_str = chrome::kProfileDirPrefix; |
[email protected] | 9c8b1f9 | 2013-09-19 14:39:59 | [diff] [blame] | 218 | profile_dir_str.append(login_profile_value); |
[email protected] | 68e3169 | 2013-05-20 13:08:27 | [diff] [blame] | 219 | profile_dir = base::FilePath(profile_dir_str); |
[email protected] | 68e3169 | 2013-05-20 13:08:27 | [diff] [blame] | 220 | } |
| 221 | log_dir = log_dir.Append(profile_dir); |
[email protected] | 0750b144 | 2010-11-02 21:59:37 | [diff] [blame] | 222 | } |
| 223 | return log_dir.Append(GetLogFileName().BaseName()); |
| 224 | } |
| 225 | |
| 226 | void RedirectChromeLogging(const CommandLine& command_line) { |
[email protected] | f9357a44 | 2014-05-15 18:44:07 | [diff] [blame^] | 227 | if (chrome_logging_redirected_) { |
[email protected] | e718e6f | 2013-04-15 16:01:59 | [diff] [blame] | 228 | // TODO(nkostylev): Support multiple active users. http://crbug.com/230345 |
[email protected] | f9357a44 | 2014-05-15 18:44:07 | [diff] [blame^] | 229 | LOG(WARNING) << "NOT redirecting logging for multi-profiles case."; |
[email protected] | e718e6f | 2013-04-15 16:01:59 | [diff] [blame] | 230 | return; |
| 231 | } |
| 232 | |
[email protected] | 30f547c | 2010-08-04 01:00:01 | [diff] [blame] | 233 | DCHECK(!chrome_logging_redirected_) << |
| 234 | "Attempted to redirect logging when it was already initialized."; |
[email protected] | 0750b144 | 2010-11-02 21:59:37 | [diff] [blame] | 235 | |
| 236 | // Redirect logs to the session log directory, if set. Otherwise |
| 237 | // defaults to the profile dir. |
[email protected] | a732916 | 2013-02-07 19:21:48 | [diff] [blame] | 238 | base::FilePath log_path = GetSessionLogFile(command_line); |
[email protected] | c7d5da99 | 2010-10-28 00:20:21 | [diff] [blame] | 239 | |
[email protected] | 61f9321 | 2011-01-24 14:37:08 | [diff] [blame] | 240 | // Creating symlink causes us to do blocking IO on UI thread. |
| 241 | // Temporarily allow it until we fix http://crbug.com/61143 |
| 242 | base::ThreadRestrictions::ScopedAllowIO allow_io; |
[email protected] | c7d5da99 | 2010-10-28 00:20:21 | [diff] [blame] | 243 | // Always force a new symlink when redirecting. |
[email protected] | a732916 | 2013-02-07 19:21:48 | [diff] [blame] | 244 | base::FilePath target_path = SetUpSymlinkIfNeeded(log_path, true); |
[email protected] | c7d5da99 | 2010-10-28 00:20:21 | [diff] [blame] | 245 | |
| 246 | // ChromeOS always logs through the symlink, so it shouldn't be |
| 247 | // deleted if it already exists. |
[email protected] | 5e3f7c2 | 2013-06-21 21:15:33 | [diff] [blame] | 248 | logging::LoggingSettings settings; |
| 249 | settings.logging_dest = DetermineLogMode(command_line); |
| 250 | settings.log_file = log_path.value().c_str(); |
[email protected] | 5e3f7c2 | 2013-06-21 21:15:33 | [diff] [blame] | 251 | if (!logging::InitLogging(settings)) { |
[email protected] | 26f025e | 2011-10-28 22:49:27 | [diff] [blame] | 252 | DLOG(ERROR) << "Unable to initialize logging to " << log_path.value(); |
[email protected] | c7d5da99 | 2010-10-28 00:20:21 | [diff] [blame] | 253 | RemoveSymlinkAndLog(log_path, target_path); |
| 254 | } else { |
| 255 | chrome_logging_redirected_ = true; |
| 256 | } |
[email protected] | 77ee1b1 | 2010-06-14 16:21:30 | [diff] [blame] | 257 | } |
[email protected] | 0750b144 | 2010-11-02 21:59:37 | [diff] [blame] | 258 | |
[email protected] | 8527119 | 2011-05-10 20:38:25 | [diff] [blame] | 259 | #endif // OS_CHROMEOS |
[email protected] | 77ee1b1 | 2010-06-14 16:21:30 | [diff] [blame] | 260 | |
| 261 | void InitChromeLogging(const CommandLine& command_line, |
| 262 | OldFileDeletionState delete_old_log_file) { |
| 263 | DCHECK(!chrome_logging_initialized_) << |
| 264 | "Attempted to initialize logging when it was already initialized."; |
| 265 | |
[email protected] | 4a10099 | 2011-06-14 18:22:46 | [diff] [blame] | 266 | LoggingDestination logging_dest = DetermineLogMode(command_line); |
[email protected] | d64ea4217 | 2012-09-14 05:43:59 | [diff] [blame] | 267 | LogLockingState log_locking_state = LOCK_LOG_FILE; |
[email protected] | a732916 | 2013-02-07 19:21:48 | [diff] [blame] | 268 | base::FilePath log_path; |
[email protected] | 4a10099 | 2011-06-14 18:22:46 | [diff] [blame] | 269 | #if defined(OS_CHROMEOS) |
[email protected] | a732916 | 2013-02-07 19:21:48 | [diff] [blame] | 270 | base::FilePath target_path; |
[email protected] | 4a10099 | 2011-06-14 18:22:46 | [diff] [blame] | 271 | #endif |
[email protected] | 77ee1b1 | 2010-06-14 16:21:30 | [diff] [blame] | 272 | |
[email protected] | 4a10099 | 2011-06-14 18:22:46 | [diff] [blame] | 273 | // Don't resolve the log path unless we need to. Otherwise we leave an open |
| 274 | // ALPC handle after sandbox lockdown on Windows. |
[email protected] | 5e3f7c2 | 2013-06-21 21:15:33 | [diff] [blame] | 275 | if ((logging_dest & LOG_TO_FILE) != 0) { |
[email protected] | 4a10099 | 2011-06-14 18:22:46 | [diff] [blame] | 276 | log_path = GetLogFileName(); |
[email protected] | c7d5da99 | 2010-10-28 00:20:21 | [diff] [blame] | 277 | |
[email protected] | 77ee1b1 | 2010-06-14 16:21:30 | [diff] [blame] | 278 | #if defined(OS_CHROMEOS) |
[email protected] | 4a10099 | 2011-06-14 18:22:46 | [diff] [blame] | 279 | // For BWSI (Incognito) logins, we want to put the logs in the user |
| 280 | // profile directory that is created for the temporary session instead |
| 281 | // of in the system log directory, for privacy reasons. |
[email protected] | 931d104 | 2013-04-05 17:50:44 | [diff] [blame] | 282 | if (command_line.HasSwitch(chromeos::switches::kGuestSession)) |
[email protected] | 4a10099 | 2011-06-14 18:22:46 | [diff] [blame] | 283 | log_path = GetSessionLogFile(command_line); |
[email protected] | 0750b144 | 2010-11-02 21:59:37 | [diff] [blame] | 284 | |
[email protected] | 4a10099 | 2011-06-14 18:22:46 | [diff] [blame] | 285 | // On ChromeOS we log to the symlink. We force creation of a new |
| 286 | // symlink if we've been asked to delete the old log, since that |
| 287 | // indicates the start of a new session. |
| 288 | target_path = SetUpSymlinkIfNeeded( |
| 289 | log_path, delete_old_log_file == logging::DELETE_OLD_LOG_FILE); |
[email protected] | c7d5da99 | 2010-10-28 00:20:21 | [diff] [blame] | 290 | |
[email protected] | 4a10099 | 2011-06-14 18:22:46 | [diff] [blame] | 291 | // Because ChromeOS manages the move to a new session by redirecting |
| 292 | // the link, it shouldn't remove the old file in the logging code, |
| 293 | // since that will remove the newly created link instead. |
| 294 | delete_old_log_file = logging::APPEND_TO_OLD_LOG_FILE; |
[email protected] | 77ee1b1 | 2010-06-14 16:21:30 | [diff] [blame] | 295 | #endif |
[email protected] | d64ea4217 | 2012-09-14 05:43:59 | [diff] [blame] | 296 | } else { |
| 297 | log_locking_state = DONT_LOCK_LOG_FILE; |
[email protected] | 4a10099 | 2011-06-14 18:22:46 | [diff] [blame] | 298 | } |
[email protected] | 77ee1b1 | 2010-06-14 16:21:30 | [diff] [blame] | 299 | |
[email protected] | 5e3f7c2 | 2013-06-21 21:15:33 | [diff] [blame] | 300 | logging::LoggingSettings settings; |
| 301 | settings.logging_dest = logging_dest; |
| 302 | settings.log_file = log_path.value().c_str(); |
| 303 | settings.lock_log = log_locking_state; |
| 304 | settings.delete_old = delete_old_log_file; |
[email protected] | 5e3f7c2 | 2013-06-21 21:15:33 | [diff] [blame] | 305 | bool success = logging::InitLogging(settings); |
[email protected] | c7d5da99 | 2010-10-28 00:20:21 | [diff] [blame] | 306 | |
| 307 | #if defined(OS_CHROMEOS) |
| 308 | if (!success) { |
[email protected] | 26f025e | 2011-10-28 22:49:27 | [diff] [blame] | 309 | DPLOG(ERROR) << "Unable to initialize logging to " << log_path.value() |
[email protected] | c7d5da99 | 2010-10-28 00:20:21 | [diff] [blame] | 310 | << " (which should be a link to " << target_path.value() << ")"; |
| 311 | RemoveSymlinkAndLog(log_path, target_path); |
[email protected] | 7109dc3 | 2011-02-02 22:58:29 | [diff] [blame] | 312 | chrome_logging_failed_ = true; |
[email protected] | c7d5da99 | 2010-10-28 00:20:21 | [diff] [blame] | 313 | return; |
| 314 | } |
| 315 | #else |
| 316 | if (!success) { |
[email protected] | 26f025e | 2011-10-28 22:49:27 | [diff] [blame] | 317 | DPLOG(ERROR) << "Unable to initialize logging to " << log_path.value(); |
[email protected] | 7109dc3 | 2011-02-02 22:58:29 | [diff] [blame] | 318 | chrome_logging_failed_ = true; |
[email protected] | c7d5da99 | 2010-10-28 00:20:21 | [diff] [blame] | 319 | return; |
| 320 | } |
| 321 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 322 | |
[email protected] | 81e0a85 | 2010-08-17 00:38:12 | [diff] [blame] | 323 | // Default to showing error dialogs. |
| 324 | if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kNoErrorDialogs)) |
| 325 | logging::SetShowErrorDialogs(true); |
| 326 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 327 | // we want process and thread IDs because we have a lot of things running |
[email protected] | c7d5da99 | 2010-10-28 00:20:21 | [diff] [blame] | 328 | logging::SetLogItems(true, // enable_process_id |
| 329 | true, // enable_thread_id |
[email protected] | 51f5a60c | 2012-09-01 06:07:55 | [diff] [blame] | 330 | true, // enable_timestamp |
| 331 | false); // enable_tickcount |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 332 | |
| 333 | // We call running in unattended mode "headless", and allow |
| 334 | // headless mode to be configured either by the Environment |
| 335 | // Variable or by the Command Line Switch. This is for |
| 336 | // automated test purposes. |
[email protected] | 76b90d31 | 2010-08-03 03:00:50 | [diff] [blame] | 337 | scoped_ptr<base::Environment> env(base::Environment::Create()); |
[email protected] | 9432ade | 2010-08-04 23:43:20 | [diff] [blame] | 338 | if (env->HasVar(env_vars::kHeadless) || |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 339 | command_line.HasSwitch(switches::kNoErrorDialogs)) |
| 340 | SuppressDialogs(); |
| 341 | |
[email protected] | 3deeb6b3 | 2010-10-05 21:53:52 | [diff] [blame] | 342 | // Use a minimum log level if the command line asks for one, |
| 343 | // otherwise leave it at the default level (INFO). |
| 344 | if (command_line.HasSwitch(switches::kLoggingLevel)) { |
| 345 | std::string log_level = command_line.GetSwitchValueASCII( |
| 346 | switches::kLoggingLevel); |
| 347 | int level = 0; |
| 348 | if (base::StringToInt(log_level, &level) && |
| 349 | level >= 0 && level < LOG_NUM_SEVERITIES) { |
[email protected] | bb5185c5 | 2008-08-29 19:51:06 | [diff] [blame] | 350 | logging::SetMinLogLevel(level); |
[email protected] | 3deeb6b3 | 2010-10-05 21:53:52 | [diff] [blame] | 351 | } else { |
[email protected] | 26f025e | 2011-10-28 22:49:27 | [diff] [blame] | 352 | DLOG(WARNING) << "Bad log level: " << log_level; |
[email protected] | 3deeb6b3 | 2010-10-05 21:53:52 | [diff] [blame] | 353 | } |
[email protected] | bb5185c5 | 2008-08-29 19:51:06 | [diff] [blame] | 354 | } |
| 355 | |
[email protected] | 2b07b841 | 2009-11-25 15:26:34 | [diff] [blame] | 356 | #if defined(OS_WIN) |
| 357 | // Enable trace control and transport through event tracing for Windows. |
[email protected] | b650bf4 | 2012-03-15 01:02:24 | [diff] [blame] | 358 | logging::LogEventProvider::Initialize(kChromeTraceProviderName); |
[email protected] | 2b07b841 | 2009-11-25 15:26:34 | [diff] [blame] | 359 | #endif |
| 360 | |
[email protected] | c02cb801 | 2014-03-14 18:39:53 | [diff] [blame] | 361 | #if DCHECK_IS_ON && defined(NDEBUG) && defined(OS_WIN) |
| 362 | if (command_line.HasSwitch(switches::kSilentDumpOnDCHECK)) |
[email protected] | ab7ee5b | 2011-03-01 22:24:14 | [diff] [blame] | 363 | logging::SetLogReportHandler(DumpProcessAssertHandler); |
| 364 | #endif |
[email protected] | ab7ee5b | 2011-03-01 22:24:14 | [diff] [blame] | 365 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 366 | chrome_logging_initialized_ = true; |
| 367 | } |
| 368 | |
| 369 | // This is a no-op, but we'll keep it around in case |
| 370 | // we need to do more cleanup in the future. |
| 371 | void CleanupChromeLogging() { |
[email protected] | 7109dc3 | 2011-02-02 22:58:29 | [diff] [blame] | 372 | if (chrome_logging_failed_) |
| 373 | return; // We failed to initiailize logging, no cleanup. |
| 374 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 375 | DCHECK(chrome_logging_initialized_) << |
| 376 | "Attempted to clean up logging when it wasn't initialized."; |
| 377 | |
| 378 | CloseLogFile(); |
| 379 | |
| 380 | chrome_logging_initialized_ = false; |
[email protected] | 30f547c | 2010-08-04 01:00:01 | [diff] [blame] | 381 | chrome_logging_redirected_ = false; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 382 | } |
| 383 | |
[email protected] | a732916 | 2013-02-07 19:21:48 | [diff] [blame] | 384 | base::FilePath GetLogFileName() { |
[email protected] | c83dd91 | 2010-04-06 18:50:51 | [diff] [blame] | 385 | std::string filename; |
[email protected] | 76b90d31 | 2010-08-03 03:00:50 | [diff] [blame] | 386 | scoped_ptr<base::Environment> env(base::Environment::Create()); |
[email protected] | 0b58a81 | 2013-05-31 01:15:17 | [diff] [blame] | 387 | if (env->GetVar(env_vars::kLogFileName, &filename) && !filename.empty()) |
| 388 | return base::FilePath::FromUTF8Unsafe(filename); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 389 | |
[email protected] | a732916 | 2013-02-07 19:21:48 | [diff] [blame] | 390 | const base::FilePath log_filename(FILE_PATH_LITERAL("chrome_debug.log")); |
| 391 | base::FilePath log_path; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 392 | |
| 393 | if (PathService::Get(chrome::DIR_LOGS, &log_path)) { |
[email protected] | 73e2348 | 2009-08-13 00:26:58 | [diff] [blame] | 394 | log_path = log_path.Append(log_filename); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 395 | return log_path; |
| 396 | } else { |
| 397 | // error with path service, just use some default file somewhere |
| 398 | return log_filename; |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | bool DialogsAreSuppressed() { |
| 403 | return dialogs_are_suppressed_; |
| 404 | } |
| 405 | |
| 406 | size_t GetFatalAssertions(AssertionList* assertions) { |
| 407 | // In this function, we don't assume that assertions is non-null, so |
| 408 | // that if you just want an assertion count, you can pass in NULL. |
| 409 | if (assertions) |
| 410 | assertions->clear(); |
| 411 | size_t assertion_count = 0; |
| 412 | |
| 413 | std::ifstream log_file; |
[email protected] | 73e2348 | 2009-08-13 00:26:58 | [diff] [blame] | 414 | log_file.open(GetLogFileName().value().c_str()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 415 | if (!log_file.is_open()) |
| 416 | return 0; |
| 417 | |
| 418 | std::string utf8_line; |
| 419 | std::wstring wide_line; |
[email protected] | 4a3dab2 | 2009-11-11 17:36:50 | [diff] [blame] | 420 | while (!log_file.eof()) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 421 | getline(log_file, utf8_line); |
| 422 | if (utf8_line.find(":FATAL:") != std::string::npos) { |
[email protected] | 036a5f3 | 2013-12-25 00:26:11 | [diff] [blame] | 423 | wide_line = base::UTF8ToWide(utf8_line); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 424 | if (assertions) |
| 425 | assertions->push_back(wide_line); |
| 426 | ++assertion_count; |
| 427 | } |
| 428 | } |
| 429 | log_file.close(); |
| 430 | |
| 431 | return assertion_count; |
| 432 | } |
| 433 | |
[email protected] | a732916 | 2013-02-07 19:21:48 | [diff] [blame] | 434 | base::FilePath GenerateTimestampedName(const base::FilePath& base_path, |
| 435 | base::Time timestamp) { |
[email protected] | 395a039 | 2012-04-12 15:35:03 | [diff] [blame] | 436 | base::Time::Exploded time_deets; |
| 437 | timestamp.LocalExplode(&time_deets); |
| 438 | std::string suffix = base::StringPrintf("_%02d%02d%02d-%02d%02d%02d", |
| 439 | time_deets.year, |
| 440 | time_deets.month, |
| 441 | time_deets.day_of_month, |
| 442 | time_deets.hour, |
| 443 | time_deets.minute, |
| 444 | time_deets.second); |
| 445 | return base_path.InsertBeforeExtensionASCII(suffix); |
| 446 | } |
| 447 | |
[email protected] | c83dd91 | 2010-04-06 18:50:51 | [diff] [blame] | 448 | } // namespace logging |