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