loggingservices/filelogger/SSVR/FLOGMAN.CPP
changeset 0 08ec8eefde2f
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  @internalComponent
       
    19 */
       
    20 
       
    21 #include "FLOGMAN.H"
       
    22 #include "FLOGSTD.H"
       
    23 
       
    24 const TInt KLoggerGranularity=5;
       
    25 
       
    26 /**
       
    27 Literal const that hold back slash.
       
    28 */
       
    29 const TText KBackSlash='\\';
       
    30 
       
    31 /**
       
    32 full path for log folder
       
    33 */
       
    34 _LIT(KLogFolder,"C:\\LOGS\\");
       
    35 
       
    36 /**
       
    37 CFileLoggerManager definitions
       
    38 */
       
    39 
       
    40 CFileLoggerManager* CFileLoggerManager::NewL()
       
    41 /**
       
    42 Creates a new CFileLoggerManager Object 
       
    43 */
       
    44 	{
       
    45 
       
    46 	CFileLoggerManager* r=new(ELeave) CFileLoggerManager();
       
    47 	CleanupStack::PushL(r);
       
    48 	r->ConstructL();
       
    49 	CleanupStack::Pop();
       
    50 	return r;
       
    51 	}
       
    52 
       
    53 CFileLoggerManager::~CFileLoggerManager()
       
    54 /**
       
    55 Destructor
       
    56 */
       
    57 	{
       
    58 
       
    59 	TInt i;
       
    60 	TInt count=iLogger.Count();
       
    61 	for (i=0; i<count; i++)
       
    62 		{	
       
    63 		delete iLogger[i];
       
    64 		iLogger[i]=NULL;
       
    65 		}
       
    66 
       
    67 	iLogger.Delete(0,count);
       
    68 	iFs.Close();
       
    69 	}
       
    70 
       
    71 CFileLoggerManager::CFileLoggerManager()
       
    72 	: iLogger(KLoggerGranularity)
       
    73 /**
       
    74 Default Constructor 
       
    75 */
       
    76 	{}
       
    77 
       
    78 void CFileLoggerManager::ConstructL()
       
    79 	{
       
    80 
       
    81 	User::LeaveIfError(iFs.Connect());
       
    82 	}
       
    83 
       
    84 void CFileLoggerManager::FindOrCreateLogL(TLogFile& aLogFile)
       
    85 /**
       
    86 Searches the log file. If not found create it otherwise just
       
    87 increment access count.
       
    88 
       
    89 @param aLogFile Name of log file
       
    90 */
       
    91 	{
       
    92 
       
    93 	TInt indexFound=FindLogger(aLogFile);	
       
    94 	if (indexFound!=KErrNotFound)
       
    95 		{
       
    96 		iLogger[indexFound]->CancelDeletionTimer();
       
    97 		iLogger[indexFound]->IncrementAccessCount();
       
    98 		aLogFile.SetValid(iLogger[indexFound]->LogFile().Valid());
       
    99 		return;
       
   100 		}
       
   101 
       
   102 	CFileLogger* log=CFileLogger::NewL(this,aLogFile,iFs);
       
   103 	CleanupStack::PushL(log);
       
   104 	iLogger.AppendL(log);
       
   105 	CleanupStack::Pop();
       
   106 	}
       
   107 
       
   108 void CFileLoggerManager::CloseLog(const TLogFile& aLogFile)
       
   109 /**
       
   110 Searches the log file, if not found return. Decrement the access
       
   111 count and check for the client usage. If no client is using it 
       
   112 any more, delete the log file.
       
   113 
       
   114 @param aLogFile Name of log file
       
   115 */
       
   116 	{
       
   117 
       
   118 	TInt indexFound=FindLogger(aLogFile);
       
   119 	if (indexFound==KErrNotFound)
       
   120 		return;
       
   121 
       
   122 	if (!iLogger[indexFound]->DeletionTimerActive())
       
   123 		{
       
   124 		iLogger[indexFound]->DecrementAccessCount();
       
   125 
       
   126 		if (iLogger[indexFound]->AccessCount()==0)	// if no client is using it any more
       
   127 			{
       
   128 			TBool started=iLogger[indexFound]->StartDeletionTimer();
       
   129 			if (!started)
       
   130 				{
       
   131 				delete iLogger[indexFound];
       
   132 				iLogger.Delete(indexFound,1);
       
   133 				}
       
   134 			}
       
   135 		}
       
   136 	}
       
   137 
       
   138 void CFileLoggerManager::WriteToLogL(const TLogFile& aLogFile, const TDesC8& aBuf)
       
   139 /**
       
   140 Searches and Writes to log file.
       
   141 
       
   142 @param aLogFile Name of log file
       
   143 @param aBuf Text to write
       
   144 */
       
   145 	{
       
   146 
       
   147 	TInt indexFound=FindLogger(aLogFile);
       
   148 	if (indexFound==KErrNotFound)
       
   149 		{
       
   150 		User::Leave(KErrNotFound);
       
   151 		return;
       
   152 		}
       
   153 
       
   154 	iLogger[indexFound]->WriteLogL(aBuf);
       
   155 	}
       
   156 
       
   157 void CFileLoggerManager::DeleteLogger(CFileLogger* aLogger)
       
   158 	{
       
   159 
       
   160 	TInt index=FindLogger(aLogger->LogFile());
       
   161 	__ASSERT_DEBUG(index!=KErrNotFound, User::Invariant());
       
   162 	if (index!=KErrNotFound)
       
   163 		{
       
   164 		delete iLogger[index];
       
   165 		iLogger.Delete(index,1);
       
   166 		}
       
   167 	}
       
   168 
       
   169 TInt CFileLoggerManager::FindLogger(const TLogFile& aLogFile) const
       
   170 /**
       
   171 Searches log file.
       
   172 @param aLogFile Name of the log file
       
   173 */
       
   174 	{
       
   175 	
       
   176 	TInt i;
       
   177 	TInt indexFound=KErrNotFound;
       
   178 	for (i=0; i<iLogger.Count(); i++)
       
   179 		{
       
   180 		if (iLogger[i]->LogFile()==aLogFile)
       
   181 			{
       
   182 			indexFound=i;
       
   183 			break;
       
   184 			}
       
   185 		}
       
   186 	return indexFound;
       
   187 	}
       
   188 
       
   189 /**
       
   190 CFileLogger defintions
       
   191 */
       
   192 
       
   193 CFileLogger* CFileLogger::NewL(CFileLoggerManager* aLoggerManager,TLogFile& aLogFile, RFs& aFs)
       
   194 /**
       
   195 Creates CFileLogger
       
   196 
       
   197 */
       
   198 	{
       
   199 
       
   200 	CFileLogger* r=new(ELeave) CFileLogger(aLoggerManager,aLogFile,aFs);
       
   201 	CleanupStack::PushL(r);
       
   202 	r->ConstructL(aLogFile);
       
   203 	CleanupStack::Pop();
       
   204 	return r;
       
   205 	}
       
   206 
       
   207 CFileLogger::~CFileLogger()
       
   208 /**
       
   209 Destructor
       
   210 */
       
   211 	{
       
   212 
       
   213 	__ASSERT_DEBUG(iAccessCount==0, User::Invariant());
       
   214 	iFile.Close();
       
   215 	delete iTimer;
       
   216 	}	
       
   217 
       
   218 CFileLogger::CFileLogger(CFileLoggerManager* aLoggerManager,TLogFile& aLogFile, RFs& aFs)
       
   219 	: iLoggerManager(aLoggerManager), iFs(aFs), iLogFile(aLogFile), iAccessCount(0)
       
   220 /**
       
   221 Access count will be incremented when construcion is complete
       
   222 
       
   223 */
       
   224 	{}
       
   225 
       
   226 void CFileLogger::ConstructL(TLogFile& aLogFile)
       
   227 /**
       
   228 Decide whether to create a log (only create a log if the folder exists) and if 
       
   229 so delete any existing one, if the mode is overwirte, append to any existing one 
       
   230 if the mode is append. Open or create and the file and position to the end of it.
       
   231 If an error occurs, the iValid flag will just be left as FALSE, as initialised.
       
   232 
       
   233 @param aLogFile Name of the log file
       
   234 */
       
   235 	{
       
   236 
       
   237 	iTimer=CLoggerDeletionTimer::NewL(this);
       
   238 
       
   239 	iLogFile.SetValid(EFalse);
       
   240 	aLogFile.SetValid(EFalse);
       
   241 
       
   242 	TFileName logFolder,logFilename;
       
   243 	GetFolderAndFileNameL(logFolder,logFilename);
       
   244 	
       
   245 	TUint n;
       
   246 	if (iFs.Att(logFolder,n)==KErrNone)		// the folder exists => do logging
       
   247 		{
       
   248 		TInt ret=KErrNone;
       
   249 		TBool fileExists=EFalse;
       
   250 		if (iLogFile.Mode()==EFileLoggingModeOverwrite)
       
   251 			{
       
   252 			ret=iFs.Delete(logFilename);
       
   253 			if (ret!=KErrNotFound && ret!=KErrNone)
       
   254 				User::Leave(ret);
       
   255 			}
       
   256 		else		// append
       
   257 			{
       
   258 			if (iFs.Att(logFilename,n)==KErrNone)
       
   259 				fileExists=ETrue;
       
   260 			}
       
   261 
       
   262 		if (!fileExists)
       
   263 			ret=iFile.Create(iFs,logFilename,EFileShareAny|EFileWrite);
       
   264 		else
       
   265 			ret=iFile.Open(iFs,logFilename,EFileShareAny|EFileWrite);
       
   266 		if (ret==KErrNone)	// if we managed to open/create it
       
   267 			{
       
   268 			TInt pos=0;
       
   269 			ret=iFile.Seek(ESeekEnd,pos);
       
   270 			if (ret==KErrNone)
       
   271 				{
       
   272 				iLogFile.SetValid(ETrue);
       
   273 				aLogFile.SetValid(ETrue);
       
   274 				IncrementAccessCount();
       
   275 				return;
       
   276 				}
       
   277 			}
       
   278 		iFile.Close();
       
   279 		User::Leave(ret);
       
   280 		}
       
   281 	IncrementAccessCount();
       
   282 	}
       
   283 
       
   284 void CFileLogger::WriteLogL(const TDesC8& aBuf)
       
   285 /**
       
   286 Checks for log file and then Writes the text
       
   287 
       
   288 @param aBuf Text to write
       
   289 */
       
   290 	{
       
   291 
       
   292 	if (iLogFile.Valid())
       
   293 		{
       
   294 		User::LeaveIfError(iFile.Write(aBuf));
       
   295 		User::LeaveIfError(iFile.Flush());
       
   296 		}
       
   297 	}	
       
   298 
       
   299 TBool CFileLogger::StartDeletionTimer()
       
   300 /**
       
   301 Starts timer if this was a valid log file (i.e. it was open)
       
   302 */
       
   303 	{
       
   304 
       
   305 	if (!iLogFile.Valid())
       
   306 		return EFalse;
       
   307 	iTimer->After(KShutdownPause);
       
   308 	return ETrue;
       
   309 	}
       
   310 
       
   311 void CFileLogger::CancelDeletionTimer()
       
   312 /**
       
   313 */
       
   314 	{
       
   315 
       
   316 	iTimer->Cancel();
       
   317 	}
       
   318 
       
   319 void CFileLogger::DeletionTimerExpired()
       
   320 /**
       
   321 */
       
   322 	{
       
   323 
       
   324 	iLoggerManager->DeleteLogger(this);
       
   325 	}
       
   326 
       
   327 void CFileLogger::GetFolderAndFileNameL(TFileName& aFolder,TFileName& aFilename) const
       
   328 /**
       
   329 Work out the full path of the folder and file name of the log
       
   330 
       
   331 @param aFolder Full path of log file
       
   332 @param aFilename Name of log file
       
   333 */
       
   334 	{
       
   335 
       
   336 	aFolder.Append(KLogFolder);
       
   337 	if ((aFolder.MaxLength()-aFolder.Length()-iLogFile.Directory().Length()-1)>=0)
       
   338 		{
       
   339 		aFolder.Append(iLogFile.Directory());
       
   340 		aFolder.Append(KBackSlash);
       
   341 		}
       
   342 	else
       
   343 		User::Leave(KErrOverflow);
       
   344 
       
   345 	if ((aFilename.MaxLength()-aFolder.Length()-iLogFile.Name().Length())>=0)
       
   346 		{
       
   347 		aFilename.Append(aFolder);
       
   348 		aFilename.Append(iLogFile.Name());
       
   349 		}
       
   350 	else
       
   351 		User::Leave(KErrOverflow);
       
   352 	}
       
   353 
       
   354 /**
       
   355 CLoggerDeletionTimer class definitions
       
   356 */
       
   357 
       
   358 CLoggerDeletionTimer* CLoggerDeletionTimer::NewL(CFileLogger* aLogger)
       
   359 /**
       
   360 Creates CLoggerDeletionTimer
       
   361 */
       
   362 	{
       
   363 
       
   364 	CLoggerDeletionTimer* r=new(ELeave) CLoggerDeletionTimer(aLogger);
       
   365 	CleanupStack::PushL(r);
       
   366 	r->ConstructL();
       
   367 	CleanupStack::Pop();
       
   368 	return r;
       
   369 	}
       
   370 
       
   371 CLoggerDeletionTimer::~CLoggerDeletionTimer()
       
   372 /**
       
   373 Destructor
       
   374 */
       
   375 	{}
       
   376 
       
   377 CLoggerDeletionTimer::CLoggerDeletionTimer(CFileLogger* aLogger)
       
   378 	: CTimer(EPriorityIdle), iLogger(aLogger)
       
   379 	{
       
   380 	CActiveScheduler::Add(this);
       
   381 	}
       
   382 
       
   383 void CLoggerDeletionTimer::RunL()
       
   384 	{
       
   385 	
       
   386 	iLogger->DeletionTimerExpired();
       
   387 	}