sl@0
|
1 |
// Copyright (c) 2002-2009 Nokia Corporation and/or its subsidiary(-ies).
|
sl@0
|
2 |
// All rights reserved.
|
sl@0
|
3 |
// This component and the accompanying materials are made available
|
sl@0
|
4 |
// under the terms of "Eclipse Public License v1.0"
|
sl@0
|
5 |
// which accompanies this distribution, and is available
|
sl@0
|
6 |
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
sl@0
|
7 |
//
|
sl@0
|
8 |
// Initial Contributors:
|
sl@0
|
9 |
// Nokia Corporation - initial contribution.
|
sl@0
|
10 |
//
|
sl@0
|
11 |
// Contributors:
|
sl@0
|
12 |
//
|
sl@0
|
13 |
// Description:
|
sl@0
|
14 |
//
|
sl@0
|
15 |
|
sl@0
|
16 |
// EPOC includes
|
sl@0
|
17 |
#include <e32base.h>
|
sl@0
|
18 |
|
sl@0
|
19 |
// Test system includes
|
sl@0
|
20 |
#include "LogFile.h"
|
sl@0
|
21 |
|
sl@0
|
22 |
|
sl@0
|
23 |
/*
|
sl@0
|
24 |
* destructor for CFileLogger.
|
sl@0
|
25 |
*/
|
sl@0
|
26 |
CFileLogger::~CFileLogger()
|
sl@0
|
27 |
{
|
sl@0
|
28 |
// make sure session to file server is closed
|
sl@0
|
29 |
Close();
|
sl@0
|
30 |
}
|
sl@0
|
31 |
|
sl@0
|
32 |
|
sl@0
|
33 |
/**
|
sl@0
|
34 |
*
|
sl@0
|
35 |
* Static constructor for CFileLogger.
|
sl@0
|
36 |
*
|
sl@0
|
37 |
* @return "CFileLogger*"
|
sl@0
|
38 |
* The constructed CFileLogger
|
sl@0
|
39 |
*
|
sl@0
|
40 |
* @xxxx
|
sl@0
|
41 |
*
|
sl@0
|
42 |
*/
|
sl@0
|
43 |
CFileLogger* CFileLogger::NewL()
|
sl@0
|
44 |
{
|
sl@0
|
45 |
CFileLogger* s = new(ELeave) CFileLogger;
|
sl@0
|
46 |
return s;
|
sl@0
|
47 |
}
|
sl@0
|
48 |
|
sl@0
|
49 |
/**
|
sl@0
|
50 |
*
|
sl@0
|
51 |
* Create and open a log file. The file is shareable, and a mutex
|
sl@0
|
52 |
* and file seeks employed in case more than one session is using
|
sl@0
|
53 |
* the file.
|
sl@0
|
54 |
*
|
sl@0
|
55 |
* @param "const TDesC& aDir"
|
sl@0
|
56 |
* The file path of the logfile to be created. If the path
|
sl@0
|
57 |
* does not exist, the logfile will not be created.
|
sl@0
|
58 |
*
|
sl@0
|
59 |
* @param "const TDesC& aName"
|
sl@0
|
60 |
* The file name of same.
|
sl@0
|
61 |
*
|
sl@0
|
62 |
* @xxxx
|
sl@0
|
63 |
*
|
sl@0
|
64 |
*/
|
sl@0
|
65 |
void CFileLogger::CreateLog(const TDesC& aDir, const TDesC& aName)
|
sl@0
|
66 |
{
|
sl@0
|
67 |
// if aDir or aName are empty, then panic
|
sl@0
|
68 |
__ASSERT_ALWAYS(aDir.Ptr() != NULL, User::Panic(_L("CFileLogger"), 0));
|
sl@0
|
69 |
__ASSERT_ALWAYS(aName.Ptr() != NULL, User::Panic(_L("CFileLogger"), 1));
|
sl@0
|
70 |
|
sl@0
|
71 |
|
sl@0
|
72 |
// if aDir doesn't exist, create it anyway
|
sl@0
|
73 |
TUint aAttValue;
|
sl@0
|
74 |
if (iFs.Att(aDir, aAttValue) != KErrNone)
|
sl@0
|
75 |
{
|
sl@0
|
76 |
TInt returnCode = iFs.MkDirAll(aDir);
|
sl@0
|
77 |
if (returnCode != KErrNone)
|
sl@0
|
78 |
{
|
sl@0
|
79 |
iEnabled = EFalse;
|
sl@0
|
80 |
User::Panic(_L("CFileLogger"), 2);
|
sl@0
|
81 |
}
|
sl@0
|
82 |
}
|
sl@0
|
83 |
|
sl@0
|
84 |
iEnabled = ETrue;
|
sl@0
|
85 |
iLogName = aName;
|
sl@0
|
86 |
|
sl@0
|
87 |
// create logfile mutex
|
sl@0
|
88 |
RMutex logMutex;
|
sl@0
|
89 |
logMutex.CreateGlobal(iLogName);
|
sl@0
|
90 |
|
sl@0
|
91 |
// make the correct file name
|
sl@0
|
92 |
TFileName logFile;
|
sl@0
|
93 |
logFile.Format(_L("%S%S"), &aDir, &aName); // aDir will be \\-terminated
|
sl@0
|
94 |
|
sl@0
|
95 |
// If the file does not exist, create it. If it does, truncate it
|
sl@0
|
96 |
// Open it for sharing
|
sl@0
|
97 |
TInt returnCode=iLogfile.Replace(iFs, logFile, EFileWrite | EFileStreamText | EFileShareAny);
|
sl@0
|
98 |
|
sl@0
|
99 |
// check if open fails
|
sl@0
|
100 |
if (returnCode==KErrNone)
|
sl@0
|
101 |
{
|
sl@0
|
102 |
//file has opened ok
|
sl@0
|
103 |
//add the start to the HTML
|
sl@0
|
104 |
iLogfile.Write(_L8("<html><body><pre>\n"));
|
sl@0
|
105 |
TInt dummy;
|
sl@0
|
106 |
iLogfile.Seek(ESeekEnd, dummy);
|
sl@0
|
107 |
}
|
sl@0
|
108 |
else
|
sl@0
|
109 |
iEnabled = EFalse;
|
sl@0
|
110 |
}
|
sl@0
|
111 |
|
sl@0
|
112 |
/**
|
sl@0
|
113 |
*
|
sl@0
|
114 |
* Close the logfile. The flag iEnabled will be clear if there
|
sl@0
|
115 |
* is no logfile to close.
|
sl@0
|
116 |
*
|
sl@0
|
117 |
* @xxxx
|
sl@0
|
118 |
*
|
sl@0
|
119 |
*/
|
sl@0
|
120 |
void CFileLogger::CloseLog()
|
sl@0
|
121 |
{
|
sl@0
|
122 |
|
sl@0
|
123 |
// this should be mutex-safe as each thread has its own handle to
|
sl@0
|
124 |
// the log file
|
sl@0
|
125 |
// if logging enabled, flush buffers
|
sl@0
|
126 |
if (iEnabled)
|
sl@0
|
127 |
{
|
sl@0
|
128 |
iLogfile.Flush();
|
sl@0
|
129 |
iLogfile.Close();
|
sl@0
|
130 |
iEnabled = EFalse;
|
sl@0
|
131 |
}
|
sl@0
|
132 |
|
sl@0
|
133 |
// disconnect from the file server
|
sl@0
|
134 |
iFs.Close();
|
sl@0
|
135 |
}
|
sl@0
|
136 |
|
sl@0
|
137 |
/**
|
sl@0
|
138 |
*
|
sl@0
|
139 |
* Write output to the logfile.
|
sl@0
|
140 |
*
|
sl@0
|
141 |
* @param "const TDesC& aMsg"
|
sl@0
|
142 |
* The message (string) to be written.
|
sl@0
|
143 |
*
|
sl@0
|
144 |
* @xxxx
|
sl@0
|
145 |
*
|
sl@0
|
146 |
*/
|
sl@0
|
147 |
void CFileLogger::WriteLog(const TDesC& aMsg)
|
sl@0
|
148 |
{
|
sl@0
|
149 |
|
sl@0
|
150 |
if (!iEnabled)
|
sl@0
|
151 |
return;
|
sl@0
|
152 |
|
sl@0
|
153 |
// convert from unicode to 8 bit
|
sl@0
|
154 |
TBuf8<KMaxLogLineLength> lineBuf8;
|
sl@0
|
155 |
const TText* msgBuf = aMsg.Ptr();
|
sl@0
|
156 |
for (TInt i = 0; i < aMsg.Length() ;i++)
|
sl@0
|
157 |
lineBuf8.Append(STATIC_CAST(TText, msgBuf[i]));
|
sl@0
|
158 |
|
sl@0
|
159 |
// wait on mutex
|
sl@0
|
160 |
RMutex logMutex;
|
sl@0
|
161 |
logMutex.OpenGlobal(iLogName);
|
sl@0
|
162 |
|
sl@0
|
163 |
logMutex.Wait();
|
sl@0
|
164 |
|
sl@0
|
165 |
// write to log file
|
sl@0
|
166 |
TInt dummy=0;
|
sl@0
|
167 |
iLogfile.Seek(ESeekEnd, dummy);
|
sl@0
|
168 |
iLogfile.Write(lineBuf8);
|
sl@0
|
169 |
// seek to end after write - in case another process or thread is also using the file
|
sl@0
|
170 |
dummy=0; //dummy is updated after a seek to contain the new file position. With ESeekEnd
|
sl@0
|
171 |
//it is used as an offest value to control the seek.
|
sl@0
|
172 |
iLogfile.Seek(ESeekEnd, dummy);
|
sl@0
|
173 |
|
sl@0
|
174 |
logMutex.Signal();
|
sl@0
|
175 |
logMutex.Close();
|
sl@0
|
176 |
}
|
sl@0
|
177 |
|
sl@0
|
178 |
/**
|
sl@0
|
179 |
*
|
sl@0
|
180 |
* Connect to the file server. Used as a helper function
|
sl@0
|
181 |
* by classes owning a CFileLogger, to ensure that a
|
sl@0
|
182 |
* file server session can be opened
|
sl@0
|
183 |
*
|
sl@0
|
184 |
* @return "TInt"
|
sl@0
|
185 |
* standard EPOC error code
|
sl@0
|
186 |
*
|
sl@0
|
187 |
* @xxxx
|
sl@0
|
188 |
*
|
sl@0
|
189 |
*/
|
sl@0
|
190 |
TInt CFileLogger::Connect()
|
sl@0
|
191 |
{
|
sl@0
|
192 |
TInt err = iFs.Connect();
|
sl@0
|
193 |
if (err == KErrNone)
|
sl@0
|
194 |
iFsOpen = ETrue;
|
sl@0
|
195 |
return err;
|
sl@0
|
196 |
}
|
sl@0
|
197 |
|
sl@0
|
198 |
/**
|
sl@0
|
199 |
*
|
sl@0
|
200 |
* Close a file server session. Used as a helper function
|
sl@0
|
201 |
* by classes owning a CFileLogger.
|
sl@0
|
202 |
*
|
sl@0
|
203 |
* @xxxx
|
sl@0
|
204 |
*
|
sl@0
|
205 |
*/
|
sl@0
|
206 |
void CFileLogger::Close()
|
sl@0
|
207 |
{
|
sl@0
|
208 |
if (iFsOpen)
|
sl@0
|
209 |
{
|
sl@0
|
210 |
iFs.Close();
|
sl@0
|
211 |
iFsOpen = EFalse;
|
sl@0
|
212 |
}
|
sl@0
|
213 |
}
|