From 7425fd1b231fcdb3c260877a13f794a0c7361e80 Mon Sep 17 00:00:00 2001 From: Ken Sumrall Date: Wed, 3 Apr 2013 13:43:11 -0700 Subject: [PATCH] klog: Have klog_write() call klog_init() if needed Also change klog_init() to do nothing if already initialized. Change-Id: Ia2dfe914c9d9fd119fb8939508d57b15c9884663 --- include/cutils/klog.h | 6 ++++++ libcutils/klog.c | 5 ++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/include/cutils/klog.h b/include/cutils/klog.h index 133554304..ba728ace1 100644 --- a/include/cutils/klog.h +++ b/include/cutils/klog.h @@ -17,12 +17,18 @@ #ifndef _CUTILS_KLOG_H_ #define _CUTILS_KLOG_H_ +#include + +__BEGIN_DECLS + 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))); +__END_DECLS + #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) diff --git a/libcutils/klog.c b/libcutils/klog.c index b586a575b..812af3bbf 100644 --- a/libcutils/klog.c +++ b/libcutils/klog.c @@ -35,6 +35,9 @@ void klog_set_level(int level) { void klog_init(void) { static const char *name = "/dev/__kmsg__"; + + if (klog_fd >= 0) return; /* Already initialized */ + if (mknod(name, S_IFCHR | 0600, (1 << 8) | 11) == 0) { klog_fd = open(name, O_WRONLY); fcntl(klog_fd, F_SETFD, FD_CLOEXEC); @@ -50,7 +53,7 @@ void klog_write(int level, const char *fmt, ...) va_list ap; if (level > klog_level) return; - if (klog_fd < 0) return; + if (klog_fd < 0) klog_init(); va_start(ap, fmt); vsnprintf(buf, LOG_BUF_MAX, fmt, ap);