0
|
1 |
/*------------------------------------------------------------------
|
|
2 |
-
|
|
3 |
* Software Name : UserEmulator
|
|
4 |
* Version : v4.2.1309
|
|
5 |
*
|
|
6 |
* Copyright (c) 2009 France Telecom. All rights reserved.
|
|
7 |
* This software is distributed under the License
|
|
8 |
* "Eclipse Public License - v 1.0" the text of which is available
|
|
9 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
10 |
*
|
|
11 |
* Initial Contributors:
|
|
12 |
* France Telecom
|
|
13 |
*
|
|
14 |
* Contributors:
|
|
15 |
*------------------------------------------------------------------
|
|
16 |
-
|
|
17 |
* File Name: Logger.cpp
|
|
18 |
*
|
|
19 |
* Created: 13/08/2009
|
|
20 |
* Author(s): Marcell Kiss, Reshma Sandeep Das
|
|
21 |
*
|
|
22 |
* Description:
|
|
23 |
* This class is used for logging purposes
|
|
24 |
*------------------------------------------------------------------
|
|
25 |
-
|
|
26 |
*
|
|
27 |
*/
|
|
28 |
|
|
29 |
//System Includes
|
|
30 |
#include <eikenv.h> // CEikonEnv
|
|
31 |
#include <f32file.h> // TParsePtrC
|
|
32 |
#include <bautils.h> // BaflUtils
|
|
33 |
#include <aknnotewrappers.h>
|
|
34 |
|
|
35 |
//User Includes
|
|
36 |
#include "Logger.h"
|
|
37 |
#include "Settings.h"
|
|
38 |
|
|
39 |
//Constants
|
|
40 |
_LIT8(KTimeFormat,"%04d-%02d-%02d, %02d:%02d:%02d:%02d");
|
|
41 |
_LIT8(KHashTimeFormat,"#%04d-%02d-%02d, %02d:%02d:%02d:%02d");
|
|
42 |
_LIT(KDriveError, "Drive might be locked. Missing or incomplete log file possible.");
|
|
43 |
const TInt KTimeRecordSize = 26;
|
|
44 |
// -----------------------------------------------------------------------------
|
|
45 |
// CLogger::ConstructL
|
|
46 |
// -----------------------------------------------------------------------------
|
|
47 |
//
|
|
48 |
void CLogger::ConstructL()
|
|
49 |
{
|
|
50 |
TParsePtrC parser(iSettings.iLogPath);
|
|
51 |
BaflUtils::EnsurePathExistsL(iEEnv->FsSession(), parser.DriveAndPath());
|
|
52 |
User::LeaveIfError(iFs.Connect());
|
|
53 |
iDriveLockError = EFalse;
|
|
54 |
}
|
|
55 |
|
|
56 |
// -----------------------------------------------------------------------------
|
|
57 |
// CLogger::CLogger
|
|
58 |
// -----------------------------------------------------------------------------
|
|
59 |
//
|
|
60 |
CLogger::CLogger(CUserEmulatorScriptsView& aScriptView,CSettings& aSettings, CEikonEnv* aEikonEnv)
|
|
61 |
: iScriptView(aScriptView),iSettings(aSettings),iEEnv(aEikonEnv)
|
|
62 |
{
|
|
63 |
}
|
|
64 |
|
|
65 |
// -----------------------------------------------------------------------------
|
|
66 |
// CLogger::~CLogger
|
|
67 |
// -----------------------------------------------------------------------------
|
|
68 |
//
|
|
69 |
CLogger::~CLogger()
|
|
70 |
{
|
|
71 |
iFile.Close();
|
|
72 |
iFs.Close();
|
|
73 |
}
|
|
74 |
// -----------------------------------------------------------------------------
|
|
75 |
// CLogger::WriteLogL
|
|
76 |
// -----------------------------------------------------------------------------
|
|
77 |
//
|
|
78 |
void CLogger::WriteLogL(const TDesC16& aText, TBool aHeader, TBool aHash)
|
|
79 |
{
|
|
80 |
if(Write(aText,aHeader,aHash)!=KErrNone)
|
|
81 |
{
|
|
82 |
if(!iDriveLockError)
|
|
83 |
{
|
|
84 |
iDriveLockError=ETrue;
|
|
85 |
CAknErrorNote* errorNote = new (ELeave) CAknErrorNote(ETrue);
|
|
86 |
errorNote->ExecuteLD(KDriveError);
|
|
87 |
}
|
|
88 |
}
|
|
89 |
else
|
|
90 |
iDriveLockError=EFalse;
|
|
91 |
}
|
|
92 |
// -----------------------------------------------------------------------------
|
|
93 |
// CLogger::Write
|
|
94 |
// -----------------------------------------------------------------------------
|
|
95 |
//
|
|
96 |
TInt CLogger::Write(const TDesC16& aText, TBool aHeader, TBool aHash)
|
|
97 |
{
|
|
98 |
if( iSettings.iLogStatus)
|
|
99 |
{
|
|
100 |
TBool bExists = BaflUtils::FileExists(iFs, iSettings.iLogPath);
|
|
101 |
if(!bExists)
|
|
102 |
{
|
|
103 |
TParsePtrC parser(iSettings.iLogPath);
|
|
104 |
TRAPD(err, BaflUtils::EnsurePathExistsL(iEEnv->FsSession(), parser.DriveAndPath()));
|
|
105 |
if(err != KErrNone)
|
|
106 |
return err;
|
|
107 |
CloseFileHandles();
|
|
108 |
TInt err1=iFile.Replace(iFs, iSettings.iLogPath, EFileShareAny | EFileWrite);
|
|
109 |
if(err1 != KErrNone)
|
|
110 |
return err1;
|
|
111 |
|
|
112 |
TRAPD(err2,iScriptView.CmdGetSystemInfoL(ETrue));
|
|
113 |
}
|
|
114 |
else
|
|
115 |
{
|
|
116 |
//File exists..Check if the file is already open
|
|
117 |
TBool isOpen;
|
|
118 |
TInt err = iFs.IsFileOpen(iSettings.iLogPath,isOpen);
|
|
119 |
TInt err1=KErrNone;
|
|
120 |
if(err != KErrNone)
|
|
121 |
return err;
|
|
122 |
|
|
123 |
if(!isOpen)
|
|
124 |
{
|
|
125 |
err1=iFile.Replace(iFs, iSettings.iLogPath, EFileShareAny | EFileWrite);
|
|
126 |
if(err1!=KErrNone)
|
|
127 |
return err1;
|
|
128 |
TRAPD(err,iScriptView.CmdGetSystemInfoL(ETrue));
|
|
129 |
}
|
|
130 |
|
|
131 |
}
|
|
132 |
|
|
133 |
iLogBuffer.Copy(aText);
|
|
134 |
|
|
135 |
if(!aHeader)
|
|
136 |
{
|
|
137 |
TTime time;
|
|
138 |
time.HomeTime();
|
|
139 |
TDateTime dateTime;
|
|
140 |
dateTime = time.DateTime();
|
|
141 |
TBuf8<KTimeRecordSize> timeRecord;
|
|
142 |
if(aHash)
|
|
143 |
timeRecord.Format(KHashTimeFormat, dateTime.Year(), dateTime.Month()+1,
|
|
144 |
dateTime.Day()+1, dateTime.Hour(), dateTime.Minute(), dateTime.Second(),dateTime.MicroSecond()/1000);
|
|
145 |
else
|
|
146 |
timeRecord.Format(KTimeFormat, dateTime.Year(), dateTime.Month()+1,
|
|
147 |
dateTime.Day()+1, dateTime.Hour(), dateTime.Minute(), dateTime.Second(),dateTime.MicroSecond()/1000);
|
|
148 |
timeRecord.Append(KComma);
|
|
149 |
iLogBuffer.Insert(0, timeRecord);
|
|
150 |
}
|
|
151 |
TInt res=iFile.Write(iLogBuffer);
|
|
152 |
if(res!=KErrNone)
|
|
153 |
return res;
|
|
154 |
res=iFile.Flush();
|
|
155 |
if(res!=KErrNone)
|
|
156 |
return res;
|
|
157 |
}
|
|
158 |
return KErrNone;
|
|
159 |
}
|
|
160 |
// -----------------------------------------------------------------------------
|
|
161 |
// CLogger::WriteLogL
|
|
162 |
// -----------------------------------------------------------------------------
|
|
163 |
//
|
|
164 |
void CLogger::WriteLogL(const TDesC8& aText, TBool aHeader, TBool aHash)
|
|
165 |
{
|
|
166 |
if(Write(aText,aHeader,aHash)!=KErrNone)
|
|
167 |
{
|
|
168 |
if(!iDriveLockError)
|
|
169 |
{
|
|
170 |
iDriveLockError=ETrue;
|
|
171 |
CAknErrorNote* errorNote = new (ELeave) CAknErrorNote(ETrue);
|
|
172 |
errorNote->ExecuteLD(KDriveError);
|
|
173 |
}
|
|
174 |
}
|
|
175 |
else
|
|
176 |
iDriveLockError=EFalse;
|
|
177 |
}
|
|
178 |
// -----------------------------------------------------------------------------
|
|
179 |
// CLogger::Write
|
|
180 |
// -----------------------------------------------------------------------------
|
|
181 |
//
|
|
182 |
TInt CLogger::Write(const TDesC8& aText, TBool aHeader, TBool aHash)
|
|
183 |
{
|
|
184 |
if( iSettings.iLogStatus)
|
|
185 |
{
|
|
186 |
TBool bExists = BaflUtils::FileExists(iFs, iSettings.iLogPath);
|
|
187 |
if(!bExists)
|
|
188 |
{
|
|
189 |
TParsePtrC parser(iSettings.iLogPath);
|
|
190 |
TRAPD(err, BaflUtils::EnsurePathExistsL(iEEnv->FsSession(), parser.DriveAndPath()));
|
|
191 |
if(err != KErrNone)
|
|
192 |
return err;
|
|
193 |
CloseFileHandles();
|
|
194 |
TInt err1=iFile.Replace(iFs, iSettings.iLogPath, EFileShareAny | EFileWrite);
|
|
195 |
if(err1 != KErrNone)
|
|
196 |
return err1;
|
|
197 |
|
|
198 |
TRAPD(err2,iScriptView.CmdGetSystemInfoL(ETrue));
|
|
199 |
}
|
|
200 |
else
|
|
201 |
{
|
|
202 |
//File exists..Check if the file is already open
|
|
203 |
TBool isOpen;
|
|
204 |
TInt err = iFs.IsFileOpen(iSettings.iLogPath,isOpen);
|
|
205 |
TInt err1=KErrNone;
|
|
206 |
if(err != KErrNone)
|
|
207 |
return err;
|
|
208 |
|
|
209 |
if(!isOpen)
|
|
210 |
{
|
|
211 |
err1=iFile.Replace(iFs, iSettings.iLogPath, EFileShareAny | EFileWrite);
|
|
212 |
if(err1!=KErrNone)
|
|
213 |
return err1;
|
|
214 |
TRAPD(err,iScriptView.CmdGetSystemInfoL(ETrue));
|
|
215 |
}
|
|
216 |
}
|
|
217 |
iLogBuffer.Copy(aText);
|
|
218 |
|
|
219 |
//Log Date and time
|
|
220 |
_LIT8(KComma, ",");
|
|
221 |
_LIT8(KTimeFormat,"%04d-%02d-%02d %02d:%02d:%02d:%02d");
|
|
222 |
_LIT8(KHashTimeFormat,"#%04d-%02d-%02d %02d:%02d:%02d:%02d");
|
|
223 |
|
|
224 |
const TInt KTimeRecordSize = 20;
|
|
225 |
|
|
226 |
if(!aHeader)
|
|
227 |
{
|
|
228 |
TTime time;
|
|
229 |
time.HomeTime();
|
|
230 |
TDateTime dateTime;
|
|
231 |
dateTime = time.DateTime();
|
|
232 |
TBuf8<KTimeRecordSize> timeRecord;
|
|
233 |
if(aHash)
|
|
234 |
timeRecord.Format(KHashTimeFormat, dateTime.Year(), dateTime.Month()+1,
|
|
235 |
dateTime.Day()+1, dateTime.Hour(), dateTime.Minute(), dateTime.Second(),dateTime.MicroSecond()/1000);
|
|
236 |
else
|
|
237 |
timeRecord.Format(KTimeFormat, dateTime.Year(), dateTime.Month()+1,
|
|
238 |
dateTime.Day()+1, dateTime.Hour(), dateTime.Minute(), dateTime.Second(),dateTime.MicroSecond()/1000);
|
|
239 |
timeRecord.Append(KComma);
|
|
240 |
iLogBuffer.Insert(0, timeRecord);
|
|
241 |
}
|
|
242 |
|
|
243 |
TInt res=iFile.Write(iLogBuffer);
|
|
244 |
if(res!=KErrNone)
|
|
245 |
return res;
|
|
246 |
res=iFile.Flush();
|
|
247 |
if(res!=KErrNone)
|
|
248 |
return res;
|
|
249 |
|
|
250 |
}
|
|
251 |
return KErrNone;
|
|
252 |
}
|
|
253 |
// -----------------------------------------------------------------------------
|
|
254 |
// CLogger::CloseFileHandles
|
|
255 |
// -----------------------------------------------------------------------------
|
|
256 |
//
|
|
257 |
void CLogger::CloseFileHandles()
|
|
258 |
{
|
|
259 |
iFile.Close();
|
|
260 |
}
|