Handle streams separately in tree_add_track()
[cmus.git] / debug.c
blob3c6baf38e4483e1aa3a431408a98905a03de45bb
1 /*
2 * Copyright 2004-2005 Timo Hirvonen
3 */
5 #include "debug.h"
6 #include "prog.h"
8 #include <stdio.h>
9 #include <stdarg.h>
11 #if DEBUG > 1
12 static FILE *debug_stream = NULL;
13 #endif
15 void debug_init(void)
17 #if DEBUG > 1
18 char filename[512];
19 const char *tmp = getenv("TMPDIR");
21 if (!tmp || !tmp[0])
22 tmp = "/tmp";
24 snprintf(filename, sizeof(filename), "%s/cmus-debug", tmp);
25 debug_stream = fopen(filename, "w");
26 if (debug_stream == NULL)
27 die_errno("error opening `%s' for writing", filename);
28 #endif
31 /* This function must be defined even if debugging is disabled in the program
32 * because debugging might still be enabled in some plugin.
34 void __debug_bug(const char *function, const char *fmt, ...)
36 const char *format = "\n%s: BUG: ";
37 va_list ap;
39 /* debug_stream exists only if debugging is enabled */
40 #if DEBUG > 1
41 fprintf(debug_stream, format, function);
42 va_start(ap, fmt);
43 vfprintf(debug_stream, fmt, ap);
44 va_end(ap);
45 #endif
47 /* always print bug message to stderr */
48 fprintf(stderr, format, function);
49 va_start(ap, fmt);
50 vfprintf(stderr, fmt, ap);
51 va_end(ap);
52 exit(127);
55 void __debug_print(const char *function, const char *fmt, ...)
57 #if DEBUG > 1
58 va_list ap;
60 fprintf(debug_stream, "%s: ", function);
61 va_start(ap, fmt);
62 vfprintf(debug_stream, fmt, ap);
63 va_end(ap);
64 fflush(debug_stream);
65 #endif