2009-12-19 22:37:15 +01:00
|
|
|
/*
|
2010-11-29 22:28:23 +01:00
|
|
|
* vim:ts=4:sw=4:expandtab
|
2009-12-19 22:37:15 +01:00
|
|
|
*
|
|
|
|
* i3 - an improved dynamic tiling window manager
|
|
|
|
*
|
2010-11-29 22:28:23 +01:00
|
|
|
* © 2009-2010 Michael Stapelberg and contributors
|
2009-12-19 22:37:15 +01:00
|
|
|
*
|
|
|
|
* See file LICENSE for license information.
|
|
|
|
*
|
|
|
|
* src/log.c: handles the setting of loglevels, contains the logging functions.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdbool.h>
|
2011-07-10 14:33:19 +02:00
|
|
|
#include <stdlib.h>
|
2011-07-10 23:11:21 +02:00
|
|
|
#include <sys/time.h>
|
2011-10-01 02:29:30 +02:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
2009-12-19 22:37:15 +01:00
|
|
|
|
|
|
|
#include "util.h"
|
|
|
|
#include "log.h"
|
|
|
|
|
|
|
|
/* loglevels.h is autogenerated at make time */
|
|
|
|
#include "loglevels.h"
|
|
|
|
|
2010-04-17 17:46:11 +02:00
|
|
|
static uint64_t loglevel = 0;
|
2011-08-17 12:16:32 +02:00
|
|
|
static bool verbose = false;
|
2011-07-10 14:33:19 +02:00
|
|
|
static FILE *errorfile;
|
|
|
|
char *errorfilename;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Initializes logging by creating an error logfile in /tmp (or
|
|
|
|
* XDG_RUNTIME_DIR, see get_process_filename()).
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
void init_logging() {
|
|
|
|
errorfilename = get_process_filename("errorlog");
|
|
|
|
if (errorfilename == NULL) {
|
|
|
|
ELOG("Could not initialize errorlog\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
errorfile = fopen(errorfilename, "w");
|
2011-10-01 02:29:30 +02:00
|
|
|
if (fcntl(fileno(errorfile), F_SETFD, FD_CLOEXEC)) {
|
|
|
|
ELOG("Could not set close-on-exec flag\n");
|
|
|
|
}
|
2011-07-10 14:33:19 +02:00
|
|
|
}
|
2009-12-19 22:37:15 +01:00
|
|
|
|
2010-11-29 22:28:23 +01:00
|
|
|
/*
|
2009-12-19 22:37:15 +01:00
|
|
|
* Set verbosity of i3. If verbose is set to true, informative messages will
|
|
|
|
* be printed to stdout. If verbose is set to false, only errors will be
|
|
|
|
* printed.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
void set_verbosity(bool _verbose) {
|
2010-11-29 22:28:23 +01:00
|
|
|
verbose = _verbose;
|
2009-12-19 22:37:15 +01:00
|
|
|
}
|
|
|
|
|
2010-11-29 22:28:23 +01:00
|
|
|
/*
|
2009-12-19 22:37:15 +01:00
|
|
|
* Enables the given loglevel.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
void add_loglevel(const char *level) {
|
2010-11-29 22:28:23 +01:00
|
|
|
/* Handle the special loglevel "all" */
|
|
|
|
if (strcasecmp(level, "all") == 0) {
|
|
|
|
loglevel = UINT64_MAX;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < sizeof(loglevels) / sizeof(char*); i++) {
|
|
|
|
if (strcasecmp(loglevels[i], level) != 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* The position in the array (plus one) is the amount of times
|
|
|
|
* which we need to shift 1 to the left to get our bitmask for
|
|
|
|
* the specific loglevel. */
|
|
|
|
loglevel |= (1 << (i+1));
|
|
|
|
break;
|
|
|
|
}
|
2009-12-19 22:37:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Logs the given message to stdout while prefixing the current time to it.
|
|
|
|
* This is to be called by *LOG() which includes filename/linenumber/function.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
void vlog(char *fmt, va_list args) {
|
2011-10-18 19:32:47 +02:00
|
|
|
static char timebuf[64];
|
|
|
|
static struct tm result;
|
2010-11-29 22:28:23 +01:00
|
|
|
|
|
|
|
/* Get current time */
|
|
|
|
time_t t = time(NULL);
|
|
|
|
/* Convert time to local time (determined by the locale) */
|
2011-10-18 19:32:47 +02:00
|
|
|
struct tm *tmp = localtime_r(&t, &result);
|
2010-11-29 22:28:23 +01:00
|
|
|
/* Generate time prefix */
|
|
|
|
strftime(timebuf, sizeof(timebuf), "%x %X - ", tmp);
|
2011-07-10 23:11:21 +02:00
|
|
|
#ifdef DEBUG_TIMING
|
|
|
|
struct timeval tv;
|
|
|
|
gettimeofday(&tv, NULL);
|
|
|
|
printf("%s%d.%d - ", timebuf, tv.tv_sec, tv.tv_usec);
|
|
|
|
#else
|
2010-11-29 22:28:23 +01:00
|
|
|
printf("%s", timebuf);
|
2011-07-10 23:11:21 +02:00
|
|
|
#endif
|
2010-11-29 22:28:23 +01:00
|
|
|
vprintf(fmt, args);
|
2009-12-19 22:37:15 +01:00
|
|
|
}
|
|
|
|
|
2010-11-29 22:28:23 +01:00
|
|
|
/*
|
2009-12-19 22:37:15 +01:00
|
|
|
* Logs the given message to stdout while prefixing the current time to it,
|
|
|
|
* but only if verbose mode is activated.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
void verboselog(char *fmt, ...) {
|
2010-11-29 22:28:23 +01:00
|
|
|
va_list args;
|
2009-12-19 22:37:15 +01:00
|
|
|
|
2010-11-29 22:28:23 +01:00
|
|
|
if (!verbose)
|
|
|
|
return;
|
2009-12-19 22:37:15 +01:00
|
|
|
|
2010-11-29 22:28:23 +01:00
|
|
|
va_start(args, fmt);
|
|
|
|
vlog(fmt, args);
|
|
|
|
va_end(args);
|
2009-12-19 22:37:15 +01:00
|
|
|
}
|
|
|
|
|
2010-11-29 22:28:23 +01:00
|
|
|
/*
|
2009-12-19 22:37:15 +01:00
|
|
|
* Logs the given message to stdout while prefixing the current time to it.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
void errorlog(char *fmt, ...) {
|
2010-11-29 22:28:23 +01:00
|
|
|
va_list args;
|
2009-12-19 22:37:15 +01:00
|
|
|
|
2010-11-29 22:28:23 +01:00
|
|
|
va_start(args, fmt);
|
|
|
|
vlog(fmt, args);
|
|
|
|
va_end(args);
|
2011-07-10 14:33:19 +02:00
|
|
|
|
|
|
|
/* also log to the error logfile, if opened */
|
|
|
|
va_start(args, fmt);
|
|
|
|
vfprintf(errorfile, fmt, args);
|
|
|
|
fflush(errorfile);
|
|
|
|
va_end(args);
|
2009-12-19 22:37:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Logs the given message to stdout while prefixing the current time to it,
|
|
|
|
* but only if the corresponding debug loglevel was activated.
|
|
|
|
* This is to be called by DLOG() which includes filename/linenumber
|
|
|
|
*
|
|
|
|
*/
|
2010-04-17 17:46:11 +02:00
|
|
|
void debuglog(uint64_t lev, char *fmt, ...) {
|
2010-11-29 22:28:23 +01:00
|
|
|
va_list args;
|
2009-12-19 22:37:15 +01:00
|
|
|
|
2010-11-29 22:28:23 +01:00
|
|
|
if ((loglevel & lev) == 0)
|
|
|
|
return;
|
2009-12-19 22:37:15 +01:00
|
|
|
|
2010-11-29 22:28:23 +01:00
|
|
|
va_start(args, fmt);
|
|
|
|
vlog(fmt, args);
|
|
|
|
va_end(args);
|
2009-12-19 22:37:15 +01:00
|
|
|
}
|