Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "base/debug/stack_trace.h" | 5 #include "base/debug/stack_trace.h" |
| 6 #include "base/syslog_logging.h" | 6 #include "base/syslog_logging.h" |
| 7 | 7 |
| 8 #if defined(OS_WIN) | 8 #if defined(OS_WIN) |
| 9 #include "base/bind.h" | |
| 10 #include "base/callback_helpers.h" | |
| 9 #include "base/win/eventlog_messages.h" | 11 #include "base/win/eventlog_messages.h" |
| 10 | 12 |
| 11 #include <windows.h> | 13 #include <windows.h> |
| 12 #elif defined(OS_LINUX) | 14 #elif defined(OS_LINUX) |
| 13 #include <syslog.h> | 15 #include <syslog.h> |
| 14 #endif | 16 #endif |
| 15 | 17 |
| 16 #include <cstring> | 18 #include <cstring> |
| 17 #include <ostream> | 19 #include <ostream> |
| 18 #include <string> | 20 #include <string> |
| 19 | 21 |
| 20 namespace logging { | 22 namespace logging { |
| 21 | 23 |
| 24 #if defined(OS_WIN) | |
| 25 | |
| 26 namespace { | |
| 27 std::string* g_event_source_name = nullptr; | |
| 28 } | |
| 29 | |
| 30 void SetEventSourceName(const std::string& name) { | |
| 31 DCHECK_EQ(nullptr, g_event_source_name); | |
| 32 g_event_source_name = new std::string(name); | |
| 33 } | |
| 34 #endif // defined(OS_WIN) | |
| 35 | |
| 22 EventLogMessage::EventLogMessage(const char* file, | 36 EventLogMessage::EventLogMessage(const char* file, |
| 23 int line, | 37 int line, |
| 24 LogSeverity severity) | 38 LogSeverity severity) |
| 25 : log_message_(file, line, severity) { | 39 : log_message_(file, line, severity) { |
| 26 } | 40 } |
| 27 | 41 |
| 28 EventLogMessage::~EventLogMessage() { | 42 EventLogMessage::~EventLogMessage() { |
| 29 #if defined(OS_WIN) | 43 #if defined(OS_WIN) |
| 30 const char kEventSource[] = "chrome"; | 44 // If g_event_source_name is nullptr (which it is per default) SYSLOG will |
| 31 HANDLE event_log_handle = RegisterEventSourceA(NULL, kEventSource); | 45 // degrade gracefully to regular LOG with an extra error suffix. If you see |
|
Nico
2016/12/02 03:08:41
Is this useful? Why wouldn't this CHECK(false) ins
pastarmovj
2016/12/02 19:30:59
I see this as a debugging facility for the debiggi
Nico
2016/12/02 20:28:58
History shows that nobody looks at log spam. Eithe
pastarmovj
2016/12/05 08:40:10
Done.
| |
| 46 // this happening most probably you are using SYSLOG before you called | |
| 47 // SetEventSourceName. | |
| 48 if (g_event_source_name == nullptr) { | |
| 49 stream() << " !!EVENTLOG SOURCE NAME NOT SET!!"; | |
| 50 return; | |
| 51 } | |
| 52 HANDLE event_log_handle = | |
| 53 RegisterEventSourceA(NULL, g_event_source_name->c_str()); | |
| 32 if (event_log_handle == NULL) { | 54 if (event_log_handle == NULL) { |
| 33 stream() << " !!NOT ADDED TO EVENTLOG!!"; | 55 stream() << " !!NOT ADDED TO EVENTLOG!!"; |
| 34 return; | 56 return; |
| 35 } | 57 } |
| 36 | 58 |
| 59 base::ScopedClosureRunner auto_deregister( | |
| 60 base::Bind(base::IgnoreResult(&DeregisterEventSource), event_log_handle)); | |
| 37 std::string message(log_message_.str()); | 61 std::string message(log_message_.str()); |
| 38 WORD log_type = EVENTLOG_ERROR_TYPE; | 62 WORD log_type = EVENTLOG_ERROR_TYPE; |
| 39 switch (log_message_.severity()) { | 63 switch (log_message_.severity()) { |
| 40 case LOG_INFO: | 64 case LOG_INFO: |
| 41 log_type = EVENTLOG_INFORMATION_TYPE; | 65 log_type = EVENTLOG_INFORMATION_TYPE; |
| 42 break; | 66 break; |
| 43 case LOG_WARNING: | 67 case LOG_WARNING: |
| 44 log_type = EVENTLOG_WARNING_TYPE; | 68 log_type = EVENTLOG_WARNING_TYPE; |
| 45 break; | 69 break; |
| 46 case LOG_ERROR: | 70 case LOG_ERROR: |
| 47 case LOG_FATAL: | 71 case LOG_FATAL: |
| 48 // The price of getting the stack trace is not worth the hassle for | 72 // The price of getting the stack trace is not worth the hassle for |
| 49 // non-error conditions. | 73 // non-error conditions. |
| 50 base::debug::StackTrace trace; | 74 base::debug::StackTrace trace; |
| 51 message.append(trace.ToString()); | 75 message.append(trace.ToString()); |
| 52 log_type = EVENTLOG_ERROR_TYPE; | 76 log_type = EVENTLOG_ERROR_TYPE; |
| 53 break; | 77 break; |
| 54 } | 78 } |
| 55 LPCSTR strings[1] = {message.data()}; | 79 LPCSTR strings[1] = {message.data()}; |
| 56 if (!ReportEventA(event_log_handle, log_type, BROWSER_CATEGORY, | 80 if (!ReportEventA(event_log_handle, log_type, BROWSER_CATEGORY, |
| 57 MSG_LOG_MESSAGE, NULL, 1, 0, strings, NULL)) { | 81 MSG_LOG_MESSAGE, NULL, 1, 0, strings, NULL)) { |
| 58 stream() << " !!NOT ADDED TO EVENTLOG!!"; | 82 stream() << " !!NOT ADDED TO EVENTLOG!!"; |
| 59 } | 83 } |
| 60 DeregisterEventSource(event_log_handle); | |
| 61 #elif defined(OS_LINUX) | 84 #elif defined(OS_LINUX) |
| 62 const char kEventSource[] = "chrome"; | 85 const char kEventSource[] = "chrome"; |
| 63 openlog(kEventSource, LOG_NOWAIT | LOG_PID, LOG_USER); | 86 openlog(kEventSource, LOG_NOWAIT | LOG_PID, LOG_USER); |
| 64 // We can't use the defined names for the logging severity from syslog.h | 87 // We can't use the defined names for the logging severity from syslog.h |
| 65 // because they collide with the names of our own severity levels. Therefore | 88 // because they collide with the names of our own severity levels. Therefore |
| 66 // we use the actual values which of course do not match ours. | 89 // we use the actual values which of course do not match ours. |
| 67 // See sys/syslog.h for reference. | 90 // See sys/syslog.h for reference. |
| 68 int priority = 3; | 91 int priority = 3; |
| 69 switch (log_message_.severity()) { | 92 switch (log_message_.severity()) { |
| 70 case LOG_INFO: | 93 case LOG_INFO: |
| 71 priority = 6; | 94 priority = 6; |
| 72 break; | 95 break; |
| 73 case LOG_WARNING: | 96 case LOG_WARNING: |
| 74 priority = 4; | 97 priority = 4; |
| 75 break; | 98 break; |
| 76 case LOG_ERROR: | 99 case LOG_ERROR: |
| 77 priority = 3; | 100 priority = 3; |
| 78 break; | 101 break; |
| 79 case LOG_FATAL: | 102 case LOG_FATAL: |
| 80 priority = 2; | 103 priority = 2; |
| 81 break; | 104 break; |
| 82 } | 105 } |
| 83 syslog(priority, "%s", log_message_.str().c_str()); | 106 syslog(priority, "%s", log_message_.str().c_str()); |
| 84 closelog(); | 107 closelog(); |
| 85 #endif // defined(OS_WIN) | 108 #endif // defined(OS_WIN) |
| 86 } | 109 } |
| 87 | 110 |
| 88 } // namespace logging | 111 } // namespace logging |
| OLD | NEW |