-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtftp12Log.c
273 lines (233 loc) · 5.46 KB
/
tftp12Log.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#include "tftp12log.h"
#define TFTP12_MAX_LOGMSG_SIZE (512)
#define TFTP12_SHOW_LOG_BUFFER_SIZE (2048)
#define TFTP12_LOG_SIZE_MAX (10*1024)/*日志文件最大1MB*/
#define TFTP12_LOG_SIZE_CHECK_COUNT (500) /*每500次写log检查一次文件大小*/
#define TFTP12_SERVER_LOG_FILE (1)
#define TFTP12_CLIENT_LOG_FILE (0)
static char* const fileName[] = {
"tftp12ClinetLog.txt",
"tftp12ServerLog.txt",
};
typedef struct _tftp12lognode
{
INT32 enable;
INT32 fileNameID;
FILE *file;
INT32 sizeCheckCount;
//信号量
}TFTP12LogNode;
struct
{
INT32 initFlag;
TFTP12LogNode client;
TFTP12LogNode server;
// INT32 tftp12ClinetLogEnable;
// INT32 tftp12ServerLogEnable;
// //客户端信号量保护
// //服务器文件信号量保护
//
// INT32 sizeCheckCount;
// FILE *clientLogFile;
// FILE *serverLogFile;
}TFTP12LogInfo;
static void tftp12ShowLog(FILE *logfile);
// static void tftp12LogClear(FILE node->file)
// {
//
// }
/*需要添加文件描述符的锁*/
/*每次清理一半的文件*/
static void tftp12LogSizeCheck(TFTP12LogNode *node)
{
INT32 fileSize = 0;
INT32 realRead = 0;
INT32 realWrite = 0;
INT32 i = 0;
INT32 enterPos = 0;
INT8 *logTem = NULL;
//semTake(server)
if (node->file != NULL)
{
fseek(node->file, 0, SEEK_END);
fileSize = ftell(node->file);
fseek(node->file, 0, SEEK_SET);
/*如果日志文件大小大于了最大文件体积限制*/
if (fileSize > TFTP12_LOG_SIZE_MAX)
{
logTem = (INT8 *)malloc(fileSize + 1);
if (logTem == NULL)
{
printf("\ntftp12LogSizeCheck malloc failed");
return;
}
realRead = fread(logTem, 1, fileSize, node->file);
if (realRead <= 0)
{
return;
}
/*从中间开始往后找到最近的换行符,剪掉*/
enterPos = realRead / 2;
while ((logTem[enterPos] != '\n') && (enterPos < realRead))
{
enterPos++;
}
/*关闭文件,为清空文件做准备*/
FCLOSE_Z(node->file);
node->file = fopen(fileName[node->fileNameID], "w+");
if (node->file != NULL)
{
realWrite = fwrite(&logTem[enterPos + 1], 1, ((realRead - (enterPos + 1))), node->file);
if (realWrite != ((realRead - (enterPos + 1))))
{
printf("\nlog size check write bytes error");
}
}
/*关闭文件,重新以a+方式打开*/
FCLOSE_Z(node->file);
node->file = fopen(fileName[node->fileNameID], "a+");
if (node->file == NULL)
{
printf("\nlog file check reopen failed");
node->enable = FALSE;
}
free(logTem);
}
}
//semGiVe(server);
}
void logtest()
{
INT32 i = 0;
tftp12LogInit();
tftp12ClientLogMsg("testlog num:%d", i);
i++;
tftp12ClientLogMsg("testlog num:%d", i);
i++;
tftp12ClientLogMsg("testlog num:%d", i);
i++;
tftp12ClientLogMsg("testlog num:%d", i);
tftp12ClientLogMsg("中文测试\n");
tftp12ShowLogClient();
i = 100;
tftp12ClientLogMsg("testlog num:%d", i);
tftp12LogSizeCheck(&(TFTP12LogInfo.client));
tftp12ClientLogMsg("后缀添加测试\n");
}
static void tftp12ShowLog(FILE *logfile)
{
INT32 i = 0;
INT32 readBytes = 0;
INT32 logFileCurrentPos = 0;
INT8 *readBuf = (INT8 *)malloc(TFTP12_SHOW_LOG_BUFFER_SIZE);
if (readBuf == NULL)
{
printf("\nMemory alloc failed!");
return;
}
if (logfile == NULL)
{
printf("\nLog file is NULL");
return;
}
//semTake()
/*保存当前文件指针的位置*/
logFileCurrentPos = ftell(logfile);
fseek(logfile, 0, SEEK_SET);
readBytes = TFTP12_SHOW_LOG_BUFFER_SIZE;
while (readBytes == TFTP12_SHOW_LOG_BUFFER_SIZE)
{
readBytes = fread(readBuf, 1, TFTP12_SHOW_LOG_BUFFER_SIZE, logfile);
if (readBytes > 0)
{
for (i = 0; i < readBytes; i++)
{
printf("%c", readBuf[i]);
}
}
}
fseek(logfile, logFileCurrentPos, SEEK_SET);
free(readBuf);
}
void tftp12ShowLogClient(void)
{
tftp12ShowLog(TFTP12LogInfo.client.file);
}
void tftp12ShowLogServer(void)
{
tftp12ShowLog(TFTP12LogInfo.server.file);
}
void tftp12LogInit(void)
{
if (TFTP12LogInfo.initFlag == TRUE)
{
return;
}
memset(&TFTP12LogInfo, 0, sizeof(TFTP12LogInfo));
TFTP12LogInfo.client.fileNameID = TFTP12_CLIENT_LOG_FILE;
TFTP12LogInfo.server.fileNameID = TFTP12_SERVER_LOG_FILE;
TFTP12LogInfo.client.file = fopen(fileName[TFTP12LogInfo.client.fileNameID], "a+");
TFTP12LogInfo.server.file = fopen(fileName[TFTP12LogInfo.server.fileNameID], "a+");
if (TFTP12LogInfo.client.file == NULL)
{
printf("\nopen client logfile failed");
}
else
{
TFTP12LogInfo.client.enable = TRUE;
}
if (TFTP12LogInfo.server.file == NULL)
{
printf("\nopen server logfile failed");
}
else
{
TFTP12LogInfo.server.enable = TRUE;
}
TFTP12LogInfo.initFlag = TRUE;
/*初始化的时候检查一次日志文件大小*/
tftp12LogSizeCheck(&(TFTP12LogInfo.client));
tftp12LogSizeCheck(&(TFTP12LogInfo.server));
}
static void tftp12LogMsgMain(FILE *file, char *format, va_list ap)
{
char msg[TFTP12_MAX_LOGMSG_SIZE];
INT32 msgLen = 0;
/*添加一个终止符,防止访问越界,不用内存清零,浪费时间*/
msg[TFTP12_MAX_LOGMSG_SIZE - 1] = '\0';
//需要添加时间戳
_vsnprintf(msg, TFTP12_MAX_LOGMSG_SIZE, format, ap);
msgLen = strlen(msg);
msg[msgLen] = '\n';
msg[msgLen + 1] = '\0';
fputs(msg, file);
fflush(file);
return;
}
void tftp12ClientLogMsg(char *format, ...)
{
va_list ap;
va_start(ap, format);
if (TFTP12LogInfo.client.enable == TRUE)
{
//semTake()
tftp12LogMsgMain(TFTP12LogInfo.client.file, format, ap);
//semGive();
}
va_end(ap);
}
void tftp12ServerLogMsg(char *format, ...)
{
va_list ap;
va_start(ap, format);
if (TFTP12LogInfo.server.enable == TRUE)
{
//semTake()
tftp12LogMsgMain(TFTP12LogInfo.server.file, format, ap);
//semGive();
}
va_end(ap);
}
void tftp12ServerLogTask(void *arg)
{
}