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

Unified Diff: chromeos/syslog_logging.cc

Issue 6509006: libchromeos: add process control library, syslog logging capability, and run unit tests (Closed) Base URL: ssh://git@gitrw.chromium.org:9222/common.git@master
Patch Set: sync Created 9 years, 10 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
Index: chromeos/syslog_logging.cc
diff --git a/chromeos/syslog_logging.cc b/chromeos/syslog_logging.cc
new file mode 100644
index 0000000000000000000000000000000000000000..f1a31aabeb8a94d9038138e5124c16f4aa9c09df
--- /dev/null
+++ b/chromeos/syslog_logging.cc
@@ -0,0 +1,97 @@
+// Copyright (c) 2011 The Chromium OS 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 <chromeos/syslog_logging.h>
+
+#include <string>
+#include <syslog.h>
+
+// syslog.h and base/logging.h both try to #define LOG_INFO and LOG_WARNING.
+// We need to #undef at least these two before including base/logging.h. The
+// others are included to be consistent.
+namespace {
+const int kSyslogInfo = LOG_INFO;
+const int kSyslogWarning = LOG_WARNING;
+const int kSyslogError = LOG_ERR;
+const int kSyslogCritical = LOG_CRIT;
+
+#undef LOG_INFO
+#undef LOG_WARNING
+#undef LOG_ERR
+#undef LOG_INFO
+} // namespace
+
+#include <base/logging.h>
+
+static std::string s_ident;
+static std::string s_accumulated;
+static bool s_accumulate;
+static bool s_log_to_syslog;
+static bool s_log_to_stderr;
+
+static bool HandleMessage(int severity, const std::string &message) {
+ switch (severity) {
+ case logging::LOG_INFO:
+ severity = kSyslogInfo;
+ break;
+
+ case logging::LOG_WARNING:
+ severity = kSyslogWarning;
+ break;
+
+ case logging::LOG_ERROR:
+ case logging::LOG_ERROR_REPORT:
+ severity = kSyslogError;
+ break;
+
+ case logging::LOG_FATAL:
+ severity = kSyslogCritical;
+ break;
+ }
+
+ // The first "] " should be the end of the header added by the logging
+ // code. The meat of the message is two characters after that.
+ size_t pos = message.find("] ");
+ if (pos != std::string::npos && message.length() > pos + 2) {
+ pos += 2;
+ } else {
+ pos = 0;
+ }
+
+ const char* str = message.c_str() + pos;
+
+ if (s_log_to_syslog)
+ syslog(severity, "%s", str);
+ if (s_accumulate)
+ s_accumulated.append(str);
+ return !s_log_to_stderr;
+}
+
+namespace syslog_logging {
+void InitLogging(bool log_to_syslog, bool log_to_stderr) {
+ logging::InitLogging("/dev/null",
+ logging::LOG_ONLY_TO_SYSTEM_DEBUG_LOG,
+ logging::DONT_LOCK_LOG_FILE,
+ logging::APPEND_TO_OLD_LOG_FILE);
+ logging::SetLogMessageHandler(HandleMessage);
+ s_log_to_syslog = log_to_syslog;
+ s_log_to_stderr = log_to_stderr;
+}
+void OpenLog(const char* ident, bool log_pid) {
+ s_ident = ident;
+ openlog(s_ident.c_str(), log_pid ? LOG_PID : 0, LOG_USER);
+}
+void AccumulateToString(bool accumulate) {
+ s_accumulate = accumulate;
+}
+std::string GetAccumulatedLogging() {
+ return s_accumulated;
+}
+void ClearAccumulatedLog() {
+ s_accumulated.clear();
+}
+bool Contains(const char* string) {
+ return s_accumulated.find(string) != std::string::npos;
+}
+} // namespace

Powered by Google App Engine
This is Rietveld 408576698