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 | a3eac43 | 2016-12-07 17:38:52 | [diff] [blame] | 9 | #include "base/bind.h" |
| 10 | #include "base/callback_helpers.h" |
pastarmovj | 35a1fb75 | 2016-11-17 13:18:43 | [diff] [blame] | 11 | #include "base/win/eventlog_messages.h" |
| 12 | |
pastarmovj | 89f7ee1 | 2016-09-20 14:58:13 | [diff] [blame] | 13 | #include <windows.h> |
| 14 | #elif defined(OS_LINUX) |
| 15 | #include <syslog.h> |
| 16 | #endif |
| 17 | |
| 18 | #include <cstring> |
| 19 | #include <ostream> |
| 20 | #include <string> |
| 21 | |
| 22 | namespace logging { |
| 23 | |
pastarmovj | a3eac43 | 2016-12-07 17:38:52 | [diff] [blame] | 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 | |
pastarmovj | 89f7ee1 | 2016-09-20 14:58:13 | [diff] [blame] | 36 | EventLogMessage::EventLogMessage(const char* file, |
| 37 | int line, |
| 38 | LogSeverity severity) |
| 39 | : log_message_(file, line, severity) { |
| 40 | } |
| 41 | |
| 42 | EventLogMessage::~EventLogMessage() { |
| 43 | #if defined(OS_WIN) |
pastarmovj | a3eac43 | 2016-12-07 17:38:52 | [diff] [blame] | 44 | // 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 |
| 46 | // you are using SYSLOG before you called SetEventSourceName. |
| 47 | if (g_event_source_name == nullptr) |
| 48 | return; |
| 49 | |
| 50 | HANDLE event_log_handle = |
| 51 | RegisterEventSourceA(NULL, g_event_source_name->c_str()); |
pastarmovj | 89f7ee1 | 2016-09-20 14:58:13 | [diff] [blame] | 52 | if (event_log_handle == NULL) { |
| 53 | stream() << " !!NOT ADDED TO EVENTLOG!!"; |
| 54 | return; |
| 55 | } |
| 56 | |
pastarmovj | a3eac43 | 2016-12-07 17:38:52 | [diff] [blame] | 57 | base::ScopedClosureRunner auto_deregister( |
| 58 | base::Bind(base::IgnoreResult(&DeregisterEventSource), event_log_handle)); |
pastarmovj | 89f7ee1 | 2016-09-20 14:58:13 | [diff] [blame] | 59 | std::string message(log_message_.str()); |
pastarmovj | 89f7ee1 | 2016-09-20 14:58:13 | [diff] [blame] | 60 | WORD log_type = EVENTLOG_ERROR_TYPE; |
| 61 | switch (log_message_.severity()) { |
| 62 | case LOG_INFO: |
| 63 | log_type = EVENTLOG_INFORMATION_TYPE; |
| 64 | break; |
| 65 | case LOG_WARNING: |
| 66 | log_type = EVENTLOG_WARNING_TYPE; |
| 67 | break; |
| 68 | case LOG_ERROR: |
| 69 | case LOG_FATAL: |
| 70 | // The price of getting the stack trace is not worth the hassle for |
| 71 | // non-error conditions. |
| 72 | base::debug::StackTrace trace; |
| 73 | message.append(trace.ToString()); |
| 74 | log_type = EVENTLOG_ERROR_TYPE; |
| 75 | break; |
| 76 | } |
pastarmovj | c87074d | 2016-11-15 14:16:54 | [diff] [blame] | 77 | LPCSTR strings[1] = {message.data()}; |
pastarmovj | 35a1fb75 | 2016-11-17 13:18:43 | [diff] [blame] | 78 | if (!ReportEventA(event_log_handle, log_type, BROWSER_CATEGORY, |
| 79 | MSG_LOG_MESSAGE, NULL, 1, 0, strings, NULL)) { |
pastarmovj | 89f7ee1 | 2016-09-20 14:58:13 | [diff] [blame] | 80 | stream() << " !!NOT ADDED TO EVENTLOG!!"; |
| 81 | } |
pastarmovj | 89f7ee1 | 2016-09-20 14:58:13 | [diff] [blame] | 82 | #elif defined(OS_LINUX) |
| 83 | const char kEventSource[] = "chrome"; |
| 84 | openlog(kEventSource, LOG_NOWAIT | LOG_PID, LOG_USER); |
| 85 | // 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 |
| 87 | // we use the actual values which of course do not match ours. |
| 88 | // See sys/syslog.h for reference. |
| 89 | int priority = 3; |
| 90 | switch (log_message_.severity()) { |
| 91 | case LOG_INFO: |
| 92 | priority = 6; |
| 93 | break; |
| 94 | case LOG_WARNING: |
| 95 | priority = 4; |
| 96 | break; |
| 97 | case LOG_ERROR: |
| 98 | priority = 3; |
| 99 | break; |
| 100 | case LOG_FATAL: |
| 101 | priority = 2; |
| 102 | break; |
| 103 | } |
| 104 | syslog(priority, "%s", log_message_.str().c_str()); |
| 105 | closelog(); |
| 106 | #endif // defined(OS_WIN) |
| 107 | } |
| 108 | |
| 109 | } // namespace logging |