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/debug/stack_trace.h"
13 #include "base/strings/string_util.h"
14 #include "base/win/scoped_handle.h"
15 #include "base/win/win_util.h"
16 #elif defined(OS_LINUX) || defined(OS_CHROMEOS)
17 // <syslog.h> defines LOG_INFO, LOG_WARNING macros that could conflict with
18 // base::LOG_INFO, base::LOG_WARNING.
33 std::string* g_event_source_name = nullptr;
34 uint16_t g_category = 0;
35 uint32_t g_event_id = 0;
36 std::wstring* g_user_sid = nullptr;
38 class EventLogHandleTraits {
40 using Handle = HANDLE;
43 static bool CloseHandle(HANDLE handle) {
44 return ::DeregisterEventSource(handle) != FALSE;
47 // Returns true if the handle value is valid.
48 static bool IsHandleValid(HANDLE handle) { return handle != nullptr; }
50 // Returns null handle value.
51 static HANDLE NullHandle() { return nullptr; }
54 DISALLOW_IMPLICIT_CONSTRUCTORS(EventLogHandleTraits);
57 using ScopedEventLogHandle =
58 base::win::GenericScopedHandle<EventLogHandleTraits,
59 base::win::DummyVerifierTraits>;
63 void SetEventSource(const std::string& name,
66 DCHECK_EQ(nullptr, g_event_source_name);
67 g_event_source_name = new std::string(name);
68 g_category = category;
69 g_event_id = event_id;
70 DCHECK_EQ(nullptr, g_user_sid);
71 g_user_sid = new std::wstring();
72 base::win::GetUserSidString(g_user_sid);
75 void ResetEventSourceForTesting() {
76 delete g_event_source_name;
77 g_event_source_name = nullptr;
82 #endif // defined(OS_WIN)
84 EventLogMessage::EventLogMessage(const char* file,
87 : log_message_(file, line, severity) {
90 EventLogMessage::~EventLogMessage() {
92 // If g_event_source_name is nullptr (which it is per default) SYSLOG will
93 // degrade gracefully to regular LOG. If you see this happening most probably
94 // you are using SYSLOG before you called SetEventSourceName.
95 if (g_event_source_name == nullptr)
98 ScopedEventLogHandle event_log_handle(
99 RegisterEventSourceA(nullptr, g_event_source_name->c_str()));
101 if (!event_log_handle.IsValid()) {
102 stream() << " !!NOT ADDED TO EVENTLOG!!";
106 std::string message(log_message_.str());
107 WORD log_type = EVENTLOG_ERROR_TYPE;
108 switch (log_message_.severity()) {
110 log_type = EVENTLOG_INFORMATION_TYPE;
112 case LOGGING_WARNING:
113 log_type = EVENTLOG_WARNING_TYPE;
117 // The price of getting the stack trace is not worth the hassle for
118 // non-error conditions.
119 base::debug::StackTrace trace;
120 message.append(trace.ToString());
121 log_type = EVENTLOG_ERROR_TYPE;
124 LPCSTR strings[1] = {message.data()};
125 PSID user_sid = nullptr;
126 if (!::ConvertStringSidToSid(g_user_sid->c_str(), &user_sid)) {
127 stream() << " !!ERROR GETTING USER SID!!";
130 if (!ReportEventA(event_log_handle.Get(), log_type, g_category, g_event_id,
131 user_sid, 1, 0, strings, nullptr)) {
132 stream() << " !!NOT ADDED TO EVENTLOG!!";
135 if (user_sid != nullptr)
136 ::LocalFree(user_sid);
137 #elif defined(OS_LINUX) || defined(OS_CHROMEOS)
138 const char kEventSource[] = "chrome";
139 openlog(kEventSource, LOG_NOWAIT | LOG_PID, LOG_USER);
140 // We can't use the defined names for the logging severity from syslog.h
141 // because they collide with the names of our own severity levels. Therefore
142 // we use the actual values which of course do not match ours.
143 // See sys/syslog.h for reference.
145 switch (log_message_.severity()) {
149 case LOGGING_WARNING:
159 syslog(priority, "%s", log_message_.str().c_str());
161 #endif // defined(OS_WIN)
164 } // namespace logging