|
1 // Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
2 // All rights reserved. |
|
3 // This component and the accompanying materials are made available |
|
4 // under the terms of "Eclipse Public License v1.0" |
|
5 // which accompanies this distribution, and is available |
|
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
7 // |
|
8 // Initial Contributors: |
|
9 // Nokia Corporation - initial contribution. |
|
10 // |
|
11 // Contributors: |
|
12 // |
|
13 // Description: |
|
14 // |
|
15 |
|
16 /** |
|
17 @file |
|
18 */ |
|
19 |
|
20 #include <f32file.h> |
|
21 #include <flogger.h> |
|
22 #include "FLOGSTD.H" |
|
23 |
|
24 /** |
|
25 Macro for Single blank space. |
|
26 |
|
27 */ |
|
28 #define BLANK _S("") |
|
29 |
|
30 const TInt KHexDumpWidth=16; |
|
31 |
|
32 /** |
|
33 Literal constant that hold a string and unicode hex. |
|
34 |
|
35 */ |
|
36 _LIT(KFirstFormatString,"%s%04x : "); |
|
37 |
|
38 /** |
|
39 Literal constant that hold a string and unicode hex. |
|
40 |
|
41 */ |
|
42 _LIT(KSecondFormatString,"%02x "); |
|
43 |
|
44 /** |
|
45 Character Constant. |
|
46 |
|
47 */ |
|
48 _LIT(KThirdFormatString,"%c"); |
|
49 |
|
50 /** |
|
51 Literal Constant for 3 blank spaces. |
|
52 |
|
53 */ |
|
54 _LIT(KThreeSpaces," "); |
|
55 |
|
56 /** |
|
57 Literal Constant for 2 blank spaces. |
|
58 |
|
59 */ |
|
60 _LIT(KTwoSpaces," "); |
|
61 |
|
62 |
|
63 IMPORT_C extern const TBool KFloggerSTI; |
|
64 |
|
65 /* |
|
66 RFileLogger class definition |
|
67 */ |
|
68 |
|
69 EXPORT_C RFileLogger::RFileLogger() |
|
70 : RSessionBase(), iFormatter(), iLastError(KErrNone), iLogSTI(KFloggerSTI) |
|
71 /** Creates a default RFileLogger object. */ |
|
72 {} |
|
73 |
|
74 EXPORT_C RFileLogger::~RFileLogger() |
|
75 /** Empty destructor. |
|
76 Clients with open sessions must end the session by calling Close() beforehand. |
|
77 Note that it does not delete the log file.*/ |
|
78 {} |
|
79 |
|
80 EXPORT_C TVersion RFileLogger::Version() const |
|
81 /** Returns the version number. |
|
82 |
|
83 @return The version number. */ |
|
84 { |
|
85 |
|
86 return(TVersion(KFLogSrvMajorVersionNumber,KFLogSrvMinorVersionNumber,KFLogSrvBuildVersionNumber)); |
|
87 } |
|
88 |
|
89 EXPORT_C TInt RFileLogger::Connect() |
|
90 /** Connects to the file logger server with the default number of message slots, which is 8. |
|
91 |
|
92 This function does not need to be called if you are using the static versions |
|
93 of Write(), WriteFormat() or HexDump(). |
|
94 |
|
95 @return KErrNone if successful, otherwise one of the system-wide error codes. */ |
|
96 { |
|
97 |
|
98 TInt ret=DoConnect(); |
|
99 if (ret==KErrNotFound) |
|
100 { |
|
101 ret=FLogger::Start(); |
|
102 if (ret==KErrNone || ret==KErrAlreadyExists) |
|
103 ret=DoConnect(); |
|
104 } |
|
105 return ret; |
|
106 } |
|
107 |
|
108 EXPORT_C void RFileLogger::SetDateAndTime(TBool aUseDate,TBool aUseTime) |
|
109 /** Specifies whether time and/or date should be appended to log data. |
|
110 |
|
111 Appending of the time and date to log entries can be switched on and off |
|
112 at anytime between creation and destruction of RFileLogger. |
|
113 |
|
114 This function does not need to be called if you are using the static versions |
|
115 of Write(), WriteFormat() or HexDump(). |
|
116 |
|
117 @param aUseDate Use ETrue to log the date, otherwise EFalse. |
|
118 @param aUseTime Use ETrue to log the time, otherwise EFalse. */ |
|
119 { |
|
120 |
|
121 iFormatter.SetDateAndTime(aUseDate,aUseTime); |
|
122 } |
|
123 |
|
124 EXPORT_C void RFileLogger::CreateLog(const TDesC& aDir, const TDesC& aName, TFileLoggingMode aMode) |
|
125 /** Creates or opens a file for logging. |
|
126 |
|
127 When specifying a directory for logging only specify the relative path to |
|
128 'C:\\Logs\\' and do not append a '\\' to the end of the path either. CreateLog() |
|
129 only creates the specified log file if the directory exists. This means that |
|
130 switching logging on and off can be achieved without having to re-compile, |
|
131 by just removing or renaming the log directory. |
|
132 |
|
133 aLogFile.iValid is set according to whether logging will actually occur or not. |
|
134 |
|
135 @param aDir The directory relative to 'C:\\Logs\\' where the log file resides |
|
136 or is to be created. |
|
137 @param aName The name of the log file. |
|
138 @param aMode The mode in which the log file should be opened, either EAppend |
|
139 or EOverwrite. */ |
|
140 { |
|
141 |
|
142 iLogFile.Set(aDir,aName,aMode); |
|
143 TPckg<TLogFile> logFilePckg(iLogFile); |
|
144 iLastError=SendReceive(ECreateLog, TIpcArgs( &logFilePckg)); |
|
145 |
|
146 if (iLastError!=KErrNone && !LogSTI()) |
|
147 iLogFile.SetValid(EFalse); |
|
148 } |
|
149 |
|
150 EXPORT_C void RFileLogger::CloseLog() |
|
151 /** Closes the log file, iLogFile. |
|
152 |
|
153 This function closes the log file previously created or opened by CreateLog(). */ |
|
154 { |
|
155 TPckg<TLogFile> logFilePckg(iLogFile); |
|
156 iLastError=SendReceive(ECloseLog,TIpcArgs( &logFilePckg)); |
|
157 } |
|
158 |
|
159 EXPORT_C TBool RFileLogger::LogValid() const |
|
160 /** Returns the status of the log. |
|
161 |
|
162 @return ETrue if the log file is valid; otherwise EFalse. */ |
|
163 { |
|
164 |
|
165 return iLogFile.Valid(); |
|
166 } |
|
167 |
|
168 EXPORT_C TInt RFileLogger::LastError() const |
|
169 /** Returns the last error status that has been set. |
|
170 |
|
171 @return This value can be set to any of the valid error codes from any of |
|
172 the functions in this class. */ |
|
173 { |
|
174 |
|
175 return iLastError; |
|
176 } |
|
177 |
|
178 EXPORT_C TBool RFileLogger::LogSTI() const |
|
179 /** Returns patchable constant value. |
|
180 |
|
181 @return This value specify the logging output. The default value EFalse |
|
182 cause that the logs are stored to filesystem. ETrue intorduced as in CR 1688 |
|
183 is used for logging using RDebug::Print */ |
|
184 { |
|
185 |
|
186 return iLogSTI; |
|
187 } |
|
188 |
|
189 // |
|
190 // 16-bit non-static writes |
|
191 // |
|
192 |
|
193 EXPORT_C void RFileLogger::Write(const TDesC16& aText) |
|
194 /** Writes a string of Unicode characters to an open log, iLogFile, if it is a valid file. |
|
195 |
|
196 Note that the text will be converted to an 8 bit format for the log file. |
|
197 |
|
198 @param aText The Unicode string to write to the open log. */ |
|
199 { |
|
200 |
|
201 if (iLogFile.Valid() || LogSTI()) |
|
202 { |
|
203 TBuf8<KLogBufferSize> buf; |
|
204 iLastError=iFormatter.FormatTextToWritableBuffer(buf,aText); |
|
205 if (iLastError==KErrNone) |
|
206 DoWrite(buf); |
|
207 } |
|
208 } |
|
209 |
|
210 EXPORT_C void RFileLogger::WriteFormat(TRefByValue<const TDesC16> aFmt,...) |
|
211 /** Formats the remaining arguments of the function according to aFmt, and writes the |
|
212 result to the log, iLogFile, if it is a valid file. |
|
213 |
|
214 The format string aFmt contains literal text, embedded with directives, for converting |
|
215 the trailing list of arguments into text. The number and type of arguments is dictated |
|
216 by the structure of the directives in aFmt. |
|
217 |
|
218 Note that the text will be converted to an 8 bit format for the log file. |
|
219 |
|
220 @param aFmt The 16-bit non modifiable descriptor containing the format string. |
|
221 The TRefByValue class provides a constructor which takes a TDesC16 type. */ |
|
222 { |
|
223 |
|
224 VA_LIST list; |
|
225 VA_START(list,aFmt); |
|
226 DoWriteFormat(aFmt,list); |
|
227 } |
|
228 |
|
229 EXPORT_C void RFileLogger::WriteFormat(TRefByValue<const TDesC16> aFmt, VA_LIST& aList) |
|
230 /** Formats the arguments pointed to by aList according to aFmt, and writes the |
|
231 result to the log, iLogFile, if it is a valid file. |
|
232 |
|
233 The format string aFmt contains literal text, embedded with directives, |
|
234 for converting the trailing list of arguments into text. The number and type |
|
235 of arguments pointed to by aList is dictated by the structure of the directives |
|
236 in aFmt. |
|
237 |
|
238 Note that the text will be converted to an 8 bit format for the log file. |
|
239 |
|
240 @param aFmt The 16-bit non modifiable descriptor containing the format string. |
|
241 The TRefByValue class provides a constructor which takes a TDesC16 type. |
|
242 @param aList A pointer to an argument list. */ |
|
243 { |
|
244 |
|
245 DoWriteFormat(aFmt,aList); |
|
246 } |
|
247 |
|
248 // |
|
249 // 8-bit non-static writes |
|
250 // |
|
251 |
|
252 EXPORT_C void RFileLogger::Write(const TDesC8& aText) |
|
253 /** Writes a string of 8-bit characters to an open log, iLogFile, if it is a valid file. |
|
254 |
|
255 @param aText The 8-bit character string to write to the open log. */ |
|
256 { |
|
257 |
|
258 if (iLogFile.Valid() || LogSTI()) |
|
259 { |
|
260 TBuf8<KLogBufferSize> buf; |
|
261 iLastError=iFormatter.FormatTextToWritableBuffer(buf,aText); |
|
262 if (iLastError==KErrNone) |
|
263 DoWrite(buf); |
|
264 } |
|
265 } |
|
266 |
|
267 EXPORT_C void RFileLogger::WriteFormat(TRefByValue<const TDesC8> aFmt,...) |
|
268 /** Formats the remaining arguments of the function according to aFmt, and writes the |
|
269 result to the log, iLogFile, if it is a valid file. |
|
270 |
|
271 The format string aFmt contains literal text, embedded with directives, |
|
272 for converting the trailing list of arguments into text. The number and type |
|
273 of arguments is dictated by the structure of the directives in aFmt. |
|
274 |
|
275 @param aFmt The 8 bit non modifiable descriptor containing the format string. |
|
276 The TRefByValue class provides a constructor which takes a TDesC8 type.*/ |
|
277 { |
|
278 |
|
279 VA_LIST list; |
|
280 VA_START(list,aFmt); |
|
281 DoWriteFormat(aFmt,list); |
|
282 } |
|
283 |
|
284 EXPORT_C void RFileLogger::WriteFormat(TRefByValue<const TDesC8> aFmt, VA_LIST& aList) |
|
285 /** Formats the arguments pointed to by aList according to aFmt, and writes the result |
|
286 to the log, iLogFile, if it is a valid file. |
|
287 |
|
288 The format string aFmt contains literal text, embedded with directives, |
|
289 for converting the trailing list of arguments into text. The number and type |
|
290 of arguments pointed to by aList is dictated by the structure of the directives |
|
291 in aFmt. |
|
292 |
|
293 @param aFmt The 8 bit non modifiable descriptor containing the format string. |
|
294 The TRefByValue class provides a constructor which takes a TDesC8 type. |
|
295 @param aList A pointer to an argument list. */ |
|
296 { |
|
297 |
|
298 DoWriteFormat(aFmt,aList); |
|
299 } |
|
300 |
|
301 // |
|
302 // 16-bit static writes |
|
303 // |
|
304 |
|
305 EXPORT_C void RFileLogger::Write(const TDesC& aDir, const TDesC& aName, TFileLoggingMode aMode, const TDesC16& aText) |
|
306 /** Writes a string of 16-bit characters to an open log. |
|
307 |
|
308 Connects to the logging server, creates/opens the log file and write aText to it. |
|
309 |
|
310 This is a "static write". |
|
311 |
|
312 Note that the text will be converted to an 8 bit format for the log file. |
|
313 |
|
314 @param aDir The directory relative to 'C:\\Logs\\' where the log file resides. |
|
315 @param aName The name of the log file. |
|
316 @param aMode The mode in which the log file should be opened: either EAppend or EOverwrite. |
|
317 @param aText The Unicode string to write to the log. */ |
|
318 { |
|
319 |
|
320 RFileLogger logger; |
|
321 TInt ret=logger.Connect(); |
|
322 if (ret==KErrNone) |
|
323 { |
|
324 logger.SetDateAndTime(ETrue,ETrue); |
|
325 logger.iLogFile.Set(aDir,aName,aMode); |
|
326 TBuf8<KLogBufferSize> buf; |
|
327 ret=logger.iFormatter.FormatTextToWritableBuffer(buf,aText); |
|
328 if (ret==KErrNone) |
|
329 { |
|
330 if (logger.LogSTI()) |
|
331 { |
|
332 logger.DoStaticWrite(buf); |
|
333 } else |
|
334 { |
|
335 TPckg<TLogFile> logFilePckg(logger.iLogFile); |
|
336 logger.SendReceive(ECreateWriteAndCloseLog, TIpcArgs ( &logFilePckg, &buf)); // ignore error |
|
337 } |
|
338 } |
|
339 } |
|
340 logger.Close(); |
|
341 } |
|
342 |
|
343 EXPORT_C void RFileLogger::WriteFormat(const TDesC& aDir, const TDesC& aName, TFileLoggingMode aMode, TRefByValue<const TDesC16> aFmt,...) |
|
344 /** Formats the remaining arguments of the function according to aFmt and writes the |
|
345 result to the log. |
|
346 |
|
347 The format string aFmt contains literal text, embedded with directives, |
|
348 for converting the trailing list of arguments into text. The number and type |
|
349 of arguments is dictated by the structure of the directives in aFmt. |
|
350 |
|
351 Connects to the logging server, creates/opens the log file and writes the text arguments to it. |
|
352 |
|
353 This is a "static write". |
|
354 |
|
355 Note that the text will be converted to an 8 bit format for the log file. |
|
356 |
|
357 @param aDir The directory relative to 'C:\\Logs\\' where the log file resides. |
|
358 @param aName The name of the log file. |
|
359 @param aMode The mode in which the log file should be opened: either EAppend |
|
360 or EOverwrite. |
|
361 @param aFmt The 16 bit non modifiable descriptor containing the format string. |
|
362 The TRefByValue class provides a constructor which takes a TDesC16 type. */ |
|
363 { |
|
364 |
|
365 VA_LIST list; |
|
366 VA_START(list,aFmt); |
|
367 DoStaticWriteFormat(aDir,aName,aMode,aFmt,list); |
|
368 } |
|
369 |
|
370 EXPORT_C void RFileLogger::WriteFormat(const TDesC& aDir, const TDesC& aName, TFileLoggingMode aMode, TRefByValue<const TDesC16> aFmt, VA_LIST& aList) |
|
371 /** Formats the arguments pointed to by aList according to aFmt, and writes the result |
|
372 to the log. |
|
373 |
|
374 The format string aFmt contains literal text, embedded with directives, |
|
375 for converting the trailing list of arguments into text. The number and type |
|
376 of arguments pointed to by aList is dictated by the structure of the directives |
|
377 in aFmt. |
|
378 |
|
379 Connects to the logging server, creates/opens the log file and writes the text arguments to it. |
|
380 |
|
381 This is a "static write". |
|
382 |
|
383 Note that the text will be converted to an 8 bit format for the log file. |
|
384 |
|
385 @param aDir The directory relative to 'C:\\Logs\\' where the log file resides. |
|
386 @param aName The name of the log file. |
|
387 @param aMode The mode in which the log file should be opened: either EAppend |
|
388 or EOverwrite. |
|
389 @param aFmt The 16 bit non modifiable descriptor containing the format string. |
|
390 The TRefByValue class provides a constructor which takes a TDesC16 type. |
|
391 @param aList A pointer to an argument list. */ |
|
392 { |
|
393 |
|
394 DoStaticWriteFormat(aDir,aName,aMode,aFmt,aList); |
|
395 } |
|
396 |
|
397 // |
|
398 // 8-bit static writes |
|
399 // |
|
400 |
|
401 EXPORT_C void RFileLogger::Write(const TDesC& aDir, const TDesC& aName, TFileLoggingMode aMode, const TDesC8& aText) |
|
402 /** Writes a string of 8-bit characters to an open log. |
|
403 |
|
404 Connects to the logging server, creates/opens the log file and writes aText to it. |
|
405 |
|
406 This is a "static write". |
|
407 |
|
408 @param aDir The directory relative to 'C:\\Logs\\' where the log file resides. |
|
409 @param aName The name of the log file. |
|
410 @param aMode The mode in which the log file should be opened: either EAppend |
|
411 or EOverwrite. |
|
412 @param aText The 8-bit string to write to the log. */ |
|
413 |
|
414 { |
|
415 |
|
416 RFileLogger logger; |
|
417 TInt ret=logger.Connect(); |
|
418 if (ret==KErrNone) |
|
419 { |
|
420 logger.SetDateAndTime(ETrue,ETrue); |
|
421 logger.iLogFile.Set(aDir,aName,aMode); |
|
422 // TBuf8<KLogBufferSize> buf; |
|
423 TBuf8<1600> buf; //Want at least an mtu sized buffer |
|
424 //PG 14/08/2002 - If mode is set to *Raw, Don't change format of client buffer |
|
425 if(aMode == EFileLoggingModeAppendRaw || aMode == EFileLoggingModeOverwriteRaw) |
|
426 buf.Copy(aText); |
|
427 else |
|
428 ret=logger.iFormatter.FormatTextToWritableBuffer(buf,aText); |
|
429 if (ret==KErrNone) |
|
430 logger.DoStaticWrite(buf); |
|
431 } |
|
432 logger.Close(); |
|
433 } |
|
434 |
|
435 EXPORT_C void RFileLogger::WriteFormat(const TDesC& aDir, const TDesC& aName, TFileLoggingMode aMode, TRefByValue<const TDesC8> aFmt,...) |
|
436 /** Formats the remaining arguments of the function according to aFmt and writes the |
|
437 result to the log. |
|
438 |
|
439 The format string aFmt contains literal text, embedded with directives, |
|
440 for converting the trailing list of arguments into text. The number and type |
|
441 of arguments is dictated by the structure of the directives in aFmt. |
|
442 |
|
443 Connects to the logging server, creates/opens the log file and writes the text arguments to it. |
|
444 |
|
445 This is a "static write". |
|
446 |
|
447 @param aDir The directory relative to 'C:\\Logs\\' where the log file resides. |
|
448 @param aName The name of the log file. |
|
449 @param aMode The mode in which the log file should be opened: either EAppend |
|
450 or EOverwrite. |
|
451 @param aFmt The 8 bit non modifiable descriptor containing the format string. |
|
452 The TRefByValue class provides a constructor which takes a TDesC8 type. */ |
|
453 { |
|
454 |
|
455 VA_LIST list; |
|
456 VA_START(list,aFmt); |
|
457 DoStaticWriteFormat(aDir,aName,aMode,aFmt,list); |
|
458 } |
|
459 |
|
460 EXPORT_C void RFileLogger::WriteFormat(const TDesC& aDir, const TDesC& aName, TFileLoggingMode aMode, TRefByValue<const TDesC8> aFmt, VA_LIST& aList) |
|
461 /** Formats the arguments pointed to by aList according to aFmt, and writes the |
|
462 result to the log. |
|
463 |
|
464 The format string aFmt contains literal text, embedded with directives, |
|
465 for converting the trailing list of arguments into text. The number and type |
|
466 of arguments pointed to by aList is dictated by the structure of the directives |
|
467 in aFmt. |
|
468 |
|
469 Connects to the logging server, creates/opens the log file and writes the text arguments to it. |
|
470 |
|
471 This is a "static write". |
|
472 |
|
473 @param aDir The directory relative to 'C:\\Logs\\' where the log file resides. |
|
474 @param aName The name of the log file. |
|
475 @param aMode The mode in which the log file should be opened: either EAppend |
|
476 or EOverwrite. |
|
477 @param aFmt The 8 bit non modifiable descriptor containing the format string. |
|
478 The TRefByValue class provides a constructor which takes a TDesC8 type. |
|
479 @param aList A pointer to an argument list. */ |
|
480 { |
|
481 |
|
482 DoStaticWriteFormat(aDir,aName,aMode,aFmt,aList); |
|
483 } |
|
484 |
|
485 // |
|
486 // Hex Dumps |
|
487 // |
|
488 |
|
489 EXPORT_C void RFileLogger::HexDump(const TText* aHeader, const TText* aMargin, const TUint8* aPtr, TInt aLen) |
|
490 /** Writes a hex dump of the specified data to the log. |
|
491 |
|
492 The format of the hex dump entry is a header followed by the hex string of |
|
493 the data followed by any printable characters (non printable characters are |
|
494 substituted with '.'). For example, |
|
495 |
|
496 @code |
|
497 RLog_Ex0000 : 41 42 6C 6B 0A 0A 45 46 20 20 78 7A ABlk..EF |
|
498 xz |
|
499 RL_cont0001 : 43 44 6C 6B 0A 0A 45 46 20 20 78 7A CDlk..EF |
|
500 xz |
|
501 RL_cont0002 : 45 46 6C 6B 0A 0A 47 48 20 20 78 7A EFlk..GH |
|
502 xz |
|
503 . |
|
504 . |
|
505 . |
|
506 @endcode |
|
507 @param aHeader A label for the hex dump entry. The label has a sequence number |
|
508 appended to it automatically. |
|
509 @param aMargin A continuation label if the hex dump exceeds one line. This |
|
510 label is displayed on all subsequent lines after line one and also has a sequence |
|
511 number appended to it. |
|
512 @param aPtr The data that is to be converted to a hex string. |
|
513 @param aLen How many of the characters in aPtr are to be converted. Conversion |
|
514 always starts from position 0 within aPtr. */ |
|
515 { |
|
516 |
|
517 if (iLogFile.Valid() || LogSTI()) |
|
518 DoHexDump(aHeader,aMargin,aPtr,aLen); |
|
519 } |
|
520 |
|
521 EXPORT_C void RFileLogger::HexDump(const TDesC& aDir, const TDesC& aName, TFileLoggingMode aMode, const TText* aHeader, const TText* aMargin, const TUint8* aPtr, TInt aLen) |
|
522 /** Writes a hex dump of the specified data to the log. |
|
523 |
|
524 The format of the hex dump entry is a header followed by the hex string of |
|
525 the data followed by any printable characters (non printable characters are |
|
526 substituted with '.'). For example, |
|
527 |
|
528 @code |
|
529 RLog_Ex0000 : 41 42 6C 6B 0A 0A 45 46 20 20 78 7A ABlk..EF |
|
530 xz |
|
531 RL_cont0001 : 43 44 6C 6B 0A 0A 45 46 20 20 78 7A CDlk..EF |
|
532 xz |
|
533 RL_cont0002 : 45 46 6C 6B 0A 0A 47 48 20 20 78 7A EFlk..GH |
|
534 xz |
|
535 . |
|
536 . |
|
537 . |
|
538 @endcode |
|
539 |
|
540 |
|
541 @param aDir The directory relative to 'C:\\Logs\\' where the log file resides. |
|
542 @param aName The name of the log file. |
|
543 @param aMode The mode in which the log file should be opened: either EAppend |
|
544 or EOverwrite. |
|
545 @param aHeader A label for the hex dump entry. The label has a sequence number |
|
546 appended to it automatically. |
|
547 @param aMargin A continuation label if the hex dump exceeds one line. This |
|
548 label is displayed on all subsequent lines after line one and also has a sequence |
|
549 number appended to it. |
|
550 @param aPtr The data that is to be converted to a hex string. |
|
551 @param aLen How many of the characters in aPtr are to be converted. Conversion |
|
552 always starts from position 0 within aPtr. */ |
|
553 { |
|
554 |
|
555 RFileLogger logger; |
|
556 TInt ret=logger.Connect(); |
|
557 if (ret==KErrNone) |
|
558 { |
|
559 logger.CreateLog(aDir,aName,aMode); |
|
560 if (logger.iLogFile.Valid()) |
|
561 { |
|
562 logger.SetDateAndTime(ETrue,ETrue); |
|
563 logger.DoHexDump(aHeader,aMargin,aPtr,aLen); |
|
564 } |
|
565 logger.CloseLog(); |
|
566 } |
|
567 logger.Close(); |
|
568 } |
|
569 |
|
570 // |
|
571 // Private functions |
|
572 // |
|
573 |
|
574 TInt RFileLogger::DoConnect() |
|
575 /** |
|
576 Connects to the flogger server - default number of message slots = 8 |
|
577 |
|
578 @return TInt indicating success code (KErrNone) or an error code. |
|
579 */ |
|
580 { |
|
581 |
|
582 return CreateSession(KFLoggerServerName,Version(),0); // no async IPC |
|
583 } |
|
584 |
|
585 void RFileLogger::DoWrite(const TDesC8& aBuf) |
|
586 /** |
|
587 Sends the pre-formatted write string to the flogger server. |
|
588 |
|
589 @pre |
|
590 session is already open. |
|
591 @param aBuf 8-bit text to be written. |
|
592 */ |
|
593 { |
|
594 if (LogSTI()) |
|
595 { |
|
596 TBuf<KLogBufferSize> n; |
|
597 n.Copy(aBuf); |
|
598 if (iLogFile.Valid()) |
|
599 { |
|
600 TFileName logFileName = iLogFile.Name(); |
|
601 RDebug::Print(_L("FLG %S %S"),&logFileName,&n); |
|
602 } else |
|
603 { |
|
604 RDebug::Print(_L("FLG %S "),&n); |
|
605 } |
|
606 } else |
|
607 { |
|
608 TPckg<TLogFile> logFilePckg(iLogFile); |
|
609 iLastError=SendReceive(EWriteLog,TIpcArgs (&logFilePckg, &aBuf)); |
|
610 } |
|
611 } |
|
612 |
|
613 void RFileLogger::DoStaticWrite(const TDesC8& aBuf) |
|
614 /** |
|
615 Sends the pre-formatted write string to the flogger server. |
|
616 |
|
617 @pre |
|
618 session is already open. aText is not longer than KLogBufferSize |
|
619 @param aBuf text to write |
|
620 @post |
|
621 The text is only written if the original connection was successful. |
|
622 No text is ever written if the directory specified in the original connection request does not exist. |
|
623 Each line is preceded in the date and time. |
|
624 */ |
|
625 { |
|
626 TPckg<TLogFile> logFilePckg(iLogFile); |
|
627 if (LogSTI()) |
|
628 { |
|
629 TBuf<KLogBufferSize> n; |
|
630 n.Copy(aBuf); |
|
631 if (iLogFile.Valid()) |
|
632 { |
|
633 TFileName logFileName = iLogFile.Name(); |
|
634 RDebug::Print(_L("FLG %S %S"),&logFileName,&n); |
|
635 } else |
|
636 { |
|
637 RDebug::Print(_L("FLG %S "),&n); |
|
638 } |
|
639 |
|
640 } else |
|
641 { |
|
642 SendReceive(ECreateWriteAndCloseLog, TIpcArgs( &logFilePckg, &aBuf)); // ignore error |
|
643 } |
|
644 } |
|
645 |
|
646 void RFileLogger::DoWriteFormat(TRefByValue<const TDesC16> aFmt, VA_LIST& aList) |
|
647 /** |
|
648 Trim format string before sending to the flogger server. |
|
649 |
|
650 @pre |
|
651 session is already open. |
|
652 @param aFmt c-style formatted text to be written. |
|
653 @param aList any variables required by the format. |
|
654 @post |
|
655 The final string is truncated to KLogBufferSize. |
|
656 @see void RFileLogger::DoWriteFormat(TRefByValue<const TDesC8> aFmt, VA_LIST& aList) |
|
657 */ |
|
658 { |
|
659 |
|
660 if (iLogFile.Valid() || LogSTI()) |
|
661 { |
|
662 TBuf8<KLogBufferSize> buf; |
|
663 iLastError=iFormatter.ConvertToWritableBuffer(buf,aFmt,aList); |
|
664 if (iLastError==KErrNone) |
|
665 DoWrite(buf); |
|
666 } |
|
667 } |
|
668 |
|
669 void RFileLogger::DoWriteFormat(TRefByValue<const TDesC8> aFmt, VA_LIST& aList) |
|
670 /** |
|
671 Trim format string before sending to the flogger server. |
|
672 |
|
673 @pre |
|
674 session is already open. |
|
675 @param aFmt c-style formatted text to be written. |
|
676 @param aList any variables required by the format. |
|
677 @post |
|
678 The final string is truncated to KLogBufferSize. |
|
679 */ |
|
680 { |
|
681 |
|
682 if (iLogFile.Valid() || LogSTI()) |
|
683 { |
|
684 TBuf8<KLogBufferSize> buf; |
|
685 iLastError=iFormatter.ConvertToWritableBuffer(buf,aFmt,aList); |
|
686 if (iLastError==KErrNone) |
|
687 DoWrite(buf); |
|
688 } |
|
689 } |
|
690 |
|
691 void RFileLogger::DoStaticWriteFormat(const TDesC& aDir, const TDesC& aName, TFileLoggingMode aMode, TRefByValue<const TDesC> aFmt, VA_LIST& aList) |
|
692 /** |
|
693 Sends a format write string to the flogger server to write to the specified file. |
|
694 |
|
695 @param aDir Path of the log file. |
|
696 @param aName Name of the log file. |
|
697 @param aFmt c-style formatted text to be written. |
|
698 @param aList any variables required by the format. |
|
699 @post |
|
700 The text is only written if the original connection was successful. |
|
701 No text is ever written if the directory specified in the original connection request does not exist. |
|
702 Each line is preceded in the date and time. |
|
703 @see void RFileLogger::DoStaticWriteFormat(const TDesC& aDir, const TDesC& aName, TFileLoggingMode aMode, TRefByValue<const TDesC8> aFmt, VA_LIST& aList) |
|
704 */ |
|
705 { |
|
706 |
|
707 RFileLogger logger; |
|
708 TInt ret=logger.Connect(); |
|
709 if (ret==KErrNone) |
|
710 { |
|
711 logger.SetDateAndTime(ETrue,ETrue); |
|
712 logger.iLogFile.Set(aDir,aName,aMode); |
|
713 TBuf8<KLogBufferSize> buf; |
|
714 ret=logger.iFormatter.ConvertToWritableBuffer(buf,aFmt,aList); |
|
715 if (ret==KErrNone) |
|
716 logger.DoStaticWrite(buf); |
|
717 } |
|
718 logger.Close(); |
|
719 } |
|
720 |
|
721 void RFileLogger::DoStaticWriteFormat(const TDesC& aDir, const TDesC& aName, TFileLoggingMode aMode, TRefByValue<const TDesC8> aFmt, VA_LIST& aList) |
|
722 /** |
|
723 Sends a format write string to the flogger server to write to the specified file. |
|
724 There is no pre-condition. |
|
725 |
|
726 @param aDir Path of the log file. |
|
727 @param aName Name of the log file. |
|
728 @param aFmt c-style formatted text to be written. |
|
729 @param aList any variables required by the format. |
|
730 @post |
|
731 The text is only written if the original connection was successful. |
|
732 No text is ever written if the directory specified in the original |
|
733 connection request does not exist. Each line is preceded in the date and time. |
|
734 */ |
|
735 { |
|
736 |
|
737 RFileLogger logger; |
|
738 TInt ret=logger.Connect(); |
|
739 if (ret==KErrNone) |
|
740 { |
|
741 logger.SetDateAndTime(ETrue,ETrue); |
|
742 logger.iLogFile.Set(aDir,aName,aMode); |
|
743 TBuf8<KLogBufferSize> buf; |
|
744 ret=logger.iFormatter.ConvertToWritableBuffer(buf,aFmt,aList); |
|
745 if (ret==KErrNone) |
|
746 logger.DoStaticWrite(buf); |
|
747 } |
|
748 logger.Close(); |
|
749 } |
|
750 |
|
751 void RFileLogger::DoHexDump(const TText* aHeader, const TText* aMargin, const TUint8* aPtr, TInt aLen) |
|
752 /** |
|
753 Static Write. Dumps arbitrary data to the log file as a standard hex dump. |
|
754 |
|
755 @pre |
|
756 session is already open. |
|
757 @param aHeader Specify a string to be printed before the first hex line. |
|
758 Leave as null or an empty string for no header. |
|
759 @param aMargin Specify a string to be printed before each subsequent line. |
|
760 Leave as null or an empty string for no Margin. |
|
761 @param aPtr pointer to the data being dumped. |
|
762 @param aLen the number of bytes to dump |
|
763 @post |
|
764 The text is only written if the original connection was successful. |
|
765 No text is ever written if the directory specified in the original connection |
|
766 request does not exist. Each line is preceded in the date and time. |
|
767 @note This function has poor performance since it performs a full connection and |
|
768 disconnection to the flogger server. Example of aHeader/aMargin. |
|
769 If "aHeader" is set to "Fn Output:" and "aMargin" is set to " ", then output |
|
770 would look like (for a print of the alphabet): |
|
771 14/11/2002 12:32:24 Fn Output:0000 : 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f 70 abcdefghijklmnop |
|
772 14/11/2002 12:32:24 0010 : 71 72 73 74 75 76 77 78 79 7a qrstuvwxyz |
|
773 */ |
|
774 { |
|
775 |
|
776 if (aPtr==NULL) // nothing to do |
|
777 return; |
|
778 |
|
779 TBuf<KLogBufferSize> buf; |
|
780 TBuf8<KLogBufferSize> temp; |
|
781 TInt i=0; |
|
782 const TText* p=aHeader; |
|
783 while (aLen>0) |
|
784 { |
|
785 if (p==NULL) |
|
786 p=BLANK; // if NULL set p to a blank string |
|
787 TInt n=(aLen>KHexDumpWidth ? KHexDumpWidth : aLen); |
|
788 buf.AppendFormat(KFirstFormatString,p,i); |
|
789 TInt j; |
|
790 for (j=0; j<n; j++) |
|
791 buf.AppendFormat(KSecondFormatString,aPtr[i+j]); |
|
792 while (j++<KHexDumpWidth) |
|
793 buf.Append(KThreeSpaces); |
|
794 buf.Append(KTwoSpaces); |
|
795 for (j=0; j<n; j++) |
|
796 buf.AppendFormat(KThirdFormatString,(aPtr[i+j]<32 || aPtr[i+j]>126) ? KFullStopChar : aPtr[i+j]); |
|
797 |
|
798 iLastError=iFormatter.FormatTextToWritableBuffer(temp,buf); |
|
799 if (iLastError==KErrNone) |
|
800 DoWrite(temp); |
|
801 |
|
802 buf.SetLength(0); |
|
803 temp.SetLength(0); |
|
804 aLen-=n; |
|
805 i+=n; |
|
806 p=aMargin; |
|
807 } |
|
808 } |
|
809 |