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