[M94 Dev][Tizen] Fix for errors for generating ninja files
[platform/framework/web/chromium-efl.git] / base / syslog_logging.cc
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.
4
5 #include "base/syslog_logging.h"
6
7 #if defined(OS_WIN)
8 #include <windows.h>
9
10 #include <sddl.h>
11
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.
19 #include <syslog.h>
20 #undef LOG_INFO
21 #undef LOG_WARNING
22 #endif
23
24 #include <ostream>
25 #include <string>
26
27 namespace logging {
28
29 #if defined(OS_WIN)
30
31 namespace {
32
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;
37
38 class EventLogHandleTraits {
39  public:
40   using Handle = HANDLE;
41
42   // Closes the handle.
43   static bool CloseHandle(HANDLE handle) {
44     return ::DeregisterEventSource(handle) != FALSE;
45   }
46
47   // Returns true if the handle value is valid.
48   static bool IsHandleValid(HANDLE handle) { return handle != nullptr; }
49
50   // Returns null handle value.
51   static HANDLE NullHandle() { return nullptr; }
52
53  private:
54   DISALLOW_IMPLICIT_CONSTRUCTORS(EventLogHandleTraits);
55 };
56
57 using ScopedEventLogHandle =
58     base::win::GenericScopedHandle<EventLogHandleTraits,
59                                    base::win::DummyVerifierTraits>;
60
61 }  // namespace
62
63 void SetEventSource(const std::string& name,
64                     uint16_t category,
65                     uint32_t event_id) {
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);
73 }
74
75 void ResetEventSourceForTesting() {
76   delete g_event_source_name;
77   g_event_source_name = nullptr;
78   delete g_user_sid;
79   g_user_sid = nullptr;
80 }
81
82 #endif  // defined(OS_WIN)
83
84 EventLogMessage::EventLogMessage(const char* file,
85                                  int line,
86                                  LogSeverity severity)
87     : log_message_(file, line, severity) {
88 }
89
90 EventLogMessage::~EventLogMessage() {
91 #if defined(OS_WIN)
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)
96     return;
97
98   ScopedEventLogHandle event_log_handle(
99       RegisterEventSourceA(nullptr, g_event_source_name->c_str()));
100
101   if (!event_log_handle.IsValid()) {
102     stream() << " !!NOT ADDED TO EVENTLOG!!";
103     return;
104   }
105
106   std::string message(log_message_.str());
107   WORD log_type = EVENTLOG_ERROR_TYPE;
108   switch (log_message_.severity()) {
109     case LOGGING_INFO:
110       log_type = EVENTLOG_INFORMATION_TYPE;
111       break;
112     case LOGGING_WARNING:
113       log_type = EVENTLOG_WARNING_TYPE;
114       break;
115     case LOGGING_ERROR:
116     case LOGGING_FATAL:
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;
122       break;
123   }
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!!";
128   }
129
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!!";
133   }
134
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.
144   int priority = 3;
145   switch (log_message_.severity()) {
146     case LOGGING_INFO:
147       priority = 6;
148       break;
149     case LOGGING_WARNING:
150       priority = 4;
151       break;
152     case LOGGING_ERROR:
153       priority = 3;
154       break;
155     case LOGGING_FATAL:
156       priority = 2;
157       break;
158   }
159   syslog(priority, "%s", log_message_.str().c_str());
160   closelog();
161 #endif  // defined(OS_WIN)
162 }
163
164 }  // namespace logging