blob: 2c44cd5b3c18da49eeb90fa02a9a5fc5b5845bdf [file] [log] [blame]
pastarmovj89f7ee12016-09-20 14:58:131// 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
pastarmovj89f7ee12016-09-20 14:58:135#include "base/syslog_logging.h"
6
7#if defined(OS_WIN)
Bruce Dawsonbfdc3fd2018-01-03 20:32:368#include <windows.h>
pastarmovja3eac432016-12-07 17:38:529#include "base/bind.h"
10#include "base/callback_helpers.h"
probergefe476002017-06-23 14:29:0111#include "base/debug/stack_trace.h"
pastarmovj89f7ee12016-09-20 14:58:1312#elif defined(OS_LINUX)
Daniel Bratell0e26719b2018-07-11 08:06:4613// <syslog.h> defines LOG_INFO, LOG_WARNING macros that could conflict with
14// base::LOG_INFO, base::LOG_WARNING.
pastarmovj89f7ee12016-09-20 14:58:1315#include <syslog.h>
Daniel Bratell0e26719b2018-07-11 08:06:4616#undef LOG_INFO
weza245bd072017-06-18 23:26:3417#undef LOG_WARNING
pastarmovj89f7ee12016-09-20 14:58:1318#endif
19
pastarmovj89f7ee12016-09-20 14:58:1320#include <ostream>
21#include <string>
22
23namespace logging {
24
pastarmovja3eac432016-12-07 17:38:5225#if defined(OS_WIN)
26
27namespace {
pastarmovja3eac432016-12-07 17:38:5228
probergefe476002017-06-23 14:29:0129std::string* g_event_source_name = nullptr;
30uint16_t g_category = 0;
31uint32_t g_event_id = 0;
32
33} // namespace
34
35void SetEventSource(const std::string& name,
36 uint16_t category,
37 uint32_t event_id) {
pastarmovja3eac432016-12-07 17:38:5238 DCHECK_EQ(nullptr, g_event_source_name);
39 g_event_source_name = new std::string(name);
probergefe476002017-06-23 14:29:0140 g_category = category;
41 g_event_id = event_id;
pastarmovja3eac432016-12-07 17:38:5242}
probergefe476002017-06-23 14:29:0143
Roger Tawaf761bb62018-10-09 20:19:2744void ResetEventSourceForTesting() {
45 delete g_event_source_name;
46 g_event_source_name = nullptr;
47}
48
pastarmovja3eac432016-12-07 17:38:5249#endif // defined(OS_WIN)
50
pastarmovj89f7ee12016-09-20 14:58:1351EventLogMessage::EventLogMessage(const char* file,
52 int line,
53 LogSeverity severity)
54 : log_message_(file, line, severity) {
55}
56
57EventLogMessage::~EventLogMessage() {
58#if defined(OS_WIN)
pastarmovja3eac432016-12-07 17:38:5259 // 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 =
probergefe476002017-06-23 14:29:0166 RegisterEventSourceA(nullptr, g_event_source_name->c_str());
67 if (event_log_handle == nullptr) {
pastarmovj89f7ee12016-09-20 14:58:1368 stream() << " !!NOT ADDED TO EVENTLOG!!";
69 return;
70 }
71
pastarmovja3eac432016-12-07 17:38:5272 base::ScopedClosureRunner auto_deregister(
73 base::Bind(base::IgnoreResult(&DeregisterEventSource), event_log_handle));
pastarmovj89f7ee12016-09-20 14:58:1374 std::string message(log_message_.str());
pastarmovj89f7ee12016-09-20 14:58:1375 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 }
pastarmovjc87074d2016-11-15 14:16:5492 LPCSTR strings[1] = {message.data()};
probergefe476002017-06-23 14:29:0193 if (!ReportEventA(event_log_handle, log_type, g_category, g_event_id, nullptr,
94 1, 0, strings, nullptr)) {
pastarmovj89f7ee12016-09-20 14:58:1395 stream() << " !!NOT ADDED TO EVENTLOG!!";
96 }
pastarmovj89f7ee12016-09-20 14:58:1397#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