block: add support for logging to dmesg
authorJo-Philipp Wich <jow@openwrt.org>
Wed, 18 Feb 2015 18:25:36 +0000 (19:25 +0100)
committerJo-Philipp Wich <jow@openwrt.org>
Wed, 18 Feb 2015 20:24:07 +0000 (21:24 +0100)
Signed-off-by: Jo-Philipp Wich <jow@openwrt.org>
block.c

diff --git a/block.c b/block.c
index 2726b1ceb7fbddcc85df9cce256b96d08823a20c..6eb19339f9a3223f3c70108d5727f8e3bfb2ce12 100644 (file)
--- a/block.c
+++ b/block.c
@@ -20,6 +20,7 @@
 #include <libgen.h>
 #include <glob.h>
 #include <dirent.h>
+#include <stdarg.h>
 
 #include <sys/stat.h>
 #include <sys/types.h>
 #include "libubi/libubi.h"
 #endif
 
+static int kmsg = 0;
+
+static void klog(int prio, const char *fmt, ...)
+{
+       va_list ap;
+       FILE *f = fopen("/dev/kmsg", "w");
+
+       if (f) {
+               fprintf(f, "<%d>", prio);
+
+               va_start(ap, fmt);
+               vfprintf(f, fmt, ap);
+               va_end(ap);
+
+               fclose(f);
+       }
+}
+
+#define KINFO(fmt, ...) do { \
+               if (kmsg) klog(LOG_INFO, "block: " fmt, ## __VA_ARGS__); \
+       } while (0)
+
 #define ERROR(fmt, ...) do { \
-               syslog(LOG_ERR, fmt, ## __VA_ARGS__); \
-               fprintf(stderr, "block: "fmt, ## __VA_ARGS__); \
+               if (kmsg) \
+                       klog(LOG_ERR, "block: " fmt, ## __VA_ARGS__); \
+               else { \
+                       syslog(LOG_ERR, fmt, ## __VA_ARGS__); \
+                       fprintf(stderr, "block: "fmt, ## __VA_ARGS__); \
+               } \
        } while (0)
 
 enum {
@@ -1062,6 +1089,8 @@ static int main_extroot(int argc, char **argv)
                return -1;
        }
 
+       kmsg = 1;
+
        mkblkdev();
        cache_load(1);