pastarmovj | 89f7ee1 | 2016-09-20 14:58:13 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "base/debug/stack_trace.h" |
| 6 | #include "base/syslog_logging.h" |
| 7 | |
| 8 | #if defined(OS_WIN) |
pastarmovj | 35a1fb75 | 2016-11-17 13:18:43 | [diff] [blame^] | 9 | #include "base/win/eventlog_messages.h" |
| 10 | |
pastarmovj | 89f7ee1 | 2016-09-20 14:58:13 | [diff] [blame] | 11 | #include <windows.h> |
| 12 | #elif defined(OS_LINUX) |
| 13 | #include <syslog.h> |
| 14 | #endif |
| 15 | |
| 16 | #include <cstring> |
| 17 | #include <ostream> |
| 18 | #include <string> |
| 19 | |
| 20 | namespace logging { |
| 21 | |
| 22 | EventLogMessage::EventLogMessage(const char* file, |
| 23 | int line, |
| 24 | LogSeverity severity) |
| 25 | : log_message_(file, line, severity) { |
| 26 | } |
| 27 | |
| 28 | EventLogMessage::~EventLogMessage() { |
| 29 | #if defined(OS_WIN) |
| 30 | const char kEventSource[] = "chrome"; |
| 31 | HANDLE event_log_handle = RegisterEventSourceA(NULL, kEventSource); |
| 32 | if (event_log_handle == NULL) { |
| 33 | stream() << " !!NOT ADDED TO EVENTLOG!!"; |
| 34 | return; |
| 35 | } |
| 36 | |
| 37 | std::string message(log_message_.str()); |
pastarmovj | 89f7ee1 | 2016-09-20 14:58:13 | [diff] [blame] | 38 | WORD log_type = EVENTLOG_ERROR_TYPE; |
| 39 | switch (log_message_.severity()) { |
| 40 | case LOG_INFO: |
| 41 | log_type = EVENTLOG_INFORMATION_TYPE; |
| 42 | break; |
| 43 | case LOG_WARNING: |
| 44 | log_type = EVENTLOG_WARNING_TYPE; |
| 45 | break; |
| 46 | case LOG_ERROR: |
| 47 | case LOG_FATAL: |
| 48 | // The price of getting the stack trace is not worth the hassle for |
| 49 | // non-error conditions. |
| 50 | base::debug::StackTrace trace; |
| 51 | message.append(trace.ToString()); |
| 52 | log_type = EVENTLOG_ERROR_TYPE; |
| 53 | break; |
| 54 | } |
pastarmovj | c87074d | 2016-11-15 14:16:54 | [diff] [blame] | 55 | LPCSTR strings[1] = {message.data()}; |
pastarmovj | 35a1fb75 | 2016-11-17 13:18:43 | [diff] [blame^] | 56 | if (!ReportEventA(event_log_handle, log_type, BROWSER_CATEGORY, |
| 57 | MSG_LOG_MESSAGE, NULL, 1, 0, strings, NULL)) { |
pastarmovj | 89f7ee1 | 2016-09-20 14:58:13 | [diff] [blame] | 58 | stream() << " !!NOT ADDED TO EVENTLOG!!"; |
| 59 | } |
| 60 | DeregisterEventSource(event_log_handle); |
| 61 | #elif defined(OS_LINUX) |
| 62 | const char kEventSource[] = "chrome"; |
| 63 | openlog(kEventSource, LOG_NOWAIT | LOG_PID, LOG_USER); |
| 64 | // 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 |
| 66 | // we use the actual values which of course do not match ours. |
| 67 | // See sys/syslog.h for reference. |
| 68 | int priority = 3; |
| 69 | switch (log_message_.severity()) { |
| 70 | case LOG_INFO: |
| 71 | priority = 6; |
| 72 | break; |
| 73 | case LOG_WARNING: |
| 74 | priority = 4; |
| 75 | break; |
| 76 | case LOG_ERROR: |
| 77 | priority = 3; |
| 78 | break; |
| 79 | case LOG_FATAL: |
| 80 | priority = 2; |
| 81 | break; |
| 82 | } |
| 83 | syslog(priority, "%s", log_message_.str().c_str()); |
| 84 | closelog(); |
| 85 | #endif // defined(OS_WIN) |
| 86 | } |
| 87 | |
| 88 | } // namespace logging |