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

Unified Diff: base/syslog_logging.cc

Issue 2296783002: Adds new logging type SYSLOG which logs to the system log. (Closed)
Patch Set: Moved the SYSLOG code to a separate file. Created 4 years, 3 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 side-by-side diff with in-line comments
Download patch
« base/syslog_logging.h ('K') | « base/syslog_logging.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/syslog_logging.cc
diff --git a/base/syslog_logging.cc b/base/syslog_logging.cc
new file mode 100644
index 0000000000000000000000000000000000000000..2fc7541dd0f504ed42ead70f7c632974563848b7
--- /dev/null
+++ b/base/syslog_logging.cc
@@ -0,0 +1,86 @@
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "base/debug/stack_trace.h"
+#include "base/syslog_logging.h"
+
+#if defined(OS_POSIX)
+#include <syslog.h>
+#endif
+
+#include <cstring>
+#include <ostream>
+#include <string>
+
+namespace logging {
+
+EventLogMessage::EventLogMessage(const char* file,
+ int line,
+ LogSeverity severity)
+ : log_message_(file, line, severity) {
+}
+
+EventLogMessage::~EventLogMessage() {
+ const char kEventSource[] = "chrome";
+ std::string message(log_message_.str());
+
+#if defined(OS_WIN)
+ 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
+ if (event_log_handle == NULL) {
+ stream() << " !!NOT ADDED TO EVENTLOG!!";
+ return;
+ }
+
+ LPCSTR strings[1] = {message.data()};
+ WORD log_type = EVENTLOG_ERROR_TYPE;
+ switch (log_message_.severity()) {
+ case LOG_INFO:
+ log_type = EVENTLOG_INFORMATION_TYPE;
+ break;
+ case LOG_WARNING:
+ log_type = EVENTLOG_WARNING_TYPE;
+ break;
+ case LOG_ERROR:
+ case LOG_FATAL:
+ // The price of getting the stack trace is not worth the hassle for
+ // non-error conditions.
+ base::debug::StackTrace trace;
+ message.append(trace.ToString());
+ log_type = EVENTLOG_ERROR_TYPE;
+ break;
+ }
+ // TODO(pastarmovj): Register Chrome's event log resource types to make the
+ // entries nicer. 1337 is just a made up event id type.
+ if (!ReportEventA(event_log_handle, log_type, 0, 1337, NULL, 1, 0,
+ strings, NULL)) {
+ stream() << " !!NOT ADDED TO EVENTLOG!!";
+ }
+ DeregisterEventSource(event_log_handle);
+#elif defined(OS_POSIX)
+ openlog(kEventSource, LOG_NOWAIT, LOG_USER);
+ // We can't use the defined names for the logging severity from syslog.h
+ // because they collide with the names of our own severity levels. Therefore
+ // 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.
+ // for reference.
+ int priority = 3;
+ switch (log_message_.severity()) {
+ case LOG_INFO:
+ priority = 6;
+ break;
+ case LOG_WARNING:
+ priority = 4;
+ break;
+ case LOG_ERROR:
+ priority = 3;
+ break;
+ case LOG_FATAL:
+ priority = 2;
+ break;
+ }
+ syslog(priority, "%s", message.c_str());
+ closelog();
+#endif
+}
+
+} // namespace logging
« base/syslog_logging.h ('K') | « base/syslog_logging.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698