Fix emulator build error
[platform/framework/web/chromium-efl.git] / base / syslog_logging.cc
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.
4
5 #include "base/syslog_logging.h"
6
7 #include "build/build_config.h"
8
9 #if BUILDFLAG(IS_WIN)
10 #include <windows.h>
11
12 #include <sddl.h>
13
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.
21 #include <syslog.h>
22 #undef LOG_INFO
23 #undef LOG_WARNING
24 #endif
25
26 #include <ostream>
27 #include <string>
28
29 namespace logging {
30
31 namespace {
32
33 // The syslog logging is on by default, but tests or fuzzers can disable it.
34 bool g_logging_enabled = true;
35
36 }  // namespace
37
38 #if BUILDFLAG(IS_WIN)
39
40 namespace {
41
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;
46
47 class EventLogHandleTraits {
48  public:
49   using Handle = HANDLE;
50
51   EventLogHandleTraits() = delete;
52   EventLogHandleTraits(const EventLogHandleTraits&) = delete;
53   EventLogHandleTraits& operator=(const EventLogHandleTraits&) = delete;
54
55   // Closes the handle.
56   static bool CloseHandle(HANDLE handle) {
57     return ::DeregisterEventSource(handle) != FALSE;
58   }
59
60   // Returns true if the handle value is valid.
61   static bool IsHandleValid(HANDLE handle) { return handle != nullptr; }
62
63   // Returns null handle value.
64   static HANDLE NullHandle() { return nullptr; }
65 };
66
67 using ScopedEventLogHandle =
68     base::win::GenericScopedHandle<EventLogHandleTraits,
69                                    base::win::DummyVerifierTraits>;
70
71 }  // namespace
72
73 void SetEventSource(const std::string& name,
74                     uint16_t category,
75                     uint32_t event_id) {
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);
83 }
84
85 void ResetEventSourceForTesting() {
86   delete g_event_source_name;
87   g_event_source_name = nullptr;
88   delete g_user_sid;
89   g_user_sid = nullptr;
90 }
91
92 #endif  // BUILDFLAG(IS_WIN)
93
94 EventLogMessage::EventLogMessage(const char* file,
95                                  int line,
96                                  LogSeverity severity)
97     : log_message_(file, line, severity) {
98 }
99
100 EventLogMessage::~EventLogMessage() {
101   if (!g_logging_enabled)
102     return;
103
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)
109     return;
110
111   ScopedEventLogHandle event_log_handle(
112       RegisterEventSourceA(nullptr, g_event_source_name->c_str()));
113
114   if (!event_log_handle.is_valid()) {
115     stream() << " !!NOT ADDED TO EVENTLOG!!";
116     return;
117   }
118
119   std::string message(log_message_.str());
120   WORD log_type = EVENTLOG_ERROR_TYPE;
121   switch (log_message_.severity()) {
122     case LOGGING_INFO:
123       log_type = EVENTLOG_INFORMATION_TYPE;
124       break;
125     case LOGGING_WARNING:
126       log_type = EVENTLOG_WARNING_TYPE;
127       break;
128     case LOGGING_ERROR:
129     case LOGGING_FATAL:
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;
135       break;
136   }
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!!";
141   }
142
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!!";
146   }
147
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.
157   int priority = 3;
158   switch (log_message_.severity()) {
159     case LOGGING_INFO:
160       priority = 6;
161       break;
162     case LOGGING_WARNING:
163       priority = 4;
164       break;
165     case LOGGING_ERROR:
166       priority = 3;
167       break;
168     case LOGGING_FATAL:
169       priority = 2;
170       break;
171   }
172   syslog(priority, "%s", log_message_.str().c_str());
173   closelog();
174 #endif  // BUILDFLAG(IS_WIN)
175 }
176
177 void SetSyslogLoggingForTesting(bool logging_enabled) {
178   g_logging_enabled = logging_enabled;
179 }
180
181 }  // namespace logging