i3
|
00001 /* 00002 * vim:ts=8:expandtab 00003 * 00004 * i3 - an improved dynamic tiling window manager 00005 * 00006 * © 2009 Michael Stapelberg and contributors 00007 * 00008 * See file LICENSE for license information. 00009 * 00010 * src/log.c: handles the setting of loglevels, contains the logging functions. 00011 * 00012 */ 00013 #include <stdarg.h> 00014 #include <stdio.h> 00015 #include <string.h> 00016 #include <stdbool.h> 00017 00018 #include "util.h" 00019 #include "log.h" 00020 00021 /* loglevels.h is autogenerated at make time */ 00022 #include "loglevels.h" 00023 00024 static uint32_t loglevel = 0; 00025 static bool verbose = false; 00026 00033 void set_verbosity(bool _verbose) { 00034 verbose = _verbose; 00035 } 00036 00041 void add_loglevel(const char *level) { 00042 /* Handle the special loglevel "all" */ 00043 if (strcasecmp(level, "all") == 0) { 00044 loglevel = UINT32_MAX; 00045 return; 00046 } 00047 00048 for (int i = 0; i < sizeof(loglevels) / sizeof(char*); i++) { 00049 if (strcasecmp(loglevels[i], level) != 0) 00050 continue; 00051 00052 /* The position in the array (plus one) is the amount of times 00053 * which we need to shift 1 to the left to get our bitmask for 00054 * the specific loglevel. */ 00055 loglevel |= (1 << (i+1)); 00056 break; 00057 } 00058 } 00059 00060 /* 00061 * Logs the given message to stdout while prefixing the current time to it. 00062 * This is to be called by *LOG() which includes filename/linenumber/function. 00063 * 00064 */ 00065 void vlog(char *fmt, va_list args) { 00066 char timebuf[64]; 00067 00068 /* Get current time */ 00069 time_t t = time(NULL); 00070 /* Convert time to local time (determined by the locale) */ 00071 struct tm *tmp = localtime(&t); 00072 /* Generate time prefix */ 00073 strftime(timebuf, sizeof(timebuf), "%x %X - ", tmp); 00074 printf("%s", timebuf); 00075 vprintf(fmt, args); 00076 } 00077 00083 void verboselog(char *fmt, ...) { 00084 va_list args; 00085 00086 if (!verbose) 00087 return; 00088 00089 va_start(args, fmt); 00090 vlog(fmt, args); 00091 va_end(args); 00092 } 00093 00098 void errorlog(char *fmt, ...) { 00099 va_list args; 00100 00101 va_start(args, fmt); 00102 vlog(fmt, args); 00103 va_end(args); 00104 } 00105 00106 /* 00107 * Logs the given message to stdout while prefixing the current time to it, 00108 * but only if the corresponding debug loglevel was activated. 00109 * This is to be called by DLOG() which includes filename/linenumber 00110 * 00111 */ 00112 void debuglog(int lev, char *fmt, ...) { 00113 va_list args; 00114 00115 if ((loglevel & lev) == 0) 00116 return; 00117 00118 va_start(args, fmt); 00119 vlog(fmt, args); 00120 va_end(args); 00121 }