|
1 /* |
|
2 * Copyright (c) 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 "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: Logger implementation. |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 // INCLUDE FILES |
|
20 #include "Logger.h" |
|
21 #include <f32file.h> |
|
22 |
|
23 #ifdef __ENABLE_LOGGING__ |
|
24 |
|
25 // CONSTANTS |
|
26 _LIT(KDebugStr, "DEBUG: "); |
|
27 _LIT(KInfoStr, "INFO: "); |
|
28 _LIT(KWarnStr, "WARN: "); |
|
29 _LIT(KErrStr, "ERROR: "); |
|
30 |
|
31 |
|
32 /*---------------------------------------------------------------------------*/ |
|
33 void LogBytes( const TDesC8& aData, TInt aMaxBytesToLog ) |
|
34 { |
|
35 TInt i = 0; |
|
36 while ( i < aData.Length() && i < aMaxBytesToLog ) |
|
37 { |
|
38 TBuf<64> hexbuf; |
|
39 TBuf<32> stringbuf; |
|
40 TInt k=0; |
|
41 while ( i < aMaxBytesToLog && i < aData.Length() && k < 16 ) |
|
42 { |
|
43 const TUint8 uint8 = aData[i]; |
|
44 hexbuf.AppendFormat( _L( "%02X "), uint8 ); |
|
45 |
|
46 if ( uint8 == '%' ) |
|
47 { |
|
48 stringbuf.Append( _L( "%%" ) ); // escape character in format string |
|
49 } |
|
50 else if ( uint8 < 32 || uint8 > 126 ) |
|
51 { |
|
52 stringbuf.Append( _L(".") ); |
|
53 } |
|
54 else |
|
55 { |
|
56 stringbuf.Append( ( TChar ) uint8 ); |
|
57 } |
|
58 i++; |
|
59 k++; |
|
60 } |
|
61 TBuf<128> finalbuf; |
|
62 finalbuf.Copy( hexbuf ); |
|
63 finalbuf.Append( _L(" | " ) ); |
|
64 finalbuf.Append( stringbuf ); |
|
65 DebugLog( finalbuf ); |
|
66 } |
|
67 } |
|
68 |
|
69 /*---------------------------------------------------------------------------*/ |
|
70 class TOverflowHandler : public TDes16Overflow |
|
71 { |
|
72 void Overflow( TDes16& aDes ) |
|
73 { |
|
74 TBuf<KLogEntryMaxLength> logString( _L("LOG ERROR: overflow: ") ); |
|
75 logString.Append( aDes.Left( |
|
76 KLogEntryMaxLength - logString.Length() ) ); |
|
77 aDes.Copy( logString ); |
|
78 } |
|
79 }; |
|
80 |
|
81 /*---------------------------------------------------------------------------*/ |
|
82 // Usage example: DOLOG(_L("Logstring")); |
|
83 void DebugLog( TRefByValue<const TDesC> aFmt, ... ) |
|
84 { |
|
85 VA_LIST list; |
|
86 VA_START( list, aFmt ); |
|
87 Log( KLogLevelDebug, aFmt, list ); |
|
88 } |
|
89 |
|
90 /*---------------------------------------------------------------------------*/ |
|
91 void InfoLog( TRefByValue<const TDesC> aFmt, ... ) |
|
92 { |
|
93 VA_LIST list; |
|
94 VA_START( list, aFmt ); |
|
95 Log( KLogLevelInfo, aFmt, list ); |
|
96 } |
|
97 |
|
98 /*---------------------------------------------------------------------------*/ |
|
99 void WarnLog( TRefByValue<const TDesC> aFmt, ... ) |
|
100 { |
|
101 VA_LIST list; |
|
102 VA_START( list, aFmt ); |
|
103 Log( KLogLevelWarning, aFmt, list ); |
|
104 } |
|
105 |
|
106 /*---------------------------------------------------------------------------*/ |
|
107 void ErrLog( TRefByValue<const TDesC> aFmt, ...) |
|
108 { |
|
109 VA_LIST list; |
|
110 VA_START( list, aFmt ); |
|
111 Log( KLogLevelError, aFmt, list ); |
|
112 } |
|
113 |
|
114 /*---------------------------------------------------------------------------*/ |
|
115 void Log( TInt aLevel, TRefByValue<const TDesC> aText, VA_LIST list ) |
|
116 { |
|
117 |
|
118 if ( aLevel < KMinimumLogLevel ) |
|
119 return; |
|
120 |
|
121 _LIT( KLineFeed, "\n" ); |
|
122 |
|
123 /** |
|
124 * Log time format (see TTime) is |
|
125 * Day-Month-Year Hours:Minutes:Seconds:Milliseconds |
|
126 * |
|
127 * Example: 30-12-2004 23:00:55:990 |
|
128 */ |
|
129 _LIT( KLogTimeFormat, "%F%D-%M-%Y %H:%T:%S:%*C3" ); |
|
130 |
|
131 TBuf8<KLogEntryMaxLength> writeBuffer; |
|
132 TBuf16<KLogEntryMaxLength> logEntry; |
|
133 RFs FileServer; |
|
134 RFile File; |
|
135 |
|
136 if ( FileServer.Connect() != KErrNone ) |
|
137 { |
|
138 FileServer.Close(); // just in case |
|
139 User::Panic( KLogPanicCategory(), KPanicFsConnectFailed ); |
|
140 return; |
|
141 } |
|
142 |
|
143 // Open file for writing, if exists. Othervise create new file. |
|
144 if ( File.Open( FileServer, KLogFileName(), EFileWrite ) != KErrNone ) |
|
145 { |
|
146 if ( File.Create( FileServer, KLogFileName(), EFileWrite ) |
|
147 != KErrNone ) |
|
148 { |
|
149 FileServer.Close(); |
|
150 User::Panic( KLogPanicCategory(), KPanicFileCreateFailed ); |
|
151 } |
|
152 } |
|
153 |
|
154 TTime currentTime; |
|
155 currentTime.UniversalTime(); |
|
156 TBuf<32> timeString; |
|
157 |
|
158 // currentTime is now in universal time. Convert it to home time. |
|
159 TLocale locale; |
|
160 TTimeIntervalSeconds universalTimeOffset( locale.UniversalTimeOffset() ); |
|
161 TTimeIntervalHours daylightSaving( 0 ); |
|
162 if ( locale.QueryHomeHasDaylightSavingOn() ) |
|
163 { |
|
164 daylightSaving = 1; |
|
165 } |
|
166 currentTime = currentTime + universalTimeOffset + daylightSaving; |
|
167 currentTime.FormatL( timeString, KLogTimeFormat ); |
|
168 |
|
169 // Add LogString to the end of file and close the file |
|
170 TInt currentSize = 0, returnCode; |
|
171 writeBuffer.Append( timeString ); |
|
172 writeBuffer.Append( _L(": ") ); |
|
173 |
|
174 if ( aLevel < KLogLevelInfo ) |
|
175 writeBuffer.Append(KDebugStr); |
|
176 else if ( aLevel < KLogLevelWarning ) |
|
177 writeBuffer.Append(KInfoStr); |
|
178 else if ( aLevel < KLogLevelError ) |
|
179 writeBuffer.Append(KWarnStr); |
|
180 else |
|
181 writeBuffer.Append(KErrStr); |
|
182 |
|
183 logEntry.AppendFormatList( aText, list ); //, &overFlowHandler ); |
|
184 writeBuffer.Append( logEntry.Left( |
|
185 KLogEntryMaxLength - writeBuffer.Length() ) ); |
|
186 writeBuffer.Append( KLineFeed ); |
|
187 File.Size( currentSize ); |
|
188 returnCode = File.Write( currentSize, |
|
189 writeBuffer, |
|
190 writeBuffer.Length() ); |
|
191 File.Close(); |
|
192 // Close file server session |
|
193 FileServer.Close(); |
|
194 |
|
195 if ( returnCode != KErrNone ) |
|
196 { |
|
197 User::Panic( KLogPanicCategory(), KPanicFileWriteFailed ); |
|
198 } |
|
199 } |
|
200 |
|
201 #endif // __ENABLE_LOGGING__ |