X-Git-Url: http://git.archive.openwrt.org/?p=project%2Fprocd.git;a=blobdiff_plain;f=syslog.c;h=7a2839e813ba4b73797768a69f535d48f763bcb7;hp=733bef6a95ea5be0290a1d42ed1a3b524976a80b;hb=d5ee716c3519c024afdd0dd16362c99665d8bc60;hpb=4fad44987a3869accf103783b4b8768c282eb86a diff --git a/syslog.c b/syslog.c index 733bef6..7a2839e 100644 --- a/syslog.c +++ b/syslog.c @@ -46,8 +46,8 @@ static char *log_dev = LOG_DEFAULT_SOCKET; static int log_size = LOG_DEFAULT_SIZE; static struct log_head *log, *log_end, *oldest, *newest; static int current_id = 0; -regex_t pat_prio; -regex_t pat_tstamp; +static regex_t pat_prio; +static regex_t pat_tstamp; static struct log_head *log_next(struct log_head *h, int size) { @@ -63,6 +63,12 @@ void log_add(char *buf, int size, int source) int priority = 0; int ret; + /* bounce out if we don't have init'ed yet (regmatch etc will blow) */ + if (!log) { + fprintf(stderr, buf); + return; + } + /* strip trailing newline */ if (buf[size - 2] == '\n') { buf[size - 2] = '\0'; @@ -121,6 +127,34 @@ void log_add(char *buf, int size, int source) newest = next; } +void log_printf(char *fmt, ...) +{ + static int buffer_len = 128; + static char *buffer; + va_list ap; + int n = 0; + + do { + if (n) + buffer_len = n + 1; + if (!buffer) + buffer = malloc(buffer_len); + if (!buffer) + return; + va_start(ap, fmt); + n = vsnprintf(buffer, buffer_len, fmt, ap); + va_end(ap); + if (n < 1) + return; + if (n >= buffer_len) { + free(buffer); + buffer = NULL; + } + } while (n >= buffer_len); + + log_add(buffer, n, SOURCE_INTERNAL); +} + static void slog_cb(struct ustream *s, int bytes) { struct ustream_buf *buf = s->r.head; @@ -262,11 +296,6 @@ int log_buffer_init(int size) return 0; } -int log_buffer_size(void) -{ - return log_size; -} - void log_init(void) { regcomp(&pat_prio, "^<([0-9]*)>(.*)", REG_EXTENDED); @@ -279,5 +308,13 @@ void log_init(void) syslog_open(); klog_open(); - openlog("procd", LOG_PID, LOG_DAEMON); + openlog("sysinit", LOG_CONS, LOG_DAEMON); +} + +void log_shutdown(void) +{ + ustream_free(&slog.stream); + ustream_free(&klog.stream); + close(slog.fd.fd); + close(klog.fd.fd); }