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