|
1 /* |
|
2 * Copyright (c) 2004 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: Part of TestConsole application. |
|
15 ** Methods for the class TFileUtil |
|
16 ** |
|
17 */ |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 #include <e32std.h> |
|
23 #include "FileUtil.h" |
|
24 #include "TestConsoleDef.h" |
|
25 |
|
26 |
|
27 //********************************************************************** |
|
28 // Method : TFileUtil::WriteLogFile() |
|
29 // Parameters: |
|
30 // Function: |
|
31 // Returns: |
|
32 //********************************************************************** |
|
33 |
|
34 TInt TFileUtil::WriteLogFile(const TDesC &name, const TDesC &p) |
|
35 { |
|
36 HBufC *msg = NULL; |
|
37 TRAPD(msgError, msg = HBufC::NewL(p.Length() + 50)); |
|
38 |
|
39 if(msgError != KErrNone) |
|
40 { |
|
41 return 1; |
|
42 } |
|
43 |
|
44 TPtr lineBuf(msg->Des()); |
|
45 lineBuf.Fill('\0', lineBuf.Length()); |
|
46 |
|
47 // get the current time and date |
|
48 TTime now; |
|
49 now.HomeTime(); |
|
50 TDateTime dateTime = now.DateTime() ; |
|
51 |
|
52 // add the current time and date |
|
53 lineBuf.Format(_L("%02d/%02d/%04d\t%02d:%02d:%02d:%02d\t"), |
|
54 dateTime.Day()+1, |
|
55 dateTime.Month()+1, |
|
56 dateTime.Year(), |
|
57 dateTime.Hour(), |
|
58 dateTime.Minute(), |
|
59 dateTime.Second(), |
|
60 (dateTime.MicroSecond() / 1000)); |
|
61 |
|
62 // followed by the formatted data |
|
63 // lineBuf.AppendFormatList(aFmt, aList, &overflow16 |
|
64 lineBuf.Append(p); |
|
65 lineBuf.Append(_L("")); |
|
66 lineBuf.Append(KTxtLineBreak); |
|
67 lineBuf.ZeroTerminate(); |
|
68 |
|
69 TFileUtil::LogMessage(name, lineBuf); |
|
70 delete msg; |
|
71 return KErrNone; |
|
72 } |
|
73 |
|
74 |
|
75 //********************************************************************** |
|
76 // Method : TFileUtil::LogMessage() |
|
77 // Parameters: |
|
78 // Function: |
|
79 // Returns: |
|
80 //********************************************************************** |
|
81 |
|
82 TInt TFileUtil::LogMessage(const TFileName& name, const TDesC& mess ) |
|
83 { |
|
84 // convert from unicode to 8 bit |
|
85 TBuf8<KMaxLogLineLength> lineBuf8; |
|
86 const TText* msgBuf = mess.Ptr(); |
|
87 for (TInt i = 0; i < mess.Length() ;i++) |
|
88 { |
|
89 lineBuf8.Append(STATIC_CAST(TText, msgBuf[i])); |
|
90 } |
|
91 |
|
92 RFs fs; |
|
93 RFile file; |
|
94 TFileName nameWithPath; |
|
95 |
|
96 nameWithPath = KDefaultPath; |
|
97 nameWithPath.Append( name ); |
|
98 nameWithPath.ZeroTerminate( ); |
|
99 TInt err = fs.Connect( ); |
|
100 |
|
101 if(err!=KErrNone) |
|
102 { |
|
103 return err; |
|
104 } |
|
105 |
|
106 err=file.Open(fs, nameWithPath, EFileStreamText|EFileWrite|EFileShareExclusive); |
|
107 if(err==KErrNotFound) |
|
108 { |
|
109 err=file.Create(fs, nameWithPath, EFileStreamText|EFileWrite|EFileShareExclusive); |
|
110 } |
|
111 if(err!=KErrNone) |
|
112 { |
|
113 return err; |
|
114 } |
|
115 |
|
116 TInt off(0); |
|
117 err=file.Seek(ESeekEnd, off); |
|
118 if(err!=KErrNone) |
|
119 { |
|
120 return err; |
|
121 } |
|
122 |
|
123 file.Write(lineBuf8); |
|
124 file.Flush(); |
|
125 file.Close(); |
|
126 fs.Close(); |
|
127 |
|
128 return KErrNone; |
|
129 } |
|
130 |
|
131 |
|
132 //********************************************************************** |
|
133 // Method : TFileUtil::WriteLogFile() |
|
134 // Parameters: |
|
135 // Function: |
|
136 // Returns: |
|
137 //********************************************************************** |
|
138 |
|
139 TInt TFileUtil::WriteLogFile(const TDesC& name, const TDesC8& p) |
|
140 { |
|
141 HBufC8* msg = NULL; |
|
142 TRAPD(msgError, msg = HBufC8::NewL(p.Length() + 50)); |
|
143 if(msgError != KErrNone) |
|
144 { |
|
145 return 1; |
|
146 } |
|
147 |
|
148 TPtr8 b( msg->Des() ); |
|
149 b.Fill('\0',b.Length()); |
|
150 TTime now; |
|
151 now.HomeTime(); |
|
152 TDateTime dateTime = now.DateTime() ; |
|
153 |
|
154 // add the current time and date |
|
155 b.AppendFormat(_L8("%02d/%02d/%04d\t%02d:%02d:%02d:%02d\t"), |
|
156 dateTime.Day()+1, |
|
157 dateTime.Month()+1, |
|
158 dateTime.Year(), |
|
159 dateTime.Hour(), |
|
160 dateTime.Minute(), |
|
161 dateTime.Second(), |
|
162 (dateTime.MicroSecond() / 1000)); |
|
163 |
|
164 b.Append(p); |
|
165 b.Append(_L8("\n")); |
|
166 b.ZeroTerminate(); |
|
167 |
|
168 TFileUtil::LogMessage(name, b); |
|
169 |
|
170 delete msg; |
|
171 return KErrNone; |
|
172 } |
|
173 |
|
174 |
|
175 //********************************************************************** |
|
176 // Method : TFileUtil::LogMessage() |
|
177 // Parameters: |
|
178 // Function: |
|
179 // Returns: |
|
180 //********************************************************************** |
|
181 TInt TFileUtil::LogMessage(const TFileName& name, const TDesC8& mess ) |
|
182 { |
|
183 RFs fs; |
|
184 RFile file; |
|
185 TFileName nameWithPath; |
|
186 |
|
187 nameWithPath = KDefaultPath; |
|
188 nameWithPath.Append( name ); |
|
189 nameWithPath.ZeroTerminate( ); |
|
190 TInt err = fs.Connect( ); |
|
191 |
|
192 if(err!=KErrNone) |
|
193 { |
|
194 return err; |
|
195 } |
|
196 |
|
197 err=file.Open(fs, nameWithPath, EFileStreamText|EFileWrite|EFileShareExclusive); |
|
198 if(err==KErrNotFound) |
|
199 { |
|
200 err=file.Create(fs, nameWithPath, EFileStreamText|EFileWrite|EFileShareExclusive); |
|
201 } |
|
202 |
|
203 if(err!=KErrNone) |
|
204 { |
|
205 return err; |
|
206 } |
|
207 |
|
208 TInt off(0); |
|
209 err=file.Seek(ESeekEnd, off); |
|
210 if(err!=KErrNone) |
|
211 { |
|
212 return err; |
|
213 } |
|
214 |
|
215 file.Write(mess); |
|
216 file.Flush(); |
|
217 file.Close(); |
|
218 fs.Close(); |
|
219 |
|
220 return KErrNone; |
|
221 } |
|
222 |
|
223 //********************************************************************** |
|
224 // Method : TFileUtil::InitLogFile() |
|
225 // Parameters: |
|
226 // Function: |
|
227 // Returns: |
|
228 //********************************************************************** |
|
229 TInt TFileUtil::InitLogFile( const TFileName& name ) |
|
230 { |
|
231 RFs fs; |
|
232 RFile file; |
|
233 TFileName nameWithPath; |
|
234 |
|
235 nameWithPath = KDefaultPath; |
|
236 nameWithPath.Append( name ); |
|
237 nameWithPath.ZeroTerminate( ); |
|
238 TInt err = fs.Connect( ); |
|
239 |
|
240 if( err != KErrNone ) |
|
241 { |
|
242 return err; |
|
243 } |
|
244 |
|
245 err = file.Replace( fs, nameWithPath, /*EFileStreamText|*/EFileWrite|EFileShareExclusive ); |
|
246 if(err!=KErrNone) |
|
247 { |
|
248 return err; |
|
249 } |
|
250 |
|
251 file.Close(); |
|
252 fs.Close(); |
|
253 |
|
254 return KErrNone; |
|
255 } |
|
256 |
|
257 |
|
258 //********************************************************************** |
|
259 // Method : TFileUtil::LogToFile() |
|
260 // Parameters: |
|
261 // Function: |
|
262 // Returns: |
|
263 //********************************************************************** |
|
264 |
|
265 void TFileUtil::LogToFile( const TDesC& aName, TRefByValue<const TDesC> aFmt, ... ) |
|
266 { |
|
267 VA_LIST aList; |
|
268 VA_START(aList, aFmt); |
|
269 |
|
270 // decode formatted data for display on console |
|
271 TBuf<KMaxLogLineLength> lineBuf; |
|
272 lineBuf.AppendFormatList(aFmt, aList, NULL);// &iOverflow16); |
|
273 |
|
274 // write to log file |
|
275 WriteLogFile(aName, lineBuf); |
|
276 |
|
277 VA_END(aList); |
|
278 } |
|
279 |
|
280 //********************************************************************** |
|
281 // Method : TFileUtil::LogMsg() |
|
282 // Parameters: |
|
283 // Function: |
|
284 // Returns: |
|
285 //********************************************************************** |
|
286 void TFileUtil::LogMsg( TDes& aDes, TRefByValue<const TDesC> aFmt, ...) |
|
287 { |
|
288 VA_LIST aList; |
|
289 VA_START(aList, aFmt); |
|
290 |
|
291 // Decode formatted data for display on console |
|
292 aDes.AppendFormatList(aFmt, aList, NULL); |
|
293 |
|
294 VA_END(aList); |
|
295 } |
|
296 |