aboutsummaryrefslogtreecommitdiff
path: root/weechat/debug.c
blob: dbd1d91bec587f4914452835e7cc3bb37c108f28 (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
44
45
46
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include "debug.h"


static FILE *tomsg_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
	sprintf(fname, "%s%s", home, suffix);

	tomsg_dfile = fopen(fname, "a");
	if (!tomsg_dfile) {
		tomsg_dfile = NULL;
		return;  // fine!
	}

	free(fname);

	// Disable buffering
	setvbuf(tomsg_dfile, NULL, _IONBF, 0);

	fprintf(tomsg_dfile, "--------\n");
}

void debug_deinit(void) {
	if (tomsg_dfile) fclose(tomsg_dfile);
}

__attribute__((format (printf, 1, 2)))
void debugf(const char *restrict format, ...) {
	if (!tomsg_dfile) return;

	va_list ap;
	va_start(ap, format);
	vfprintf(tomsg_dfile, format, ap);
	va_end(ap);
}