OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2012 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 | |
5 #include "base/debug/stack_trace.h" | |
6 #include "base/syslog_logging.h" | |
7 | |
8 #if defined(OS_POSIX) | |
9 #include <syslog.h> | |
10 #endif | |
11 | |
12 #include <cstring> | |
13 #include <ostream> | |
14 #include <string> | |
15 | |
16 namespace logging { | |
17 | |
18 EventLogMessage::EventLogMessage(const char* file, | |
19 int line, | |
20 LogSeverity severity) | |
21 : log_message_(file, line, severity) { | |
22 } | |
23 | |
24 EventLogMessage::~EventLogMessage() { | |
25 const char kEventSource[] = "chrome"; | |
26 std::string message(log_message_.str()); | |
27 | |
28 #if defined(OS_WIN) | |
29 HANDLE event_log_handle = RegisterEventSourceA(NULL, kEventSource); | |
brettw
2016/09/14 22:33:48
I'm not familiar with this API, but MSDN says the
pastarmovj
2016/09/15 21:27:10
Actually this string appears in the log as the "so
| |
30 if (event_log_handle == NULL) { | |
31 stream() << " !!NOT ADDED TO EVENTLOG!!"; | |
32 return; | |
33 } | |
34 | |
35 LPCSTR strings[1] = {message.data()}; | |
36 WORD log_type = EVENTLOG_ERROR_TYPE; | |
37 switch (log_message_.severity()) { | |
38 case LOG_INFO: | |
39 log_type = EVENTLOG_INFORMATION_TYPE; | |
40 break; | |
41 case LOG_WARNING: | |
42 log_type = EVENTLOG_WARNING_TYPE; | |
43 break; | |
44 case LOG_ERROR: | |
45 case LOG_FATAL: | |
46 // The price of getting the stack trace is not worth the hassle for | |
47 // non-error conditions. | |
48 base::debug::StackTrace trace; | |
49 message.append(trace.ToString()); | |
50 log_type = EVENTLOG_ERROR_TYPE; | |
51 break; | |
52 } | |
53 // TODO(pastarmovj): Register Chrome's event log resource types to make the | |
54 // entries nicer. 1337 is just a made up event id type. | |
55 if (!ReportEventA(event_log_handle, log_type, 0, 1337, NULL, 1, 0, | |
56 strings, NULL)) { | |
57 stream() << " !!NOT ADDED TO EVENTLOG!!"; | |
58 } | |
59 DeregisterEventSource(event_log_handle); | |
60 #elif defined(OS_POSIX) | |
61 openlog(kEventSource, LOG_NOWAIT, LOG_USER); | |
62 // We can't use the defined names for the logging severity from syslog.h | |
63 // because they collide with the names of our own severity levels. Therefore | |
64 // we use the actual values which of course do no match ours. See sys/syslog.h | |
brettw
2016/09/14 22:33:48
"do no" -> "do not"
pastarmovj
2016/09/15 21:27:10
Done.
| |
65 // for reference. | |
66 int priority = 3; | |
67 switch (log_message_.severity()) { | |
68 case LOG_INFO: | |
69 priority = 6; | |
70 break; | |
71 case LOG_WARNING: | |
72 priority = 4; | |
73 break; | |
74 case LOG_ERROR: | |
75 priority = 3; | |
76 break; | |
77 case LOG_FATAL: | |
78 priority = 2; | |
79 break; | |
80 } | |
81 syslog(priority, "%s", message.c_str()); | |
82 closelog(); | |
83 #endif | |
84 } | |
85 | |
86 } // namespace logging | |
OLD | NEW |