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