-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLogger.cpp
279 lines (229 loc) · 6.07 KB
/
Logger.cpp
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
274
275
276
277
278
279
/*
* Logger.cpp
*
* Created on: 01.09.2019
* Author: artem78
*/
#include "Logger.h"
#include <utf.h>
#if LOGGING_ENABLED
_LIT16(KLineBreak, "\r\n");
_LIT16(KTab, "\t");
/* CLogger */
CLogger::CLogger(RFile &aFile, TUint aLoggingLevels,
TOutputEncoding anOutputEncoding, TBool anAutoFlush)
: iLoggingLevels(aLoggingLevels),
iOutputEncoding(anOutputEncoding),
iAutoFlush(anAutoFlush)
{
// Leave iFileBuf buffer size as default (4Kb)
iFileBuf.Attach(aFile, 0);
}
CLogger::~CLogger()
{
iFileBuf.Synch/*L*/();
iFileBuf.Close();
if (this == LoggerStatic::iLogger)
LoggerStatic::SetLogger(NULL);
}
CLogger* CLogger::NewLC(RFile &aFile, TUint aLoggingLevels,
TOutputEncoding anOutputEncoding, TBool anAutoFlush)
{
CLogger* self = new (ELeave) CLogger(aFile, aLoggingLevels, anOutputEncoding,
anAutoFlush);
CleanupStack::PushL(self);
self->ConstructL();
return self;
}
CLogger* CLogger::NewL(RFile &aFile, TUint aLoggingLevels,
TOutputEncoding anOutputEncoding, TBool anAutoFlush)
{
CLogger* self = CLogger::NewLC(aFile, aLoggingLevels, anOutputEncoding, anAutoFlush);
CleanupStack::Pop(); // self;
return self;
}
void CLogger::ConstructL()
{
}
void CLogger::WriteL(const TDesC16 &aModule, const TDesC16 &aDes, TLoggingLevel aLoggingLevel)
{
// Check reporting level
if (aLoggingLevel != ELevelUnknown && !(aLoggingLevel & iLoggingLevels))
return;
// Strings
_LIT16(KTimeFormat, "%H:%T:%S.%*C3");
_LIT16(KOpeningBracket, "(");
_LIT16(KClosingBracket, ")");
_LIT16(KOpeningSquareBracket, "[");
_LIT16(KClosingSquareBracket, "]");
_LIT16(KThreeSpaces, " ");
_LIT16(KDebugText, "Debug");
_LIT16(KInfoText, "Info");
_LIT16(KWarningText, "Warning");
_LIT16(KErrorText, "Error");
// Print current time
TBuf16<20> timeBuff;
TTime time;
time.HomeTime();
time.FormatL(timeBuff, KTimeFormat);
WriteToFileL(timeBuff);
// Print message level
if (aLoggingLevel != ELevelUnknown)
{
WriteToFileL(KTab);
WriteToFileL(KOpeningBracket);
if (aLoggingLevel & ELevelDebug)
WriteToFileL(KDebugText);
else if (aLoggingLevel & ELevelInfo)
WriteToFileL(KInfoText);
else if (aLoggingLevel & ELevelWarning)
WriteToFileL(KWarningText);
else if (aLoggingLevel & ELevelError)
WriteToFileL(KErrorText);
WriteToFileL(KClosingBracket);
}
// Print module/class/function name
WriteToFileL(KTab);
WriteToFileL(KOpeningSquareBracket);
WriteToFileL(aModule);
WriteToFileL(KClosingSquareBracket);
WriteToFileL(KThreeSpaces);
// Print message
WriteToFileL(aDes);
WriteToFileL(KLineBreak);
if (iAutoFlush)
iFileBuf.SynchL();
}
void CLogger::WriteFormatL(const TDesC16 &aModule, TRefByValue<const TDesC16> aFmt, ...)
{
VA_LIST list;
VA_START(list, aFmt);
WriteFormatListL(aModule, aFmt, list, ETrue);
// VA_END(list); // As I understand it, this is not necessary
}
void CLogger::WriteFormatListL(const TDesC16 &aModule, const TDesC16 &aFmt, VA_LIST aList, TBool anIsDes16, TLoggingLevel aLoggingLevel)
{
const TInt KDefaultBufferSize = 4 * KKilo; // 4096 chars
RBuf16 buff;
buff.CreateL(KDefaultBufferSize);
buff.CleanupClosePushL();
if (anIsDes16)
{ // 16 bit
buff.FormatList(aFmt, aList);
}
else
{ // 8 bit
RBuf8 buff8;
TInt r = buff8.Create(KDefaultBufferSize);
if (r == KErrNone)
{
RBuf8 fmt8;
r = fmt8.Create(aFmt.Length());
if (r == KErrNone)
{
fmt8.Copy(aFmt);
buff8.FormatList(fmt8, aList);
fmt8.Close();
}
buff.Copy(buff8);
buff8.Close();
}
}
WriteL(aModule, buff, aLoggingLevel);
CleanupStack::PopAndDestroy(&buff);
}
//void CLogger::WriteEmptyLine()
// {
// iFile.WriteL(KLineBreak);
// }
void CLogger::WriteToFileL(const TDesC16 &aDes)
{
//__ASSERT_ALWAYS(aDes.Length(), return);
if (!aDes.Length())
return;
RBuf8 buff8;
TRequestStatus stat;
switch (iOutputEncoding)
{
case EASCII:
{
buff8.CreateL(aDes.Length());
buff8.CleanupClosePushL();
buff8.Copy(aDes);
}
break;
case EUnicode:
{
TPtrC8 writeBuf((const TUint8*)aDes.Ptr(),aDes.Size());
buff8.CreateL(writeBuf.Length());
buff8.CleanupClosePushL();
buff8.Copy(writeBuf);
}
break;
case EUtf8:
{
buff8.CreateL(aDes.Size()); // Not length! Max utf8 string size.
buff8.CleanupClosePushL();
CnvUtfConverter::ConvertFromUnicodeToUtf8(buff8, aDes);
}
break;
default:
break;
}
iFileBuf.WriteL(buff8, stat);
User::WaitForRequest(stat);
//iFileBuff.Synch/*L*/();
CleanupStack::PopAndDestroy(&buff8);
}
/* LoggerStatic */
CLogger* LoggerStatic::iLogger = NULL;
void LoggerStatic::SetLogger(CLogger* aLogger)
{
iLogger = aLogger;
}
void LoggerStatic::WriteFormat(CLogger::TLoggingLevel aLoggingLevel,
const TDesC8 &aModule, TRefByValue<const TDesC16> aFmt, ...)
{
// Deny write to not configured logger
if (!iLogger)
return;
RBuf16 module16;
TInt r = module16.Create(aModule.Length());
// Pushing to cleanup stack not needed
if (r != KErrNone)
return;
module16.Copy(aModule);
VA_LIST list;
VA_START(list, aFmt);
TRAP_IGNORE(iLogger->WriteFormatListL(module16, aFmt, list, ETrue, aLoggingLevel));
VA_END(list);
module16.Close();
}
void LoggerStatic::WriteFormat(CLogger::TLoggingLevel aLoggingLevel,
const TDesC8 &aModule, TRefByValue<const TDesC8> aFmt, ...)
{
// Deny write to not configured logger
if (!iLogger)
return;
RBuf16 module16;
TInt r = module16.Create(aModule.Length());
// Pushing to cleanup stack not needed
if (r != KErrNone)
return;
module16.Copy(aModule);
const TDesC8& fmt8 = aFmt;
RBuf16 fmt16;
r = fmt16.Create(fmt8.Length());
if (r == KErrNone)
{
fmt16.Copy(fmt8);
VA_LIST list;
VA_START(list, aFmt);
//WriteFormat(aLoggingLevel, aModule, fmt16, list);
TRAP_IGNORE(iLogger->WriteFormatListL(module16, fmt16, list, EFalse, aLoggingLevel));
VA_END(list);
fmt16.Close();
}
module16.Close();
}
#endif