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

Side by Side Diff: base/syslog_logging.cc

Issue 2530163002: Make the name of the event source for SYSLOG configurable. (Closed)
Patch Set: Change to per-channel naming. 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
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/win/eventlog_messages.h" 9 #include "base/win/eventlog_messages.h"
10 10
11 #include <windows.h> 11 #include <windows.h>
12 #elif defined(OS_LINUX) 12 #elif defined(OS_LINUX)
13 #include <syslog.h> 13 #include <syslog.h>
14 #endif 14 #endif
15 15
16 #include <cstring> 16 #include <cstring>
17 #include <ostream> 17 #include <ostream>
18 #include <string> 18 #include <string>
19 19
20 namespace logging { 20 namespace logging {
21 21
22 #if defined(OS_WIN)
23
24 namespace {
25 std::string* g_event_source_name = nullptr;
26 }
27
28 void SetEventSourceName(const std::string& name) {
29 DCHECK(g_event_source_name == nullptr);
grt (UTC plus 2) 2016/11/28 12:49:52 nit: DCHECK_EQ(g_event_source_name, nullptr);
pastarmovj 2016/11/28 15:53:17 Done.
30 g_event_source_name = new std::string(name);
31 }
32 #endif // defined(OS_WIN)
33
22 EventLogMessage::EventLogMessage(const char* file, 34 EventLogMessage::EventLogMessage(const char* file,
23 int line, 35 int line,
24 LogSeverity severity) 36 LogSeverity severity)
25 : log_message_(file, line, severity) { 37 : log_message_(file, line, severity) {
26 } 38 }
27 39
28 EventLogMessage::~EventLogMessage() { 40 EventLogMessage::~EventLogMessage() {
29 #if defined(OS_WIN) 41 #if defined(OS_WIN)
30 const char kEventSource[] = "chrome"; 42 // If g_event_source_name is nullptr (which it is per default) SYSLOG will
31 HANDLE event_log_handle = RegisterEventSourceA(NULL, kEventSource); 43 // degrade gracefully to regular LOG with an extra error suffix. If you see
44 // this happening most probably you are using SYSLOG before you called
45 // SetEventSourceName.
46 if (g_event_source_name == nullptr) {
47 stream() << " !!EVENTLOG SOURCE NAME NOT SET!!";
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
grt (UTC plus 2) 2016/11/28 12:49:52 nit: consider base::ScopedClosureRunner auto_der
pastarmovj 2016/11/28 15:53:17 Done.
37 std::string message(log_message_.str()); 57 std::string message(log_message_.str());
38 WORD log_type = EVENTLOG_ERROR_TYPE; 58 WORD log_type = EVENTLOG_ERROR_TYPE;
39 switch (log_message_.severity()) { 59 switch (log_message_.severity()) {
40 case LOG_INFO: 60 case LOG_INFO:
41 log_type = EVENTLOG_INFORMATION_TYPE; 61 log_type = EVENTLOG_INFORMATION_TYPE;
42 break; 62 break;
43 case LOG_WARNING: 63 case LOG_WARNING:
44 log_type = EVENTLOG_WARNING_TYPE; 64 log_type = EVENTLOG_WARNING_TYPE;
45 break; 65 break;
46 case LOG_ERROR: 66 case LOG_ERROR:
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
79 case LOG_FATAL: 99 case LOG_FATAL:
80 priority = 2; 100 priority = 2;
81 break; 101 break;
82 } 102 }
83 syslog(priority, "%s", log_message_.str().c_str()); 103 syslog(priority, "%s", log_message_.str().c_str());
84 closelog(); 104 closelog();
85 #endif // defined(OS_WIN) 105 #endif // defined(OS_WIN)
86 } 106 }
87 107
88 } // namespace logging 108 } // namespace logging
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698