Update contrib.
1 // Copyright (c) 1997-2010 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".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
27 const TInt KOpenLogFilesGranularity=5;
29 /* Internal error. Use positive number to ensure no conflict with other error code
30 Indicate that client message has panic'ed and should not be completed.
32 const TInt KErrMessagePanic = 1;
41 ELogDirectoryNameDoesNotExist =1, ///< panic if no log directory name
42 ELogFileNameDoesNotExist ///< panic if file name doesnot exist
46 CFileLoggerServer class definition
49 CFileLoggerServer* CFileLoggerServer::NewL()
51 Creates new CFileLoggerServer object
53 @return Pointer to CFileLoggerServer object
57 CFileLoggerServer* r=new(ELeave) CFileLoggerServer();
58 CleanupStack::PushL(r);
60 r->StartL(KFLoggerServerName);
65 CFileLoggerServer::CFileLoggerServer()
69 void CFileLoggerServer::ConstructL()
72 iLoggerManager=CFileLoggerManager::NewL();
73 iSessionCounter=CFileLogSessionCounter::NewL();
76 CFileLoggerServer::~CFileLoggerServer()
82 delete iSessionCounter;
83 delete iLoggerManager;
86 CFileLogSessionCounter* CFileLoggerServer::SessionCounter() const
89 return iSessionCounter;
92 void CFileLoggerServer::Error(TInt aError)
95 Message().Complete(aError);
99 CSession2* CFileLoggerServer::NewSessionL(const TVersion &aVersion, const RMessage2& /* aMessage */) const
101 Create a new server session. Check we're the right version and make a new session
103 @param aVersion version of new session
107 TVersion v(KFLogSrvMajorVersionNumber,KFLogSrvMinorVersionNumber,KFLogSrvBuildVersionNumber);
108 if (!User::QueryVersionSupported(v,aVersion))
109 User::Leave(KErrNotSupported);
111 iSessionCounter->CancelTimer();
112 return CFileLogSession::NewL(CONST_CAST(CFileLoggerServer*,this),iLoggerManager);
115 CFileLogSessionCounter class definition
118 CFileLogSessionCounter* CFileLogSessionCounter::NewL()
120 Creates new CFileLogSessionCounter object
122 @return Pointer to CFileLogSessionCounter object
126 CFileLogSessionCounter* r=new(ELeave) CFileLogSessionCounter();
127 CleanupStack::PushL(r);
133 CFileLogSessionCounter::~CFileLogSessionCounter()
139 delete iShutdownTimer;
142 CFileLogSessionCounter::CFileLogSessionCounter()
149 void CFileLogSessionCounter::ConstructL()
152 iShutdownTimer=CShutdownTimer::NewL();
155 void CFileLogSessionCounter::IncrementSessionCount()
157 Increments the Session count
164 void CFileLogSessionCounter::DecrementSessionCount()
166 Decrements the Session count
171 if (iSessionCount<=0)
172 iShutdownTimer->After(KShutdownPause);
175 void CFileLogSessionCounter::CancelTimer()
181 iShutdownTimer->Cancel();
185 CShutdownTimer class definitions
188 CShutdownTimer* CShutdownTimer::NewL()
190 Creates new CShutdownTimer object
191 @return Pointer to CShutdownTimer object
195 CShutdownTimer* r=new(ELeave) CShutdownTimer();
196 CleanupStack::PushL(r);
202 CShutdownTimer::~CShutdownTimer()
208 CShutdownTimer::CShutdownTimer()
209 : CTimer(EPriorityIdle)
214 CActiveScheduler::Add(this);
217 void CShutdownTimer::RunL()
222 CActiveScheduler::Stop();
226 CFileLogSession class definition
229 CFileLogSession* CFileLogSession::NewL(CFileLoggerServer* aServer, CFileLoggerManager* aManager)
232 CFileLogSession* r=new(ELeave) CFileLogSession(aServer,aManager);
233 CleanupStack::PushL(r);
239 CFileLogSession::CFileLogSession( CFileLoggerServer* aServer, CFileLoggerManager* aManager)
240 : CSession2(), iLoggerServer(aServer), iLoggerManager(aManager)
243 void CFileLogSession::ConstructL()
246 iLoggerServer->SessionCounter()->IncrementSessionCount(); // should be done first because it will be decremented in the destructor
247 iOpenLogFiles=new(ELeave) CArrayFixFlat<TLogFile>(KOpenLogFilesGranularity);
250 CFileLogSession::~CFileLogSession()
257 TInt count=iOpenLogFiles->Count();
259 for (i=0; i<count; i++)
261 iLoggerManager->CloseLog(iOpenLogFiles->At(i));
263 iOpenLogFiles->Delete(0,count);
264 delete iOpenLogFiles;
266 iLoggerServer->SessionCounter()->DecrementSessionCount(); // starts the timer if this is the last session
269 void CFileLogSession::ServiceL(const RMessage2& aMessage)
275 TRAPD(ret,DispatchMessageL(aMessage));
276 if (ret!=KErrMessagePanic)
278 aMessage.Complete(ret);
282 void CFileLogSession::DispatchMessageL(const RMessage2& aMessage)
285 TInt func=aMessage.Function();
286 if (func!=ECreateLog && func!=EWriteLog && func!=ECloseLog && func!=ECreateWriteAndCloseLog)
287 User::Leave(KErrNotSupported);
290 TPckg<TLogFile> logPckg(log);
291 aMessage.ReadL(0,logPckg);
292 if (log.Directory().Length()<=0)
294 aMessage.Panic(KFLoggerServerName,ELogDirectoryNameDoesNotExist);
295 User::Leave(KErrMessagePanic);
298 if (log.Name().Length()<=0)
300 aMessage.Panic(KFLoggerServerName,ELogFileNameDoesNotExist);
301 User::Leave(KErrMessagePanic);
304 // TBuf8<KLogBufferSize> buf;
306 if (func==EWriteLog || func==ECreateWriteAndCloseLog)
308 aMessage.ReadL(1,buf);
315 aMessage.WriteL(0,logPckg);
319 iLoggerManager->WriteToLogL(log,buf);
326 case ECreateWriteAndCloseLog:
328 OpenLogL(log); // Ok to leave here; assume that log not left open
329 TInt rc = aMessage.Write(0,logPckg);
330 if (rc == KErrNone && log.Valid())
331 TRAP(rc,iLoggerManager->WriteToLogL(log,buf));
333 User::LeaveIfError(rc);
338 User::Leave(KErrNotSupported);
344 void CFileLogSession::OpenLogL(TLogFile& aLogFile)
348 @param aLogFile Log file name
352 iOpenLogFiles->AppendL(aLogFile);
353 iLoggerManager->FindOrCreateLogL(aLogFile);
356 void CFileLogSession::CloseLog(TLogFile& aLogFile)
360 @param aLogFile Log file name
364 iLoggerManager->CloseLog(aLogFile);
365 TInt count=iOpenLogFiles->Count();
367 for (i=0; i<count; i++)
369 if (iOpenLogFiles->At(i)==aLogFile)
371 iOpenLogFiles->Delete(i,1);
376 __ASSERT_DEBUG(i<=count, User::Invariant());