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