loggingservices/filelogger/SSVR/FLOGSVR.CPP
changeset 0 08ec8eefde2f
child 24 b6ab70c1385f
equal deleted inserted replaced
-1:000000000000 0:08ec8eefde2f
       
     1 // Copyright (c) 1997-2009 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".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 /**
       
    17  @file
       
    18 */
       
    19 
       
    20 #include "FLOGSVR.H"
       
    21 #include "FLOGMAN.H"
       
    22 #include "FLOGSTD.H"
       
    23 
       
    24 /**
       
    25 @internalComponent
       
    26 */
       
    27 const TInt KOpenLogFilesGranularity=5;
       
    28 
       
    29 /**
       
    30 panics for log client
       
    31 
       
    32 @internalComponent
       
    33 */
       
    34 enum TLogClientPanic 
       
    35     {
       
    36 	ELogDirectoryNameDoesNotExist =1,   ///<  panic if no log directory name 
       
    37 	ELogFileNameDoesNotExist	        ///< panic if file name doesnot exist 
       
    38 	};
       
    39 
       
    40 /**
       
    41 CFileLoggerServer class definition
       
    42 */
       
    43 
       
    44 CFileLoggerServer* CFileLoggerServer::NewL()
       
    45 /**
       
    46 Creates new CFileLoggerServer object
       
    47 
       
    48 @return Pointer to CFileLoggerServer object
       
    49 */
       
    50 	{
       
    51 
       
    52 	CFileLoggerServer* r=new(ELeave) CFileLoggerServer();
       
    53 	CleanupStack::PushL(r);
       
    54 	r->ConstructL();
       
    55 	r->StartL(KFLoggerServerName);
       
    56 	CleanupStack::Pop();
       
    57 	return r;
       
    58 	}
       
    59 
       
    60 CFileLoggerServer::CFileLoggerServer()
       
    61  	: CServer2(EPriority)
       
    62  	{}
       
    63 
       
    64 void CFileLoggerServer::ConstructL()
       
    65 	{
       
    66 
       
    67 	iLoggerManager=CFileLoggerManager::NewL();
       
    68 	iSessionCounter=CFileLogSessionCounter::NewL();
       
    69 	}
       
    70 
       
    71 CFileLoggerServer::~CFileLoggerServer()
       
    72 /**
       
    73 Destructor
       
    74 */
       
    75 	{
       
    76 
       
    77 	delete iSessionCounter;
       
    78 	delete iLoggerManager;
       
    79 	}
       
    80 
       
    81 CFileLogSessionCounter* CFileLoggerServer::SessionCounter() const
       
    82 	{
       
    83 
       
    84 	return iSessionCounter;
       
    85 	}
       
    86 
       
    87 void CFileLoggerServer::Error(TInt aError)
       
    88 	{
       
    89 
       
    90 	Message().Complete(aError);
       
    91 	ReStart();
       
    92 	}
       
    93 
       
    94 CSession2* CFileLoggerServer::NewSessionL(const TVersion &aVersion, const RMessage2& /* aMessage */) const
       
    95 /**
       
    96 Create a new server session. Check we're the right version and make a new session
       
    97 
       
    98 @param aVersion version of new session
       
    99 */
       
   100 	{
       
   101 
       
   102 	TVersion v(KFLogSrvMajorVersionNumber,KFLogSrvMinorVersionNumber,KFLogSrvBuildVersionNumber);
       
   103 	if (!User::QueryVersionSupported(v,aVersion))
       
   104 		User::Leave(KErrNotSupported);
       
   105 	
       
   106 	iSessionCounter->CancelTimer();
       
   107 	return CFileLogSession::NewL(CONST_CAST(CFileLoggerServer*,this),iLoggerManager);
       
   108 	}
       
   109 /**
       
   110 CFileLogSessionCounter class definition 
       
   111 */
       
   112 
       
   113 CFileLogSessionCounter* CFileLogSessionCounter::NewL()
       
   114 /**
       
   115 Creates new CFileLogSessionCounter object
       
   116 
       
   117 @return Pointer to CFileLogSessionCounter object
       
   118 */
       
   119 	{
       
   120 	
       
   121 	CFileLogSessionCounter* r=new(ELeave) CFileLogSessionCounter();
       
   122 	CleanupStack::PushL(r);
       
   123 	r->ConstructL();
       
   124 	CleanupStack::Pop();
       
   125 	return r;
       
   126 	}
       
   127 
       
   128 CFileLogSessionCounter::~CFileLogSessionCounter()
       
   129 /**
       
   130 Destructor
       
   131 */
       
   132 	{
       
   133 
       
   134 	delete iShutdownTimer;
       
   135 	}
       
   136 
       
   137 CFileLogSessionCounter::CFileLogSessionCounter()
       
   138 	: iSessionCount(0)
       
   139 /**
       
   140 Default Constructor
       
   141 */
       
   142 	{}
       
   143 
       
   144 void CFileLogSessionCounter::ConstructL()
       
   145 	{
       
   146 
       
   147 	iShutdownTimer=CShutdownTimer::NewL();
       
   148 	}
       
   149 	
       
   150 void CFileLogSessionCounter::IncrementSessionCount()
       
   151 /**
       
   152 Increments the Session count 
       
   153 */
       
   154 	{
       
   155 
       
   156 	iSessionCount++;
       
   157 	}
       
   158 
       
   159 void CFileLogSessionCounter::DecrementSessionCount()
       
   160 /**
       
   161 Decrements the Session count
       
   162 */
       
   163 	{
       
   164 
       
   165 	iSessionCount--;
       
   166 	if (iSessionCount<=0)
       
   167 		iShutdownTimer->After(KShutdownPause);
       
   168 	}
       
   169 
       
   170 void CFileLogSessionCounter::CancelTimer()
       
   171 /**
       
   172 Cancels the timer 
       
   173 */
       
   174 	{
       
   175 
       
   176 	iShutdownTimer->Cancel();
       
   177 	}	
       
   178 
       
   179 /**
       
   180 CShutdownTimer class definitions
       
   181 */
       
   182 
       
   183 CShutdownTimer* CShutdownTimer::NewL()
       
   184 /**
       
   185 Creates new CShutdownTimer object
       
   186 @return Pointer to CShutdownTimer object
       
   187 */
       
   188 	{
       
   189 
       
   190 	CShutdownTimer* r=new(ELeave) CShutdownTimer();
       
   191 	CleanupStack::PushL(r);
       
   192 	r->ConstructL();
       
   193 	CleanupStack::Pop();
       
   194 	return r;
       
   195 	}
       
   196 
       
   197 CShutdownTimer::~CShutdownTimer()
       
   198 /**
       
   199 Destructor
       
   200 */
       
   201 	{}
       
   202 
       
   203 CShutdownTimer::CShutdownTimer()
       
   204 	: CTimer(EPriorityIdle)
       
   205 /**
       
   206 Default Constructor
       
   207 */
       
   208 	{
       
   209 	CActiveScheduler::Add(this);
       
   210 	}
       
   211 
       
   212 void CShutdownTimer::RunL()
       
   213 /**
       
   214 */
       
   215 	{
       
   216 
       
   217 	CActiveScheduler::Stop();
       
   218 	}
       
   219 
       
   220 /**
       
   221 CFileLogSession class definition
       
   222 */
       
   223 
       
   224 CFileLogSession* CFileLogSession::NewL(CFileLoggerServer* aServer, CFileLoggerManager* aManager)
       
   225  	{
       
   226    
       
   227  	CFileLogSession* r=new(ELeave) CFileLogSession(aServer,aManager);
       
   228  	CleanupStack::PushL(r);
       
   229  	r->ConstructL();
       
   230  	CleanupStack::Pop();
       
   231  	return r;
       
   232  	}
       
   233 
       
   234 CFileLogSession::CFileLogSession( CFileLoggerServer* aServer, CFileLoggerManager* aManager)
       
   235  	: CSession2(), iLoggerServer(aServer), iLoggerManager(aManager)
       
   236  	{}
       
   237 
       
   238 void CFileLogSession::ConstructL()
       
   239 	{
       
   240 
       
   241 	iLoggerServer->SessionCounter()->IncrementSessionCount();		// should be done first because it will be decremented in the destructor
       
   242 	iOpenLogFiles=new(ELeave) CArrayFixFlat<TLogFile>(KOpenLogFilesGranularity);
       
   243  	}
       
   244 
       
   245 CFileLogSession::~CFileLogSession()
       
   246 /**
       
   247 Destructor
       
   248 */
       
   249 	{
       
   250 	if  (iOpenLogFiles)
       
   251 		{
       
   252 	TInt count=iOpenLogFiles->Count();
       
   253 	TInt i;
       
   254 	for (i=0; i<count; i++)
       
   255 		{
       
   256 		iLoggerManager->CloseLog(iOpenLogFiles->At(i));
       
   257 		}
       
   258 	iOpenLogFiles->Delete(0,count);
       
   259 	delete iOpenLogFiles;
       
   260 		}
       
   261 	iLoggerServer->SessionCounter()->DecrementSessionCount();		// starts the timer if this is the last session
       
   262 	}
       
   263 
       
   264 void CFileLogSession::ServiceL(const RMessage2& aMessage)
       
   265 /**
       
   266 @internalTechnology
       
   267 */
       
   268 	{
       
   269   
       
   270  	TRAPD(ret,DispatchMessageL(aMessage));
       
   271  	aMessage.Complete(ret);
       
   272  	}
       
   273 
       
   274 void CFileLogSession::DispatchMessageL(const RMessage2& aMessage)
       
   275  	{
       
   276  
       
   277  	TInt func=aMessage.Function();
       
   278  	if (func!=ECreateLog && func!=EWriteLog && func!=ECloseLog && func!=ECreateWriteAndCloseLog)
       
   279  		User::Leave(KErrNotSupported);
       
   280  
       
   281  	TLogFile log;
       
   282  	TPckg<TLogFile> logPckg(log);
       
   283  	aMessage.ReadL(0,logPckg);
       
   284  	__ASSERT_ALWAYS(log.Directory().Length()!=0,aMessage.Panic(KFLoggerServerName,ELogDirectoryNameDoesNotExist));
       
   285  	__ASSERT_ALWAYS(log.Name().Length()!=0,aMessage.Panic(KFLoggerServerName,ELogFileNameDoesNotExist));	
       
   286  
       
   287  //	TBuf8<KLogBufferSize> buf;
       
   288  	TBuf8<1600> buf;
       
   289  	if (func==EWriteLog || func==ECreateWriteAndCloseLog)
       
   290  		aMessage.ReadL(1,buf);
       
   291  
       
   292  	switch (func)
       
   293  		{
       
   294  	case ECreateLog:
       
   295  		OpenLogL(log);
       
   296  		aMessage.WriteL(0,logPckg);
       
   297  		break;
       
   298  
       
   299  	case EWriteLog:
       
   300  		iLoggerManager->WriteToLogL(log,buf);
       
   301  		break;
       
   302  
       
   303  	case ECloseLog:
       
   304  		CloseLog(log);
       
   305  		break;
       
   306  
       
   307  	case ECreateWriteAndCloseLog:
       
   308   		{
       
   309   		OpenLogL(log); // Ok to leave here; assume that log not left open
       
   310  		TInt rc = aMessage.Write(0,logPckg);
       
   311   		if (rc == KErrNone && log.Valid())
       
   312   			TRAP(rc,iLoggerManager->WriteToLogL(log,buf));
       
   313     		CloseLog(log);
       
   314   		User::LeaveIfError(rc);
       
   315     		break;
       
   316   		}
       
   317  
       
   318  	default:
       
   319  		User::Leave(KErrNotSupported);
       
   320  		break;
       
   321  		}
       
   322  	}
       
   323  
       
   324 
       
   325 void CFileLogSession::OpenLogL(TLogFile& aLogFile)
       
   326 /**
       
   327 Opens log file
       
   328 
       
   329 @param aLogFile Log file name
       
   330 */
       
   331 	{
       
   332 
       
   333 	iOpenLogFiles->AppendL(aLogFile);
       
   334 	iLoggerManager->FindOrCreateLogL(aLogFile);
       
   335 	}
       
   336 
       
   337 void CFileLogSession::CloseLog(TLogFile& aLogFile)
       
   338 /**
       
   339 Closes the log file
       
   340 
       
   341 @param aLogFile Log file name
       
   342 */
       
   343 	{
       
   344 
       
   345 	iLoggerManager->CloseLog(aLogFile);
       
   346 	TInt count=iOpenLogFiles->Count();
       
   347 	TInt i=0;
       
   348 	for (i=0; i<count; i++)
       
   349 		{
       
   350 		if (iOpenLogFiles->At(i)==aLogFile)
       
   351 			{
       
   352 			iOpenLogFiles->Delete(i,1);
       
   353 			break;
       
   354 			}
       
   355 		}	
       
   356 
       
   357 	__ASSERT_DEBUG(i<=count, User::Invariant());
       
   358 	}
       
   359