fix
[libmpd.git] / src / debug_printf.c
blob457cf41c300c6864e041b3bc7e11f71259d2634f
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdarg.h>
4 #include <stdlib.h>
5 #include <unistd.h>
6 #include <time.h>
7 #include "config.h"
8 #include "debug_printf.h"
10 #define RED "\x1b[31;01m"
11 #define DARKRED "\x1b[31;06m"
12 #define RESET "\x1b[0m"
13 #define GREEN "\x1b[32;06m"
14 #define YELLOW "\x1b[33;06m"
16 int debug_level = 0;
17 /* Compiler does not like it when I initialize this to stdout, complaints about
18 * not being constant. stoud is a macro..
19 * So use this "hack"
21 FILE *rout = NULL;
23 void debug_set_output(FILE *fp)
25 rout = fp;
28 void debug_set_level(DebugLevel dl)
30 debug_level = (dl<0)?DEBUG_NO_OUTPUT:((dl > DEBUG_INFO)?DEBUG_INFO:dl);
34 void debug_printf_real(DebugLevel dp, const char *file,const int line,const char *function, const char *format,...)
36 if(debug_level >= dp)
38 va_list arglist;
39 time_t ts = time(NULL);
40 struct tm tm;
41 char buffer[32];
42 FILE *out = stdout;
43 if(rout) out = rout;
44 va_start(arglist,format);
46 /* Windows has no thread-safe localtime_r function, so ignore it for now */
47 #ifndef WIN32
48 localtime_r(&ts, &tm);
49 strftime(buffer, 32, "%d/%m/%y %T",&tm);
50 #else
51 buffer[0] = '\0';
52 #endif
54 if(dp == DEBUG_INFO)
56 fprintf(out,"%s: "GREEN"INFO:"RESET" %s %s():#%d:\t",buffer,file,function,line);
58 else if(dp == DEBUG_WARNING)
60 fprintf(out,"%s: "YELLOW"WARNING:"RESET" %s %s():#%i:\t",buffer,file,function,line);
62 else
64 fprintf(out,"%s: "DARKRED"ERROR:"RESET" %s %s():#%i:\t",buffer,file,function,line);
66 vfprintf(out,format, arglist);
67 if(format[strlen(format)-1] != '\n')
69 fprintf(out,"\n");
71 fflush(out);
72 va_end(arglist);