1 // Copyright 2016 The Chromium Authors
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"
7 #include "build/build_config.h"
14 #include "base/debug/stack_trace.h"
15 #include "base/strings/string_util.h"
16 #include "base/win/scoped_handle.h"
17 #include "base/win/win_util.h"
18 #elif BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
19 // <syslog.h> defines LOG_INFO, LOG_WARNING macros that could conflict with
20 // base::LOG_INFO, base::LOG_WARNING.
33 // The syslog logging is on by default, but tests or fuzzers can disable it.
34 bool g_logging_enabled = true;
42 std::string* g_event_source_name = nullptr;
43 uint16_t g_category = 0;
44 uint32_t g_event_id = 0;
45 std::wstring* g_user_sid = nullptr;
47 class EventLogHandleTraits {
49 using Handle = HANDLE;
51 EventLogHandleTraits() = delete;
52 EventLogHandleTraits(const EventLogHandleTraits&) = delete;
53 EventLogHandleTraits& operator=(const EventLogHandleTraits&) = delete;
56 static bool CloseHandle(HANDLE handle) {
57 return ::DeregisterEventSource(handle) != FALSE;
60 // Returns true if the handle value is valid.
61 static bool IsHandleValid(HANDLE handle) { return handle != nullptr; }
63 // Returns null handle value.
64 static HANDLE NullHandle() { return nullptr; }
67 using ScopedEventLogHandle =
68 base::win::GenericScopedHandle<EventLogHandleTraits,
69 base::win::DummyVerifierTraits>;
73 void SetEventSource(const std::string& name,
76 DCHECK_EQ(nullptr, g_event_source_name);
77 g_event_source_name = new std::string(name);
78 g_category = category;
79 g_event_id = event_id;
80 DCHECK_EQ(nullptr, g_user_sid);
81 g_user_sid = new std::wstring();
82 base::win::GetUserSidString(g_user_sid);
85 void ResetEventSourceForTesting() {
86 delete g_event_source_name;
87 g_event_source_name = nullptr;
92 #endif // BUILDFLAG(IS_WIN)
94 EventLogMessage::EventLogMessage(const char* file,
97 : log_message_(file, line, severity) {
100 EventLogMessage::~EventLogMessage() {
101 if (!g_logging_enabled)
104 #if BUILDFLAG(IS_WIN)
105 // If g_event_source_name is nullptr (which it is per default) SYSLOG will
106 // degrade gracefully to regular LOG. If you see this happening most probably
107 // you are using SYSLOG before you called SetEventSourceName.
108 if (g_event_source_name == nullptr)
111 ScopedEventLogHandle event_log_handle(
112 RegisterEventSourceA(nullptr, g_event_source_name->c_str()));
114 if (!event_log_handle.is_valid()) {
115 stream() << " !!NOT ADDED TO EVENTLOG!!";
119 std::string message(log_message_.str());
120 WORD log_type = EVENTLOG_ERROR_TYPE;
121 switch (log_message_.severity()) {
123 log_type = EVENTLOG_INFORMATION_TYPE;
125 case LOGGING_WARNING:
126 log_type = EVENTLOG_WARNING_TYPE;
130 // The price of getting the stack trace is not worth the hassle for
131 // non-error conditions.
132 base::debug::StackTrace trace;
133 message.append(trace.ToString());
134 log_type = EVENTLOG_ERROR_TYPE;
137 LPCSTR strings[1] = {message.data()};
138 PSID user_sid = nullptr;
139 if (!::ConvertStringSidToSid(g_user_sid->c_str(), &user_sid)) {
140 stream() << " !!ERROR GETTING USER SID!!";
143 if (!ReportEventA(event_log_handle.get(), log_type, g_category, g_event_id,
144 user_sid, 1, 0, strings, nullptr)) {
145 stream() << " !!NOT ADDED TO EVENTLOG!!";
148 if (user_sid != nullptr)
149 ::LocalFree(user_sid);
150 #elif BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
151 const char kEventSource[] = "chrome";
152 openlog(kEventSource, LOG_NOWAIT | LOG_PID, LOG_USER);
153 // We can't use the defined names for the logging severity from syslog.h
154 // because they collide with the names of our own severity levels. Therefore
155 // we use the actual values which of course do not match ours.
156 // See sys/syslog.h for reference.
158 switch (log_message_.severity()) {
162 case LOGGING_WARNING:
172 syslog(priority, "%s", log_message_.str().c_str());
174 #endif // BUILDFLAG(IS_WIN)
177 void SetSyslogLoggingForTesting(bool logging_enabled) {
178 g_logging_enabled = logging_enabled;
181 } // namespace logging