1 /*******************************************\
2 | * Ce programme est sous licence GNU GPL * |
3 | * This software is under GNU/GPL licence * |
4 | * * * * * * * * * * * * * * * * * * * * * * |
5 | * http://www.gnu.org/copyleft/gpl.html * |
6 \*******************************************/
7
8 /* Par Laurent Coustet <ed@zehome.com>
9 * http://ed.zehome.com/
10 * Made by Laurent Coustet <ed@zehome.com>
11 */
12
13
14 #include <stdarg.h>
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18
19 #include "debug.h"
20 #include "time.h"
21
22 FILE *_Debug_Output = NULL;
23
_DEBUG(int _debug_line,char * _debug_filename,int _debug_priority,const char * _debug_message,...)24 void _DEBUG(int _debug_line, char *_debug_filename,
25 int _debug_priority, const char *_debug_message, ...)
26 {
27 /* arguments variables */
28 char *z_format; /* thx dave */
29 va_list ap;
30 time_t now;
31 struct tm *curTime = NULL;
32
33 if (! _Debug_Output) return;
34
35 now = time(NULL);
36 if (now == (time_t)-1)
37 {
38 fprintf(stderr, "Can't log line: time() failed.\n");
39 perror("time");
40 return;
41 }
42
43 #ifndef WIN32
44 curTime = (struct tm *) malloc(sizeof(struct tm));
45 localtime_r(&now, curTime); /* Get the current time */
46 #else
47 curTime = localtime(&now);
48 #endif
49 if (curTime == NULL)
50 {
51 fprintf(stderr, "Can't log line: localtime(_r)() failed.\n");
52 return;
53 }
54
55 va_start(ap, _debug_message);
56
57 /* message de priorit�e inf�rieure a notre prio, on vire */
58 if (_debug_priority > DEBUGLEVEL)
59 return;
60
61 z_format = calloc(1024, 1);
62 snprintf(z_format, 1023, "[%.2d:%.2d:%.2d] [DEBUG PRIO %d][File: %s][Line: %d] %s", curTime->tm_hour, curTime->tm_min, curTime->tm_sec, _debug_priority, _debug_filename, _debug_line, _debug_message);
63
64 vfprintf(_Debug_Output, z_format, ap);
65
66 fflush(_Debug_Output);
67
68 #ifndef WIN32
69 (void)free(curTime);
70 #endif
71 (void)free(z_format);
72 va_end(ap);
73 return;
74 }
75
SetDebugFile(FILE * file)76 void SetDebugFile(FILE *file)
77 {
78 _Debug_Output = file;
79 return;
80 }
81