aboutsummaryrefslogtreecommitdiff
path: root/weechat/debug.c
blob: 969733c0e0debd0d8bf5434b7e0c19522b2c8b2a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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);
}