/* * 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 * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ #include #include #include #include #include #include #include static int log_bgnd; static int log_verb; const char *log_name; 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 *, ...) __attribute__((__format__ (printf, 1, 2))); 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) { extern char *__progname; if (__progname != NULL) log_name = __progname; log_bgnd = background; if (log_bgnd) openlog(log_name, LOG_PID | LOG_NDELAY, facility); tzset(); } void log_set(int verb) { log_verb = verb; } int log_get(void) { return (log_verb); } void log_send(const char *info, va_list ap) { 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); } } } void log_info(const char *info, ...) { va_list ap; va_start(ap, info); log_send(info, ap); va_end(ap); } void log_debug(const char *info, ...) { va_list ap; 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'; va_end(ap); if (errno) log_info("%s: %s", strerror(errno), s); else log_info("Unknown error: %s", s); if (bl != NULL) free(bl); exit(1); }