#include #include #include #include #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); }