summaryrefslogtreecommitdiffstats
path: root/libcutils/klog.c
diff options
context:
space:
mode:
authorDima Zavin <dima@android.com>2011-08-31 18:26:17 -0700
committerDima Zavin <dima@android.com>2011-09-02 12:11:17 -0700
commit8f91282ebe1963b9d27f8779ad1342302b293bd2 (patch)
tree5589b2fe97aee2a37d1758e5ac285d5747dfe758 /libcutils/klog.c
parent2d55e02d0f3c27f0c99ab889ab7b73126280a21c (diff)
downloadcore-8f91282ebe1963b9d27f8779ad1342302b293bd2.tar.gz
core-8f91282ebe1963b9d27f8779ad1342302b293bd2.tar.bz2
core-8f91282ebe1963b9d27f8779ad1342302b293bd2.zip
init/cutils: move kernel logging interface to libcutils from init
Change-Id: Ia0f91b1fcd6cae69d76bf3dd841340958db938a8 Signed-off-by: Dima Zavin <dima@android.com>
Diffstat (limited to 'libcutils/klog.c')
-rw-r--r--libcutils/klog.c60
1 files changed, 60 insertions, 0 deletions
diff --git a/libcutils/klog.c b/libcutils/klog.c
new file mode 100644
index 000000000..b586a575b
--- /dev/null
+++ b/libcutils/klog.c
@@ -0,0 +1,60 @@
+/*
+ * Copyright (C) 2008 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 <sys/stat.h>
+#include <sys/types.h>
+#include <fcntl.h>
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include <cutils/klog.h>
+
+static int klog_fd = -1;
+static int klog_level = KLOG_DEFAULT_LEVEL;
+
+void klog_set_level(int level) {
+ klog_level = level;
+}
+
+void klog_init(void)
+{
+ static const char *name = "/dev/__kmsg__";
+ if (mknod(name, S_IFCHR | 0600, (1 << 8) | 11) == 0) {
+ klog_fd = open(name, O_WRONLY);
+ fcntl(klog_fd, F_SETFD, FD_CLOEXEC);
+ unlink(name);
+ }
+}
+
+#define LOG_BUF_MAX 512
+
+void klog_write(int level, const char *fmt, ...)
+{
+ char buf[LOG_BUF_MAX];
+ va_list ap;
+
+ if (level > klog_level) return;
+ if (klog_fd < 0) return;
+
+ va_start(ap, fmt);
+ vsnprintf(buf, LOG_BUF_MAX, fmt, ap);
+ buf[LOG_BUF_MAX - 1] = 0;
+ va_end(ap);
+ write(klog_fd, buf, strlen(buf));
+}