|
1 /* |
|
2 * Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of the License "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 * |
|
14 * Description: |
|
15 * Implements common print functions used in debug mode for all security components. |
|
16 * A component that wants to use these common debugging functions should define its |
|
17 * own log header file and include this header file. In the component's header file, |
|
18 * the followings should be defined: |
|
19 * _LIT8(KComponentName, "[TheComponentName]"); |
|
20 * |
|
21 */ |
|
22 |
|
23 |
|
24 /** |
|
25 @file |
|
26 @internalTechnology |
|
27 @released |
|
28 */ |
|
29 |
|
30 #ifndef SECURITYLOG_H |
|
31 #define SECURITYLOG_H |
|
32 |
|
33 #include <e32debug.h> |
|
34 |
|
35 |
|
36 class TTruncateOverflowHandler16 : public TDes16Overflow |
|
37 { |
|
38 public: |
|
39 virtual void Overflow(TDes16& aDes); |
|
40 }; |
|
41 |
|
42 inline void TTruncateOverflowHandler16::Overflow(TDes16&) |
|
43 { |
|
44 |
|
45 } |
|
46 |
|
47 class TTruncateOverflowHandler8 : public TDes8Overflow |
|
48 { |
|
49 public: |
|
50 virtual void Overflow(TDes8& aDes); |
|
51 }; |
|
52 |
|
53 inline void TTruncateOverflowHandler8::Overflow(TDes8&) |
|
54 { |
|
55 |
|
56 } |
|
57 |
|
58 #ifdef _DEBUG |
|
59 |
|
60 const TInt KMaxLogBufferSize8 = 512; |
|
61 const TInt KMaxLogBufferSize16 = 256; |
|
62 |
|
63 #define SEC_DEBUG_PRINTF(x, a) {::CommonDebugPrintf(x, __LINE__, __FILE__, a);} |
|
64 #define SEC_DEBUG_PRINTF2(x, a, b) {::CommonDebugPrintf(x, __LINE__, __FILE__, a, b);} |
|
65 #define SEC_DEBUG_PRINTF3(x, a, b, c) {::CommonDebugPrintf(x, __LINE__, __FILE__, a, b, c);} |
|
66 #define SEC_DEBUG_PRINTF4(x, a, b, c, d) {::CommonDebugPrintf(x, __LINE__, __FILE__, a, b, c, d);} |
|
67 #define SEC_DEBUG_PRINTF5(x, a, b, c, d, e) {::CommonDebugPrintf(x, __LINE__, __FILE__, a, b, c, d, e);} |
|
68 |
|
69 #define SEC_DEBUG_CODE_SECTION(a) TRAP_IGNORE({ a; }) |
|
70 |
|
71 inline void FileFormatLine(TDes8& aBuffer, const TDesC8& aComponent, TInt aLine, char* aFile) |
|
72 { |
|
73 TTime now; |
|
74 now.HomeTime(); |
|
75 |
|
76 _LIT8(KLineFileFormat, "%Ld Line: % 5d, File: %s -- "); |
|
77 aBuffer.Append(aComponent); |
|
78 aBuffer.AppendFormat(KLineFileFormat, now.Int64(), aLine, aFile); |
|
79 } |
|
80 |
|
81 // UTF-8 overload of the CoomonDebufPrintf method. Should be used by default, |
|
82 // since it's cheaper both in CPU cycles and stack space. |
|
83 inline void CommonDebugPrintf(const TDesC8& aComponent, TInt aLine, char* aFile, TRefByValue<const TDesC8> aFormat, ...) |
|
84 { |
|
85 TTruncateOverflowHandler8 overflowHandler8; |
|
86 VA_LIST list; |
|
87 VA_START(list, aFormat); |
|
88 |
|
89 TBuf8<KMaxLogBufferSize8> buffer; |
|
90 FileFormatLine(buffer, aComponent, aLine, aFile); |
|
91 |
|
92 buffer.AppendFormatList(aFormat, list, &overflowHandler8); |
|
93 _LIT8(KLogMsgEnd, "\r\n"); |
|
94 if(buffer.MaxLength() >= (buffer.Length() + KLogMsgEnd().Length())) |
|
95 { |
|
96 buffer.Append(KLogMsgEnd); |
|
97 } |
|
98 |
|
99 RDebug::RawPrint(buffer); |
|
100 |
|
101 VA_END(list); |
|
102 } |
|
103 |
|
104 // Unicode CommonDebufPrintf overload |
|
105 inline void CommonDebugPrintf(const TDesC8& aComponent, TInt aLine, char* aFile, TRefByValue<const TDesC16> aFormat, ...) |
|
106 { |
|
107 TTruncateOverflowHandler16 overflowHandler16; |
|
108 VA_LIST list; |
|
109 VA_START(list, aFormat); |
|
110 |
|
111 TBuf8<KMaxLogBufferSize16> header; |
|
112 FileFormatLine(header, aComponent, aLine, aFile); |
|
113 |
|
114 TBuf<KMaxLogBufferSize16> buffer; |
|
115 buffer.Copy(header); |
|
116 buffer.AppendFormatList(aFormat, list , &overflowHandler16); |
|
117 |
|
118 _LIT(KLogMsgEnd, "\r\n"); |
|
119 if(buffer.MaxLength() >= (buffer.Length() + KLogMsgEnd().Length())) |
|
120 { |
|
121 buffer.Append(KLogMsgEnd); |
|
122 } |
|
123 |
|
124 RDebug::RawPrint(buffer); |
|
125 |
|
126 VA_END(list); |
|
127 } |
|
128 |
|
129 #else |
|
130 |
|
131 #define SEC_DEBUG_PRINTF(x, a) |
|
132 #define SEC_DEBUG_PRINTF2(x, a, b) |
|
133 #define SEC_DEBUG_PRINTF3(x, a, b, c) |
|
134 #define SEC_DEBUG_PRINTF4(x, a, b, c, d) |
|
135 #define SEC_DEBUG_PRINTF5(x, a, b, c, d, e) |
|
136 |
|
137 #define SEC_DEBUG_CODE_SECTION(a) |
|
138 |
|
139 #endif // _DEBUG |
|
140 |
|
141 #endif // SECURITYLOG_H |