aboutsummaryrefslogtreecommitdiff
path: root/weechat/debug.c
diff options
context:
space:
mode:
Diffstat (limited to 'weechat/debug.c')
-rw-r--r--weechat/debug.c43
1 files changed, 43 insertions, 0 deletions
diff --git a/weechat/debug.c b/weechat/debug.c
new file mode 100644
index 0000000..969733c
--- /dev/null
+++ b/weechat/debug.c
@@ -0,0 +1,43 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdarg.h>
+#include <string.h>
+#include "debug.h"
+
+
+static FILE *g_dfile;
+
+void debug_init(void) {
+ const char *home = getenv("HOME");
+ if (!home) home = "/tmp";
+
+ const char *suffix = "/Desktop/debugf.txt";
+
+ char *fname = malloc(strlen(home) + strlen(suffix) + 1);
+ if (!fname) return; // okay, fine, no debug file
+
+ g_dfile = fopen(fname, "a");
+ if (!g_dfile) {
+ g_dfile = NULL;
+ return; // fine!
+ }
+
+ // Disable buffering
+ setvbuf(g_dfile, NULL, _IONBF, 0);
+
+ fprintf(g_dfile, "--------\n");
+}
+
+void debug_deinit(void) {
+ if (g_dfile) fclose(g_dfile);
+}
+
+__attribute__((format (printf, 1, 2)))
+void debugf(const char *restrict format, ...) {
+ if (!g_dfile) return;
+
+ va_list ap;
+ va_start(ap, format);
+ vfprintf(g_dfile, format, ap);
+ va_end(ap);
+}