=================================================================== RCS file: /cvs/cvs/blind/log.c,v retrieving revision 1.4 retrieving revision 1.10 diff -u -p -r1.4 -r1.10 --- blind/log.c 2022/03/18 09:20:46 1.4 +++ blind/log.c 2022/04/03 11:01:52 1.10 @@ -1,5 +1,5 @@ /* -1;95;0c * Copyright (c) 2022 Daniel Kroczynski + * Copyright (c) 2022 Daniel Kroczynski * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -14,24 +14,29 @@ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ -#include +#include #include +#include +#include +#include #include #include -static int log_background; -static int log_verbose; -const char *log_procname; +static int log_bgnd; +static int log_verb; +const char *log_name; -void log_init(int, int); -void log_setv(int); -int log_getv(void); -void log_send(const char *, va_list) +void log_init(int, int); +void log_set(int); +int log_get(void); +void log_send(const char *, va_list) __attribute__((__format__ (printf, 1, 0))); -void log_info(const char *, ...) +void log_info(const char *, ...) __attribute__((__format__ (printf, 1, 2))); -void log_debug(const char *, ...) +void log_debug(const char *, ...) __attribute__((__format__ (printf, 1, 2))); +__dead void log_fatal(const char *, ...) + __attribute__((__format__ (printf, 1, 2))); void log_init(int background, int facility) @@ -39,35 +44,45 @@ log_init(int background, int facility) extern char *__progname; if (__progname != NULL) - log_procname = __progname; + log_name = __progname; - log_background = background; - if (log_background) - openlog(log_procname, LOG_PID | LOG_NDELAY, facility); + log_bgnd = background; + if (log_bgnd) + openlog(log_name, LOG_PID | LOG_NDELAY, facility); tzset(); } void -log_setv(int verb) +log_set(int verb) { - log_verbose = verb; + log_verb = verb; } int -log_getv(void) +log_get(void) { - return (log_verbose); + return (log_verb); } void log_send(const char *info, va_list ap) { - if (log_background) - vsyslog(LOG_INFO, info, ap); - else { - vfprintf(stderr, info, ap); - fprintf(stderr, "\n"); + char *ninf; + + if (info != NULL) { + if (log_bgnd) + vsyslog(LOG_INFO, info, ap); + else { + if (asprintf(&ninf, "%s\n", info) == -1) { + vfprintf(stderr, info, ap); + fprintf(stderr, "\n"); + } else { + vfprintf(stderr, ninf, ap); + free(ninf); + } + fflush(stderr); + } } } @@ -86,9 +101,31 @@ log_debug(const char *info, ...) { va_list ap; - if (log_verbose) { + if (log_verb) { va_start(ap, info); log_send(info, ap); va_end(ap); } +} + +void +log_fatal(const char *info, ...) +{ + extern struct blind *bl; + static char s[BUFSIZ]; + va_list ap; + + va_start(ap, info); + if (info != NULL) + (void)vsnprintf(s, sizeof(s), info, ap); + else + s[0] = '\0'; + if (errno) + log_info("%s: %s", strerror(errno), s); + else + log_info("Unknown error: %s", s); + va_end(ap); + if (bl != NULL) + free(bl); + exit(1); }