1 /* |
|
2 * Copyright (c) 2010 Ixonos Plc. |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of the "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - Initial contribution |
|
11 * |
|
12 * Contributors: |
|
13 * Ixonos Plc |
|
14 * |
|
15 * Description: |
|
16 * |
|
17 */ |
|
18 |
|
19 |
|
20 |
|
21 #include <hal.h> |
|
22 #include <charconv.h> |
|
23 #include <bautils.h> |
|
24 #include "logfile.h" |
|
25 #include "logfile.pan" |
|
26 |
|
27 _LIT8(KCrLf8, "\r\n"); |
|
28 _LIT(KCrLf, "\r\n"); |
|
29 |
|
30 static const TInt KAsciiStart = 0x20; |
|
31 static const TInt KAsciiEnd = 0x7f; |
|
32 static const TInt KHexCharLeft = '<'; |
|
33 static const TInt KHexCharRight = '>'; |
|
34 |
|
35 //static const TInt KNumberOfDecimalPlaces = 3; |
|
36 |
|
37 EXPORT_C CLogFile* CLogFile::NewL(const TDesC& aFileName, TBool aInitialiseLog) |
|
38 { |
|
39 CLogFile* self = NewLC(aFileName, aInitialiseLog); |
|
40 CleanupStack::Pop(self); |
|
41 return(self); |
|
42 } |
|
43 |
|
44 |
|
45 EXPORT_C CLogFile* CLogFile::NewLC(const TDesC& aFileName, TBool aInitialiseLog) |
|
46 { |
|
47 CLogFile* self = new (ELeave) CLogFile(); |
|
48 CleanupStack::PushL(self); |
|
49 self->ConstructL(aFileName, aInitialiseLog); |
|
50 return(self); |
|
51 } |
|
52 |
|
53 |
|
54 CLogFile::CLogFile() |
|
55 { |
|
56 // No implementation required |
|
57 } |
|
58 |
|
59 |
|
60 EXPORT_C CLogFile::~CLogFile() |
|
61 { |
|
62 iLogFile.Flush(); |
|
63 iLogFile.Close(); |
|
64 iSession.Close(); |
|
65 } |
|
66 |
|
67 |
|
68 void CLogFile::ConstructL(const TDesC& aFileName, TBool aInitialiseLog) |
|
69 { |
|
70 |
|
71 #ifdef ORIGINAL_TIMESTAMP |
|
72 TInt period; |
|
73 User::LeaveIfError(HAL::Get(HALData::ESystemTickPeriod, period)); |
|
74 |
|
75 iLogMillisecsPerTick = period / 1000; |
|
76 |
|
77 if (iLogMillisecsPerTick == 0) |
|
78 { |
|
79 iLogMillisecsPerTick = 1; |
|
80 } |
|
81 #endif |
|
82 |
|
83 User::LeaveIfError(iSession.Connect()); |
|
84 |
|
85 if (aInitialiseLog) |
|
86 { |
|
87 User::LeaveIfError(iLogFile.Replace(iSession, aFileName, EFileShareAny | EFileWrite)); |
|
88 } |
|
89 else |
|
90 { |
|
91 TInt err = iLogFile.Open(iSession, aFileName, EFileShareAny | EFileWrite); |
|
92 |
|
93 switch (err) |
|
94 { |
|
95 case KErrNone: // Opened ok, so seek to end of file |
|
96 { |
|
97 TInt position = 0; |
|
98 User::LeaveIfError(iLogFile.Seek(ESeekEnd, position)); |
|
99 } |
|
100 break; |
|
101 |
|
102 case KErrNotFound: // File doesn't exist, so create it |
|
103 User::LeaveIfError(iLogFile.Create(iSession, aFileName, EFileShareAny | EFileWrite)); |
|
104 break; |
|
105 |
|
106 default: // Unexepected error |
|
107 User::Leave(err); |
|
108 break; |
|
109 } |
|
110 } |
|
111 } |
|
112 |
|
113 |
|
114 EXPORT_C void CLogFile::LogTime() |
|
115 { |
|
116 StartWrite(); |
|
117 LogTimeInternal(); |
|
118 EndWrite(); |
|
119 } |
|
120 |
|
121 |
|
122 EXPORT_C void CLogFile::Log(const TDesC8& aText) |
|
123 { |
|
124 StartWrite(); |
|
125 LogTextInternal(aText); |
|
126 EndWrite(); |
|
127 } |
|
128 |
|
129 |
|
130 EXPORT_C void CLogFile::Log(const TDesC& aText) |
|
131 { |
|
132 StartWrite(); |
|
133 |
|
134 TRAP_IGNORE( DoLogTextL(aText) ); |
|
135 |
|
136 EndWrite(); |
|
137 } |
|
138 |
|
139 void CLogFile::DoLogTextL(const TDesC& aText) |
|
140 { |
|
141 // Create character converter |
|
142 CCnvCharacterSetConverter* characterConverter = CCnvCharacterSetConverter::NewLC(); |
|
143 CCnvCharacterSetConverter::TAvailability converterAvailability; |
|
144 converterAvailability = characterConverter->PrepareToConvertToOrFromL(KCharacterSetIdentifierAscii, iSession); |
|
145 |
|
146 for (TInt i = 0; i < aText.Length(); i++) |
|
147 { |
|
148 if (aText.Mid(i).Find(KCrLf) == 0) |
|
149 { |
|
150 LogNewline(); |
|
151 i++; |
|
152 } |
|
153 else if (converterAvailability == CCnvCharacterSetConverter::EAvailable) |
|
154 { |
|
155 // Convert character from unicode |
|
156 TBuf<1> unicodeBuffer; |
|
157 TBuf8<10> asciiBuffer; |
|
158 |
|
159 unicodeBuffer.Append(aText[i]); |
|
160 TInt status = characterConverter->ConvertFromUnicode(asciiBuffer, unicodeBuffer); |
|
161 |
|
162 if (status >= 0) |
|
163 { |
|
164 LogTextInternal(asciiBuffer); |
|
165 } |
|
166 } |
|
167 else // character converter not available |
|
168 { |
|
169 TBuf8<1> asciiBuffer; |
|
170 asciiBuffer.Append(static_cast<TUint8>(aText[i])); |
|
171 LogTextInternal(asciiBuffer); |
|
172 } |
|
173 } |
|
174 |
|
175 CleanupStack::PopAndDestroy(characterConverter); |
|
176 } |
|
177 |
|
178 EXPORT_C void CLogFile::Log(TUint8 aByte) |
|
179 { |
|
180 StartWrite(); |
|
181 LogByteInternal(aByte); |
|
182 EndWrite(); |
|
183 } |
|
184 |
|
185 |
|
186 EXPORT_C void CLogFile::Log(TUint aNumber) |
|
187 { |
|
188 StartWrite(); |
|
189 LogIntInternal(aNumber); |
|
190 EndWrite(); |
|
191 } |
|
192 |
|
193 |
|
194 EXPORT_C void CLogFile::LogBytes(const TDesC8& aBuffer) |
|
195 { |
|
196 StartWrite(); |
|
197 |
|
198 for (TInt i = 0; i < aBuffer.Length(); i++) |
|
199 { |
|
200 LogByteInternal(aBuffer[i]); |
|
201 } |
|
202 |
|
203 EndWrite(); |
|
204 } |
|
205 |
|
206 |
|
207 void CLogFile::LogTimeInternal() |
|
208 { |
|
209 TBuf8<50> text; |
|
210 |
|
211 #ifdef ORIGINAL_TIMESTAMP |
|
212 |
|
213 TInt timeInMillisecs = User::TickCount() * iLogMillisecsPerTick; |
|
214 TInt secs = timeInMillisecs / 1000; |
|
215 TInt millisecs = timeInMillisecs % 1000; |
|
216 text.Num(secs); |
|
217 text.Append('.'); |
|
218 Write(text); |
|
219 text.Num(millisecs); |
|
220 |
|
221 while (text.Length() < KNumberOfDecimalPlaces) |
|
222 { |
|
223 text.Insert(0, _L8("0")); |
|
224 } |
|
225 |
|
226 text.Append('-'); |
|
227 Write(text); |
|
228 |
|
229 #else |
|
230 |
|
231 TTime time; |
|
232 time.HomeTime(); |
|
233 TBuf<31> dateString; |
|
234 _LIT(KDateString4,"%-B%:0%J%:1%T%:2%S%.%*C4%:3%+B "); |
|
235 TRAPD(err, time.FormatL(dateString,KDateString4) ); |
|
236 if (KErrNone == err) |
|
237 { |
|
238 text.Append(dateString); |
|
239 } |
|
240 else |
|
241 { |
|
242 text.Append( _L("### date string format error: ") ); |
|
243 text.AppendNum(err); |
|
244 } |
|
245 Write(text); |
|
246 |
|
247 #endif // ORIGINAL_TIMESTAMP |
|
248 } |
|
249 |
|
250 |
|
251 void CLogFile::LogTextInternal(const TDesC8& aText) |
|
252 { |
|
253 TPtrC8 tail(aText.Ptr(), aText.Length()); |
|
254 |
|
255 TInt newLinePosition = tail.Find(KCrLf8); |
|
256 while (newLinePosition != KErrNotFound) |
|
257 { |
|
258 if (newLinePosition > 0) |
|
259 { |
|
260 Write(tail.Left(newLinePosition)); |
|
261 tail.Set(aText.Ptr() + newLinePosition, tail.Length() - newLinePosition); |
|
262 } |
|
263 LogNewline(); |
|
264 tail.Set(aText.Ptr() + KCrLf8.iTypeLength, tail.Length() - KCrLf8.iTypeLength); |
|
265 |
|
266 newLinePosition = tail.Find(KCrLf8); |
|
267 } |
|
268 |
|
269 // No more newlines left so print remainder |
|
270 Write(tail); |
|
271 |
|
272 } |
|
273 |
|
274 |
|
275 void CLogFile::LogByteInternal(TUint8 aByte) |
|
276 { |
|
277 if ((aByte >= KAsciiStart) && (aByte < KAsciiEnd)) |
|
278 { |
|
279 // Display as ASCII char |
|
280 TBuf8<1> str; |
|
281 str.Append(aByte); |
|
282 Write(str); |
|
283 } |
|
284 else |
|
285 { |
|
286 // Display as hex number |
|
287 TBuf8<4> str; |
|
288 str.Append(KHexCharLeft); |
|
289 str.AppendNum((TUint)aByte, EHex); |
|
290 str.Append(KHexCharRight); |
|
291 Write(str); |
|
292 } |
|
293 } |
|
294 |
|
295 |
|
296 void CLogFile::LogIntInternal(TUint aNumber) |
|
297 { |
|
298 // Display as ASCII char |
|
299 TBuf8<20> str; |
|
300 str.Append(KHexCharLeft); |
|
301 str.AppendNum(aNumber, EHex); |
|
302 str.Append(KHexCharRight); |
|
303 Write(str); |
|
304 } |
|
305 |
|
306 |
|
307 EXPORT_C void CLogFile::LogNewline() |
|
308 { |
|
309 Write(KCrLf8); |
|
310 |
|
311 if (iAutoTimestamp) |
|
312 { |
|
313 LogTimeInternal(); |
|
314 } |
|
315 } |
|
316 |
|
317 |
|
318 void CLogFile::StartWrite() |
|
319 { |
|
320 ASSERT(iCheckNestDepth == 0); |
|
321 iCheckNestDepth++; |
|
322 |
|
323 if (iAutoNewline) |
|
324 { |
|
325 LogNewline(); |
|
326 } |
|
327 } |
|
328 |
|
329 |
|
330 void CLogFile::EndWrite() |
|
331 { |
|
332 if (iAutoFlush) |
|
333 { |
|
334 iLogFile.Flush(); |
|
335 } |
|
336 |
|
337 iCheckNestDepth--; |
|
338 ASSERT(iCheckNestDepth == 0); |
|
339 } |
|
340 |
|
341 void CLogFile::Write(const TDesC8& aText) |
|
342 { |
|
343 |
|
344 if (iLogFile.Write(aText) != KErrNone) |
|
345 { |
|
346 // As the framework may be trapping User::Panic we need to |
|
347 // produce the panic at a lower level. |
|
348 RThread().Panic(KLogFilePanic, ELogFileWriteFailed); |
|
349 } |
|
350 } |
|
351 |
|
352 EXPORT_C void CLogFile::SetAutoFlush(TBool aOn) |
|
353 { |
|
354 iAutoFlush = aOn; |
|
355 } |
|
356 |
|
357 |
|
358 EXPORT_C void CLogFile::SetAutoTimeStamp(TBool aOn) |
|
359 { |
|
360 iAutoTimestamp = aOn; |
|
361 } |
|
362 |
|
363 |
|
364 EXPORT_C void CLogFile::SetAutoNewline(TBool aOn) |
|
365 { |
|
366 iAutoNewline = aOn; |
|
367 } |
|
368 |
|
369 |
|
370 EXPORT_C void CLogFile::StaticLog(const TDesC& aFileName, const TDesC8& aText) |
|
371 { |
|
372 // This needs to be inside a TRAP statement. Calling StaticLogL |
|
373 // from certain places, for example AppUi destructors, |
|
374 // would result in E32USER-CBase 66 panic. |
|
375 TRAP_IGNORE( CLogFile::StaticLogL(aFileName,aText) ); |
|
376 } |
|
377 |
|
378 |
|
379 EXPORT_C void CLogFile::StaticLog(const TDesC& aFileName, const TDesC& aText) |
|
380 { |
|
381 // This needs to be inside a TRAP statement. Calling StaticLogL |
|
382 // from certain places, for example AppUi destructors, |
|
383 // would result in E32USER-CBase 66 panic. |
|
384 TRAP_IGNORE( CLogFile::StaticLogL(aFileName,aText) ); |
|
385 } |
|
386 |
|
387 |
|
388 EXPORT_C void CLogFile::StaticLogL(const TDesC& aFileName, const TDesC8& aText) |
|
389 { |
|
390 CLogFile* logFile = NewLC(aFileName, EFalse); |
|
391 logFile->SetAutoNewline(ETrue); |
|
392 logFile->SetAutoTimeStamp(ETrue); |
|
393 logFile->Log(aText); |
|
394 CleanupStack::PopAndDestroy(logFile); |
|
395 } |
|
396 |
|
397 |
|
398 EXPORT_C void CLogFile::StaticLogL(const TDesC& aFileName, const TDesC& aText) |
|
399 { |
|
400 CLogFile* logFile = NewLC(aFileName, EFalse); |
|
401 logFile->SetAutoNewline(ETrue); |
|
402 logFile->SetAutoTimeStamp(ETrue); |
|
403 logFile->Log(aText); |
|
404 CleanupStack::PopAndDestroy(logFile); |
|
405 } |
|
406 |
|
407 void CLogFile::GetFileName(TDes& aFileName) const |
|
408 { |
|
409 iLogFile.FullName(aFileName); |
|
410 } |
|
411 |
|
412 |
|
413 // End of File |
|