license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 1 | // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
| 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 |
| 19 | #include "chrome/common/devtools_messages.h" |
| 20 | #include "chrome/common/plugin_messages.h" |
| 21 | #include "chrome/common/render_messages.h" |
| 22 | #include "chrome/common/worker_messages.h" |
| 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 | |
| 29 | #include <iostream> |
| 30 | #include <fstream> |
| 31 | |
| 32 | #include "chrome/common/logging_chrome.h" |
| 33 | |
| 34 | #include "base/command_line.h" |
[email protected] | 0eb9f434 | 2008-11-21 18:48:52 | [diff] [blame] | 35 | #include "base/compiler_specific.h" |
[email protected] | ec65780 | 2008-11-07 20:05:39 | [diff] [blame] | 36 | #include "base/debug_util.h" |
[email protected] | 73e2348 | 2009-08-13 00:26:58 | [diff] [blame] | 37 | #include "base/file_path.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 38 | #include "base/file_util.h" |
| 39 | #include "base/logging.h" |
| 40 | #include "base/path_service.h" |
| 41 | #include "base/string_util.h" |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 42 | #include "base/sys_info.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 43 | #include "chrome/common/chrome_paths.h" |
| 44 | #include "chrome/common/chrome_switches.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 45 | #include "chrome/common/env_vars.h" |
[email protected] | 51d5e16 | 2009-07-27 19:23:54 | [diff] [blame] | 46 | #include "ipc/ipc_logging.h" |
[email protected] | d55aaa13 | 2009-09-28 21:08:04 | [diff] [blame] | 47 | #include "ipc/ipc_message.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 48 | |
| 49 | // When true, this means that error dialogs should not be shown. |
| 50 | static bool dialogs_are_suppressed_ = false; |
| 51 | |
| 52 | // This should be true for exactly the period between the end of |
| 53 | // InitChromeLogging() and the beginning of CleanupChromeLogging(). |
| 54 | static bool chrome_logging_initialized_ = false; |
| 55 | |
| 56 | // Assertion handler for logging errors that occur when dialogs are |
| 57 | // silenced. To record a new error, pass the log string associated |
| 58 | // with that error in the str parameter. |
[email protected] | 0eb9f434 | 2008-11-21 18:48:52 | [diff] [blame] | 59 | MSVC_DISABLE_OPTIMIZE(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 60 | static void SilentRuntimeAssertHandler(const std::string& str) { |
[email protected] | ec65780 | 2008-11-07 20:05:39 | [diff] [blame] | 61 | DebugUtil::BreakDebugger(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 62 | } |
[email protected] | 6cc67d7f | 2009-02-12 06:48:24 | [diff] [blame] | 63 | static void SilentRuntimeReportHandler(const std::string& str) { |
| 64 | } |
[email protected] | 0eb9f434 | 2008-11-21 18:48:52 | [diff] [blame] | 65 | MSVC_ENABLE_OPTIMIZE(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 66 | |
| 67 | // Suppresses error/assertion dialogs and enables the logging of |
| 68 | // those errors into silenced_errors_. |
| 69 | static void SuppressDialogs() { |
| 70 | if (dialogs_are_suppressed_) |
| 71 | return; |
| 72 | |
| 73 | logging::SetLogAssertHandler(SilentRuntimeAssertHandler); |
[email protected] | 6cc67d7f | 2009-02-12 06:48:24 | [diff] [blame] | 74 | logging::SetLogReportHandler(SilentRuntimeReportHandler); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 75 | |
[email protected] | ec65780 | 2008-11-07 20:05:39 | [diff] [blame] | 76 | #if defined(OS_WIN) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 77 | UINT new_flags = SEM_FAILCRITICALERRORS | |
| 78 | SEM_NOGPFAULTERRORBOX | |
| 79 | SEM_NOOPENFILEERRORBOX; |
| 80 | |
| 81 | // Preserve existing error mode, as discussed at http://t/dmea |
| 82 | UINT existing_flags = SetErrorMode(new_flags); |
| 83 | SetErrorMode(existing_flags | new_flags); |
[email protected] | ec65780 | 2008-11-07 20:05:39 | [diff] [blame] | 84 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 85 | |
| 86 | dialogs_are_suppressed_ = true; |
| 87 | } |
| 88 | |
| 89 | namespace logging { |
| 90 | |
| 91 | void InitChromeLogging(const CommandLine& command_line, |
| 92 | OldFileDeletionState delete_old_log_file) { |
| 93 | DCHECK(!chrome_logging_initialized_) << |
| 94 | "Attempted to initialize logging when it was already initialized."; |
| 95 | |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 96 | #if defined(OS_POSIX) && defined(IPC_MESSAGE_LOG_ENABLED) |
| 97 | IPC::Logging::SetLoggerFunctions(g_log_function_mapping); |
| 98 | #endif |
| 99 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 100 | // only use OutputDebugString in debug mode |
| 101 | #ifdef NDEBUG |
| 102 | bool enable_logging = false; |
[email protected] | b7e0a2a | 2009-10-13 02:07:25 | [diff] [blame] | 103 | const char *kInvertLoggingSwitch = switches::kEnableLogging; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 104 | const logging::LoggingDestination kDefaultLoggingMode = |
| 105 | logging::LOG_ONLY_TO_FILE; |
| 106 | #else |
| 107 | bool enable_logging = true; |
[email protected] | b7e0a2a | 2009-10-13 02:07:25 | [diff] [blame] | 108 | const char *kInvertLoggingSwitch = switches::kDisableLogging; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 109 | const logging::LoggingDestination kDefaultLoggingMode = |
| 110 | logging::LOG_TO_BOTH_FILE_AND_SYSTEM_DEBUG_LOG; |
| 111 | #endif |
| 112 | |
| 113 | if (command_line.HasSwitch(kInvertLoggingSwitch)) |
| 114 | enable_logging = !enable_logging; |
| 115 | |
| 116 | logging::LoggingDestination log_mode; |
| 117 | if (enable_logging) { |
[email protected] | 3bba958 | 2009-08-05 01:29:12 | [diff] [blame] | 118 | // Let --enable-logging=stderr force only stderr, particularly useful for |
| 119 | // 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] | 120 | if (command_line.GetSwitchValueASCII(switches::kEnableLogging) == "stderr") |
[email protected] | 3bba958 | 2009-08-05 01:29:12 | [diff] [blame] | 121 | log_mode = logging::LOG_ONLY_TO_SYSTEM_DEBUG_LOG; |
| 122 | else |
| 123 | log_mode = kDefaultLoggingMode; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 124 | } else { |
| 125 | log_mode = logging::LOG_NONE; |
| 126 | } |
| 127 | |
[email protected] | 73e2348 | 2009-08-13 00:26:58 | [diff] [blame] | 128 | logging::InitLogging(GetLogFileName().value().c_str(), |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 129 | log_mode, |
| 130 | logging::LOCK_LOG_FILE, |
| 131 | delete_old_log_file); |
| 132 | |
| 133 | // we want process and thread IDs because we have a lot of things running |
| 134 | logging::SetLogItems(true, true, false, true); |
| 135 | |
| 136 | // We call running in unattended mode "headless", and allow |
| 137 | // headless mode to be configured either by the Environment |
| 138 | // Variable or by the Command Line Switch. This is for |
| 139 | // automated test purposes. |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 140 | if (base::SysInfo::HasEnvVar(env_vars::kHeadless) || |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 141 | command_line.HasSwitch(switches::kNoErrorDialogs)) |
| 142 | SuppressDialogs(); |
| 143 | |
[email protected] | c4e52f0d | 2009-11-06 19:55:16 | [diff] [blame] | 144 | std::string log_filter_prefix = |
| 145 | command_line.GetSwitchValueASCII(switches::kLogFilterPrefix); |
| 146 | logging::SetLogFilterPrefix(log_filter_prefix.c_str()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 147 | |
[email protected] | bb5185c5 | 2008-08-29 19:51:06 | [diff] [blame] | 148 | // Use a minimum log level if the command line has one, otherwise set the |
| 149 | // default to LOG_WARNING. |
[email protected] | c4e52f0d | 2009-11-06 19:55:16 | [diff] [blame] | 150 | std::string log_level = command_line.GetSwitchValueASCII( |
| 151 | switches::kLoggingLevel); |
[email protected] | bb5185c5 | 2008-08-29 19:51:06 | [diff] [blame] | 152 | int level = 0; |
[email protected] | c4e52f0d | 2009-11-06 19:55:16 | [diff] [blame] | 153 | if (StringToInt(log_level, &level)) { |
[email protected] | bb5185c5 | 2008-08-29 19:51:06 | [diff] [blame] | 154 | if ((level >= 0) && (level < LOG_NUM_SEVERITIES)) |
| 155 | logging::SetMinLogLevel(level); |
| 156 | } else { |
| 157 | logging::SetMinLogLevel(LOG_WARNING); |
| 158 | } |
| 159 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 160 | chrome_logging_initialized_ = true; |
| 161 | } |
| 162 | |
| 163 | // This is a no-op, but we'll keep it around in case |
| 164 | // we need to do more cleanup in the future. |
| 165 | void CleanupChromeLogging() { |
| 166 | DCHECK(chrome_logging_initialized_) << |
| 167 | "Attempted to clean up logging when it wasn't initialized."; |
| 168 | |
| 169 | CloseLogFile(); |
| 170 | |
| 171 | chrome_logging_initialized_ = false; |
| 172 | } |
| 173 | |
[email protected] | 73e2348 | 2009-08-13 00:26:58 | [diff] [blame] | 174 | FilePath GetLogFileName() { |
[email protected] | ec65780 | 2008-11-07 20:05:39 | [diff] [blame] | 175 | std::wstring filename = base::SysInfo::GetEnvVar(env_vars::kLogFileName); |
[email protected] | 73e2348 | 2009-08-13 00:26:58 | [diff] [blame] | 176 | if (!filename.empty()) |
| 177 | return FilePath::FromWStringHack(filename); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 178 | |
[email protected] | 73e2348 | 2009-08-13 00:26:58 | [diff] [blame] | 179 | const FilePath log_filename(FILE_PATH_LITERAL("chrome_debug.log")); |
| 180 | FilePath log_path; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 181 | |
| 182 | if (PathService::Get(chrome::DIR_LOGS, &log_path)) { |
[email protected] | 73e2348 | 2009-08-13 00:26:58 | [diff] [blame] | 183 | log_path = log_path.Append(log_filename); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 184 | return log_path; |
| 185 | } else { |
| 186 | // error with path service, just use some default file somewhere |
| 187 | return log_filename; |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | bool DialogsAreSuppressed() { |
| 192 | return dialogs_are_suppressed_; |
| 193 | } |
| 194 | |
| 195 | size_t GetFatalAssertions(AssertionList* assertions) { |
| 196 | // In this function, we don't assume that assertions is non-null, so |
| 197 | // that if you just want an assertion count, you can pass in NULL. |
| 198 | if (assertions) |
| 199 | assertions->clear(); |
| 200 | size_t assertion_count = 0; |
| 201 | |
| 202 | std::ifstream log_file; |
[email protected] | 73e2348 | 2009-08-13 00:26:58 | [diff] [blame] | 203 | log_file.open(GetLogFileName().value().c_str()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 204 | if (!log_file.is_open()) |
| 205 | return 0; |
| 206 | |
| 207 | std::string utf8_line; |
| 208 | std::wstring wide_line; |
[email protected] | 4a3dab2 | 2009-11-11 17:36:50 | [diff] [blame^] | 209 | while (!log_file.eof()) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 210 | getline(log_file, utf8_line); |
| 211 | if (utf8_line.find(":FATAL:") != std::string::npos) { |
| 212 | wide_line = UTF8ToWide(utf8_line); |
| 213 | if (assertions) |
| 214 | assertions->push_back(wide_line); |
| 215 | ++assertion_count; |
| 216 | } |
| 217 | } |
| 218 | log_file.close(); |
| 219 | |
| 220 | return assertion_count; |
| 221 | } |
| 222 | |
| 223 | } // namespace logging |