|
1 // Copyright (c) 2000-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 #include <e32cons.h> |
|
17 #include <flogger.h> |
|
18 #include <e32svr.h> |
|
19 #include <push/pushmessage.h> |
|
20 #include <push/pushlog.h> |
|
21 #include "testlog.h" |
|
22 |
|
23 _LIT(KPushLogDir, "push"); |
|
24 _LIT(KPushLogFile, "WapPushLogging.txt"); |
|
25 const TInt KWatcherLogBuffer = 256; |
|
26 |
|
27 |
|
28 /** |
|
29 * CWapPushLog::NewL |
|
30 * |
|
31 */ |
|
32 CWapPushLog* CWapPushLog::NewL(CConsoleBase& aConsole) |
|
33 { |
|
34 CWapPushLog* self = new(ELeave)CWapPushLog(aConsole); |
|
35 CleanupStack::PushL(self); |
|
36 self->ConstructL(); |
|
37 CleanupStack::Pop(); |
|
38 return self; |
|
39 } |
|
40 |
|
41 CWapPushLog::CWapPushLog(CConsoleBase& aConsole) : iConsole(aConsole) |
|
42 { |
|
43 } |
|
44 |
|
45 |
|
46 /** |
|
47 * Destructor |
|
48 */ |
|
49 CWapPushLog::~CWapPushLog() |
|
50 { |
|
51 } |
|
52 |
|
53 void CWapPushLog::ConstructL() |
|
54 { |
|
55 } |
|
56 |
|
57 /** |
|
58 * Printf |
|
59 * |
|
60 * @param aFmt |
|
61 * Printf formatting rules |
|
62 */ |
|
63 void CWapPushLog::WPLPrintf(const TDesC& aDescription) |
|
64 { |
|
65 // Write to log file |
|
66 |
|
67 if (iLogFilename.Length()) |
|
68 RFileLogger::Write(KPushLogDir, iLogFilename, EFileLoggingModeAppend, aDescription); |
|
69 else |
|
70 RFileLogger::Write(KPushLogDir, KPushLogFile, EFileLoggingModeAppend, aDescription); |
|
71 |
|
72 // Write to console |
|
73 _LIT(KPushLogFmt,"Push:%S\n"); |
|
74 TPtrC buf = aDescription.Left(Min(KWatcherLogBuffer, aDescription.Length())); |
|
75 iConsole.Printf(KPushLogFmt,&buf); |
|
76 } |
|
77 |
|
78 /** |
|
79 * Takes the data in a Push Message and prints it to console and the logs it to file. |
|
80 * Uses the accessor functions of the CPushMessage class and this classes Printf function |
|
81 * to perform this. Currently 4 headers and the message body are displayed and |
|
82 * logged. The headers are: |
|
83 * PushID, Content-Type, X-Application-ID, Expiry Date, |
|
84 * |
|
85 * @param CPushMessage& aMessage |
|
86 * in: a reference to a Push Message. |
|
87 */ |
|
88 void CWapPushLog::WPLPrintfL(CPushMessage& aMessage) |
|
89 { |
|
90 const TInt KWatcherLogBuffer=256; |
|
91 TBuf<KWatcherLogBuffer> buf; |
|
92 |
|
93 // Content type |
|
94 _LIT(KLogContentFmt,"Content Type : \t%S"); |
|
95 TPtrC contentPointer; |
|
96 aMessage.GetContentType(contentPointer); |
|
97 buf.Format(KLogContentFmt,&contentPointer); |
|
98 buf.Append('\n'); |
|
99 if( buf.Length() > 100 ) |
|
100 { |
|
101 // Flogger has a max of 150 (inc time stamp, and max buf is 256, if data is larger than 100 split onto |
|
102 // 2 lines |
|
103 WPLPrintf(buf.Left(100)); // Print first 150 chars |
|
104 WPLPrintf(buf.Mid(99)); // Print the rest |
|
105 } |
|
106 else |
|
107 { |
|
108 WPLPrintf(buf); |
|
109 } |
|
110 |
|
111 // Date Field |
|
112 _LIT(KLogDateTimeFmt," %-B%:0%J%:1%T%:2%S%+B %D %N %Y %4 %5 %3"); |
|
113 _LIT(KLogDateFmt,"Date :\t%S"); |
|
114 TBool foundField; |
|
115 TTime timeValue; |
|
116 foundField = aMessage.GetHeaderField(EHttpDate, timeValue); |
|
117 if (foundField) |
|
118 { |
|
119 TRAPD(err, timeValue.FormatL(buf, KLogDateTimeFmt)); |
|
120 if (err == KErrNone) |
|
121 { |
|
122 TBuf<KWatcherLogBuffer> dateBuf; |
|
123 dateBuf.Format(KLogDateFmt, &buf); |
|
124 WPLPrintf(dateBuf); |
|
125 } |
|
126 } |
|
127 |
|
128 //Expires Field |
|
129 aMessage.GetHeaderField(EHttpExpires, timeValue); |
|
130 if (foundField) |
|
131 { |
|
132 TRAPD(err, timeValue.FormatL(buf, KLogDateTimeFmt)); |
|
133 if (err == KErrNone) |
|
134 { |
|
135 TBuf<KWatcherLogBuffer> dateBuf; |
|
136 dateBuf.Format(KLogDateFmt, &buf); |
|
137 WPLPrintf(dateBuf); |
|
138 } |
|
139 } |
|
140 |
|
141 // X-Wap-Application-ID |
|
142 TBool isInt = EFalse; |
|
143 TInt ID = 0; |
|
144 TPtrC8 generalPtr; |
|
145 _LIT(KLogAppIdIntFmt,"App ID :\t%X"); |
|
146 TRAPD( error, foundField = aMessage.GetAppIdL(generalPtr, ID, isInt) ); |
|
147 if (error == KErrNone && foundField) // App ID is present |
|
148 { |
|
149 if (isInt) // Field in integer format |
|
150 { |
|
151 buf.Format(KLogAppIdIntFmt,ID); |
|
152 } |
|
153 else // should be descriptor format |
|
154 { |
|
155 buf.Copy(generalPtr); |
|
156 } |
|
157 WPLPrintf(buf); |
|
158 } |
|
159 |
|
160 // Server Address |
|
161 TPtrC8 serverAddr; |
|
162 _LIT(KLogServerAddressIntFmt,"Server Address : "); |
|
163 foundField = aMessage.GetServerAddress(serverAddr); |
|
164 if (foundField) // Server Address is present |
|
165 { |
|
166 buf.Copy(serverAddr); |
|
167 buf.Insert( 0, KLogServerAddressIntFmt); |
|
168 WPLPrintf(buf); |
|
169 } |
|
170 |
|
171 // Push ID |
|
172 TUint8 pushID=0; |
|
173 _LIT(KLogPushIDIntFmt,"PushID : %d"); |
|
174 aMessage.GetPushId(pushID); |
|
175 buf.Format(KLogPushIDIntFmt,pushID); |
|
176 WPLPrintf(buf); |
|
177 |
|
178 //Message Header Binary |
|
179 _LIT(KLogMsgHdr,"Header Binary:"); |
|
180 WPLPrintf(KLogMsgHdr); |
|
181 aMessage.GetHeader(generalPtr); |
|
182 |
|
183 HBufC* tempHdr = HBufC::NewLC(generalPtr.Length()); |
|
184 tempHdr->Des().Copy(generalPtr); |
|
185 WPLLogBinaryAsHex(*tempHdr); |
|
186 CleanupStack::PopAndDestroy(); //tempHdr |
|
187 |
|
188 //Message Body |
|
189 aMessage.GetMessageBody(generalPtr); |
|
190 |
|
191 // Dump Body As Text |
|
192 _LIT(KLogMsgBody,"Body Text:"); |
|
193 WPLPrintf(KLogMsgBody); |
|
194 HBufC* tempBody = HBufC::NewLC(generalPtr.Length()); |
|
195 tempBody->Des().Copy(generalPtr); |
|
196 WPLPrintf(*tempBody); |
|
197 |
|
198 // Dump Body As Hex |
|
199 _LIT(KBodyBinary,"\nBody Binary:"); |
|
200 WPLPrintf(KBodyBinary); |
|
201 WPLLogBinaryAsHex(*tempBody); |
|
202 |
|
203 CleanupStack::PopAndDestroy(); //tempBody |
|
204 } |
|
205 |
|
206 |
|
207 /** |
|
208 * Prints out Buffer data in the format: |
|
209 * %X %X %X %X %X %X %X\n etc |
|
210 * For example |
|
211 * AB CD 01 12 34 A2 |
|
212 * |
|
213 * @param aDescription |
|
214 * in: the descriptor to be dumped into the log |
|
215 */ |
|
216 void CWapPushLog::WPLLogBinaryAsHex(const TDesC& aDescription) |
|
217 { |
|
218 const TInt KWatcherLogBuffer=256; |
|
219 _LIT(KHexSpace,"%02X "); |
|
220 TBuf<KWatcherLogBuffer> hexBuf; |
|
221 TBuf<KWatcherLogBuffer> buf; |
|
222 |
|
223 TInt i = 0, bodyLen = aDescription.Length(); |
|
224 |
|
225 for (; i < bodyLen; i++) |
|
226 { |
|
227 hexBuf.Format(KHexSpace,aDescription[i]); |
|
228 buf.Append(hexBuf); |
|
229 if ( i && ((i+1) % 8) == 0 ) |
|
230 { |
|
231 WPLPrintf(buf); |
|
232 buf.Zero(); |
|
233 } |
|
234 } |
|
235 |
|
236 if (buf.Length()) |
|
237 WPLPrintf(buf); |
|
238 } |
|
239 void CWapPushLog::WPLLogError(const TDesC& aDescription,TInt aError) |
|
240 { |
|
241 _LIT(KErrorLogFmt,"Push: %S, Error\t%d"); |
|
242 const TInt KWatcherLogBuffer=256; |
|
243 TBuf<KWatcherLogBuffer> buf; |
|
244 buf.Format(KErrorLogFmt,aDescription,aError); |
|
245 buf.Append('\n'); |
|
246 WPLPrintf(buf); |
|
247 } |
|
248 |
|
249 |
|
250 void CWapPushLog::SetLogFileName(const TFileName aFilename) |
|
251 { |
|
252 iLogFilename=aFilename; |
|
253 } |
|
254 |
|
255 const TDesC& CWapPushLog::LogFileName() const |
|
256 { |
|
257 if (iLogFilename.Length()) |
|
258 return iLogFilename; |
|
259 else |
|
260 return KPushLogFile; |
|
261 } |