From a0f941e7ae0e6935152e5ce42bfb8b45d224c25d Mon Sep 17 00:00:00 2001 From: Tom Smeding Date: Mon, 13 Jul 2020 19:53:05 +0200 Subject: weechat: Abstract debug file into separate module --- weechat/debug.c | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 weechat/debug.c (limited to 'weechat/debug.c') 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 +#include +#include +#include +#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); +} -- cgit v1.2.3-70-g09d2