| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "build/build_config.h" |
| 6 |
| 7 #if defined(OS_WIN) |
| 5 #include <windows.h> | 8 #include <windows.h> |
| 9 #endif |
| 6 | 10 |
| 7 #include <iostream> | 11 #include <iostream> |
| 8 #include <fstream> | 12 #include <fstream> |
| 9 | 13 |
| 10 #include "chrome/common/logging_chrome.h" | 14 #include "chrome/common/logging_chrome.h" |
| 11 | 15 |
| 12 #include "base/command_line.h" | 16 #include "base/command_line.h" |
| 17 #include "base/debug_util.h" |
| 13 #include "base/file_util.h" | 18 #include "base/file_util.h" |
| 14 #include "base/logging.h" | 19 #include "base/logging.h" |
| 15 #include "base/path_service.h" | 20 #include "base/path_service.h" |
| 16 #include "base/string_util.h" | 21 #include "base/string_util.h" |
| 17 #include "base/sys_info.h" | 22 #include "base/sys_info.h" |
| 18 #include "chrome/common/chrome_paths.h" | 23 #include "chrome/common/chrome_paths.h" |
| 19 #include "chrome/common/chrome_switches.h" | 24 #include "chrome/common/chrome_switches.h" |
| 20 #include "chrome/common/env_vars.h" | 25 #include "chrome/common/env_vars.h" |
| 21 | 26 |
| 22 // When true, this means that error dialogs should not be shown. | 27 // When true, this means that error dialogs should not be shown. |
| 23 static bool dialogs_are_suppressed_ = false; | 28 static bool dialogs_are_suppressed_ = false; |
| 24 | 29 |
| 25 // This should be true for exactly the period between the end of | 30 // This should be true for exactly the period between the end of |
| 26 // InitChromeLogging() and the beginning of CleanupChromeLogging(). | 31 // InitChromeLogging() and the beginning of CleanupChromeLogging(). |
| 27 static bool chrome_logging_initialized_ = false; | 32 static bool chrome_logging_initialized_ = false; |
| 28 | 33 |
| 29 // Assertion handler for logging errors that occur when dialogs are | 34 // Assertion handler for logging errors that occur when dialogs are |
| 30 // silenced. To record a new error, pass the log string associated | 35 // silenced. To record a new error, pass the log string associated |
| 31 // with that error in the str parameter. | 36 // with that error in the str parameter. |
| 32 #pragma optimize("", off) | 37 #pragma optimize("", off) |
| 33 static void SilentRuntimeAssertHandler(const std::string& str) { | 38 static void SilentRuntimeAssertHandler(const std::string& str) { |
| 34 __debugbreak(); | 39 DebugUtil::BreakDebugger(); |
| 35 } | 40 } |
| 36 #pragma optimize("", on) | 41 #pragma optimize("", on) |
| 37 | 42 |
| 38 // Suppresses error/assertion dialogs and enables the logging of | 43 // Suppresses error/assertion dialogs and enables the logging of |
| 39 // those errors into silenced_errors_. | 44 // those errors into silenced_errors_. |
| 40 static void SuppressDialogs() { | 45 static void SuppressDialogs() { |
| 41 if (dialogs_are_suppressed_) | 46 if (dialogs_are_suppressed_) |
| 42 return; | 47 return; |
| 43 | 48 |
| 44 logging::SetLogAssertHandler(SilentRuntimeAssertHandler); | 49 logging::SetLogAssertHandler(SilentRuntimeAssertHandler); |
| 45 | 50 |
| 51 #if defined(OS_WIN) |
| 46 UINT new_flags = SEM_FAILCRITICALERRORS | | 52 UINT new_flags = SEM_FAILCRITICALERRORS | |
| 47 SEM_NOGPFAULTERRORBOX | | 53 SEM_NOGPFAULTERRORBOX | |
| 48 SEM_NOOPENFILEERRORBOX; | 54 SEM_NOOPENFILEERRORBOX; |
| 49 | 55 |
| 50 // Preserve existing error mode, as discussed at http://t/dmea | 56 // Preserve existing error mode, as discussed at http://t/dmea |
| 51 UINT existing_flags = SetErrorMode(new_flags); | 57 UINT existing_flags = SetErrorMode(new_flags); |
| 52 SetErrorMode(existing_flags | new_flags); | 58 SetErrorMode(existing_flags | new_flags); |
| 59 #endif |
| 53 | 60 |
| 54 dialogs_are_suppressed_ = true; | 61 dialogs_are_suppressed_ = true; |
| 55 } | 62 } |
| 56 | 63 |
| 57 namespace logging { | 64 namespace logging { |
| 58 | 65 |
| 59 void InitChromeLogging(const CommandLine& command_line, | 66 void InitChromeLogging(const CommandLine& command_line, |
| 60 OldFileDeletionState delete_old_log_file) { | 67 OldFileDeletionState delete_old_log_file) { |
| 61 DCHECK(!chrome_logging_initialized_) << | 68 DCHECK(!chrome_logging_initialized_) << |
| 62 "Attempted to initialize logging when it was already initialized."; | 69 "Attempted to initialize logging when it was already initialized."; |
| (...skipping 14 matching lines...) Expand all Loading... |
| 77 if (command_line.HasSwitch(kInvertLoggingSwitch)) | 84 if (command_line.HasSwitch(kInvertLoggingSwitch)) |
| 78 enable_logging = !enable_logging; | 85 enable_logging = !enable_logging; |
| 79 | 86 |
| 80 logging::LoggingDestination log_mode; | 87 logging::LoggingDestination log_mode; |
| 81 if (enable_logging) { | 88 if (enable_logging) { |
| 82 log_mode = kDefaultLoggingMode; | 89 log_mode = kDefaultLoggingMode; |
| 83 } else { | 90 } else { |
| 84 log_mode = logging::LOG_NONE; | 91 log_mode = logging::LOG_NONE; |
| 85 } | 92 } |
| 86 | 93 |
| 87 logging::InitLogging(GetLogFileName().c_str(), | 94 #if defined(OS_POSIX) |
| 95 std::string log_file_name = WideToUTF8(GetLogFileName()); |
| 96 #elif defined(OS_WIN) |
| 97 std::wstring log_file_name = GetLogFileName(); |
| 98 #endif |
| 99 |
| 100 logging::InitLogging(log_file_name.c_str(), |
| 88 log_mode, | 101 log_mode, |
| 89 logging::LOCK_LOG_FILE, | 102 logging::LOCK_LOG_FILE, |
| 90 delete_old_log_file); | 103 delete_old_log_file); |
| 91 | 104 |
| 92 // we want process and thread IDs because we have a lot of things running | 105 // we want process and thread IDs because we have a lot of things running |
| 93 logging::SetLogItems(true, true, false, true); | 106 logging::SetLogItems(true, true, false, true); |
| 94 | 107 |
| 95 // We call running in unattended mode "headless", and allow | 108 // We call running in unattended mode "headless", and allow |
| 96 // headless mode to be configured either by the Environment | 109 // headless mode to be configured either by the Environment |
| 97 // Variable or by the Command Line Switch. This is for | 110 // Variable or by the Command Line Switch. This is for |
| (...skipping 25 matching lines...) Expand all Loading... |
| 123 void CleanupChromeLogging() { | 136 void CleanupChromeLogging() { |
| 124 DCHECK(chrome_logging_initialized_) << | 137 DCHECK(chrome_logging_initialized_) << |
| 125 "Attempted to clean up logging when it wasn't initialized."; | 138 "Attempted to clean up logging when it wasn't initialized."; |
| 126 | 139 |
| 127 CloseLogFile(); | 140 CloseLogFile(); |
| 128 | 141 |
| 129 chrome_logging_initialized_ = false; | 142 chrome_logging_initialized_ = false; |
| 130 } | 143 } |
| 131 | 144 |
| 132 std::wstring GetLogFileName() { | 145 std::wstring GetLogFileName() { |
| 133 wchar_t filename[MAX_PATH]; | 146 std::wstring filename = base::SysInfo::GetEnvVar(env_vars::kLogFileName); |
| 134 unsigned status = GetEnvironmentVariable(env_vars::kLogFileName, | 147 if (filename != L"") |
| 135 filename, MAX_PATH); | 148 return filename; |
| 136 if (status && (status <= MAX_PATH)) | |
| 137 return std::wstring(filename); | |
| 138 | 149 |
| 139 const std::wstring log_filename(L"chrome_debug.log"); | 150 const std::wstring log_filename(L"chrome_debug.log"); |
| 140 std::wstring log_path; | 151 std::wstring log_path; |
| 141 | 152 |
| 142 if (PathService::Get(chrome::DIR_LOGS, &log_path)) { | 153 if (PathService::Get(chrome::DIR_LOGS, &log_path)) { |
| 143 file_util::AppendToPath(&log_path, log_filename); | 154 file_util::AppendToPath(&log_path, log_filename); |
| 144 return log_path; | 155 return log_path; |
| 145 } else { | 156 } else { |
| 146 // error with path service, just use some default file somewhere | 157 // error with path service, just use some default file somewhere |
| 147 return log_filename; | 158 return log_filename; |
| 148 } | 159 } |
| 149 } | 160 } |
| 150 | 161 |
| 151 bool DialogsAreSuppressed() { | 162 bool DialogsAreSuppressed() { |
| 152 return dialogs_are_suppressed_; | 163 return dialogs_are_suppressed_; |
| 153 } | 164 } |
| 154 | 165 |
| 155 size_t GetFatalAssertions(AssertionList* assertions) { | 166 size_t GetFatalAssertions(AssertionList* assertions) { |
| 156 // In this function, we don't assume that assertions is non-null, so | 167 // In this function, we don't assume that assertions is non-null, so |
| 157 // that if you just want an assertion count, you can pass in NULL. | 168 // that if you just want an assertion count, you can pass in NULL. |
| 158 if (assertions) | 169 if (assertions) |
| 159 assertions->clear(); | 170 assertions->clear(); |
| 160 size_t assertion_count = 0; | 171 size_t assertion_count = 0; |
| 161 | 172 |
| 162 std::ifstream log_file; | 173 std::ifstream log_file; |
| 174 #if defined(OS_WIN) |
| 163 log_file.open(GetLogFileName().c_str()); | 175 log_file.open(GetLogFileName().c_str()); |
| 176 #elif defined(OS_POSIX) |
| 177 log_file.open(WideToUTF8(GetLogFileName()).c_str()); |
| 178 #endif |
| 164 if (!log_file.is_open()) | 179 if (!log_file.is_open()) |
| 165 return 0; | 180 return 0; |
| 166 | 181 |
| 167 std::string utf8_line; | 182 std::string utf8_line; |
| 168 std::wstring wide_line; | 183 std::wstring wide_line; |
| 169 while(!log_file.eof()) { | 184 while(!log_file.eof()) { |
| 170 getline(log_file, utf8_line); | 185 getline(log_file, utf8_line); |
| 171 if (utf8_line.find(":FATAL:") != std::string::npos) { | 186 if (utf8_line.find(":FATAL:") != std::string::npos) { |
| 172 wide_line = UTF8ToWide(utf8_line); | 187 wide_line = UTF8ToWide(utf8_line); |
| 173 if (assertions) | 188 if (assertions) |
| 174 assertions->push_back(wide_line); | 189 assertions->push_back(wide_line); |
| 175 ++assertion_count; | 190 ++assertion_count; |
| 176 } | 191 } |
| 177 } | 192 } |
| 178 log_file.close(); | 193 log_file.close(); |
| 179 | 194 |
| 180 return assertion_count; | 195 return assertion_count; |
| 181 } | 196 } |
| 182 | 197 |
| 183 } // namespace logging | 198 } // namespace logging |
| 184 | 199 |
| OLD | NEW |