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

Side by Side Diff: base/syslog_logging.cc

Issue 2946983002: Move eventlog_provider from //base/win to //chrome/common/win (Closed)
Patch Set: Address review comments on #3 Created 3 years, 6 months 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/syslog_logging.h"
6
5 #include "base/debug/stack_trace.h" 7 #include "base/debug/stack_trace.h"
grt (UTC plus 2) 2017/06/21 21:48:01 nit: move this into the OS_WIN block below?
proberge 2017/06/22 14:35:47 Done.
6 #include "base/syslog_logging.h"
7 8
8 #if defined(OS_WIN) 9 #if defined(OS_WIN)
9 #include "base/bind.h" 10 #include "base/bind.h"
10 #include "base/callback_helpers.h" 11 #include "base/callback_helpers.h"
11 #include "base/win/eventlog_messages.h"
12
13 #include <windows.h>
14 #elif defined(OS_LINUX) 12 #elif defined(OS_LINUX)
15 #include <syslog.h> 13 #include <syslog.h>
16 #endif 14 #endif
17 15
18 #include <cstring>
19 #include <ostream> 16 #include <ostream>
20 #include <string> 17 #include <string>
21 18
22 namespace logging { 19 namespace logging {
23 20
24 #if defined(OS_WIN) 21 #if defined(OS_WIN)
25 22
26 namespace { 23 namespace {
24
27 std::string* g_event_source_name = nullptr; 25 std::string* g_event_source_name = nullptr;
26 WORD g_category = 0;
27 DWORD g_event_id = 0;
28
29 } // namespace
30
31 void SetEventSource(const std::string& name, WORD category, DWORD event_id) {
32 DCHECK_EQ(nullptr, g_event_source_name);
33 g_event_source_name = new std::string(name);
34 g_category = category;
35 g_event_id = event_id;
28 } 36 }
29 37
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) 38 #endif // defined(OS_WIN)
35 39
36 EventLogMessage::EventLogMessage(const char* file, 40 EventLogMessage::EventLogMessage(const char* file,
37 int line, 41 int line,
38 LogSeverity severity) 42 LogSeverity severity)
39 : log_message_(file, line, severity) { 43 : log_message_(file, line, severity) {
40 } 44 }
41 45
42 EventLogMessage::~EventLogMessage() { 46 EventLogMessage::~EventLogMessage() {
43 #if defined(OS_WIN) 47 #if defined(OS_WIN)
44 // If g_event_source_name is nullptr (which it is per default) SYSLOG will 48 // If g_event_source_name is nullptr (which it is per default) SYSLOG will
45 // degrade gracefully to regular LOG. If you see this happening most probably 49 // degrade gracefully to regular LOG. If you see this happening most probably
46 // you are using SYSLOG before you called SetEventSourceName. 50 // you are using SYSLOG before you called SetEventSourceName.
47 if (g_event_source_name == nullptr) 51 if (g_event_source_name == nullptr)
48 return; 52 return;
49 53
50 HANDLE event_log_handle = 54 HANDLE event_log_handle =
51 RegisterEventSourceA(NULL, g_event_source_name->c_str()); 55 RegisterEventSourceA(nullptr, g_event_source_name->c_str());
52 if (event_log_handle == NULL) { 56 if (event_log_handle == nullptr) {
53 stream() << " !!NOT ADDED TO EVENTLOG!!"; 57 stream() << " !!NOT ADDED TO EVENTLOG!!";
54 return; 58 return;
55 } 59 }
56 60
57 base::ScopedClosureRunner auto_deregister( 61 base::ScopedClosureRunner auto_deregister(
58 base::Bind(base::IgnoreResult(&DeregisterEventSource), event_log_handle)); 62 base::Bind(base::IgnoreResult(&DeregisterEventSource), event_log_handle));
59 std::string message(log_message_.str()); 63 std::string message(log_message_.str());
60 WORD log_type = EVENTLOG_ERROR_TYPE; 64 WORD log_type = EVENTLOG_ERROR_TYPE;
61 switch (log_message_.severity()) { 65 switch (log_message_.severity()) {
62 case LOG_INFO: 66 case LOG_INFO:
63 log_type = EVENTLOG_INFORMATION_TYPE; 67 log_type = EVENTLOG_INFORMATION_TYPE;
64 break; 68 break;
65 case LOG_WARNING: 69 case LOG_WARNING:
66 log_type = EVENTLOG_WARNING_TYPE; 70 log_type = EVENTLOG_WARNING_TYPE;
67 break; 71 break;
68 case LOG_ERROR: 72 case LOG_ERROR:
69 case LOG_FATAL: 73 case LOG_FATAL:
70 // The price of getting the stack trace is not worth the hassle for 74 // The price of getting the stack trace is not worth the hassle for
71 // non-error conditions. 75 // non-error conditions.
72 base::debug::StackTrace trace; 76 base::debug::StackTrace trace;
73 message.append(trace.ToString()); 77 message.append(trace.ToString());
74 log_type = EVENTLOG_ERROR_TYPE; 78 log_type = EVENTLOG_ERROR_TYPE;
75 break; 79 break;
76 } 80 }
77 LPCSTR strings[1] = {message.data()}; 81 LPCSTR strings[1] = {message.data()};
78 if (!ReportEventA(event_log_handle, log_type, BROWSER_CATEGORY, 82 if (!ReportEventA(event_log_handle, log_type, g_category, g_event_id, nullptr,
79 MSG_LOG_MESSAGE, NULL, 1, 0, strings, NULL)) { 83 1, 0, strings, nullptr)) {
80 stream() << " !!NOT ADDED TO EVENTLOG!!"; 84 stream() << " !!NOT ADDED TO EVENTLOG!!";
81 } 85 }
82 #elif defined(OS_LINUX) 86 #elif defined(OS_LINUX)
83 const char kEventSource[] = "chrome"; 87 const char kEventSource[] = "chrome";
84 openlog(kEventSource, LOG_NOWAIT | LOG_PID, LOG_USER); 88 openlog(kEventSource, LOG_NOWAIT | LOG_PID, LOG_USER);
85 // We can't use the defined names for the logging severity from syslog.h 89 // We can't use the defined names for the logging severity from syslog.h
86 // because they collide with the names of our own severity levels. Therefore 90 // because they collide with the names of our own severity levels. Therefore
87 // we use the actual values which of course do not match ours. 91 // we use the actual values which of course do not match ours.
88 // See sys/syslog.h for reference. 92 // See sys/syslog.h for reference.
89 int priority = 3; 93 int priority = 3;
(...skipping 10 matching lines...) Expand all
100 case LOG_FATAL: 104 case LOG_FATAL:
101 priority = 2; 105 priority = 2;
102 break; 106 break;
103 } 107 }
104 syslog(priority, "%s", log_message_.str().c_str()); 108 syslog(priority, "%s", log_message_.str().c_str());
105 closelog(); 109 closelog();
106 #endif // defined(OS_WIN) 110 #endif // defined(OS_WIN)
107 } 111 }
108 112
109 } // namespace logging 113 } // namespace logging
OLDNEW
« base/syslog_logging.h ('K') | « base/syslog_logging.h ('k') | base/win/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698