os/persistentdata/loggingservices/filelogger/SSVR/FLOGMAN.CPP
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/persistentdata/loggingservices/filelogger/SSVR/FLOGMAN.CPP	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,387 @@
     1.4 +// Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.5 +// All rights reserved.
     1.6 +// This component and the accompanying materials are made available
     1.7 +// under the terms of "Eclipse Public License v1.0"
     1.8 +// which accompanies this distribution, and is available
     1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.10 +//
    1.11 +// Initial Contributors:
    1.12 +// Nokia Corporation - initial contribution.
    1.13 +//
    1.14 +// Contributors:
    1.15 +//
    1.16 +// Description:
    1.17 +//
    1.18 +
    1.19 +/**
    1.20 + @file
    1.21 + @internalComponent
    1.22 +*/
    1.23 +
    1.24 +#include "FLOGMAN.H"
    1.25 +#include "FLOGSTD.H"
    1.26 +
    1.27 +const TInt KLoggerGranularity=5;
    1.28 +
    1.29 +/**
    1.30 +Literal const that hold back slash.
    1.31 +*/
    1.32 +const TText KBackSlash='\\';
    1.33 +
    1.34 +/**
    1.35 +full path for log folder
    1.36 +*/
    1.37 +_LIT(KLogFolder,"C:\\LOGS\\");
    1.38 +
    1.39 +/**
    1.40 +CFileLoggerManager definitions
    1.41 +*/
    1.42 +
    1.43 +CFileLoggerManager* CFileLoggerManager::NewL()
    1.44 +/**
    1.45 +Creates a new CFileLoggerManager Object 
    1.46 +*/
    1.47 +	{
    1.48 +
    1.49 +	CFileLoggerManager* r=new(ELeave) CFileLoggerManager();
    1.50 +	CleanupStack::PushL(r);
    1.51 +	r->ConstructL();
    1.52 +	CleanupStack::Pop();
    1.53 +	return r;
    1.54 +	}
    1.55 +
    1.56 +CFileLoggerManager::~CFileLoggerManager()
    1.57 +/**
    1.58 +Destructor
    1.59 +*/
    1.60 +	{
    1.61 +
    1.62 +	TInt i;
    1.63 +	TInt count=iLogger.Count();
    1.64 +	for (i=0; i<count; i++)
    1.65 +		{	
    1.66 +		delete iLogger[i];
    1.67 +		iLogger[i]=NULL;
    1.68 +		}
    1.69 +
    1.70 +	iLogger.Delete(0,count);
    1.71 +	iFs.Close();
    1.72 +	}
    1.73 +
    1.74 +CFileLoggerManager::CFileLoggerManager()
    1.75 +	: iLogger(KLoggerGranularity)
    1.76 +/**
    1.77 +Default Constructor 
    1.78 +*/
    1.79 +	{}
    1.80 +
    1.81 +void CFileLoggerManager::ConstructL()
    1.82 +	{
    1.83 +
    1.84 +	User::LeaveIfError(iFs.Connect());
    1.85 +	}
    1.86 +
    1.87 +void CFileLoggerManager::FindOrCreateLogL(TLogFile& aLogFile)
    1.88 +/**
    1.89 +Searches the log file. If not found create it otherwise just
    1.90 +increment access count.
    1.91 +
    1.92 +@param aLogFile Name of log file
    1.93 +*/
    1.94 +	{
    1.95 +
    1.96 +	TInt indexFound=FindLogger(aLogFile);	
    1.97 +	if (indexFound!=KErrNotFound)
    1.98 +		{
    1.99 +		iLogger[indexFound]->CancelDeletionTimer();
   1.100 +		iLogger[indexFound]->IncrementAccessCount();
   1.101 +		aLogFile.SetValid(iLogger[indexFound]->LogFile().Valid());
   1.102 +		return;
   1.103 +		}
   1.104 +
   1.105 +	CFileLogger* log=CFileLogger::NewL(this,aLogFile,iFs);
   1.106 +	CleanupStack::PushL(log);
   1.107 +	iLogger.AppendL(log);
   1.108 +	CleanupStack::Pop();
   1.109 +	}
   1.110 +
   1.111 +void CFileLoggerManager::CloseLog(const TLogFile& aLogFile)
   1.112 +/**
   1.113 +Searches the log file, if not found return. Decrement the access
   1.114 +count and check for the client usage. If no client is using it 
   1.115 +any more, delete the log file.
   1.116 +
   1.117 +@param aLogFile Name of log file
   1.118 +*/
   1.119 +	{
   1.120 +
   1.121 +	TInt indexFound=FindLogger(aLogFile);
   1.122 +	if (indexFound==KErrNotFound)
   1.123 +		return;
   1.124 +
   1.125 +	if (!iLogger[indexFound]->DeletionTimerActive())
   1.126 +		{
   1.127 +		iLogger[indexFound]->DecrementAccessCount();
   1.128 +
   1.129 +		if (iLogger[indexFound]->AccessCount()==0)	// if no client is using it any more
   1.130 +			{
   1.131 +			TBool started=iLogger[indexFound]->StartDeletionTimer();
   1.132 +			if (!started)
   1.133 +				{
   1.134 +				delete iLogger[indexFound];
   1.135 +				iLogger.Delete(indexFound,1);
   1.136 +				}
   1.137 +			}
   1.138 +		}
   1.139 +	}
   1.140 +
   1.141 +void CFileLoggerManager::WriteToLogL(const TLogFile& aLogFile, const TDesC8& aBuf)
   1.142 +/**
   1.143 +Searches and Writes to log file.
   1.144 +
   1.145 +@param aLogFile Name of log file
   1.146 +@param aBuf Text to write
   1.147 +*/
   1.148 +	{
   1.149 +
   1.150 +	TInt indexFound=FindLogger(aLogFile);
   1.151 +	if (indexFound==KErrNotFound)
   1.152 +		{
   1.153 +		User::Leave(KErrNotFound);
   1.154 +		return;
   1.155 +		}
   1.156 +
   1.157 +	iLogger[indexFound]->WriteLogL(aBuf);
   1.158 +	}
   1.159 +
   1.160 +void CFileLoggerManager::DeleteLogger(CFileLogger* aLogger)
   1.161 +	{
   1.162 +
   1.163 +	TInt index=FindLogger(aLogger->LogFile());
   1.164 +	__ASSERT_DEBUG(index!=KErrNotFound, User::Invariant());
   1.165 +	if (index!=KErrNotFound)
   1.166 +		{
   1.167 +		delete iLogger[index];
   1.168 +		iLogger.Delete(index,1);
   1.169 +		}
   1.170 +	}
   1.171 +
   1.172 +TInt CFileLoggerManager::FindLogger(const TLogFile& aLogFile) const
   1.173 +/**
   1.174 +Searches log file.
   1.175 +@param aLogFile Name of the log file
   1.176 +*/
   1.177 +	{
   1.178 +	
   1.179 +	TInt i;
   1.180 +	TInt indexFound=KErrNotFound;
   1.181 +	for (i=0; i<iLogger.Count(); i++)
   1.182 +		{
   1.183 +		if (iLogger[i]->LogFile()==aLogFile)
   1.184 +			{
   1.185 +			indexFound=i;
   1.186 +			break;
   1.187 +			}
   1.188 +		}
   1.189 +	return indexFound;
   1.190 +	}
   1.191 +
   1.192 +/**
   1.193 +CFileLogger defintions
   1.194 +*/
   1.195 +
   1.196 +CFileLogger* CFileLogger::NewL(CFileLoggerManager* aLoggerManager,TLogFile& aLogFile, RFs& aFs)
   1.197 +/**
   1.198 +Creates CFileLogger
   1.199 +
   1.200 +*/
   1.201 +	{
   1.202 +
   1.203 +	CFileLogger* r=new(ELeave) CFileLogger(aLoggerManager,aLogFile,aFs);
   1.204 +	CleanupStack::PushL(r);
   1.205 +	r->ConstructL(aLogFile);
   1.206 +	CleanupStack::Pop();
   1.207 +	return r;
   1.208 +	}
   1.209 +
   1.210 +CFileLogger::~CFileLogger()
   1.211 +/**
   1.212 +Destructor
   1.213 +*/
   1.214 +	{
   1.215 +
   1.216 +	__ASSERT_DEBUG(iAccessCount==0, User::Invariant());
   1.217 +	iFile.Close();
   1.218 +	delete iTimer;
   1.219 +	}	
   1.220 +
   1.221 +CFileLogger::CFileLogger(CFileLoggerManager* aLoggerManager,TLogFile& aLogFile, RFs& aFs)
   1.222 +	: iLoggerManager(aLoggerManager), iFs(aFs), iLogFile(aLogFile), iAccessCount(0)
   1.223 +/**
   1.224 +Access count will be incremented when construcion is complete
   1.225 +
   1.226 +*/
   1.227 +	{}
   1.228 +
   1.229 +void CFileLogger::ConstructL(TLogFile& aLogFile)
   1.230 +/**
   1.231 +Decide whether to create a log (only create a log if the folder exists) and if 
   1.232 +so delete any existing one, if the mode is overwirte, append to any existing one 
   1.233 +if the mode is append. Open or create and the file and position to the end of it.
   1.234 +If an error occurs, the iValid flag will just be left as FALSE, as initialised.
   1.235 +
   1.236 +@param aLogFile Name of the log file
   1.237 +*/
   1.238 +	{
   1.239 +
   1.240 +	iTimer=CLoggerDeletionTimer::NewL(this);
   1.241 +
   1.242 +	iLogFile.SetValid(EFalse);
   1.243 +	aLogFile.SetValid(EFalse);
   1.244 +
   1.245 +	TFileName logFolder,logFilename;
   1.246 +	GetFolderAndFileNameL(logFolder,logFilename);
   1.247 +	
   1.248 +	TUint n;
   1.249 +	if (iFs.Att(logFolder,n)==KErrNone)		// the folder exists => do logging
   1.250 +		{
   1.251 +		TInt ret=KErrNone;
   1.252 +		TBool fileExists=EFalse;
   1.253 +		if (iLogFile.Mode()==EFileLoggingModeOverwrite)
   1.254 +			{
   1.255 +			ret=iFs.Delete(logFilename);
   1.256 +			if (ret!=KErrNotFound && ret!=KErrNone)
   1.257 +				User::Leave(ret);
   1.258 +			}
   1.259 +		else		// append
   1.260 +			{
   1.261 +			if (iFs.Att(logFilename,n)==KErrNone)
   1.262 +				fileExists=ETrue;
   1.263 +			}
   1.264 +
   1.265 +		if (!fileExists)
   1.266 +			ret=iFile.Create(iFs,logFilename,EFileShareAny|EFileWrite);
   1.267 +		else
   1.268 +			ret=iFile.Open(iFs,logFilename,EFileShareAny|EFileWrite);
   1.269 +		if (ret==KErrNone)	// if we managed to open/create it
   1.270 +			{
   1.271 +			TInt pos=0;
   1.272 +			ret=iFile.Seek(ESeekEnd,pos);
   1.273 +			if (ret==KErrNone)
   1.274 +				{
   1.275 +				iLogFile.SetValid(ETrue);
   1.276 +				aLogFile.SetValid(ETrue);
   1.277 +				IncrementAccessCount();
   1.278 +				return;
   1.279 +				}
   1.280 +			}
   1.281 +		iFile.Close();
   1.282 +		User::Leave(ret);
   1.283 +		}
   1.284 +	IncrementAccessCount();
   1.285 +	}
   1.286 +
   1.287 +void CFileLogger::WriteLogL(const TDesC8& aBuf)
   1.288 +/**
   1.289 +Checks for log file and then Writes the text
   1.290 +
   1.291 +@param aBuf Text to write
   1.292 +*/
   1.293 +	{
   1.294 +
   1.295 +	if (iLogFile.Valid())
   1.296 +		{
   1.297 +		User::LeaveIfError(iFile.Write(aBuf));
   1.298 +		User::LeaveIfError(iFile.Flush());
   1.299 +		}
   1.300 +	}	
   1.301 +
   1.302 +TBool CFileLogger::StartDeletionTimer()
   1.303 +/**
   1.304 +Starts timer if this was a valid log file (i.e. it was open)
   1.305 +*/
   1.306 +	{
   1.307 +
   1.308 +	if (!iLogFile.Valid())
   1.309 +		return EFalse;
   1.310 +	iTimer->After(KShutdownPause);
   1.311 +	return ETrue;
   1.312 +	}
   1.313 +
   1.314 +void CFileLogger::CancelDeletionTimer()
   1.315 +/**
   1.316 +*/
   1.317 +	{
   1.318 +
   1.319 +	iTimer->Cancel();
   1.320 +	}
   1.321 +
   1.322 +void CFileLogger::DeletionTimerExpired()
   1.323 +/**
   1.324 +*/
   1.325 +	{
   1.326 +
   1.327 +	iLoggerManager->DeleteLogger(this);
   1.328 +	}
   1.329 +
   1.330 +void CFileLogger::GetFolderAndFileNameL(TFileName& aFolder,TFileName& aFilename) const
   1.331 +/**
   1.332 +Work out the full path of the folder and file name of the log
   1.333 +
   1.334 +@param aFolder Full path of log file
   1.335 +@param aFilename Name of log file
   1.336 +*/
   1.337 +	{
   1.338 +
   1.339 +	aFolder.Append(KLogFolder);
   1.340 +	if ((aFolder.MaxLength()-aFolder.Length()-iLogFile.Directory().Length()-1)>=0)
   1.341 +		{
   1.342 +		aFolder.Append(iLogFile.Directory());
   1.343 +		aFolder.Append(KBackSlash);
   1.344 +		}
   1.345 +	else
   1.346 +		User::Leave(KErrOverflow);
   1.347 +
   1.348 +	if ((aFilename.MaxLength()-aFolder.Length()-iLogFile.Name().Length())>=0)
   1.349 +		{
   1.350 +		aFilename.Append(aFolder);
   1.351 +		aFilename.Append(iLogFile.Name());
   1.352 +		}
   1.353 +	else
   1.354 +		User::Leave(KErrOverflow);
   1.355 +	}
   1.356 +
   1.357 +/**
   1.358 +CLoggerDeletionTimer class definitions
   1.359 +*/
   1.360 +
   1.361 +CLoggerDeletionTimer* CLoggerDeletionTimer::NewL(CFileLogger* aLogger)
   1.362 +/**
   1.363 +Creates CLoggerDeletionTimer
   1.364 +*/
   1.365 +	{
   1.366 +
   1.367 +	CLoggerDeletionTimer* r=new(ELeave) CLoggerDeletionTimer(aLogger);
   1.368 +	CleanupStack::PushL(r);
   1.369 +	r->ConstructL();
   1.370 +	CleanupStack::Pop();
   1.371 +	return r;
   1.372 +	}
   1.373 +
   1.374 +CLoggerDeletionTimer::~CLoggerDeletionTimer()
   1.375 +/**
   1.376 +Destructor
   1.377 +*/
   1.378 +	{}
   1.379 +
   1.380 +CLoggerDeletionTimer::CLoggerDeletionTimer(CFileLogger* aLogger)
   1.381 +	: CTimer(EPriorityIdle), iLogger(aLogger)
   1.382 +	{
   1.383 +	CActiveScheduler::Add(this);
   1.384 +	}
   1.385 +
   1.386 +void CLoggerDeletionTimer::RunL()
   1.387 +	{
   1.388 +	
   1.389 +	iLogger->DeletionTimerExpired();
   1.390 +	}