From 325d23c5dad92c22720c6d1027c5761aaf12319c Mon Sep 17 00:00:00 2001 From: Jo-Philipp Wich Date: Wed, 18 Feb 2015 19:25:36 +0100 Subject: [PATCH] block: add support for logging to dmesg Signed-off-by: Jo-Philipp Wich --- block.c | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/block.c b/block.c index 2726b1c..6eb1933 100644 --- a/block.c +++ b/block.c @@ -20,6 +20,7 @@ #include #include #include +#include #include #include @@ -41,9 +42,35 @@ #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); -- 2.25.1