Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(14)

Side by Side Diff: components/crash/content/app/crashpad.cc

Issue 2034393004: Allow multiple logging::LogMessage{Handler,Listener}s Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: clean up, MockLog uses listener Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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 "components/crash/content/app/crashpad.h" 5 #include "components/crash/content/app/crashpad.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 #include <string.h> 8 #include <string.h>
9 9
10 #if BUILDFLAG(ENABLE_KASKO) 10 #if BUILDFLAG(ENABLE_KASKO)
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
54 54
55 void SetCrashKeyValue(const base::StringPiece& key, 55 void SetCrashKeyValue(const base::StringPiece& key,
56 const base::StringPiece& value) { 56 const base::StringPiece& value) {
57 g_simple_string_dictionary->SetKeyValue(key.data(), value.data()); 57 g_simple_string_dictionary->SetKeyValue(key.data(), value.data());
58 } 58 }
59 59
60 void ClearCrashKey(const base::StringPiece& key) { 60 void ClearCrashKey(const base::StringPiece& key) {
61 g_simple_string_dictionary->RemoveKey(key.data()); 61 g_simple_string_dictionary->RemoveKey(key.data());
62 } 62 }
63 63
64 bool LogMessageHandler(int severity, 64 void LogMessageListener(int severity,
65 const char* file, 65 const char* file,
66 int line, 66 int line,
67 size_t message_start, 67 size_t message_start,
68 const std::string& string) { 68 const std::string& string) {
69 // Only handle FATAL. 69 // Only handle FATAL.
70 if (severity != logging::LOG_FATAL) { 70 if (severity != logging::LOG_FATAL) {
71 return false; 71 return;
72 } 72 }
73 73
74 // In case of an out-of-memory condition, this code could be reentered when 74 // In case of an out-of-memory condition, this code could be reentered when
75 // constructing and storing the key. Using a static is not thread-safe, but if 75 // constructing and storing the key. Using a static is not thread-safe, but if
76 // multiple threads are in the process of a fatal crash at the same time, this 76 // multiple threads are in the process of a fatal crash at the same time, this
77 // should work. 77 // should work.
78 static bool guarded = false; 78 static bool guarded = false;
79 if (guarded) { 79 if (guarded) {
80 return false; 80 return;
81 } 81 }
82 base::AutoReset<bool> guard(&guarded, true); 82 base::AutoReset<bool> guard(&guarded, true);
83 83
84 // Only log last path component. This matches logging.cc. 84 // Only log last path component. This matches logging.cc.
85 if (file) { 85 if (file) {
86 const char* slash = strrchr(file, '/'); 86 const char* slash = strrchr(file, '/');
87 if (slash) { 87 if (slash) {
88 file = slash + 1; 88 file = slash + 1;
89 } 89 }
90 } 90 }
91 91
92 CHECK_LE(message_start, string.size()); 92 CHECK_LE(message_start, string.size());
93 std::string message = base::StringPrintf("%s:%d: %s", file, line, 93 std::string message = base::StringPrintf("%s:%d: %s", file, line,
94 string.c_str() + message_start); 94 string.c_str() + message_start);
95 SetCrashKeyValue("LOG_FATAL", message); 95 SetCrashKeyValue("LOG_FATAL", message);
96 96
97 // Rather than including the code to force the crash here, allow the caller to 97 // Rather than including the code to force the crash here, allow the caller to
98 // do it. 98 // do it.
99 return false;
100 } 99 }
101 100
102 void DumpWithoutCrashing() { 101 void DumpWithoutCrashing() {
103 CRASHPAD_SIMULATE_CRASH(); 102 CRASHPAD_SIMULATE_CRASH();
104 } 103 }
105 104
106 #if BUILDFLAG(ENABLE_KASKO) 105 #if BUILDFLAG(ENABLE_KASKO)
107 // TODO(ananta) 106 // TODO(ananta)
108 // We cannot depend on functionality in base which pulls in dependencies on 107 // We cannot depend on functionality in base which pulls in dependencies on
109 // user32 directly or indirectly. The GetLoadedModulesSnapshot is a copy of the 108 // user32 directly or indirectly. The GetLoadedModulesSnapshot is a copy of the
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after
239 #endif 238 #endif
240 239
241 SetCrashKeyValue("ptype", browser_process ? base::StringPiece("browser") 240 SetCrashKeyValue("ptype", browser_process ? base::StringPiece("browser")
242 : base::StringPiece(process_type)); 241 : base::StringPiece(process_type));
243 #if defined(OS_POSIX) 242 #if defined(OS_POSIX)
244 SetCrashKeyValue("pid", base::IntToString(getpid())); 243 SetCrashKeyValue("pid", base::IntToString(getpid()));
245 #elif defined(OS_WIN) 244 #elif defined(OS_WIN)
246 SetCrashKeyValue("pid", base::IntToString(::GetCurrentProcessId())); 245 SetCrashKeyValue("pid", base::IntToString(::GetCurrentProcessId()));
247 #endif 246 #endif
248 247
249 logging::SetLogMessageHandler(LogMessageHandler); 248 logging::AddLogMessageListener(LogMessageListener);
250 249
251 // If clients called CRASHPAD_SIMULATE_CRASH() instead of 250 // If clients called CRASHPAD_SIMULATE_CRASH() instead of
252 // base::debug::DumpWithoutCrashing(), these dumps would appear as crashes in 251 // base::debug::DumpWithoutCrashing(), these dumps would appear as crashes in
253 // the correct function, at the correct file and line. This would be 252 // the correct function, at the correct file and line. This would be
254 // preferable to having all occurrences show up in DumpWithoutCrashing() at 253 // preferable to having all occurrences show up in DumpWithoutCrashing() at
255 // the same file and line. 254 // the same file and line.
256 base::debug::SetDumpWithoutCrashingFunction(DumpWithoutCrashing); 255 base::debug::SetDumpWithoutCrashingFunction(DumpWithoutCrashing);
257 256
258 #if defined(OS_MACOSX) 257 #if defined(OS_MACOSX)
259 // On Mac, we only want the browser to initialize the database, but not the 258 // On Mac, we only want the browser to initialize the database, but not the
(...skipping 215 matching lines...) Expand 10 before | Expand all | Expand 10 after
475 base::UTF16ToUTF8(value)); 474 base::UTF16ToUTF8(value));
476 } 475 }
477 476
478 void __declspec(dllexport) __cdecl ClearCrashKeyValueImpl(const wchar_t* key) { 477 void __declspec(dllexport) __cdecl ClearCrashKeyValueImpl(const wchar_t* key) {
479 crash_reporter::ClearCrashKey(base::UTF16ToUTF8(key)); 478 crash_reporter::ClearCrashKey(base::UTF16ToUTF8(key));
480 } 479 }
481 480
482 } // extern "C" 481 } // extern "C"
483 482
484 #endif // OS_WIN 483 #endif // OS_WIN
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698