Add logging system with warnl, warnxl, errl, errxl

This commit is contained in:
David Delassus 2012-01-21 18:27:36 +01:00
parent ebc83c904b
commit e31ebb9702
10 changed files with 164 additions and 25 deletions

View File

@ -19,6 +19,7 @@ SRCS= \
src/fifo.c \ src/fifo.c \
src/status.c \ src/status.c \
src/mouse.c \ src/mouse.c \
src/log.c \
src/wmfs.c src/wmfs.c

View File

@ -278,7 +278,7 @@ config_keybind(void)
/* func = */ /* func = */
if(!(k->func = uicb_name_func(fetch_opt_first(ks[i], "", "func").str))) if(!(k->func = uicb_name_func(fetch_opt_first(ks[i], "", "func").str)))
{ {
warnx("configuration: Unknown Function \"%s\".", warnxl("configuration: Unknown Function \"%s\".",
fetch_opt_first(ks[i], "", "func").str); fetch_opt_first(ks[i], "", "func").str);
k->func = uicb_spawn; k->func = uicb_spawn;
} }
@ -300,11 +300,11 @@ config_init(void)
{ {
if(get_conf(W->confpath) == -1) if(get_conf(W->confpath) == -1)
{ {
warn("parsing configuration file (%s) failed.", W->confpath); warnl("parsing configuration file (%s) failed.", W->confpath);
sprintf(W->confpath, "%s/"CONFIG_DEFAULT_PATH, getenv("HOME")); sprintf(W->confpath, "%s/"CONFIG_DEFAULT_PATH, getenv("HOME"));
if(get_conf(W->confpath) == -1) if(get_conf(W->confpath) == -1)
errx(1, "parsing default configuration file (%s) failed.", W->confpath); errxl(1, "parsing default configuration file (%s) failed.", W->confpath);
} }
config_theme(); config_theme();

View File

@ -18,7 +18,7 @@ fifo_open(void)
close(W->fifo.fd); close(W->fifo.fd);
if(!(W->fifo.fd = open(W->fifo.path, O_RDONLY | O_NDELAY, 0))) if(!(W->fifo.fd = open(W->fifo.path, O_RDONLY | O_NDELAY, 0)))
warnx("Can't open FIFO: %s\n", strerror(errno)); warnxl("Can't open FIFO: %s\n", strerror(errno));
} }
void void
@ -31,7 +31,7 @@ fifo_init(void)
unlink(W->fifo.path); unlink(W->fifo.path);
if(mkfifo(W->fifo.path, 0644) < 0) if(mkfifo(W->fifo.path, 0644) < 0)
warnx("Can't create FIFO: %s\n", strerror(errno)); warnxl("Can't create FIFO: %s\n", strerror(errno));
fifo_open(); fifo_open();
} }

110
src/log.c Normal file
View File

@ -0,0 +1,110 @@
/*
* wmfs2 by Martin Duquesnoy <xorg62@gmail.com> { for(i = 2011; i < 2111; ++i) ©(i); }
* File created by David Delassus.
* For license, see COPYING.
*/
#include "wmfs.h"
#include "util.h"
void
log_init(void)
{
char *path = NULL;
xasprintf(&path, "%s/wmfs-%s.log", P_tmpdir, DisplayString(W->dpy));
if(path)
{
/* open log */
if(!(W->log = fopen(path, "a")))
warnx("Can't open log file '%s': %s\n", path, strerror(errno));
free(path);
}
}
/** Print a warning on standard error output and in the log file
* \param format Format string (same syntax as printf)
*/
void
warnl(const char *format, ...)
{
va_list args;
if(W->log)
{
va_start(args, format);
vfprintf(W->log, format, args);
fprintf(W->log, "\n");
va_end(args);
}
va_start(args, format);
vwarn(format, args);
va_end(args);
}
/** Print an error on standard error output and in the log file
* \param eval Exit value
* \param format Format string (same syntax as printf)
*/
void
errl(int eval, const char *format, ...)
{
va_list args;
if(W->log)
{
va_start(args, format);
vfprintf(W->log, format, args);
fprintf(W->log, "\n");
va_end(args);
fclose (W->log);
}
va_start(args, format);
verr(eval, format, args);
}
/** Print a warning on standard error output and in the log file
* \param format Format string (same syntax as printf)
*/
void
warnxl(const char *format, ...)
{
va_list args;
if(W->log)
{
va_start(args, format);
vfprintf(W->log, format, args);
fprintf(W->log, "\n");
va_end(args);
}
va_start(args, format);
vwarnx(format, args);
va_end(args);
}
/** Print an error on standard error output and in the log file
* \param eval Exit value
* \param format Format string (same syntax as printf)
*/
void
errxl(int eval, const char *format, ...)
{
va_list args;
if(W->log)
{
va_start(args, format);
vfprintf(W->log, format, args);
fprintf(W->log, "\n");
va_end(args);
fclose (W->log);
}
va_start(args, format);
verrx(eval, format, args);
}

18
src/log.h Normal file
View File

@ -0,0 +1,18 @@
/*
* wmfs2 by Martin Duquesnoy <xorg62@gmail.com> { for(i = 2011; i < 2111; ++i) ©(i); }
* File created by David Delassus.
* For license, see COPYING.
*/
#ifndef __LOG_H
#define __LOG_H
#include <errno.h>
void log_init(void);
void warnl(const char *format, ...);
void warnxl(const char *format, ...);
void errl(int eval, const char *format, ...);
void errxl(int eval, const char *format, ...);
#endif /* __LOG_H */

View File

@ -123,7 +123,7 @@ push_keyword(struct keyword *tail, enum keyword_t type, char *buf, size_t *offse
#ifdef DEBUG #ifdef DEBUG
for(i = 0; kw_t_name[i].type != NONE; ++i) for(i = 0; kw_t_name[i].type != NONE; ++i)
if(kw_t_name[i].type == kw->type) if(kw_t_name[i].type == kw->type)
warnx("%s %s %s:%d\n", kw_t_name[i].name, warnxl("%s %s %s:%d\n", kw_t_name[i].name,
(kw->name) ? kw->name : "", (kw->name) ? kw->name : "",
kw->file->name, kw->line); kw->file->name, kw->line);
#endif #endif
@ -174,20 +174,20 @@ parse_keywords(const char *filename)
if(stat(filename, &st) == -1 || (fd = open(filename, O_RDONLY)) == -1) if(stat(filename, &st) == -1 || (fd = open(filename, O_RDONLY)) == -1)
{ {
warn("%s", filename); warnxl("%s", filename);
return NULL; return NULL;
} }
if(!st.st_size) if(!st.st_size)
{ {
warnx("%s: empty file", filename); warnxl("%s: empty file", filename);
close(fd); close(fd);
return NULL; return NULL;
} }
if(!realpath(filename, path)) if(!realpath(filename, path))
{ {
warn("%s", filename); warnxl("%s", filename);
close(fd); close(fd);
return NULL; return NULL;
} }
@ -196,7 +196,7 @@ parse_keywords(const char *filename)
if(read(fd, buf, st.st_size) == -1) if(read(fd, buf, st.st_size) == -1)
{ {
warn("%s", filename); warnxl("%s", filename);
free(buf); free(buf);
close(fd); close(fd);
return NULL; return NULL;
@ -334,7 +334,7 @@ parse_keywords(const char *filename)
free(buf); free(buf);
free(bufname); free(bufname);
close(fd); close(fd);
warnx("%s read", file->name); warnxl("%s read", file->name);
return (error ? NULL: head); return (error ? NULL: head);
} }
@ -385,7 +385,7 @@ include(struct keyword *head)
if(!(kw = parse_keywords(filename))) if(!(kw = parse_keywords(filename)))
{ {
warnx("no config found in include file %s", head->name); warnxl("no config found in include file %s", head->name);
if(filename != head->name) if(filename != head->name)
free(filename); free(filename);
@ -630,7 +630,7 @@ free_conf(void)
if(f[i] == kw->file) if(f[i] == kw->file)
{ {
if(!(f = realloc(f, sizeof(*f) * (++i)))) if(!(f = realloc(f, sizeof(*f) * (++i))))
err(EXIT_FAILURE, "realloc"); errl(EXIT_FAILURE, "realloc");
f[i - 1] = kw->file; f[i - 1] = kw->file;
} }

View File

@ -67,7 +67,7 @@ print_unused(struct conf_sec *sec)
SLIST_FOREACH(o, &sec->optlist, entry) SLIST_FOREACH(o, &sec->optlist, entry)
if(!o->used) if(!o->used)
warnx("%s:%d, unused param %s", o->filename, o->line, o->name); warnxl("%s:%d, unused param %s", o->filename, o->line, o->name);
TAILQ_FOREACH(s, &sec->sub, entry) TAILQ_FOREACH(s, &sec->sub, entry)
if(!TAILQ_EMPTY(&s->sub)) if(!TAILQ_EMPTY(&s->sub))

View File

@ -21,11 +21,11 @@ xmalloc(size_t nmemb, size_t size)
void *ret; void *ret;
if(SIZE_MAX / nmemb < size) if(SIZE_MAX / nmemb < size)
err(EXIT_FAILURE, "xmalloc(%zu, %zu), " errl(EXIT_FAILURE, "xmalloc(%zu, %zu), "
"size_t overflow detected", nmemb, size); "size_t overflow detected", nmemb, size);
if((ret = malloc(nmemb * size)) == NULL) if((ret = malloc(nmemb * size)) == NULL)
err(EXIT_FAILURE, "malloc(%zu)", nmemb * size); errl(EXIT_FAILURE, "malloc(%zu)", nmemb * size);
return ret; return ret;
} }
@ -41,7 +41,7 @@ xcalloc(size_t nmemb, size_t size)
void *ret; void *ret;
if((ret = calloc(nmemb, size)) == NULL) if((ret = calloc(nmemb, size)) == NULL)
err(EXIT_FAILURE, "calloc(%zu * %zu)", nmemb, size); errl(EXIT_FAILURE, "calloc(%zu * %zu)", nmemb, size);
return ret; return ret;
} }
@ -62,7 +62,7 @@ xasprintf(char **strp, const char *fmt, ...)
va_end(args); va_end(args);
if (ret == -1) if (ret == -1)
err(EXIT_FAILURE, "asprintf(%s)", fmt); errl(EXIT_FAILURE, "asprintf(%s)", fmt);
return ret; return ret;
} }
@ -77,7 +77,7 @@ xstrdup(const char *str)
char *ret = NULL; char *ret = NULL;
if(str == NULL || (ret = strdup(str)) == NULL) if(str == NULL || (ret = strdup(str)) == NULL)
warnx("strdup(%s)", str); warnxl("strdup(%s)", str);
return ret; return ret;
} }
@ -101,7 +101,7 @@ spawn(const char *format, ...)
if (len >= sizeof(cmd)) if (len >= sizeof(cmd))
{ {
warnx("command too long (> 512 bytes)"); warnxl("command too long (> 512 bytes)");
return -1; return -1;
} }
@ -112,11 +112,11 @@ spawn(const char *format, ...)
{ {
setsid(); setsid();
if (execl(sh, sh, "-c", cmd, (char*)NULL) == -1) if (execl(sh, sh, "-c", cmd, (char*)NULL) == -1)
warn("execl(sh -c %s)", cmd); warnl("execl(sh -c %s)", cmd);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
else if (pid == -1) else if (pid == -1)
warn("fork"); warnl("fork");
return pid; return pid;
} }

View File

@ -25,7 +25,7 @@ wmfs_error_handler(Display *d, XErrorEvent *event)
/* Check if there is another WM running */ /* Check if there is another WM running */
if(event->error_code == BadAccess if(event->error_code == BadAccess
&& W->root == event->resourceid) && W->root == event->resourceid)
errx(EXIT_FAILURE, "Another Window Manager is already running."); errl(EXIT_FAILURE, "Another Window Manager is already running.");
/* Ignore focus change error for unmapped client /* Ignore focus change error for unmapped client
* 42 = X_SetInputFocus * 42 = X_SetInputFocus
@ -39,7 +39,7 @@ wmfs_error_handler(Display *d, XErrorEvent *event)
if(XGetErrorText(d, event->error_code, mess, 128)) if(XGetErrorText(d, event->error_code, mess, 128))
warnx("%s(%d) opcodes %d/%d\n resource #%lx\n", warnxl("%s(%d) opcodes %d/%d\n resource #%lx\n",
mess, mess,
event->error_code, event->error_code,
event->request_code, event->request_code,
@ -82,7 +82,7 @@ wmfs_init_font(char *font, struct theme *t)
if(!(t->font.fontset = XCreateFontSet(W->dpy, font, &misschar, &d, &defstring))) if(!(t->font.fontset = XCreateFontSet(W->dpy, font, &misschar, &d, &defstring)))
{ {
warnx("Can't load font '%s'", font); warnxl("Can't load font '%s'", font);
t->font.fontset = XCreateFontSet(W->dpy, "fixed", &misschar, &d, &defstring); t->font.fontset = XCreateFontSet(W->dpy, "fixed", &misschar, &d, &defstring);
} }
@ -376,6 +376,7 @@ wmfs_loop(void)
static inline void static inline void
wmfs_init(void) wmfs_init(void)
{ {
log_init();
wmfs_xinit(); wmfs_xinit();
ewmh_init(); ewmh_init();
screen_init(); screen_init();
@ -455,6 +456,10 @@ wmfs_quit(void)
unlink(W->fifo.path); unlink(W->fifo.path);
} }
/* close log */
if(W->log)
fclose(W->log), W->log = NULL;
W->flags &= ~WMFS_RUNNING; W->flags &= ~WMFS_RUNNING;
XCloseDisplay(W->dpy); XCloseDisplay(W->dpy);

View File

@ -22,6 +22,8 @@
#include <X11/Xatom.h> #include <X11/Xatom.h>
/* Local */ /* Local */
#include "log.h"
#define CONFIG_DEFAULT_PATH ".config/wmfs/wmfsrc" #define CONFIG_DEFAULT_PATH ".config/wmfs/wmfsrc"
#define ButtonMask (ButtonPressMask | ButtonReleaseMask | ButtonMotionMask) #define ButtonMask (ButtonPressMask | ButtonReleaseMask | ButtonMotionMask)
@ -289,6 +291,9 @@ struct wmfs
int fd; int fd;
} fifo; } fifo;
/* Log file */
FILE *log;
/* Lists heads */ /* Lists heads */
struct struct
{ {