Browse Source

Add logging system with warnl, warnxl, errl, errxl

David Delassus 12 years ago
parent
commit
e31ebb9702
10 changed files with 164 additions and 25 deletions
  1. 1 0
      Makefile.in
  2. 3 3
      src/config.c
  3. 2 2
      src/fifo.c
  4. 110 0
      src/log.c
  5. 18 0
      src/log.h
  6. 8 8
      src/parse.c
  7. 1 1
      src/parse_api.c
  8. 8 8
      src/util.c
  9. 8 3
      src/wmfs.c
  10. 5 0
      src/wmfs.h

+ 1 - 0
Makefile.in

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

+ 3 - 3
src/config.c

@@ -278,7 +278,7 @@ config_keybind(void)
           /* func = */
           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);
                k->func = uicb_spawn;
           }
@@ -300,11 +300,11 @@ config_init(void)
 {
      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"));
 
           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();

+ 2 - 2
src/fifo.c

@@ -18,7 +18,7 @@ fifo_open(void)
           close(W->fifo.fd);
 
      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
@@ -31,7 +31,7 @@ fifo_init(void)
           unlink(W->fifo.path);
 
      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();
 }

+ 110 - 0
src/log.c

@@ -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 - 0
src/log.h

@@ -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 */

+ 8 - 8
src/parse.c

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

+ 1 - 1
src/parse_api.c

@@ -67,7 +67,7 @@ print_unused(struct conf_sec *sec)
 
      SLIST_FOREACH(o, &sec->optlist, entry)
           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)
           if(!TAILQ_EMPTY(&s->sub))

+ 8 - 8
src/util.c

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

+ 8 - 3
src/wmfs.c

@@ -25,7 +25,7 @@ wmfs_error_handler(Display *d, XErrorEvent *event)
       /* Check if there is another WM running */
       if(event->error_code == BadAccess
                 && 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
        * 42 = X_SetInputFocus
@@ -39,7 +39,7 @@ wmfs_error_handler(Display *d, XErrorEvent *event)
 
 
      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,
                     event->error_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)))
      {
-          warnx("Can't load font '%s'", font);
+          warnxl("Can't load font '%s'", font);
           t->font.fontset = XCreateFontSet(W->dpy, "fixed", &misschar, &d, &defstring);
      }
 
@@ -376,6 +376,7 @@ wmfs_loop(void)
 static inline void
 wmfs_init(void)
 {
+     log_init();
      wmfs_xinit();
      ewmh_init();
      screen_init();
@@ -455,6 +456,10 @@ wmfs_quit(void)
           unlink(W->fifo.path);
      }
 
+     /* close log */
+     if(W->log)
+          fclose(W->log), W->log = NULL;
+
      W->flags &= ~WMFS_RUNNING;
 
      XCloseDisplay(W->dpy);

+ 5 - 0
src/wmfs.h

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