summary refs log tree commit diff
diff options
context:
space:
mode:
authorokan <okan>2018-02-02 13:40:55 +0000
committerokan <okan>2018-02-02 13:40:55 +0000
commit34e15dbd7a2226de417fe60c69de777667b8d6af (patch)
treec3fd73ceed258cff45ffb8ed1114fe706885626e
parent9bf750b0549f10fdd4f5898dbab40ad4c946eee6 (diff)
downloadcwm-34e15dbd7a2226de417fe60c69de777667b8d6af.tar.gz
cwm-34e15dbd7a2226de417fe60c69de777667b8d6af.tar.xz
cwm-34e15dbd7a2226de417fe60c69de777667b8d6af.zip
Add a simple debug logging mechanism.
-rw-r--r--calmwm.h10
-rw-r--r--util.c30
2 files changed, 40 insertions, 0 deletions
diff --git a/calmwm.h b/calmwm.h
index b2183e7..9035964 100644
--- a/calmwm.h
+++ b/calmwm.h
@@ -31,6 +31,13 @@
 #include <X11/extensions/Xrandr.h>
 #include <X11/keysym.h>
 
+/* #define DEBUG */
+#ifdef DEBUG
+#define DPRINTF(...)	log_debug(__func__, __VA_ARGS__)
+#else
+#define DPRINTF(...)	do {} while (0)
+#endif /* DEBUG */
+
 #undef MIN
 #undef MAX
 #define MIN(x, y) ((x) < (y) ? (x) : (y))
@@ -584,6 +591,9 @@ void 			 xu_ewmh_restore_net_wm_state(struct client_ctx *);
 char			*u_argv(char * const *);
 void			 u_exec(char *);
 void			 u_spawn(char *);
+void			 log_debug(const char *, const char *, ...)
+			    __attribute__((__format__ (printf, 2, 3)))
+			    __attribute__((__nonnull__ (2)));
 
 void			*xcalloc(size_t, size_t);
 void			*xmalloc(size_t);
diff --git a/util.c b/util.c
index 3c35f33..e6e8443 100644
--- a/util.c
+++ b/util.c
@@ -31,6 +31,8 @@
 
 #include "calmwm.h"
 
+static void	 log_msg(const char *, va_list);
+
 void
 u_spawn(char *argstr)
 {
@@ -104,3 +106,31 @@ u_argv(char * const *argv)
 	}
 	return(p);
 }
+
+static void
+log_msg(const char *msg, va_list ap)
+{
+	char	*fmt;
+
+	if (asprintf(&fmt, "%s\n", msg) == -1) {
+		vfprintf(stderr, msg, ap);
+		fprintf(stderr, "\n");
+	} else {
+		vfprintf(stderr, fmt, ap);
+		free(fmt);
+	}
+	fflush(stderr);
+}
+
+void
+log_debug(const char *func, const char *msg, ...)
+{
+	char	*fmt;
+	va_list	 ap;
+
+	va_start(ap, msg);
+	if (asprintf(&fmt, "%s: %s", func, msg) == -1)
+		exit(1);
+	log_msg(fmt, ap);
+	va_end(ap);
+}