-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlog_process.c
More file actions
73 lines (58 loc) · 1.21 KB
/
log_process.c
File metadata and controls
73 lines (58 loc) · 1.21 KB
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
#include "log_process.h"
#define NEW_LOG_FILE_NAME "eventlog.txt"
#define OLD_LOG_FILE_NAME "eventlogbak.txt"
FILE *m_pFd;
unsigned long get_file_size(FILE *fp)
{
unsigned long file_size = 0;
file_size = ftell(fp);
return file_size;
}
void EventLogFileOpen(void)
{
m_pFd = fopen(NEW_LOG_FILE_NAME, "a+");
}
int PrintEventLog(const char *pcEventSource, char *pcStrBuff)
{
int ret;
char *pTimeStr;
EventLogFileSizeJudge();
ret = fprintf(m_pFd, "####Time:%s[%s]: %s\n", "11:22:33",
pcEventSource, pcStrBuff);
int f_ret = fflush(m_pFd);
if(f_ret != 0)
{
printf("f_ret=%d,pid=%d,\n",f_ret, getpid() );
perror("fflush");
sleep(5000);
}
}
void EventLogFileSizeJudge(void)
{
int iFileSize = 0;
int ret;
int m_iFileMaxSize = 50000;
FILE *pTemp;
iFileSize = ftell(m_pFd);
if (iFileSize > m_iFileMaxSize)
{
fclose(m_pFd);//close NEW_LOG_FILE_NAME
ret = rename(NEW_LOG_FILE_NAME, OLD_LOG_FILE_NAME);
if (0 == ret) {
EventLogFileOpen();
}
else {
perror("rename");
}
}
}
int Print_PID(int pid)
{
char StrBuff[100];
memset(StrBuff,0,sizeof(StrBuff));
sprintf(StrBuff, "pid=%d", getpid());
char buf2[500];
sprintf(buf2, "pid=%d", pid);
PrintEventLog(StrBuff, buf2 );
usleep(500);
}