Home
tlog.c - ics2txt - convert icalendar .ics file to plain text HTML git clone git://bitreich.org/ics2txt git://enlrupgkhuxnvlhsf6lc3fziv5h2hhfrinws65d7roiv6bfj7d652fid.onion/ics2txt DIR Log DIR Files DIR Refs DIR Tags DIR README --- tlog.c (1353B) --- 1 #include "log.h" 2 3 #include <assert.h> 4 #include <string.h> 5 6 /* 7 * log.c - log to standard error according to the log level 8 * 9 * Instead of logging to syslog, delegate logging to a separate 10 * tool, such as FreeBSD's daemon(8), POSIX's logger(1). 11 */ 12 13 #include <errno.h> 14 #include <stdio.h> 15 #include <stdlib.h> 16 17 #define LOG_DEFAULT 3 /* info */ 18 19 int log_level = -1; 20 char *log_arg0 = NULL; 21 22 void 23 vlogf(int level, char const *flag, char const *fmt, va_list va) 24 { 25 char *env; 26 int e = errno; 27 28 if (log_level < 0) { 29 env = getenv("LOG"); 30 log_level = (env == NULL ? 0 : atoi(env)); 31 log_level = (log_level > 0 ? log_level : LOG_DEFAULT); 32 } 33 34 if (log_level < level) 35 return; 36 37 if (log_arg0 != NULL) 38 fprintf(stderr, "%s: ", log_arg0); 39 40 fprintf(stderr, "%s: ", flag); 41 vfprintf(stderr, fmt, va); 42 43 if (e != 0) 44 fprintf(stderr, ": %s", strerror(e)); 45 46 fprintf(stderr, "\n"); 47 fflush(stderr); 48 } 49 50 void 51 die(char const *fmt, ...) 52 { 53 va_list va; 54 55 va_start(va, fmt); 56 vlogf(1, "error", fmt, va); 57 va_end(va); 58 exit(1); 59 } 60 61 void 62 warn(char const *fmt, ...) 63 { 64 va_list va; 65 66 va_start(va, fmt); 67 vlogf(2, "warn", fmt, va); 68 va_end(va); 69 } 70 71 void 72 info(char const *fmt, ...) 73 { 74 va_list va; 75 76 va_start(va, fmt); 77 vlogf(3, "info", fmt, va); 78 va_end(va); 79 } 80 81 void 82 debug(char const *fmt, ...) 83 { 84 va_list va; 85 86 va_start(va, fmt); 87 vlogf(4, "debug", fmt, va); 88 va_end(va); 89 }