udebug: fix file descriptor initialization for __udebug_buf_map
[project/libubox.git] / ulog.c
diff --git a/ulog.c b/ulog.c
index 296605dde876f116217c228ef45a6ed4e77e61f2..6c2c1f83b302cbb5f383e2d805dc9294061ac76f 100644 (file)
--- a/ulog.c
+++ b/ulog.c
@@ -29,18 +29,20 @@ static int _ulog_facility = -1;
 static int _ulog_threshold = LOG_DEBUG;
 static int _ulog_initialized = 0;
 static const char *_ulog_ident = NULL;
+static struct udebug_buf *udb = NULL;
 
 static const char *ulog_default_ident(void)
 {
        FILE *self;
        static char line[64];
        char *p = NULL;
+       char *sbuf;
 
        if ((self = fopen("/proc/self/status", "r")) != NULL) {
                while (fgets(line, sizeof(line), self)) {
                        if (!strncmp(line, "Name:", 5)) {
-                               strtok(line, "\t\n");
-                               p = strtok(NULL, "\t\n");
+                               strtok_r(line, "\t\n", &sbuf);
+                               p = strtok_r(NULL, "\t\n", &sbuf);
                                break;
                        }
                }
@@ -86,6 +88,7 @@ static void ulog_defaults(void)
        _ulog_initialized = 1;
 }
 
+__attribute__((format(printf, 2, 0)))
 static void ulog_kmsg(int priority, const char *fmt, va_list ap)
 {
        FILE *kmsg;
@@ -101,6 +104,7 @@ static void ulog_kmsg(int priority, const char *fmt, va_list ap)
        }
 }
 
+__attribute__((format(printf, 2, 0)))
 static void ulog_stdio(int priority, const char *fmt, va_list ap)
 {
        FILE *out = stderr;
@@ -111,11 +115,17 @@ static void ulog_stdio(int priority, const char *fmt, va_list ap)
        vfprintf(out, fmt, ap);
 }
 
+__attribute__((format(printf, 2, 0)))
 static void ulog_syslog(int priority, const char *fmt, va_list ap)
 {
        vsyslog(priority, fmt, ap);
 }
 
+void ulog_udebug(struct udebug_buf *_udb)
+{
+       udb = _udb;
+}
+
 void ulog_open(int channels, int facility, const char *ident)
 {
        ulog_close();
@@ -145,6 +155,14 @@ void ulog(int priority, const char *fmt, ...)
 {
        va_list ap;
 
+       if (udb) {
+               va_start(ap, fmt);
+               udebug_entry_init(udb);
+               udebug_entry_vprintf(udb, fmt, ap);
+               udebug_entry_add(udb);
+               va_end(ap);
+       }
+
        if (priority > _ulog_threshold)
                return;