Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1030)

Side by Side Diff: base/syslog_logging.cc

Issue 2530163002: Make the name of the event source for SYSLOG configurable. (Closed)
Patch Set: Remove NOTREACHED and log suffix. Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/syslog_logging.h ('k') | chrome/common/BUILD.gn » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/debug/stack_trace.h" 5 #include "base/debug/stack_trace.h"
6 #include "base/syslog_logging.h" 6 #include "base/syslog_logging.h"
7 7
8 #if defined(OS_WIN) 8 #if defined(OS_WIN)
9 #include "base/bind.h"
10 #include "base/callback_helpers.h"
9 #include "base/win/eventlog_messages.h" 11 #include "base/win/eventlog_messages.h"
10 12
11 #include <windows.h> 13 #include <windows.h>
12 #elif defined(OS_LINUX) 14 #elif defined(OS_LINUX)
13 #include <syslog.h> 15 #include <syslog.h>
14 #endif 16 #endif
15 17
16 #include <cstring> 18 #include <cstring>
17 #include <ostream> 19 #include <ostream>
18 #include <string> 20 #include <string>
19 21
20 namespace logging { 22 namespace logging {
21 23
24 #if defined(OS_WIN)
25
26 namespace {
27 std::string* g_event_source_name = nullptr;
28 }
29
30 void SetEventSourceName(const std::string& name) {
31 DCHECK_EQ(nullptr, g_event_source_name);
32 g_event_source_name = new std::string(name);
33 }
34 #endif // defined(OS_WIN)
35
22 EventLogMessage::EventLogMessage(const char* file, 36 EventLogMessage::EventLogMessage(const char* file,
23 int line, 37 int line,
24 LogSeverity severity) 38 LogSeverity severity)
25 : log_message_(file, line, severity) { 39 : log_message_(file, line, severity) {
26 } 40 }
27 41
28 EventLogMessage::~EventLogMessage() { 42 EventLogMessage::~EventLogMessage() {
29 #if defined(OS_WIN) 43 #if defined(OS_WIN)
30 const char kEventSource[] = "chrome"; 44 // If g_event_source_name is nullptr (which it is per default) SYSLOG will
31 HANDLE event_log_handle = RegisterEventSourceA(NULL, kEventSource); 45 // degrade gracefully to regular LOG. If you see this happening most probably
46 // you are using SYSLOG before you called SetEventSourceName.
47 if (g_event_source_name == nullptr)
48 return;
49
50 HANDLE event_log_handle =
51 RegisterEventSourceA(NULL, g_event_source_name->c_str());
32 if (event_log_handle == NULL) { 52 if (event_log_handle == NULL) {
33 stream() << " !!NOT ADDED TO EVENTLOG!!"; 53 stream() << " !!NOT ADDED TO EVENTLOG!!";
34 return; 54 return;
35 } 55 }
36 56
57 base::ScopedClosureRunner auto_deregister(
58 base::Bind(base::IgnoreResult(&DeregisterEventSource), event_log_handle));
37 std::string message(log_message_.str()); 59 std::string message(log_message_.str());
38 WORD log_type = EVENTLOG_ERROR_TYPE; 60 WORD log_type = EVENTLOG_ERROR_TYPE;
39 switch (log_message_.severity()) { 61 switch (log_message_.severity()) {
40 case LOG_INFO: 62 case LOG_INFO:
41 log_type = EVENTLOG_INFORMATION_TYPE; 63 log_type = EVENTLOG_INFORMATION_TYPE;
42 break; 64 break;
43 case LOG_WARNING: 65 case LOG_WARNING:
44 log_type = EVENTLOG_WARNING_TYPE; 66 log_type = EVENTLOG_WARNING_TYPE;
45 break; 67 break;
46 case LOG_ERROR: 68 case LOG_ERROR:
47 case LOG_FATAL: 69 case LOG_FATAL:
48 // The price of getting the stack trace is not worth the hassle for 70 // The price of getting the stack trace is not worth the hassle for
49 // non-error conditions. 71 // non-error conditions.
50 base::debug::StackTrace trace; 72 base::debug::StackTrace trace;
51 message.append(trace.ToString()); 73 message.append(trace.ToString());
52 log_type = EVENTLOG_ERROR_TYPE; 74 log_type = EVENTLOG_ERROR_TYPE;
53 break; 75 break;
54 } 76 }
55 LPCSTR strings[1] = {message.data()}; 77 LPCSTR strings[1] = {message.data()};
56 if (!ReportEventA(event_log_handle, log_type, BROWSER_CATEGORY, 78 if (!ReportEventA(event_log_handle, log_type, BROWSER_CATEGORY,
57 MSG_LOG_MESSAGE, NULL, 1, 0, strings, NULL)) { 79 MSG_LOG_MESSAGE, NULL, 1, 0, strings, NULL)) {
58 stream() << " !!NOT ADDED TO EVENTLOG!!"; 80 stream() << " !!NOT ADDED TO EVENTLOG!!";
59 } 81 }
60 DeregisterEventSource(event_log_handle);
61 #elif defined(OS_LINUX) 82 #elif defined(OS_LINUX)
62 const char kEventSource[] = "chrome"; 83 const char kEventSource[] = "chrome";
63 openlog(kEventSource, LOG_NOWAIT | LOG_PID, LOG_USER); 84 openlog(kEventSource, LOG_NOWAIT | LOG_PID, LOG_USER);
64 // We can't use the defined names for the logging severity from syslog.h 85 // We can't use the defined names for the logging severity from syslog.h
65 // because they collide with the names of our own severity levels. Therefore 86 // because they collide with the names of our own severity levels. Therefore
66 // we use the actual values which of course do not match ours. 87 // we use the actual values which of course do not match ours.
67 // See sys/syslog.h for reference. 88 // See sys/syslog.h for reference.
68 int priority = 3; 89 int priority = 3;
69 switch (log_message_.severity()) { 90 switch (log_message_.severity()) {
70 case LOG_INFO: 91 case LOG_INFO:
71 priority = 6; 92 priority = 6;
72 break; 93 break;
73 case LOG_WARNING: 94 case LOG_WARNING:
74 priority = 4; 95 priority = 4;
75 break; 96 break;
76 case LOG_ERROR: 97 case LOG_ERROR:
77 priority = 3; 98 priority = 3;
78 break; 99 break;
79 case LOG_FATAL: 100 case LOG_FATAL:
80 priority = 2; 101 priority = 2;
81 break; 102 break;
82 } 103 }
83 syslog(priority, "%s", log_message_.str().c_str()); 104 syslog(priority, "%s", log_message_.str().c_str());
84 closelog(); 105 closelog();
85 #endif // defined(OS_WIN) 106 #endif // defined(OS_WIN)
86 } 107 }
87 108
88 } // namespace logging 109 } // namespace logging
OLDNEW
« no previous file with comments | « base/syslog_logging.h ('k') | chrome/common/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698