add files
[idlebox.git] / log.c
blobe002cf40b98939a2570cb5916ca875f386ebc00b
1 #include <sys/types.h>
2 #include <sys/stat.h>
3 #include <fcntl.h>
4 #include <unistd.h>
5 #include <stdarg.h>
6 #include <stdio.h>
7 #include <string.h>
9 #include "log.h"
11 int log_fd = -1;
12 int log_level = 6;
14 void log_init(void)
16 static const char *name = "/dev/__kmsg__";
17 if (mknod(name, S_IFCHR | 0600, (1 << 8) | 11) == 0) {
18 log_fd = open(name, O_WRONLY);
19 fcntl(log_fd, F_SETFD, FD_CLOEXEC);
20 unlink(name);
24 #define LOG_BUF_MAX 512
25 void log_write(int level, const char *fmt, ...)
27 char buf[LOG_BUF_MAX];
28 va_list ap;
30 if (level > log_level) return;
31 if (log_fd < 0) return;
33 va_start(ap, fmt);
34 vsnprintf(buf, LOG_BUF_MAX, fmt, ap);
35 buf[LOG_BUF_MAX - 1] = 0;
36 va_end(ap);
37 write(log_fd, buf, strlen(buf));