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