| 6 |
| 7 | #if defined(OS_WIN) |
| 8 | #include <windows.h> |
| 9 |
| 10 | #include <sddl.h> |
| 11 |
| 12 | #include "base/bind.h" |
| 13 | #include "base/callback_helpers.h" |
| 14 | #include "base/debug/stack_trace.h" |
| 15 | #include "base/strings/string_util.h" |
| 16 | #include "base/win/win_util.h" |
| 17 | #elif defined(OS_LINUX) |
| 18 | // <syslog.h> defines LOG_INFO, LOG_WARNING macros that could conflict with |
| 19 | // base::LOG_INFO, base::LOG_WARNING. |
| 20 | #include <syslog.h> |
| 21 | #undef LOG_INFO |
| 22 | #undef LOG_WARNING |
| 23 | #endif |
| 24 |
| 25 | #include <ostream> |
| 26 | #include <string> |
| 27 |
| 28 | namespace logging { |
| 29 |
| 30 | #if defined(OS_WIN) |
| 31 |
| 32 | namespace { |
| 33 |
| 34 | std::string* g_event_source_name = nullptr; |
| 35 | uint16_t g_category = 0; |
| 36 | uint32_t g_event_id = 0; |
| 37 | std::wstring* g_user_sid = nullptr; |
| 38 |
| 39 | } // namespace |
| 40 |
| 41 | void SetEventSource(const std::string& name, |
| 42 | uint16_t category, |
| 43 | uint32_t event_id) { |
| 44 | DCHECK_EQ(nullptr, g_event_source_name); |
| 45 | g_event_source_name = new std::string(name); |
| 46 | g_category = category; |
| 47 | g_event_id = event_id; |
| 48 | DCHECK_EQ(nullptr, g_user_sid); |
| 49 | g_user_sid = new std::wstring(); |
| 50 | base::win::GetUserSidString(g_user_sid); |
| 51 | } |
| 52 |
| 53 | void ResetEventSourceForTesting() { |
| 54 | delete g_event_source_name; |
| 55 | g_event_source_name = nullptr; |
| 56 | delete g_user_sid; |
| 57 | g_user_sid = nullptr; |
| 58 | } |
| 59 |
| 60 | #endif // defined(OS_WIN) |
| 61 |
| 62 | EventLogMessage::EventLogMessage(const char* file, |
| 63 | int line, |
| 64 | LogSeverity severity) |
| 65 | : log_message_(file, line, severity) { |
| 66 | } |
| 67 |
| 68 | EventLogMessage::~EventLogMessage() { |
| 69 | #if defined(OS_WIN) |
| 70 | // If g_event_source_name is nullptr (which it is per default) SYSLOG will |
| 71 | // degrade gracefully to regular LOG. If you see this happening most probably |
| 72 | // you are using SYSLOG before you called SetEventSourceName. |
| 73 | if (g_event_source_name == nullptr) |
| 74 | return; |
| 75 |
| 76 | HANDLE event_log_handle = |
| 77 | RegisterEventSourceA(nullptr, g_event_source_name->c_str()); |
| 78 | if (event_log_handle == nullptr) { |
| 79 | stream() << " !!NOT ADDED TO EVENTLOG!!"; |
| 80 | return; |
| 81 | } |
| 82 |
| 83 | base::ScopedClosureRunner auto_deregister(base::BindOnce( |
| 84 | base::IgnoreResult(&DeregisterEventSource), event_log_handle)); |
| 85 | std::string message(log_message_.str()); |
| 86 | WORD log_type = EVENTLOG_ERROR_TYPE; |
| 87 | switch (log_message_.severity()) { |
| 88 | case LOG_INFO: |
| 89 | log_type = EVENTLOG_INFORMATION_TYPE; |
| 90 | break; |
| 91 | case LOG_WARNING: |
| 92 | log_type = EVENTLOG_WARNING_TYPE; |
| 93 | break; |
| 94 | case LOG_ERROR: |
| 95 | case LOG_FATAL: |
| 96 | // The price of getting the stack trace is not worth the hassle for |
| 97 | // non-error conditions. |
| 98 | base::debug::StackTrace trace; |
| 99 | message.append(trace.ToString()); |
| 100 | log_type = EVENTLOG_ERROR_TYPE; |
| 101 | break; |
| 102 | } |
| 103 | LPCSTR strings[1] = {message.data()}; |
| 104 | PSID user_sid = nullptr; |
| 105 | if (!::ConvertStringSidToSid(g_user_sid->c_str(), &user_sid)) { |
| 106 | stream() << " !!ERROR GETTING USER SID!!"; |
| 107 | } |
| 108 |
| 109 | if (!ReportEventA(event_log_handle, log_type, g_category, g_event_id, |
| 110 | user_sid, 1, 0, strings, nullptr)) { |
| 111 | stream() << " !!NOT ADDED TO EVENTLOG!!"; |
| 112 | } |
| 113 |
| 114 | if (user_sid != nullptr) |
| 115 | ::LocalFree(user_sid); |
| 116 | #elif defined(OS_LINUX) |
| 117 | const char kEventSource[] = "chrome"; |
| 118 | openlog(kEventSource, LOG_NOWAIT | LOG_PID, LOG_USER); |
| 119 | // We can't use the defined names for the logging severity from syslog.h |
| 120 | // because they collide with the names of our own severity levels. Therefore |
| 121 | // we use the actual values which of course do not match ours. |
| 122 | // See sys/syslog.h for reference. |
| 123 | int priority = 3; |
| 124 | switch (log_message_.severity()) { |
| 125 | case LOG_INFO: |
| 126 | priority = 6; |
| 127 | break; |
| 128 | case LOG_WARNING: |
| 129 | priority = 4; |
| 130 | break; |
| 131 | case LOG_ERROR: |
| 132 | priority = 3; |
| 133 | break; |
| 134 | case LOG_FATAL: |
| 135 | priority = 2; |
| 136 | break; |
| 137 | } |
| 138 | syslog(priority, "%s", log_message_.str().c_str()); |
| 139 | closelog(); |
| 140 | #endif // defined(OS_WIN) |
| 141 | } |
| 142 |
| 143 | } // namespace logging |
| 144 |