[M73 Dev][Tizen] Fix compilation errors for TV profile
[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 #include "base/bind.h"
10 #include "base/callback_helpers.h"
11 #include "base/debug/stack_trace.h"
12 #elif defined(OS_LINUX)
13 // <syslog.h> defines LOG_INFO, LOG_WARNING macros that could conflict with
14 // base::LOG_INFO, base::LOG_WARNING.
15 #include <syslog.h>
16 #undef LOG_INFO
17 #undef LOG_WARNING
18 #endif
19
20 #include <ostream>
21 #include <string>
22
23 namespace logging {
24
25 #if defined(OS_WIN)
26
27 namespace {
28
29 std::string* g_event_source_name = nullptr;
30 uint16_t g_category = 0;
31 uint32_t g_event_id = 0;
32
33 }  // namespace
34
35 void SetEventSource(const std::string& name,
36                     uint16_t category,
37                     uint32_t event_id) {
38   DCHECK_EQ(nullptr, g_event_source_name);
39   g_event_source_name = new std::string(name);
40   g_category = category;
41   g_event_id = event_id;
42 }
43
44 void ResetEventSourceForTesting() {
45   delete g_event_source_name;
46   g_event_source_name = nullptr;
47 }
48
49 #endif  // defined(OS_WIN)
50
51 EventLogMessage::EventLogMessage(const char* file,
52                                  int line,
53                                  LogSeverity severity)
54     : log_message_(file, line, severity) {
55 }
56
57 EventLogMessage::~EventLogMessage() {
58 #if defined(OS_WIN)
59   // If g_event_source_name is nullptr (which it is per default) SYSLOG will
60   // degrade gracefully to regular LOG. If you see this happening most probably
61   // you are using SYSLOG before you called SetEventSourceName.
62   if (g_event_source_name == nullptr)
63     return;
64
65   HANDLE event_log_handle =
66       RegisterEventSourceA(nullptr, g_event_source_name->c_str());
67   if (event_log_handle == nullptr) {
68     stream() << " !!NOT ADDED TO EVENTLOG!!";
69     return;
70   }
71
72   base::ScopedClosureRunner auto_deregister(
73       base::Bind(base::IgnoreResult(&DeregisterEventSource), event_log_handle));
74   std::string message(log_message_.str());
75   WORD log_type = EVENTLOG_ERROR_TYPE;
76   switch (log_message_.severity()) {
77     case LOG_INFO:
78       log_type = EVENTLOG_INFORMATION_TYPE;
79       break;
80     case LOG_WARNING:
81       log_type = EVENTLOG_WARNING_TYPE;
82       break;
83     case LOG_ERROR:
84     case LOG_FATAL:
85       // The price of getting the stack trace is not worth the hassle for
86       // non-error conditions.
87       base::debug::StackTrace trace;
88       message.append(trace.ToString());
89       log_type = EVENTLOG_ERROR_TYPE;
90       break;
91   }
92   LPCSTR strings[1] = {message.data()};
93   if (!ReportEventA(event_log_handle, log_type, g_category, g_event_id, nullptr,
94                     1, 0, strings, nullptr)) {
95     stream() << " !!NOT ADDED TO EVENTLOG!!";
96   }
97 #elif defined(OS_LINUX)
98   const char kEventSource[] = "chrome";
99   openlog(kEventSource, LOG_NOWAIT | LOG_PID, LOG_USER);
100   // We can't use the defined names for the logging severity from syslog.h
101   // because they collide with the names of our own severity levels. Therefore
102   // we use the actual values which of course do not match ours.
103   // See sys/syslog.h for reference.
104   int priority = 3;
105   switch (log_message_.severity()) {
106     case LOG_INFO:
107       priority = 6;
108       break;
109     case LOG_WARNING:
110       priority = 4;
111       break;
112     case LOG_ERROR:
113       priority = 3;
114       break;
115     case LOG_FATAL:
116       priority = 2;
117       break;
118   }
119   syslog(priority, "%s", log_message_.str().c_str());
120   closelog();
121 #endif  // defined(OS_WIN)
122 }
123
124 }  // namespace logging