=================================================================== RCS file: /cvs/cvs/blind/log.c,v retrieving revision 1.5 retrieving revision 1.11 diff -u -p -r1.5 -r1.11 --- blind/log.c 2022/03/18 19:55:19 1.5 +++ blind/log.c 2022/04/03 13:52:00 1.11 @@ -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,21 +14,21 @@ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ +#include +#include #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_initialize(int, int); -void log_setverbose(int); -int log_getverbose(void); +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 *, ...) @@ -39,41 +39,49 @@ __dead void log_fatal(const char *, ...) __attribute__((__format__ (printf, 1, 2))); void -log_initialize(int background, int facility) +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_setverbose(int verb) +log_set(int verb) { - log_verbose = verb; + log_verb = verb; } int -log_getverbose(void) +log_get(void) { - return (log_verbose); + return (log_verb); } void log_send(const char *info, va_list ap) { + char *ninf; + if (info != NULL) { - if (log_background) + if (log_bgnd) vsyslog(LOG_INFO, info, ap); else { - vfprintf(stderr, info, ap); - fprintf(stderr, "\n"); + if (asprintf(&ninf, "%s\n", info) == -1) { + vfprintf(stderr, info, ap); + fprintf(stderr, "\n"); + } else { + vfprintf(stderr, ninf, ap); + free(ninf); + } + fflush(stderr); } } } @@ -93,7 +101,7 @@ 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); @@ -103,12 +111,24 @@ log_debug(const char *info, ...) void log_fatal(const char *info, ...) { + extern struct blind *bl; + static char s[BUFSIZ]; va_list ap; va_start(ap, info); - log_send(info, ap); + if (info != NULL) + (void)vsnprintf(s, sizeof(s), info, ap); + else + s[0] = '\0'; + va_end(ap); - log_info("%s", strerror(errno)); + if (errno) + log_info("%s: %s", strerror(errno), s); + else + log_info("Unknown error: %s", s); + + if (bl != NULL) + free(bl); + exit(1); } -