|
1 // Copyright (c) 2005-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 @internalTechnology |
|
19 */ |
|
20 |
|
21 |
|
22 #include <e32base.h> |
|
23 #include <bluetooth/logger.h> |
|
24 |
|
25 #ifdef __FLOG_ACTIVE |
|
26 _LIT8(KSubsystem, "BT"); |
|
27 _LIT8(KLogCmpt, LOG_COMPONENT_LOGGER); |
|
28 #endif |
|
29 |
|
30 /** |
|
31 This constant is used to direct how to pad the logged text to ensure it |
|
32 lines up, as tab characters are involved, lining up requires knowledge of |
|
33 how many spaces a tab character is viewed as. |
|
34 */ |
|
35 #ifdef __FLOG_ACTIVE |
|
36 const TUint KTabWidthInViewer = 4; |
|
37 const TUint KMaxTabsLength = 1 + KMaxTagLength/KTabWidthInViewer; |
|
38 #endif |
|
39 |
|
40 NONSHARABLE_CLASS(TLogData) |
|
41 { |
|
42 public: |
|
43 #ifdef __FLOG_ACTIVE |
|
44 TLogData(); |
|
45 |
|
46 void SetLogTags(const TDesC8& aCmpt); |
|
47 |
|
48 TInt iAccessCount; |
|
49 |
|
50 RFileLogger iLogEngine; |
|
51 TBuf8<KMaxTagLength> iCurrentComponent; |
|
52 |
|
53 TBuf8<KMaxTabsLength> iInitTabPad; |
|
54 |
|
55 TUint iIndent; |
|
56 #endif |
|
57 }; |
|
58 |
|
59 |
|
60 #ifdef __FLOG_ACTIVE |
|
61 TLogData::TLogData() |
|
62 : iAccessCount(0), iCurrentComponent(KNullDesC8), iIndent(0) |
|
63 {} |
|
64 |
|
65 void TLogData::SetLogTags(const TDesC8& aCmpt) |
|
66 { |
|
67 if (aCmpt != iCurrentComponent) |
|
68 { |
|
69 iCurrentComponent = aCmpt.Left(KMaxTagLength); |
|
70 TInt numPadChars = KMaxTagLength-iCurrentComponent.Length(); // must be >=0 |
|
71 TInt padTabs = (numPadChars/KTabWidthInViewer) + ((numPadChars%KTabWidthInViewer > 0) ? 1 : 0); |
|
72 iInitTabPad.Zero(); |
|
73 iInitTabPad.Fill('\t', padTabs); |
|
74 iLogEngine.SetLogTags(KSubsystem, iCurrentComponent); |
|
75 } |
|
76 } |
|
77 #endif |
|
78 |
|
79 #define GETLOG TLogData* __logger = static_cast<TLogData*>(Dll::Tls()); |
|
80 |
|
81 |
|
82 |
|
83 EXPORT_C /*static*/ TInt CBtLog::Connect() |
|
84 { |
|
85 #ifdef __FLOG_ACTIVE |
|
86 GETLOG; |
|
87 |
|
88 if (!__logger) |
|
89 { |
|
90 CBtLog::Write(KLogCmpt, _L8("Opening new logger connection")); |
|
91 __logger = new TLogData(); |
|
92 if (!__logger) |
|
93 { |
|
94 CBtLog::Write(KLogCmpt, _L8("Opening logger connection failed, no memory")); |
|
95 return KErrNoMemory; |
|
96 } |
|
97 |
|
98 __logger->iLogEngine.Connect(); |
|
99 Dll::SetTls(__logger); |
|
100 } |
|
101 |
|
102 __logger->iAccessCount++; |
|
103 CBtLog::WriteFormat(KLogCmpt, _L8("Opening -- %d instances now open"), __logger->iAccessCount); |
|
104 |
|
105 return KErrNone; |
|
106 #else |
|
107 return KErrNotSupported; |
|
108 #endif |
|
109 } |
|
110 |
|
111 |
|
112 EXPORT_C /*static*/ void CBtLog::Close() |
|
113 { |
|
114 #ifdef __FLOG_ACTIVE |
|
115 GETLOG; |
|
116 |
|
117 if (__logger) |
|
118 { |
|
119 TInt& count = __logger->iAccessCount; |
|
120 |
|
121 if (count) |
|
122 { |
|
123 count--; |
|
124 CBtLog::WriteFormat(KLogCmpt, _L8("Closing -- %d instance(s) left open"), count); |
|
125 if (!count) |
|
126 { |
|
127 __logger->iLogEngine.Close(); |
|
128 delete __logger; |
|
129 Dll::SetTls(NULL); |
|
130 CBtLog::Write(KLogCmpt, _L8("Fully closed and deleted, now flogging statically.")); |
|
131 } |
|
132 } |
|
133 else |
|
134 { |
|
135 CBtLog::Write(KLogCmpt, _L8("Not closing -- not opened")); |
|
136 } |
|
137 } |
|
138 #endif |
|
139 } |
|
140 |
|
141 |
|
142 |
|
143 EXPORT_C /*static*/ void CBtLog::Write(const TDesC8& IF_FLOGGING(aCmpt), const TDesC8& IF_FLOGGING(aText)) |
|
144 { |
|
145 #ifdef __FLOG_ACTIVE |
|
146 GETLOG; |
|
147 |
|
148 if (__logger) |
|
149 { |
|
150 __logger->SetLogTags(aCmpt); |
|
151 __logger->iLogEngine.Write(aText); |
|
152 } |
|
153 else |
|
154 { |
|
155 RFileLogger::Write(KSubsystem, aCmpt, aText); |
|
156 } |
|
157 #endif |
|
158 } |
|
159 |
|
160 |
|
161 EXPORT_C /*static*/ void CBtLog::WriteFormat(const TDesC8& IF_FLOGGING(aCmpt), TRefByValue<const TDesC8> IF_FLOGGING(aFmt), ...) |
|
162 { |
|
163 #ifdef __FLOG_ACTIVE |
|
164 VA_LIST list; |
|
165 VA_START(list, aFmt); |
|
166 |
|
167 GETLOG; |
|
168 |
|
169 if (__logger) |
|
170 { |
|
171 __logger->SetLogTags(aCmpt); |
|
172 __logger->iLogEngine.WriteFormat(aFmt, list); |
|
173 } |
|
174 else |
|
175 { |
|
176 RFileLogger::WriteFormat(KSubsystem, aCmpt, aFmt, list); |
|
177 } |
|
178 #endif |
|
179 } |
|
180 |
|
181 |
|
182 EXPORT_C /*static*/ void CBtLog::WriteFormat(const TDesC8& IF_FLOGGING(aCmpt), TRefByValue<const TDesC8> IF_FLOGGING(aFmt), VA_LIST& IF_FLOGGING(aList)) |
|
183 { |
|
184 #ifdef __FLOG_ACTIVE |
|
185 GETLOG; |
|
186 |
|
187 if (__logger) |
|
188 { |
|
189 __logger->SetLogTags(aCmpt); |
|
190 __logger->iLogEngine.WriteFormat(aFmt, aList); |
|
191 } |
|
192 else |
|
193 { |
|
194 RFileLogger::WriteFormat(KSubsystem, aCmpt, aFmt, aList); |
|
195 } |
|
196 #endif |
|
197 } |
|
198 |
|
199 |
|
200 EXPORT_C /*static*/ void CBtLog::Write(const TDesC8& IF_FLOGGING(aCmpt), const TDesC16& IF_FLOGGING(aText)) |
|
201 { |
|
202 #ifdef __FLOG_ACTIVE |
|
203 GETLOG; |
|
204 |
|
205 if (__logger) |
|
206 { |
|
207 __logger->SetLogTags(aCmpt); |
|
208 __logger->iLogEngine.Write(aText); |
|
209 } |
|
210 else |
|
211 { |
|
212 RFileLogger::WriteFormat(KSubsystem, aCmpt, aText); |
|
213 } |
|
214 #endif |
|
215 } |
|
216 |
|
217 |
|
218 EXPORT_C /*static*/ void CBtLog::WriteFormat(const TDesC8& IF_FLOGGING(aCmpt), TRefByValue<const TDesC16> IF_FLOGGING(aFmt), ...) |
|
219 { |
|
220 #ifdef __FLOG_ACTIVE |
|
221 VA_LIST list; |
|
222 VA_START(list, aFmt); |
|
223 |
|
224 GETLOG; |
|
225 |
|
226 if (__logger) |
|
227 { |
|
228 __logger->SetLogTags(aCmpt); |
|
229 __logger->iLogEngine.WriteFormat(aFmt, list); |
|
230 } |
|
231 else |
|
232 { |
|
233 RFileLogger::WriteFormat(KSubsystem, aCmpt, aFmt, list); |
|
234 } |
|
235 #endif |
|
236 } |
|
237 |
|
238 |
|
239 EXPORT_C /*static*/ void CBtLog::WriteFormat(const TDesC8& IF_FLOGGING(aCmpt), TRefByValue<const TDesC16> IF_FLOGGING(aFmt), VA_LIST& IF_FLOGGING(aList)) |
|
240 { |
|
241 #ifdef __FLOG_ACTIVE |
|
242 GETLOG; |
|
243 |
|
244 if (__logger) |
|
245 { |
|
246 __logger->SetLogTags(aCmpt); |
|
247 __logger->iLogEngine.WriteFormat(aFmt, aList); |
|
248 } |
|
249 else |
|
250 { |
|
251 RFileLogger::WriteFormat(KSubsystem, aCmpt, aFmt, aList); |
|
252 } |
|
253 #endif |
|
254 } |
|
255 |
|
256 |
|
257 EXPORT_C /*static*/ void CBtLog::HexDump(const TDesC8& IF_FLOGGING(aCmpt), const TText* IF_FLOGGING(aHeader), const TText* IF_FLOGGING(aMargin), const TUint8* IF_FLOGGING(aPtr), TInt IF_FLOGGING(aLen)) |
|
258 { |
|
259 #ifdef __FLOG_ACTIVE |
|
260 GETLOG; |
|
261 |
|
262 if (__logger) |
|
263 { |
|
264 __logger->SetLogTags(aCmpt); |
|
265 __logger->iLogEngine.HexDump(aHeader, aMargin, aPtr, aLen); |
|
266 } |
|
267 else |
|
268 { |
|
269 RFileLogger::HexDump(KSubsystem, aCmpt, TPtrC8(aPtr, aLen), KNullDesC8); |
|
270 } |
|
271 #endif |
|
272 } |
|
273 |
|
274 |
|
275 |
|
276 |
|
277 |
|
278 |
|
279 |
|
280 /** |
|
281 Leave (if error) verbosely- log name of file and line number just before |
|
282 leaving. |
|
283 @param aFile The file we're leaving from. |
|
284 @param aLine The line number we're leaving from. |
|
285 @param aReason The leave code. |
|
286 */ |
|
287 EXPORT_C void VerboseLeaveIfErrorL(const TDesC8& IF_FLOGGING(aCpt), |
|
288 char* IF_FLOGGING(aFile), |
|
289 TInt IF_FLOGGING(aLine), |
|
290 TInt IF_FLOGGING(aReason)) |
|
291 { |
|
292 #ifdef __FLOG_ACTIVE |
|
293 // only leave if non-zero value |
|
294 if ( aReason >= KErrNone ) |
|
295 { |
|
296 return; |
|
297 } |
|
298 |
|
299 _LIT8(KLeavePrefix, "LEAVE: "); |
|
300 |
|
301 TPtrC8 fullFileName((const TUint8*)aFile); |
|
302 TPtrC8 fileName(fullFileName.Ptr()+fullFileName.LocateReverse('\\')+1); |
|
303 |
|
304 TBuf8<256> buf; |
|
305 buf.Append(KLeavePrefix); |
|
306 buf.AppendFormat(_L8("aReason = %d [file %S, line %d]"), aReason, &fileName, |
|
307 aLine); |
|
308 CBtLog::Write(aCpt, buf); |
|
309 |
|
310 // finally |
|
311 User::Leave(aReason); |
|
312 #endif |
|
313 } |
|
314 |
|
315 /** |
|
316 Leave verbosely- log name of file and line number just before |
|
317 leaving. |
|
318 @param aFile The file we're leaving from. |
|
319 @param aLine The line number we're leaving from. |
|
320 @param aReason The leave code. |
|
321 */ |
|
322 EXPORT_C void VerboseLeaveL(const TDesC8& IF_FLOGGING(aCpt), |
|
323 char* IF_FLOGGING(aFile), |
|
324 TInt IF_FLOGGING(aLine), |
|
325 TInt IF_FLOGGING(aReason)) |
|
326 { |
|
327 #ifdef __FLOG_ACTIVE |
|
328 |
|
329 _LIT8(KLeavePrefix, "LEAVE: "); |
|
330 |
|
331 TPtrC8 fullFileName((const TUint8*)aFile); |
|
332 TPtrC8 fileName(fullFileName.Ptr()+fullFileName.LocateReverse('\\')+1); |
|
333 |
|
334 TBuf8<256> buf; |
|
335 buf.Append(KLeavePrefix); |
|
336 buf.AppendFormat(_L8("aReason = %d [file %S, line %d]"), aReason, &fileName, |
|
337 aLine); |
|
338 CBtLog::Write(aCpt, buf); |
|
339 |
|
340 // finally |
|
341 User::Leave(aReason); |
|
342 #endif |
|
343 } |
|
344 /** |
|
345 Panic verbosely- log name of file and line number just before panicking. |
|
346 @param aFile The file that's panicking. |
|
347 @param aLine The line number that's panicking. |
|
348 @param aReason The panic code. |
|
349 @param aPanicName The text of the panic code. |
|
350 @param aPanicCategory The panic category. |
|
351 */ |
|
352 EXPORT_C void VerbosePanic(const TDesC8& IF_FLOGGING(aCpt), |
|
353 char* IF_FLOGGING(aFile), |
|
354 TInt IF_FLOGGING(aLine), |
|
355 TInt IF_FLOGGING(aPanicCode), |
|
356 TText8* IF_FLOGGING(aPanicName), |
|
357 const TDesC& IF_FLOGGING(aPanicCategory)) |
|
358 { |
|
359 #ifdef __FLOG_ACTIVE |
|
360 _LIT8(KPanicPrefix, "PANIC: code "); |
|
361 |
|
362 TPtrC8 fullFileName((const TUint8*)aFile); |
|
363 TPtrC8 fileName(fullFileName.Ptr()+fullFileName.LocateReverse('\\')+1); |
|
364 |
|
365 TBuf8<256> buf; |
|
366 buf.Append(KPanicPrefix); |
|
367 buf.AppendFormat(_L8("%d = %s [file %S, line %d]"), |
|
368 aPanicCode, |
|
369 aPanicName, |
|
370 &fileName, |
|
371 aLine); |
|
372 CBtLog::Write(aCpt, buf); |
|
373 |
|
374 // finally |
|
375 User::Panic(aPanicCategory, aPanicCode); |
|
376 #endif |
|
377 } |
|
378 |
|
379 /** |
|
380 Panic the given message verbosely- log name of file and line number just |
|
381 before panicking. |
|
382 @param aMsg Message to panic. |
|
383 @param aFile The file that's panicking. |
|
384 @param aLine The line number that's panicking. |
|
385 @param aReason The panic code. |
|
386 @param aPanicName The text of the panic code. |
|
387 @param aPanicCategory The panic category. |
|
388 */ |
|
389 EXPORT_C void VerboseMsgPanic(const TDesC8& IF_FLOGGING(aCpt), |
|
390 char* IF_FLOGGING(aFile), |
|
391 TInt IF_FLOGGING(aLine), |
|
392 const RMessage2& aMsg, |
|
393 const TDesC& aCat, |
|
394 TInt aPanicCode) |
|
395 { |
|
396 #ifdef __FLOG_ACTIVE |
|
397 _LIT8(KPanicPrefix, "PANICKING CLIENT: code "); |
|
398 |
|
399 TPtrC8 fullFileName((const TUint8*)aFile); |
|
400 TPtrC8 fileName(fullFileName.Ptr()+fullFileName.LocateReverse('\\')+1); |
|
401 |
|
402 TBuf8<256> buf; |
|
403 buf.Append(KPanicPrefix); |
|
404 buf.AppendFormat(_L8("%d [file %S, line %d]"), |
|
405 aPanicCode, |
|
406 &fileName, |
|
407 aLine); |
|
408 CBtLog::Write(aCpt, buf); |
|
409 #endif |
|
410 // finally |
|
411 aMsg.Panic(aCat, aPanicCode); |
|
412 } |
|
413 |
|
414 #ifdef __FLOG_ACTIVE |
|
415 _LIT8(KInstrumentIn, "%S%S>>%S this = [0x%08x]"); |
|
416 _LIT8(KInstrumentOut, "%S%S<<%S"); |
|
417 _LIT8(KInstrumentStaticIn, "%S%S>>%S static"); |
|
418 _LIT8(KInstrumentStaticOut, "%S%S<<%S"); |
|
419 #define BT_LOGGER_INDENT_CHAR ' ' |
|
420 |
|
421 TUint TLoggerBase::PreIncrementIndent() |
|
422 { |
|
423 GETLOG; |
|
424 return (__logger ? ++(__logger->iIndent) : 0); |
|
425 } |
|
426 |
|
427 TUint TLoggerBase::PostDecrementIndent() |
|
428 { |
|
429 GETLOG; |
|
430 return (__logger && (__logger->iIndent > 0) ? (__logger->iIndent)-- : 0); |
|
431 } |
|
432 |
|
433 TUint TLoggerBase::Indent() |
|
434 { |
|
435 GETLOG; |
|
436 return (__logger ? (__logger->iIndent) : 0); |
|
437 } |
|
438 #endif |
|
439 |
|
440 #ifdef __FLOG_ACTIVE |
|
441 EXPORT_C TFunctionLogger::TFunctionLogger(const TDesC8& aCpt, const TDesC8& aString, TAny* aThis) |
|
442 { |
|
443 GETLOG; |
|
444 iCpt.Set(aCpt); |
|
445 iString.Set(aString); |
|
446 if (PreIncrementIndent() < KMaxIndent) |
|
447 { |
|
448 iIndentBuf.Fill(BT_LOGGER_INDENT_CHAR, Indent()); |
|
449 } |
|
450 |
|
451 TPtrC8 initPad(KNullDesC8); |
|
452 if(__logger) |
|
453 { |
|
454 __logger->SetLogTags(iCpt); |
|
455 initPad.Set(__logger->iInitTabPad); |
|
456 } |
|
457 CBtLog::WriteFormat(iCpt, KInstrumentIn, &initPad, &iIndentBuf, &iString, aThis); |
|
458 } |
|
459 |
|
460 EXPORT_C TFunctionLogger::~TFunctionLogger() |
|
461 { |
|
462 GETLOG; |
|
463 if (Indent() < KMaxIndent) |
|
464 { |
|
465 iIndentBuf.Fill(BT_LOGGER_INDENT_CHAR, PostDecrementIndent()); |
|
466 } |
|
467 else |
|
468 { |
|
469 PostDecrementIndent(); |
|
470 } |
|
471 |
|
472 TPtrC8 initPad(KNullDesC8); |
|
473 if(__logger) |
|
474 { |
|
475 __logger->SetLogTags(iCpt); |
|
476 initPad.Set(__logger->iInitTabPad); |
|
477 } |
|
478 CBtLog::WriteFormat(iCpt, KInstrumentOut, &initPad, &iIndentBuf, &iString); |
|
479 } |
|
480 |
|
481 EXPORT_C TStaticFunctionLogger::TStaticFunctionLogger(const TDesC8& aCpt, const TDesC8& aString) |
|
482 { |
|
483 GETLOG; |
|
484 iCpt.Set(aCpt); |
|
485 iString.Set(aString); |
|
486 if (PreIncrementIndent() < KMaxIndent) |
|
487 { |
|
488 iIndentBuf.Fill(BT_LOGGER_INDENT_CHAR, Indent()); |
|
489 } |
|
490 |
|
491 TPtrC8 initPad(KNullDesC8); |
|
492 if(__logger) |
|
493 { |
|
494 __logger->SetLogTags(iCpt); |
|
495 initPad.Set(__logger->iInitTabPad); |
|
496 } |
|
497 CBtLog::WriteFormat(iCpt, KInstrumentStaticIn, &initPad, &iIndentBuf, &iString); |
|
498 } |
|
499 |
|
500 EXPORT_C TStaticFunctionLogger::~TStaticFunctionLogger() |
|
501 { |
|
502 GETLOG; |
|
503 if (Indent() < KMaxIndent) |
|
504 { |
|
505 iIndentBuf.Fill(BT_LOGGER_INDENT_CHAR, PostDecrementIndent()); |
|
506 } |
|
507 else |
|
508 { |
|
509 PostDecrementIndent(); |
|
510 } |
|
511 |
|
512 TPtrC8 initPad(KNullDesC8); |
|
513 if(__logger) |
|
514 { |
|
515 __logger->SetLogTags(iCpt); |
|
516 initPad.Set(__logger->iInitTabPad); |
|
517 } |
|
518 CBtLog::WriteFormat(iCpt, KInstrumentStaticOut, &initPad, &iIndentBuf, &iString); |
|
519 } |
|
520 #else |
|
521 #ifdef __UREL_RDEBUG_PRINT |
|
522 #include <e32debug.h> |
|
523 EXPORT_C TFunctionLogger::TFunctionLogger(const TDesC8& aCpt, const TDesC8& aString, TAny* aThis) |
|
524 { |
|
525 iCpt.Set(aCpt); |
|
526 iString.Set(aString); |
|
527 TBuf8<1024> buf; |
|
528 buf.Format(_L8("%S >>%S this = [0x%08x]"), &iCpt, &iString, aThis); |
|
529 |
|
530 RDebug::Print(buf.Expand()); |
|
531 } |
|
532 |
|
533 EXPORT_C TFunctionLogger::~TFunctionLogger() |
|
534 { |
|
535 TBuf8<1024> buf; |
|
536 buf.Format(_L8("%S <<%S"), &iCpt, &iString); |
|
537 RDebug::Print(buf.Expand()); |
|
538 } |
|
539 |
|
540 EXPORT_C TStaticFunctionLogger::TStaticFunctionLogger(const TDesC8& aCpt, const TDesC8& aString) |
|
541 { |
|
542 iCpt.Set(aCpt); |
|
543 iString.Set(aString); |
|
544 TBuf8<1024> buf; |
|
545 buf.Format(_L8("%S >>%S static"), &iCpt, &iString); |
|
546 |
|
547 RDebug::Print(buf.Expand()); |
|
548 } |
|
549 |
|
550 EXPORT_C TStaticFunctionLogger::~TStaticFunctionLogger() |
|
551 { |
|
552 TBuf8<1024> buf; |
|
553 buf.Format(_L8("%S <<%S"), &iCpt, &iString); |
|
554 RDebug::Print(buf.Expand()); |
|
555 } |
|
556 #else // NO DEBUG |
|
557 EXPORT_C TFunctionLogger::TFunctionLogger(const TDesC8& /*aCpt*/, const TDesC8& /*aString*/, TAny* /*aThis*/) |
|
558 { |
|
559 } |
|
560 |
|
561 EXPORT_C TFunctionLogger::~TFunctionLogger() |
|
562 { |
|
563 } |
|
564 |
|
565 EXPORT_C TStaticFunctionLogger::TStaticFunctionLogger(const TDesC8& /*aCpt*/, const TDesC8& /*aString*/) |
|
566 { |
|
567 } |
|
568 |
|
569 EXPORT_C TStaticFunctionLogger::~TStaticFunctionLogger() |
|
570 { |
|
571 } |
|
572 #endif |
|
573 #endif |