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

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