xref: /aosp_15_r20/external/libchrome/base/syslog_logging.cc (revision 635a864187cb8b6c713ff48b7e790a6b21769273)
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 
SetEventSource(const std::string & name,uint16_t category,uint32_t event_id)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 #endif  // defined(OS_WIN)
45 
EventLogMessage(const char * file,int line,LogSeverity severity)46 EventLogMessage::EventLogMessage(const char* file,
47                                  int line,
48                                  LogSeverity severity)
49     : log_message_(file, line, severity) {
50 }
51 
~EventLogMessage()52 EventLogMessage::~EventLogMessage() {
53 #if defined(OS_WIN)
54   // If g_event_source_name is nullptr (which it is per default) SYSLOG will
55   // degrade gracefully to regular LOG. If you see this happening most probably
56   // you are using SYSLOG before you called SetEventSourceName.
57   if (g_event_source_name == nullptr)
58     return;
59 
60   HANDLE event_log_handle =
61       RegisterEventSourceA(nullptr, g_event_source_name->c_str());
62   if (event_log_handle == nullptr) {
63     stream() << " !!NOT ADDED TO EVENTLOG!!";
64     return;
65   }
66 
67   base::ScopedClosureRunner auto_deregister(
68       base::Bind(base::IgnoreResult(&DeregisterEventSource), event_log_handle));
69   std::string message(log_message_.str());
70   WORD log_type = EVENTLOG_ERROR_TYPE;
71   switch (log_message_.severity()) {
72     case LOG_INFO:
73       log_type = EVENTLOG_INFORMATION_TYPE;
74       break;
75     case LOG_WARNING:
76       log_type = EVENTLOG_WARNING_TYPE;
77       break;
78     case LOG_ERROR:
79     case LOG_FATAL:
80       // The price of getting the stack trace is not worth the hassle for
81       // non-error conditions.
82       base::debug::StackTrace trace;
83       message.append(trace.ToString());
84       log_type = EVENTLOG_ERROR_TYPE;
85       break;
86   }
87   LPCSTR strings[1] = {message.data()};
88   if (!ReportEventA(event_log_handle, log_type, g_category, g_event_id, nullptr,
89                     1, 0, strings, nullptr)) {
90     stream() << " !!NOT ADDED TO EVENTLOG!!";
91   }
92 #elif defined(OS_LINUX)
93   const char kEventSource[] = "chrome";
94   openlog(kEventSource, LOG_NOWAIT | LOG_PID, LOG_USER);
95   // We can't use the defined names for the logging severity from syslog.h
96   // because they collide with the names of our own severity levels. Therefore
97   // we use the actual values which of course do not match ours.
98   // See sys/syslog.h for reference.
99   int priority = 3;
100   switch (log_message_.severity()) {
101     case LOG_INFO:
102       priority = 6;
103       break;
104     case LOG_WARNING:
105       priority = 4;
106       break;
107     case LOG_ERROR:
108       priority = 3;
109       break;
110     case LOG_FATAL:
111       priority = 2;
112       break;
113   }
114   syslog(priority, "%s", log_message_.str().c_str());
115   closelog();
116 #endif  // defined(OS_WIN)
117 }
118 
119 }  // namespace logging
120