|
1 // Copyright (c) 2002-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 // $Header$ |
|
15 // CCmdLog.cpp |
|
16 // rev: mjdavey, symbian@mjdss.com, July 2002 |
|
17 // for: Typhoon (7.0s) & JetStream (8.0) |
|
18 // This module implements the CLogFile class |
|
19 // Include Files |
|
20 // |
|
21 // |
|
22 |
|
23 #include <e32std.h> // |
|
24 #include <s32file.h> // |
|
25 #include <f32file.h> // |
|
26 #include <e32hal.h> // |
|
27 #include <fbs.h> // |
|
28 |
|
29 //----------------------------------------------------------------------------- |
|
30 |
|
31 #include "CLogFile.h" // This module |
|
32 |
|
33 //----------------------------------------------------------------------------- |
|
34 // End-of-line |
|
35 |
|
36 _LIT8(KEndOfLine8,"\r\n"); |
|
37 _LIT (KEndOfLine ,"\r\n"); |
|
38 |
|
39 //----------------------------------------------------------------------------- |
|
40 |
|
41 void TLogFileOverflow::Overflow( TDes16& aDes ) |
|
42 { |
|
43 _LIT(KErrOverflowMsg, "..."); |
|
44 if( aDes.MaxLength() >= KErrOverflowMsg().Length() + aDes.Length() ) |
|
45 aDes.Append(KErrOverflowMsg); |
|
46 } |
|
47 |
|
48 //----------------------------------------------------------------------------- |
|
49 |
|
50 CLogFile *CLogFile::NewL( TInt aMaxLength ) |
|
51 { |
|
52 CLogFile* self = NewLC( aMaxLength ); |
|
53 CleanupStack::Pop(); |
|
54 return self; |
|
55 } |
|
56 |
|
57 //----------------------------------------------------------------------------- |
|
58 |
|
59 CLogFile* CLogFile::NewLC( TInt aMaxLength ) |
|
60 { |
|
61 CLogFile* self = new (ELeave) CLogFile(); |
|
62 CleanupStack::PushL(self); |
|
63 self->ConstructL( aMaxLength ); |
|
64 return self; |
|
65 } |
|
66 |
|
67 //----------------------------------------------------------------------------- |
|
68 |
|
69 CLogFile::~CLogFile( ) |
|
70 { |
|
71 Close(); |
|
72 delete iBuffer; |
|
73 iBuffer = NULL; |
|
74 } |
|
75 |
|
76 //----------------------------------------------------------------------------- |
|
77 |
|
78 void CLogFile::ConstructL( TInt aMaxLength ) |
|
79 { |
|
80 iIsOpen = EFalse; |
|
81 iError = KErrNone; |
|
82 iFileName.Zero(); |
|
83 iBufLength = aMaxLength; |
|
84 iBuffer = NULL; |
|
85 } |
|
86 |
|
87 //----------------------------------------------------------------------------- |
|
88 |
|
89 TInt CLogFile::Open( const TDesC& aFileName ) |
|
90 { |
|
91 iError = KErrNone; |
|
92 if ( IsOpen() ) |
|
93 DoClose( ); |
|
94 |
|
95 iFileName.Copy( aFileName ); |
|
96 iError = DoOpen(); |
|
97 |
|
98 return iError; |
|
99 } |
|
100 |
|
101 //----------------------------------------------------------------------------- |
|
102 |
|
103 TInt CLogFile::Open( ) |
|
104 { |
|
105 iError = (!IsOpen()) ? DoOpen() : KErrNone; |
|
106 return iError; |
|
107 } |
|
108 |
|
109 //----------------------------------------------------------------------------- |
|
110 |
|
111 void CLogFile::Close( ) |
|
112 { |
|
113 if ( IsOpen() ) |
|
114 DoClose(); |
|
115 |
|
116 delete iBuffer; |
|
117 iBuffer = NULL; |
|
118 } |
|
119 |
|
120 //----------------------------------------------------------------------------- |
|
121 |
|
122 TBool CLogFile::IsOpen( ) |
|
123 { |
|
124 return iIsOpen; |
|
125 } |
|
126 |
|
127 //----------------------------------------------------------------------------- |
|
128 |
|
129 TPtrC CLogFile::FileName( ) const |
|
130 { |
|
131 TPtrC filename( iFileName ); |
|
132 return filename; |
|
133 } |
|
134 |
|
135 //----------------------------------------------------------------------------- |
|
136 |
|
137 TInt CLogFile::Error( ) |
|
138 { |
|
139 return iError; |
|
140 } |
|
141 |
|
142 //----------------------------------------------------------------------------- |
|
143 |
|
144 void CLogFile::PrintHeading( ) |
|
145 { |
|
146 iError = KErrNone; |
|
147 if ( IsOpen() ) |
|
148 { |
|
149 iError = DoHeading(); |
|
150 (void)DoFlush(); |
|
151 } |
|
152 } |
|
153 |
|
154 //----------------------------------------------------------------------------- |
|
155 |
|
156 void CLogFile::Write( const TDesC& aText ) |
|
157 { |
|
158 iError = KErrNone; |
|
159 if ( IsOpen() ) |
|
160 { |
|
161 iError = DoWrite( aText ); |
|
162 (void)DoFlush(); |
|
163 } |
|
164 } |
|
165 |
|
166 //----------------------------------------------------------------------------- |
|
167 |
|
168 void CLogFile::Writeln( const TDesC& aText ) |
|
169 { |
|
170 iError = KErrNone; |
|
171 if ( IsOpen() ) |
|
172 { |
|
173 iError = DoWrite( aText ); |
|
174 (void)DoWrite( KEndOfLine8 ); |
|
175 (void)DoFlush(); |
|
176 } |
|
177 } |
|
178 |
|
179 //----------------------------------------------------------------------------- |
|
180 |
|
181 void CLogFile::Writeln( ) |
|
182 { |
|
183 iError = (IsOpen()) ? DoWrite( KEndOfLine8 ) : KErrNone; |
|
184 } |
|
185 |
|
186 //----------------------------------------------------------------------------- |
|
187 |
|
188 void CLogFile::Printf( TRefByValue<const TDesC> aFmt, ... ) |
|
189 { |
|
190 VA_LIST list; |
|
191 VA_START(list, aFmt); |
|
192 Printf(aFmt, list); |
|
193 } |
|
194 |
|
195 //----------------------------------------------------------------------------- |
|
196 |
|
197 void CLogFile::Printf(TRefByValue<const TDesC> aFmt, VA_LIST& aList) |
|
198 { |
|
199 iError = KErrNone; |
|
200 if (IsOpen()) |
|
201 { |
|
202 iError = DoWrite(aFmt, aList); |
|
203 (void)DoFlush(); |
|
204 } |
|
205 } |
|
206 |
|
207 //----------------------------------------------------------------------------- |
|
208 |
|
209 TInt CLogFile::DoOpen ( ) |
|
210 { |
|
211 // Allocate the formatting buffer if there is none so far. |
|
212 if ( iBuffer == NULL ) |
|
213 iBuffer = HBufC::New( iBufLength ); |
|
214 |
|
215 if ( iBuffer == NULL ) |
|
216 return KErrNoMemory; |
|
217 |
|
218 // Connect file server. |
|
219 TInt ret = KErrNone; |
|
220 if ( ret = iFs.Connect(), ret != KErrNone ) |
|
221 return ret; |
|
222 |
|
223 // Open/Create the file. Notice, that this does not create the log |
|
224 // directory i.e. the directory must have been created in advance. |
|
225 TUint mode = EFileStreamText|EFileWrite|EFileShareAny; |
|
226 if ( ret = iFile.Open( iFs, iFileName, mode ), ret == KErrNotFound ) |
|
227 // Create the file since it wasn't there. |
|
228 ret = iFile.Create( iFs, iFileName, mode ); |
|
229 |
|
230 if ( ret != KErrNone ) |
|
231 { |
|
232 iFs.Close(); |
|
233 return ret; |
|
234 } |
|
235 |
|
236 // Seek to the end; error in seek is not recorded. |
|
237 TInt filepos; |
|
238 (void)iFile.Seek( ESeekEnd, filepos ); |
|
239 |
|
240 // Mark the file open and return. |
|
241 iIsOpen = ETrue; |
|
242 return ret; |
|
243 } |
|
244 |
|
245 //----------------------------------------------------------------------------- |
|
246 |
|
247 void CLogFile::DoClose( ) |
|
248 { |
|
249 iFile.Close(); |
|
250 iFs.Close(); |
|
251 iIsOpen = EFalse; |
|
252 } |
|
253 |
|
254 //----------------------------------------------------------------------------- |
|
255 |
|
256 TInt CLogFile::DoFlush( ) |
|
257 { |
|
258 return iFile.Flush(); |
|
259 } |
|
260 |
|
261 //----------------------------------------------------------------------------- |
|
262 |
|
263 TInt CLogFile::DoHeading( ) |
|
264 { |
|
265 // Make a temporary buffer to format the date and time and format. |
|
266 HBufC* buf = HBufC::New( 80 ); |
|
267 if ( buf == NULL ) |
|
268 return KErrNoMemory; |
|
269 |
|
270 TPtr timebuf = buf->Des(); |
|
271 TPtr buffer = iBuffer->Des(); |
|
272 TTime time; |
|
273 time.HomeTime(); |
|
274 TInt ret = KErrNone; |
|
275 TRAP(ret, time.FormatL( timebuf, _L("%H:%T:%S on %E, %F%D-%*M-%Y")) ); |
|
276 if ( ret == KErrNone ) |
|
277 // Then format the standard header (file name and date/time). An |
|
278 // overflow in here does not cause panic I hope. |
|
279 TRAP( ret, |
|
280 buffer.AppendFormat( _L("%S%S - %S%S%S"), |
|
281 &iOverflow, |
|
282 &KEndOfLine, |
|
283 &iFileName, |
|
284 &timebuf, |
|
285 &KEndOfLine, |
|
286 &KEndOfLine ) |
|
287 ); |
|
288 |
|
289 // Delete the temporary buffer and if formatting went ok, write. |
|
290 delete buf; |
|
291 |
|
292 if ( ret == KErrNone ) |
|
293 ret = DoWrite( buffer ); |
|
294 return ret; |
|
295 } |
|
296 |
|
297 //----------------------------------------------------------------------------- |
|
298 |
|
299 TInt CLogFile::DoWrite(TRefByValue<const TDesC> aFmt, VA_LIST& aList) |
|
300 { |
|
301 // Format to the buffer and write. Overflow in formatting is captured |
|
302 // in order to not cause any panic. |
|
303 TPtr buffer = iBuffer->Des(); |
|
304 buffer.Zero(); |
|
305 TInt ret = KErrNone; |
|
306 TRAP(ret, buffer.AppendFormatList(aFmt, aList, &iOverflow)); |
|
307 if (ret == KErrNone) |
|
308 ret = DoWrite(buffer); |
|
309 return ret; |
|
310 } |
|
311 |
|
312 //----------------------------------------------------------------------------- |
|
313 |
|
314 TInt CLogFile::DoWrite( const TDesC& aText ) |
|
315 { |
|
316 #ifndef _UNICODE |
|
317 |
|
318 // Just write as such. |
|
319 return iFile.Write ( aText ); |
|
320 |
|
321 #else |
|
322 |
|
323 // Convert to 8-bit and write. Every write allocates a temporary buffer |
|
324 // from heap and that buffer is allocated long enough to write the text |
|
325 // as the whole. |
|
326 HBufC8* buf8 = HBufC8::New( aText.Length() ); |
|
327 if ( buf8 == NULL ) |
|
328 return KErrNoMemory; |
|
329 TPtr8 ptr8 = buf8->Des(); |
|
330 ptr8.Copy( aText ); |
|
331 TInt ret = iFile.Write( ptr8 ); |
|
332 |
|
333 delete buf8; |
|
334 return ret; |
|
335 |
|
336 #endif // _UNICODE |
|
337 } |
|
338 |
|
339 //----------------------------------------------------------------------------- |
|
340 |
|
341 #ifdef _UNICODE |
|
342 |
|
343 TInt CLogFile::DoWrite( const TDesC8& aText ) |
|
344 { |
|
345 return iFile.Write( aText ); |
|
346 } |
|
347 |
|
348 #endif // _UNICODE |
|
349 |
|
350 //----------------------------------------------------------------------------- |
|
351 // End of File |
|
352 //----------------------------------------------------------------------------- |
|
353 |