summaryrefslogtreecommitdiffstats
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
parent2d55e02d0f3c27f0c99ab889ab7b73126280a21c (diff)
downloadsystem_core-8f91282ebe1963b9d27f8779ad1342302b293bd2.zip
system_core-8f91282ebe1963b9d27f8779ad1342302b293bd2.tar.gz
system_core-8f91282ebe1963b9d27f8779ad1342302b293bd2.tar.bz2
init/cutils: move kernel logging interface to libcutils from init
Change-Id: Ia0f91b1fcd6cae69d76bf3dd841340958db938a8 Signed-off-by: Dima Zavin <dima@android.com>
-rw-r--r--include/cutils/klog.h34
-rw-r--r--init/builtins.c2
-rwxr-xr-xinit/init.c2
-rw-r--r--init/log.h13
-rw-r--r--init/ueventd.c2
-rwxr-xr-xinit/util.c36
-rw-r--r--libcutils/Android.mk1
-rw-r--r--libcutils/klog.c60
8 files changed, 102 insertions, 48 deletions
diff --git a/include/cutils/klog.h b/include/cutils/klog.h
new file mode 100644
index 0000000..1335543
--- /dev/null
+++ b/include/cutils/klog.h
@@ -0,0 +1,34 @@
+/*
+ * Copyright (C) 2010 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.
+ */
+
+#ifndef _CUTILS_KLOG_H_
+#define _CUTILS_KLOG_H_
+
+void klog_init(void);
+void klog_set_level(int level);
+void klog_close(void);
+void klog_write(int level, const char *fmt, ...)
+ __attribute__ ((format(printf, 2, 3)));
+
+#define KLOG_ERROR(tag,x...) klog_write(3, "<3>" tag ": " x)
+#define KLOG_WARNING(tag,x...) klog_write(4, "<4>" tag ": " x)
+#define KLOG_NOTICE(tag,x...) klog_write(5, "<5>" tag ": " x)
+#define KLOG_INFO(tag,x...) klog_write(6, "<6>" tag ": " x)
+#define KLOG_DEBUG(tag,x...) klog_write(7, "<7>" tag ": " x)
+
+#define KLOG_DEFAULT_LEVEL 3 /* messages <= this level are logged */
+
+#endif
diff --git a/init/builtins.c b/init/builtins.c
index 06ef96d..eccda3f 100644
--- a/init/builtins.c
+++ b/init/builtins.c
@@ -663,7 +663,7 @@ int do_chmod(int nargs, char **args) {
int do_loglevel(int nargs, char **args) {
if (nargs == 2) {
- log_set_level(atoi(args[1]));
+ klog_set_level(atoi(args[1]));
return 0;
}
return -1;
diff --git a/init/init.c b/init/init.c
index ef42e02..53b8aa5 100755
--- a/init/init.c
+++ b/init/init.c
@@ -724,7 +724,7 @@ int main(int argc, char **argv)
* talk to the outside world.
*/
open_devnull_stdio();
- log_init();
+ klog_init();
INFO("reading config file\n");
init_parse_config_file("/init.rc");
diff --git a/init/log.h b/init/log.h
index 3d93965..4aac3df 100644
--- a/init/log.h
+++ b/init/log.h
@@ -17,17 +17,12 @@
#ifndef _INIT_LOG_H_
#define _INIT_LOG_H_
-void log_init(void);
-void log_set_level(int level);
-void log_close(void);
-void log_write(int level, const char *fmt, ...)
- __attribute__ ((format(printf, 2, 3)));
+#include <cutils/klog.h>
-#define ERROR(x...) log_write(3, "<3>init: " x)
-#define NOTICE(x...) log_write(5, "<5>init: " x)
-#define INFO(x...) log_write(6, "<6>init: " x)
+#define ERROR(x...) KLOG_ERROR("init", x)
+#define NOTICE(x...) KLOG_NOTICE("init", x)
+#define INFO(x...) KLOG_INFO("init", x)
-#define LOG_DEFAULT_LEVEL 3 /* messages <= this level are logged */
#define LOG_UEVENTS 0 /* log uevent messages if 1. verbose */
#endif
diff --git a/init/ueventd.c b/init/ueventd.c
index 1328d19..ddf42be 100644
--- a/init/ueventd.c
+++ b/init/ueventd.c
@@ -47,7 +47,7 @@ int ueventd_main(int argc, char **argv)
signal(SIGCHLD, SIG_IGN);
open_devnull_stdio();
- log_init();
+ klog_init();
INFO("starting ueventd\n");
diff --git a/init/util.c b/init/util.c
index d8ec88e..bc24b08 100755
--- a/init/util.c
+++ b/init/util.c
@@ -37,42 +37,6 @@
#include "list.h"
#include "util.h"
-static int log_fd = -1;
-/* Inital log level before init.rc is parsed and this this is reset. */
-static int log_level = LOG_DEFAULT_LEVEL;
-
-
-void log_set_level(int level) {
- log_level = level;
-}
-
-void log_init(void)
-{
- static const char *name = "/dev/__kmsg__";
- if (mknod(name, S_IFCHR | 0600, (1 << 8) | 11) == 0) {
- log_fd = open(name, O_WRONLY);
- fcntl(log_fd, F_SETFD, FD_CLOEXEC);
- unlink(name);
- }
-}
-
-#define LOG_BUF_MAX 512
-
-void log_write(int level, const char *fmt, ...)
-{
- char buf[LOG_BUF_MAX];
- va_list ap;
-
- if (level > log_level) return;
- if (log_fd < 0) return;
-
- va_start(ap, fmt);
- vsnprintf(buf, LOG_BUF_MAX, fmt, ap);
- buf[LOG_BUF_MAX - 1] = 0;
- va_end(ap);
- write(log_fd, buf, strlen(buf));
-}
-
/*
* android_name_to_id - returns the integer uid/gid associated with the given
* name, or -1U on error.
diff --git a/libcutils/Android.mk b/libcutils/Android.mk
index 0c4e235..e183521 100644
--- a/libcutils/Android.mk
+++ b/libcutils/Android.mk
@@ -39,6 +39,7 @@ commonSources := \
config_utils.c \
cpu_info.c \
load_file.c \
+ klog.c \
open_memstream.c \
strdup16to8.c \
strdup8to16.c \
diff --git a/libcutils/klog.c b/libcutils/klog.c
new file mode 100644
index 0000000..b586a57
--- /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));
+}