forked from yaosj2k/dnsforwarder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
debug.c
executable file
·93 lines (70 loc) · 1.56 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
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include "debug.h"
#include "utils.h"
#include "common.h"
/* Global Variables */
BOOL ShowMassages = TRUE;
BOOL ErrorMessages = TRUE;
static FILE *Debug_File = NULL;
static int ThresholdLength = 0;
static int CurrentLength = 0;
static char FilePath[1024];
int Debug_Init(ConfigFileInfo *ConfigInfo)
{
if( ConfigGetBoolean(ConfigInfo, "LogOn") == FALSE )
{
return 1;
}
sprintf(FilePath, "%s%cdnsforwarder.log", ConfigGetRawString(ConfigInfo, "LogFileFolder"), PATH_SLASH_CH);
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);
}
ThresholdLength = ConfigGetInt32(ConfigInfo, "LogFileThresholdLength");
Debug_PrintFile("\n\n\n\n\nNew session\n");
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 *format, ...)
{
va_list ap;
if( Debug_Inited() == FALSE )
{
return;
}
va_start(ap, format);
CheckLength();
CurrentLength += vfprintf(Debug_File, format, ap);
fflush(Debug_File);
va_end(ap);
}