summaryrefslogtreecommitdiffstats
path: root/libc/bionic
diff options
context:
space:
mode:
authorElliott Hughes <enh@google.com>2014-07-21 16:35:24 -0700
committerElliott Hughes <enh@google.com>2014-07-21 18:55:04 -0700
commitb76613627d045acd3bdb7294f424f14c21584872 (patch)
tree04a76c36deaee21a9467989eee5132234edc3888 /libc/bionic
parented68221a8225a6696d2b0b1607ef0b2de1c1b3aa (diff)
downloadbionic-b76613627d045acd3bdb7294f424f14c21584872.zip
bionic-b76613627d045acd3bdb7294f424f14c21584872.tar.gz
bionic-b76613627d045acd3bdb7294f424f14c21584872.tar.bz2
Rewrite syslog(3) to use Android logging.
Since we don't have syslogd on Android and you can't run one on a non-rooted device, it's more useful if syslog output just goes to the regular Android logging system. Bug: 14292866 (cherry picked from commit 3ad8ecb64e9dd5614169232b84a93eb3b8aa32d7) Change-Id: I3038855ca4f22532bf6d2c45d3f8028b866975f9
Diffstat (limited to 'libc/bionic')
-rw-r--r--libc/bionic/libc_logging.cpp3
-rw-r--r--libc/bionic/syslog.cpp75
2 files changed, 78 insertions, 0 deletions
diff --git a/libc/bionic/libc_logging.cpp b/libc/bionic/libc_logging.cpp
index e656a12..88d9790 100644
--- a/libc/bionic/libc_logging.cpp
+++ b/libc/bionic/libc_logging.cpp
@@ -378,6 +378,9 @@ static void out_vformat(Out& o, const char* format, va_list args) {
} else if (c == '%') {
buffer[0] = '%';
buffer[1] = '\0';
+ } else if (c == 'm') {
+ // syslog-like %m for strerror(errno).
+ str = strerror(errno);
} else {
__assert(__FILE__, __LINE__, "conversion specifier unsupported");
}
diff --git a/libc/bionic/syslog.cpp b/libc/bionic/syslog.cpp
new file mode 100644
index 0000000..7e153eb
--- /dev/null
+++ b/libc/bionic/syslog.cpp
@@ -0,0 +1,75 @@
+/*
+ * Copyright (C) 2014 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <syslog.h>
+
+#include <stdlib.h>
+
+#include "private/libc_logging.h"
+
+static const char* syslog_log_tag = NULL;
+static int syslog_priority_mask = 0xff;
+
+void closelog() {
+}
+
+void openlog(const char* log_tag, int /*options*/, int /*facility*/) {
+ syslog_log_tag = log_tag;
+}
+
+int setlogmask(int new_mask) {
+ int old_mask = syslog_priority_mask;
+ // 0 is used to query the current mask.
+ if (new_mask != 0) {
+ syslog_priority_mask = new_mask;
+ }
+ return old_mask;
+}
+
+void syslog(int priority, const char* fmt, ...) {
+ va_list args;
+ va_start(args, fmt);
+ vsyslog(priority, fmt, args);
+ va_end(args);
+}
+
+void vsyslog(int priority, const char* fmt, va_list args) {
+ // Check whether we're supposed to be logging messages of this priority.
+ if ((syslog_priority_mask & LOG_MASK(LOG_PRI(priority))) == 0) {
+ return;
+ }
+
+ // What's our log tag?
+ const char* log_tag = syslog_log_tag;
+ if (log_tag == NULL) {
+ log_tag = getprogname();
+ }
+
+ // What's our Android log priority?
+ priority &= LOG_PRIMASK;
+ int android_log_priority;
+ if (priority < LOG_ERR) {
+ android_log_priority = ANDROID_LOG_ERROR;
+ } else if (priority == LOG_WARNING) {
+ android_log_priority = ANDROID_LOG_WARN;
+ } else if (priority <= LOG_INFO) {
+ android_log_priority = ANDROID_LOG_INFO;
+ } else {
+ android_log_priority = ANDROID_LOG_DEBUG;
+ }
+
+ __libc_format_log_va_list(android_log_priority, log_tag, fmt, args);
+}