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 |
#include "logservcli.h"
|
sl@0
|
17 |
|
sl@0
|
18 |
// System includes
|
sl@0
|
19 |
#include <e32math.h>
|
sl@0
|
20 |
#include <s32mem.h>
|
sl@0
|
21 |
|
sl@0
|
22 |
// User includes
|
sl@0
|
23 |
#include "LogServShared.h"
|
sl@0
|
24 |
|
sl@0
|
25 |
static TInt StartServer()
|
sl@0
|
26 |
//
|
sl@0
|
27 |
// Start the server process/thread which lives in an EPOCEXE object
|
sl@0
|
28 |
//
|
sl@0
|
29 |
{
|
sl@0
|
30 |
const TUidType serverUid(KNullUid,KNullUid,KServerUid3);
|
sl@0
|
31 |
|
sl@0
|
32 |
//
|
sl@0
|
33 |
// EPOC is easy, we just create a new server process. Simultaneous launching
|
sl@0
|
34 |
// of two such processes should be detected when the second one attempts to
|
sl@0
|
35 |
// create the server object, failing with KErrAlreadyExists.
|
sl@0
|
36 |
//
|
sl@0
|
37 |
RProcess server;
|
sl@0
|
38 |
TInt r=server.Create(KLogServerName,KNullDesC,serverUid);
|
sl@0
|
39 |
if (r == KErrNone)
|
sl@0
|
40 |
server.SetPriority(EPriorityForeground);
|
sl@0
|
41 |
|
sl@0
|
42 |
if (r!=KErrNone)
|
sl@0
|
43 |
return r;
|
sl@0
|
44 |
|
sl@0
|
45 |
TRequestStatus stat;
|
sl@0
|
46 |
server.Rendezvous(stat);
|
sl@0
|
47 |
if (stat!=KRequestPending)
|
sl@0
|
48 |
server.Kill(0); // abort startup
|
sl@0
|
49 |
else
|
sl@0
|
50 |
server.Resume(); // logon OK - start the server
|
sl@0
|
51 |
User::WaitForRequest(stat); // wait for start or death
|
sl@0
|
52 |
// we can't use the 'exit reason' if the server panicked as this
|
sl@0
|
53 |
// is the panic 'reason' and may be '0' which cannot be distinguished
|
sl@0
|
54 |
// from KErrNone
|
sl@0
|
55 |
r=(server.ExitType()==EExitPanic) ? KErrGeneral : stat.Int();
|
sl@0
|
56 |
server.Close();
|
sl@0
|
57 |
return r;
|
sl@0
|
58 |
|
sl@0
|
59 |
}
|
sl@0
|
60 |
|
sl@0
|
61 |
// Initialise the operation and view ids
|
sl@0
|
62 |
RLogSession::RLogSession()
|
sl@0
|
63 |
: iOperationId(KLogNullOperationId + 1), iViewId(KLogNullViewId + 1)
|
sl@0
|
64 |
{
|
sl@0
|
65 |
}
|
sl@0
|
66 |
|
sl@0
|
67 |
TInt RLogSession::Connect()
|
sl@0
|
68 |
//
|
sl@0
|
69 |
// Connect to the server, attempting to start it if necessary
|
sl@0
|
70 |
//
|
sl@0
|
71 |
{
|
sl@0
|
72 |
TInt retry=2;
|
sl@0
|
73 |
for (;;)
|
sl@0
|
74 |
{
|
sl@0
|
75 |
TInt r=CreateSession(KLogServerFullName,TVersion(0,0,0));
|
sl@0
|
76 |
if (r!=KErrNotFound && r!=KErrServerTerminated)
|
sl@0
|
77 |
return r;
|
sl@0
|
78 |
if (--retry==0)
|
sl@0
|
79 |
return r;
|
sl@0
|
80 |
r=StartServer();
|
sl@0
|
81 |
if (r!=KErrNone && r!=KErrAlreadyExists)
|
sl@0
|
82 |
return r;
|
sl@0
|
83 |
}
|
sl@0
|
84 |
}
|
sl@0
|
85 |
|
sl@0
|
86 |
|
sl@0
|
87 |
void RLogSession::Send(TInt aType, const TIpcArgs& aArgs, TRequestStatus& aStatus) const
|
sl@0
|
88 |
{
|
sl@0
|
89 |
SendWithRetryAsync(aType, aArgs, aStatus);
|
sl@0
|
90 |
}
|
sl@0
|
91 |
|
sl@0
|
92 |
TInt RLogSession::Send(TInt aType, const TIpcArgs& aArgs) const
|
sl@0
|
93 |
{
|
sl@0
|
94 |
return SendWithRetry(aType, aArgs);
|
sl@0
|
95 |
}
|
sl@0
|
96 |
|
sl@0
|
97 |
TInt RLogSession::SendWithRetry(TInt aType, const TIpcArgs& aParam) const
|
sl@0
|
98 |
{
|
sl@0
|
99 |
TInt ret = SendReceive(aType, aParam);
|
sl@0
|
100 |
if (ret < KErrNone)
|
sl@0
|
101 |
{
|
sl@0
|
102 |
if(ret == KErrServerTerminated || ret == KErrNotFound)
|
sl@0
|
103 |
{
|
sl@0
|
104 |
// Try to reconnect
|
sl@0
|
105 |
const_cast<RLogSession&>(*this).Close();
|
sl@0
|
106 |
ret = const_cast<RLogSession&>(*this).Connect();
|
sl@0
|
107 |
if(ret == KErrNone)
|
sl@0
|
108 |
{
|
sl@0
|
109 |
// Try to resend
|
sl@0
|
110 |
ret = SendReceive(aType, aParam);
|
sl@0
|
111 |
}
|
sl@0
|
112 |
}
|
sl@0
|
113 |
}
|
sl@0
|
114 |
return ret;
|
sl@0
|
115 |
}
|
sl@0
|
116 |
|
sl@0
|
117 |
|
sl@0
|
118 |
void RLogSession::SendWithRetryAsync(TInt aType, const TIpcArgs& aParam, TRequestStatus& aStatus) const
|
sl@0
|
119 |
{
|
sl@0
|
120 |
SendReceive(aType, aParam, aStatus);
|
sl@0
|
121 |
}
|
sl@0
|
122 |
|
sl@0
|
123 |
|
sl@0
|
124 |
|
sl@0
|
125 |
|
sl@0
|
126 |
|
sl@0
|
127 |
|
sl@0
|
128 |
|
sl@0
|
129 |
|
sl@0
|
130 |
|
sl@0
|
131 |
|
sl@0
|
132 |
|
sl@0
|
133 |
#ifdef LOGGING_ENABLED
|
sl@0
|
134 |
|
sl@0
|
135 |
const TInt KLogEngLogBufferSize = 256;
|
sl@0
|
136 |
|
sl@0
|
137 |
void Log::New()
|
sl@0
|
138 |
{
|
sl@0
|
139 |
_LIT(KNewLogText, "===== NEW LOG =====");
|
sl@0
|
140 |
//
|
sl@0
|
141 |
RFileLogger logger;
|
sl@0
|
142 |
TInt ret=logger.Connect();
|
sl@0
|
143 |
if (ret==KErrNone)
|
sl@0
|
144 |
{
|
sl@0
|
145 |
logger.CreateLog(KLogFolder, KLogFileName, EFileLoggingModeOverwrite);
|
sl@0
|
146 |
logger.Write(KNewLogText);
|
sl@0
|
147 |
}
|
sl@0
|
148 |
logger.Close();
|
sl@0
|
149 |
}
|
sl@0
|
150 |
|
sl@0
|
151 |
void Log::Write(const TDesC& aText)
|
sl@0
|
152 |
{
|
sl@0
|
153 |
PruneLogFile();
|
sl@0
|
154 |
|
sl@0
|
155 |
RFileLogger logger;
|
sl@0
|
156 |
TInt ret=logger.Connect();
|
sl@0
|
157 |
if (ret==KErrNone)
|
sl@0
|
158 |
{
|
sl@0
|
159 |
logger.SetDateAndTime(EFalse,EFalse);
|
sl@0
|
160 |
logger.CreateLog(KLogFolder, KLogFileName,EFileLoggingModeAppend);
|
sl@0
|
161 |
TBuf<KLogEngLogBufferSize> buf;
|
sl@0
|
162 |
|
sl@0
|
163 |
// The debug log uses hometime rather than UTC for its timestamps. This is
|
sl@0
|
164 |
// purely a debugging aid.
|
sl@0
|
165 |
TTime now;
|
sl@0
|
166 |
now.HomeTime();
|
sl@0
|
167 |
|
sl@0
|
168 |
TDateTime dateTime;
|
sl@0
|
169 |
dateTime = now.DateTime();
|
sl@0
|
170 |
buf.Format(KTimeFormat,dateTime.Hour(),dateTime.Minute(),dateTime.Second(),dateTime.MicroSecond());
|
sl@0
|
171 |
buf.AppendFormat(KTextFormat,&aText);
|
sl@0
|
172 |
|
sl@0
|
173 |
logger.Write(buf);
|
sl@0
|
174 |
}
|
sl@0
|
175 |
|
sl@0
|
176 |
logger.Close();
|
sl@0
|
177 |
}
|
sl@0
|
178 |
|
sl@0
|
179 |
void Log::WriteFormat(TRefByValue<const TDesC> aFmt,...)
|
sl@0
|
180 |
{
|
sl@0
|
181 |
VA_LIST list;
|
sl@0
|
182 |
VA_START(list,aFmt);
|
sl@0
|
183 |
|
sl@0
|
184 |
PruneLogFile();
|
sl@0
|
185 |
|
sl@0
|
186 |
TBuf<2*KLogEngLogBufferSize> buf;
|
sl@0
|
187 |
buf.SetMax();
|
sl@0
|
188 |
buf.FillZ();
|
sl@0
|
189 |
|
sl@0
|
190 |
// The debug log uses hometime rather than UTC for its timestamps. This is
|
sl@0
|
191 |
// purely a debugging aid.
|
sl@0
|
192 |
TTime now;
|
sl@0
|
193 |
now.HomeTime();
|
sl@0
|
194 |
|
sl@0
|
195 |
TDateTime dateTime;
|
sl@0
|
196 |
dateTime = now.DateTime();
|
sl@0
|
197 |
buf.Format(KTimeFormat,dateTime.Hour(),dateTime.Minute(),dateTime.Second(),dateTime.MicroSecond());
|
sl@0
|
198 |
buf.AppendFormatList(aFmt, list );
|
sl@0
|
199 |
|
sl@0
|
200 |
RFileLogger logger;
|
sl@0
|
201 |
TInt ret=logger.Connect();
|
sl@0
|
202 |
if (ret==KErrNone)
|
sl@0
|
203 |
{
|
sl@0
|
204 |
logger.SetDateAndTime(EFalse,EFalse);
|
sl@0
|
205 |
logger.CreateLog(KLogFolder, KLogFileName,EFileLoggingModeAppend);
|
sl@0
|
206 |
logger.Write(buf);
|
sl@0
|
207 |
}
|
sl@0
|
208 |
|
sl@0
|
209 |
logger.Close();
|
sl@0
|
210 |
}
|
sl@0
|
211 |
|
sl@0
|
212 |
void Log::PruneLogFile()
|
sl@0
|
213 |
{
|
sl@0
|
214 |
const TInt KMaxLogSize = 1024 * 500;
|
sl@0
|
215 |
//
|
sl@0
|
216 |
_LIT(KBaseFolder, "_:\\Logs\\");
|
sl@0
|
217 |
TFileName fileName(KBaseFolder);
|
sl@0
|
218 |
fileName[0] = 'A' + static_cast<TInt>(RFs::GetSystemDrive());
|
sl@0
|
219 |
fileName.Append(KLogFolder);
|
sl@0
|
220 |
fileName.Append(KPathDelimiter);
|
sl@0
|
221 |
fileName.Append(KLogFileName);
|
sl@0
|
222 |
//
|
sl@0
|
223 |
RFs fsSession;
|
sl@0
|
224 |
if (fsSession.Connect() == KErrNone)
|
sl@0
|
225 |
{
|
sl@0
|
226 |
TEntry entry;
|
sl@0
|
227 |
if (fsSession.Entry(fileName, entry) == KErrNone)
|
sl@0
|
228 |
{
|
sl@0
|
229 |
// Check size and delete if its too big
|
sl@0
|
230 |
if (entry.iSize >= KMaxLogSize)
|
sl@0
|
231 |
{
|
sl@0
|
232 |
TInt fileDeleteErr=fsSession.Delete(fileName);
|
sl@0
|
233 |
|
sl@0
|
234 |
// If a debug build - record error
|
sl@0
|
235 |
#ifdef _DEBUG
|
sl@0
|
236 |
if (fileDeleteErr != KErrNone)
|
sl@0
|
237 |
{
|
sl@0
|
238 |
RDebug::Print(_L("Log::PruneLogFile - Failed to delete file. Error = %d"), fileDeleteErr);
|
sl@0
|
239 |
}
|
sl@0
|
240 |
#else
|
sl@0
|
241 |
(void)fileDeleteErr;
|
sl@0
|
242 |
#endif
|
sl@0
|
243 |
}
|
sl@0
|
244 |
}
|
sl@0
|
245 |
}
|
sl@0
|
246 |
fsSession.Close();
|
sl@0
|
247 |
}
|
sl@0
|
248 |
|
sl@0
|
249 |
#endif
|
sl@0
|
250 |
|