1 /* libmpd (high level libmpdclient library)
2  * Copyright (C) 2004-2009 Qball Cow <qball@sarine.nl>
3  * Project homepage: http://gmpcwiki.sarine.nl/
4 
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9 
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14 
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19 
20 #include <stdio.h>
21 #include <string.h>
22 #include <stdarg.h>
23 #include <stdlib.h>
24 #include <unistd.h>
25 #include <time.h>
26 #include <glib.h>
27 #include "config.h"
28 #include "debug_printf.h"
29 
30 int debug_level = 0;
31 /* Compiler does not like it when I initialize this to stdout, complaints about
32  * not being constant. stoud is a macro..
33  * So use this "hack"
34  */
35 FILE *rout = NULL;
36 #define ERROR_BUFFER_SIZE 2048
37 char error_buffer[ERROR_BUFFER_SIZE];
38 
debug_set_output(FILE * fp)39 void debug_set_output(FILE *fp)
40 {
41     rout = fp;
42 }
43 
debug_set_level(DebugLevel dl)44 void debug_set_level(DebugLevel dl)
45 {
46 	debug_level = (dl<0)?DEBUG_NO_OUTPUT:((dl > DEBUG_INFO)?DEBUG_INFO:dl);
47 }
48 
49 
debug_printf_real(DebugLevel dp,const char * file,const int line,const char * function,const char * format,...)50 void debug_printf_real(DebugLevel dp, const char *file,const int line,const char *function, const char *format,...)
51 {
52 	if(debug_level >= dp)
53 	{
54 		va_list arglist;
55         time_t ts = time(NULL);
56         struct tm tm;
57         char buffer[32];
58         FILE *out = stdout;
59         char *temp;
60         if(rout) out = rout;
61         va_start(arglist,format);
62 
63   /* Windows has no thread-safe localtime_r function, so ignore it for now */
64 #ifndef WIN32
65         localtime_r(&ts, &tm);
66         strftime(buffer, 32, "%d/%m/%y %T",&tm);
67 #else
68         buffer[0] = '\0';
69 #endif
70 
71 		if(dp == DEBUG_INFO)
72 		{
73 			fprintf(out,"%s: INFO:    %s %s():#%d:\t",buffer,file,function,line);
74 		}
75 		else if(dp == DEBUG_WARNING)
76 		{
77 			fprintf(out,"%s: WARNING: %s %s():#%i:\t",buffer,file,function,line);
78 		}
79 		else
80 		{
81 			fprintf(out,"%s: ERROR:   %s %s():#%i:\t",buffer,file,function,line);
82 		}
83 		vsnprintf(error_buffer,ERROR_BUFFER_SIZE,format, arglist);
84         temp = g_locale_from_utf8(error_buffer, -1,NULL, NULL, NULL);
85         if(temp) {
86             fputs(temp,out);
87             g_free(temp);
88         }
89 		if(format[strlen(format)-1] != '\n')
90 		{
91 			fprintf(out,"\n");
92 		}
93 		fflush(out);
94 		va_end(arglist);
95 	}
96 }
97