Home
tlog.c - iomenu - interactive terminal-based selection menu HTML git clone git://bitreich.org/iomenu git://hg6vgqziawt5s4dj.onion/iomenu DIR Log DIR Files DIR Refs DIR Tags DIR README DIR LICENSE --- tlog.c (1217B) --- 1 #include "log.h" 2 3 #include <assert.h> 4 #include <errno.h> 5 #include <stdio.h> 6 #include <stdlib.h> 7 #include <string.h> 8 9 #ifndef LOG_DEFAULT 10 #define LOG_DEFAULT 3 /* info */ 11 #endif 12 13 char *arg0 = NULL; 14 static int log_level = -1; 15 16 void 17 log_vprintf(int level, char const *flag, char const *fmt, va_list va) 18 { 19 char *env; 20 int old_errno = errno; 21 22 if (log_level < 0) { 23 env = getenv("LOG"); 24 log_level = (env == NULL) ? 0 : atoi(env); 25 if (log_level == 0) 26 log_level = LOG_DEFAULT; 27 } 28 29 if (log_level < level) 30 return; 31 32 if (arg0 != NULL) 33 fprintf(stderr, "%s: ", arg0); 34 35 fprintf(stderr, "%s: ", flag); 36 vfprintf(stderr, fmt, va); 37 38 if (old_errno != 0) 39 fprintf(stderr, ": %s", strerror(old_errno)); 40 41 fprintf(stderr, "\n"); 42 fflush(stderr); 43 } 44 45 void 46 die(char const *fmt, ...) 47 { 48 va_list va; 49 va_start(va, fmt); log_vprintf(1, "error", fmt, va); va_end(va); 50 exit(1); 51 } 52 53 void 54 warn(char const *fmt, ...) 55 { 56 va_list va; 57 va_start(va, fmt); log_vprintf(2, "warn", fmt, va); va_end(va); 58 } 59 60 void 61 info(char const *fmt, ...) 62 { 63 va_list va; 64 va_start(va, fmt); log_vprintf(3, "info", fmt, va); va_end(va); 65 } 66 67 void 68 debug(char const *fmt, ...) 69 { 70 va_list va; 71 va_start(va, fmt); log_vprintf(4, "debug", fmt, va); va_end(va); 72 }