forked from yaosj2k/dnsforwarder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
debug.c
100 lines (75 loc) · 1.78 KB
/
debug.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include "debug.h"
#include "utils.h"
#include "common.h"
#ifdef INTERNAL_DEBUG
static EFFECTIVE_LOCK Debug_Mutex;
static FILE *Debug_File = NULL;
static int ThresholdLength = 0;
static int CurrentLength = 0;
static char FilePath[1024];
int Debug_Init(int _ThresholdLength)
{
GetFileDirectory(FilePath);
strcat(FilePath, PATH_SLASH_STR);
strcat(FilePath, "Debug.log");
Debug_File = fopen(FilePath, "r+");
if( Debug_File == NULL )
{
Debug_File = fopen(FilePath, "w");
CurrentLength = 0;
} else {
fseek(Debug_File, 0, SEEK_END);
CurrentLength = ftell(Debug_File);
}
EFFECTIVE_LOCK_INIT(Debug_Mutex);
ThresholdLength = _ThresholdLength;
DEBUG_FILE("\n\n\n\n\nNew session\n");
DEBUG_FILE("CurrentLength : %d\n", CurrentLength);
return 0;
}
BOOL Debug_Inited(void)
{
return !(Debug_File == NULL);
}
static void CheckLength(void)
{
if( CurrentLength >= ThresholdLength )
{
char FileRenamed[1027];
int loop;
fclose(Debug_File);
for( loop = 1; ; ++loop )
{
sprintf(FileRenamed, "%s.%d", FilePath, loop);
if( FileIsReadable(FileRenamed) == FALSE )
{
rename(FilePath, FileRenamed);
Debug_File = fopen(FilePath, "w");
CurrentLength = 0;
break;
}
}
}
}
void Debug_PrintFile(const char *Function, int Line, const char *format, ...)
{
va_list ap;
char DateAndTime[32];
if( Debug_Inited() == FALSE )
{
return;
}
va_start(ap, format);
EFFECTIVE_LOCK_GET(Debug_Mutex);
CheckLength();
GetCurDateAndTime(DateAndTime, sizeof(DateAndTime));
CurrentLength += fprintf(Debug_File, "T:%d %s:%d %s : ", GET_THREAD_ID(), Function, Line, DateAndTime);
CurrentLength += vfprintf(Debug_File, format, ap);
fflush(Debug_File);
EFFECTIVE_LOCK_RELEASE(Debug_Mutex);
va_end(ap);
}
#endif