|
1 /* |
|
2 * Copyright (c) 2000-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: Log file handler |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 |
|
20 #include "eventviewer2.h" |
|
21 #include "log_eventviewer.h" |
|
22 //#include "logcommon.h" |
|
23 |
|
24 /////////////////////////////////////////////////////////////////// |
|
25 // OpenLogFile |
|
26 // This function checks the state of the LogFile and tries to open |
|
27 // the file if it is not yet open. It also reads the header section |
|
28 // of the file and checks that the file is a correct log file. |
|
29 /////////////////////////////////////////////////////////////////// |
|
30 TInt CEventViewer::OpenLogFile() |
|
31 { |
|
32 TInt status = ReadLogFileHeader(); |
|
33 if (status != KErrNone) |
|
34 { |
|
35 return status; |
|
36 } |
|
37 |
|
38 TakeCopyOfFileHeader(iCurrFileHeader, iPreviousFileHeader); |
|
39 return KErrNone; |
|
40 } |
|
41 |
|
42 /////////////////////////////////////////////////////////////////// |
|
43 // CloseLogFile |
|
44 // This function closes the log file |
|
45 /////////////////////////////////////////////////////////////////// |
|
46 void CEventViewer::CloseLogFile() |
|
47 { |
|
48 ; |
|
49 } |
|
50 |
|
51 /////////////////////////////////////////////////////////////////// |
|
52 // DoDeleteLogFileL |
|
53 // This function deletes the event log file |
|
54 /////////////////////////////////////////////////////////////////// |
|
55 void CEventViewer::DoDeleteLogFileL() |
|
56 { |
|
57 CloseLogFile(); |
|
58 TInt status = iEventMediator.ClearEventLog(); |
|
59 LOG(Log::Printf(_L("Log cleared %d\n"), status)); |
|
60 if (status != KErrNone) |
|
61 { |
|
62 User::Leave(status); |
|
63 } |
|
64 } |
|
65 |
|
66 /////////////////////////////////////////////////////////////////// |
|
67 // ReadWholeLogFileToMemoryBuffer() |
|
68 // |
|
69 // This function reads the whole log file to a memory buffer |
|
70 // and takes a new version of file header. |
|
71 /////////////////////////////////////////////////////////////////// |
|
72 TInt CEventViewer::ReadWholeLogFileToMemoryBuffer() |
|
73 { |
|
74 TInt status; |
|
75 status = iEventMediator.GetEventLogSize(iLogFileSize); |
|
76 if (status != KErrNone) |
|
77 { |
|
78 return status; |
|
79 } |
|
80 if (iLogFileSize < EVENTLOG_FILE_HEADER_LTH) |
|
81 { |
|
82 return KErrNotFound; |
|
83 } |
|
84 |
|
85 delete iLogFileBuf; |
|
86 iLogFileBuf = 0; |
|
87 |
|
88 iLogFileBuf = HBufC8::New(iLogFileSize + 2048); // 2048: it can be bigger now |
|
89 if (iLogFileBuf == 0) |
|
90 { |
|
91 return KErrNoMemory; |
|
92 } |
|
93 |
|
94 TPtr8 ptr(iLogFileBuf->Des()); |
|
95 status = iEventMediator.GetEventLogData(ptr); |
|
96 if (status != KErrNone) |
|
97 { |
|
98 return status; |
|
99 } |
|
100 |
|
101 status = CopyPackedFileHeaderToUnpackedObject(ptr); |
|
102 if (status != KErrNone) |
|
103 { |
|
104 return status; |
|
105 } |
|
106 return KErrNone; |
|
107 } |
|
108 |
|
109 /////////////////////////////////////////////////////////////////// |
|
110 // ReadLogFileHeader |
|
111 // This function reads the packed format file header section of the |
|
112 // event log file and copies the contents of header to the |
|
113 // caller's unpacked object. |
|
114 /////////////////////////////////////////////////////////////////// |
|
115 TInt CEventViewer::ReadLogFileHeader() |
|
116 { |
|
117 TBuf8<EVENTLOG_FILE_HEADER_LTH> fileHeaderBuf; |
|
118 TInt status = iEventMediator.GetEventLogHeader(fileHeaderBuf); |
|
119 if (status != KErrNone) |
|
120 { |
|
121 return status; |
|
122 } |
|
123 |
|
124 status = CopyPackedFileHeaderToUnpackedObject(fileHeaderBuf); |
|
125 if (status != KErrNone) |
|
126 { |
|
127 return status; |
|
128 } |
|
129 |
|
130 return KErrNone; |
|
131 } |
|
132 |
|
133 /////////////////////////////////////////////////////////////////// |
|
134 // TakeCopyOfFileHeader |
|
135 // This function takes a copy of the unpacked file header. |
|
136 /////////////////////////////////////////////////////////////////// |
|
137 void CEventViewer::TakeCopyOfFileHeader(TUnpackedFileHeader& asrc, |
|
138 TUnpackedFileHeader& adest) |
|
139 { |
|
140 adest.iFileId = asrc.iFileId; |
|
141 adest.iPositionOfNextFree = asrc.iPositionOfNextFree; |
|
142 adest.iPositionOfWrapping = asrc.iPositionOfWrapping; |
|
143 adest.iCurrEventNumber = asrc.iCurrEventNumber; |
|
144 } |
|
145 |
|
146 /////////////////////////////////////////////////////////////////// |
|
147 // CopyPackedFileHeaderToUnpackedObject |
|
148 // This function copies the packet format file header to an |
|
149 // unpacked object. |
|
150 /////////////////////////////////////////////////////////////////// |
|
151 TInt CEventViewer::CopyPackedFileHeaderToUnpackedObject( TDesC8& aFileHeader) |
|
152 { |
|
153 |
|
154 // Convert the TDesC8 parameter to TUint8* format |
|
155 |
|
156 TFileHeader* fileHeader = (TFileHeader*) aFileHeader.Ptr(); |
|
157 |
|
158 // Copy the packet format header parameters to unpacked object |
|
159 |
|
160 iCurrFileHeader.iFileId = fileHeader->GetFileId(); |
|
161 iCurrFileHeader.iPositionOfNextFree = fileHeader->GetPositionOfNextFree(); |
|
162 iCurrFileHeader.iPositionOfWrapping = fileHeader->GetPositionOfWrapping(); |
|
163 iCurrFileHeader.iCurrEventNumber = fileHeader->GetCurrEventNumber(); |
|
164 |
|
165 // Check the file Id |
|
166 |
|
167 if (iCurrFileHeader.iFileId != LOG_FILE_ID) |
|
168 { |
|
169 return KErrNotFound; |
|
170 } |
|
171 |
|
172 return KErrNone; |
|
173 } |
|
174 |
|
175 /////////////////////////////////////////////////////////////////// |
|
176 // IsLogFileModified |
|
177 // Check if the logfile has been modified (file header is no more |
|
178 // the same as previously) |
|
179 /////////////////////////////////////////////////////////////////// |
|
180 TBool CEventViewer::IsLogFileModified() |
|
181 { |
|
182 |
|
183 if (iCurrFileHeader.iFileId == iPreviousFileHeader.iFileId |
|
184 && |
|
185 iCurrFileHeader.iPositionOfNextFree == iPreviousFileHeader.iPositionOfNextFree) |
|
186 { |
|
187 return EFalse; |
|
188 } |
|
189 return ETrue; |
|
190 } |
|
191 |
|
192 |
|
193 |
|
194 /////////////////////////////////////////////////////////////////// |
|
195 // GetStartPositionOfLogElem |
|
196 // This function calculates the start position of a log element. |
|
197 // As input parameter it has the position of the first byte |
|
198 // after the element. |
|
199 /////////////////////////////////////////////////////////////////// |
|
200 TInt CEventViewer::GetStartPositionOfLogElem(TUint32 aPositionOfLogElemEnd, |
|
201 TUint32* aPositionOfLogElem) |
|
202 { |
|
203 |
|
204 // Set base for the most recent log element trailer |
|
205 |
|
206 TPtr8 logFileBuf (iLogFileBuf->Des()); // Log file in memory |
|
207 TUint32 logElemTrailerPos = aPositionOfLogElemEnd - LOG_ELEM_TRAILER_LTH; |
|
208 |
|
209 if (iWrappingOccured && logElemTrailerPos <= iCurrFileHeader.iPositionOfNextFree) |
|
210 { |
|
211 |
|
212 return KErrNotFound; |
|
213 } |
|
214 |
|
215 TPtr8 elemTrailer (const_cast<TUint8*>(logFileBuf.Ptr())+ logElemTrailerPos, // Data ptr |
|
216 LOG_ELEM_TRAILER_LTH, // Data length |
|
217 LOG_ELEM_TRAILER_LTH); // Max length |
|
218 // Convert the TPtr8 parameter to TUint8* format |
|
219 |
|
220 TLogElemTrailer* elemTrailerPtr = (TLogElemTrailer*) elemTrailer.Ptr(); |
|
221 |
|
222 // Copy the packet format trailer parameters to unpacked object |
|
223 |
|
224 iLogElemTrailer.iEndMark1 = elemTrailerPtr->GetEndMark1(); |
|
225 iLogElemTrailer.iEndMark2 = elemTrailerPtr->GetEndMark2(); |
|
226 iLogElemTrailer.iEventLength = elemTrailerPtr->GetEventLength(); |
|
227 |
|
228 // Check the validity of trailer |
|
229 |
|
230 if (iLogElemTrailer.iEndMark1 != END_MARK_1 |
|
231 || |
|
232 iLogElemTrailer.iEndMark2 != END_MARK_2 |
|
233 || |
|
234 iLogElemTrailer.iEventLength > aPositionOfLogElemEnd + |
|
235 EVENTLOG_FILE_HEADER_LTH) |
|
236 { |
|
237 |
|
238 return KErrGeneral; |
|
239 } |
|
240 |
|
241 // Set position for the current log element |
|
242 |
|
243 TUint32 logElemPos = aPositionOfLogElemEnd - iLogElemTrailer.iEventLength; |
|
244 |
|
245 if (iWrappingOccured && logElemPos < iCurrFileHeader.iPositionOfNextFree) |
|
246 { |
|
247 |
|
248 return KErrNotFound; |
|
249 } |
|
250 |
|
251 TPtr8 logElem (const_cast<TUint8*>(logFileBuf.Ptr())+ logElemPos, // Data ptr |
|
252 iLogElemTrailer.iEventLength, // Data length |
|
253 iLogElemTrailer.iEventLength); |
|
254 |
|
255 // Convert the TPtr8 parameter to TUint8* format |
|
256 |
|
257 TLogElem* logElemPtr = (TLogElem*) logElem.Ptr(); |
|
258 |
|
259 // Copy the event number and event length parameters |
|
260 // from packed format log element to unpacked object |
|
261 |
|
262 iUnpackedLogElem.iEventLength = logElemPtr->GetEventLength(); |
|
263 iUnpackedLogElem.iEventNumber = logElemPtr->GetEventNumber(); |
|
264 |
|
265 // Verify the extracted data |
|
266 |
|
267 if (iUnpackedLogElem.iEventNumber > iCurrFileHeader.iCurrEventNumber |
|
268 || |
|
269 iUnpackedLogElem.iEventLength != iLogElemTrailer.iEventLength) |
|
270 { |
|
271 |
|
272 return KErrGeneral; |
|
273 } |
|
274 |
|
275 *aPositionOfLogElem = logElemPos; |
|
276 |
|
277 return KErrNone; |
|
278 |
|
279 } |
|
280 |
|
281 // |
|
282 // Event found, copy the packed format log element parameters to an |
|
283 // unpacked object |
|
284 // |
|
285 |
|
286 |
|
287 /////////////////////////////////////////////////////////////////// |
|
288 // CopyPackedFileHeaderToUnpackedObject |
|
289 // This function copies the packet format log file element to an |
|
290 // unpacked object. |
|
291 /////////////////////////////////////////////////////////////////// |
|
292 void CEventViewer::CopyPackedLogElemToUnpackedObject(TUint32 aPositionOfCurrLogElem) |
|
293 { |
|
294 // Build the log element pointer |
|
295 |
|
296 TPtr8 logFileBuf (iLogFileBuf->Des()); // Log file in memory |
|
297 TPtr8 logElem (const_cast<TUint8*>(logFileBuf.Ptr())+ aPositionOfCurrLogElem, // Data ptr |
|
298 iUnpackedLogElem.iEventLength, // Data length |
|
299 iUnpackedLogElem.iEventLength); // Max length |
|
300 |
|
301 // Convert the TPtr8 parameter to TUint8* format |
|
302 |
|
303 TLogElem* logElemPtr = (TLogElem*) logElem.Ptr(); |
|
304 |
|
305 |
|
306 // Copy the packet format header parameters to unpacked object |
|
307 |
|
308 iUnpackedLogElem.iMsgId = logElemPtr->GetMsgId(); |
|
309 iUnpackedLogElem.iTimeStamp = logElemPtr->GetTimeStamp(); |
|
310 iUnpackedLogElem.iSourceComponent = logElemPtr->GetSourceComponent(); |
|
311 iUnpackedLogElem.iCategory = logElemPtr->GetCategory(); |
|
312 iUnpackedLogElem.iDescrCount = logElemPtr->GetDescrCount(); |
|
313 |
|
314 } |
|
315 |