-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLogWriter.cpp
More file actions
201 lines (179 loc) · 3.77 KB
/
LogWriter.cpp
File metadata and controls
201 lines (179 loc) · 3.77 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
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
/**************************************************************************
* Copyright (C), AirM2M Tech. Co., Ltd.
*
* Name: LogWriter.cpp
* Author: panjun
* Version: V0.1
* Date: 2016/10/12
*
* Description:
* Implement 'LogWriter' class.
* History:
* panjun 16/10/12 Initially create file.
**************************************************************************/
#include "stdafx.h"
#include "share.h"
#define MAXFRACT 10000
#define NumFract 4
#define LOG_POOL_LENGTH 2048
static CHAR logpool[LOG_POOL_LENGTH] = {0};
LogWriter* LogWriter::theOnlyLogWriter = NULL;
LogWriter::LogWriter()
{
// determine the log file's name, yyyyMMddhhmmss.log
SYSTEMTIME st;
::GetLocalTime(&st);
::sprintf_s(this->fileName,1024,"AMLuaDebug.log", st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond);
fp = ::_fsopen(this->fileName, "w", _SH_DENYNO); // Permits read and write access
memset(logpool, 0, sizeof(logpool));
}
void itof(char **buf, int i)
{
char *s;
#define LEN 20
int rem, j;
static char rev[LEN+1];
rev[LEN] = 0;
s = &rev[LEN];
for (j= 0 ; j < NumFract ; j++)
{
rem = i % 10;
*--s = rem + '0';
i /= 10;
}
while (*s)
{
(*buf)[0] = *s++;
++(*buf);
}
}
void itoa(CHAR **buf, int val, int radix)
{
CHAR *str;
str = _itoa(val, *buf, radix);
(*buf) += strlen(str);
}
void LogWriter::dbg_print(CHAR *buf, char *fmt, va_list ap)
{
double dval;
int ival;
char *p, *sval;
char *bp, cval;
int fract;
bp= buf;
*bp= 0;
for (p= fmt; *p; p++)
{
if (*p != '%')
{
*bp++= *p;
continue;
}
switch (*++p) {
case 'd':
ival= va_arg(ap, int);
if (ival < 0){
*bp++= '-';
ival= -ival;
}
itoa(&bp, ival, 10);
break;
case 'o':
ival= va_arg(ap, int);
if (ival < 0){
*bp++= '-';
ival= -ival;
}
*bp++= '0';
itoa(&bp, ival, 8);
break;
case 'x':
ival= va_arg(ap, int);
if (ival < 0){
*bp++= '-';
ival= -ival;
}
*bp++= '0';
*bp++= 'x';
itoa(&bp, ival, 16);
break;
case 'c':
cval= va_arg(ap, int);
*bp++= cval;
break;
case 'f':
dval= va_arg(ap, double);
if (dval < 0){
*bp++= '-';
dval= -dval;
}
if (dval >= 1.0)
itoa(&bp, (int)dval, 10);
else
*bp++= '0';
*bp++= '.';
fract= (int)((dval- (double)(int)dval)*(double)(MAXFRACT));
itof(&bp, fract);
break;
case 's':
for (sval = va_arg(ap, char *) ; *sval ; sval++ )
*bp++= *sval;
break;
}
}
*bp= 0;
}
void LogWriter::LOGX(CHAR *fmt,...)
{
va_list ap;
CHAR buffer[1024];
SYSTEMTIME st;
va_start (ap, fmt);
::GetLocalTime(&st);
memset(buffer, 0, sizeof(buffer));
sprintf_s(buffer,1024,"%04d-%02d-%02d %02d:%02d:%02d ", \
st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond);
GetLogWriter()->dbg_print(buffer+strlen(buffer), fmt, ap);
strcat_s(buffer,sizeof(buffer),"\n");
if(LOG_POOL_LENGTH - strlen(logpool) - 1 > strlen(buffer))
{
strcat_s(logpool+strlen(logpool), sizeof(logpool), buffer);
} else {
LogWriter::GetLogWriter()->Log(logpool);
memset(logpool, 0, sizeof(logpool));
strcat_s(logpool, buffer);
}
va_end(ap);
}
void LogWriter::Log(CHAR* log)
{
if (fp != NULL)
{
::fprintf(this->fp, "%s", log);
::fflush(this->fp);
}
}
LogWriter::~LogWriter()
{
if (this->fp != NULL)
{
GetLogWriter()->Log(logpool);
::fclose(this->fp);
}
}
LogWriter* LogWriter::GetLogWriter()
{
if (theOnlyLogWriter == NULL)
{
theOnlyLogWriter = new LogWriter();
}
return theOnlyLogWriter;
}
void LogWriter::DeleteLogWriter()
{
if (theOnlyLogWriter != NULL)
{
delete theOnlyLogWriter;
theOnlyLogWriter = NULL;
}
}