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.
5 #include "base/syslog_logging.h"
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.
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;
41 void SetEventSource(const std::string& name,
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);
53 void ResetEventSourceForTesting() {
54 delete g_event_source_name;
55 g_event_source_name = nullptr;
60 #endif // defined(OS_WIN)
62 EventLogMessage::EventLogMessage(const char* file,
65 : log_message_(file, line, severity) {
68 EventLogMessage::~EventLogMessage() {
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)
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!!";
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()) {
89 log_type = EVENTLOG_INFORMATION_TYPE;
92 log_type = EVENTLOG_WARNING_TYPE;
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;
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!!";
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!!";
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.
124 switch (log_message_.severity()) {
138 syslog(priority, "%s", log_message_.str().c_str());
140 #endif // defined(OS_WIN)
143 } // namespace logging