serialserver/packetloopbackcsy/src/LBSLOGGER.CPP
changeset 0 dfb7c4ff071f
equal deleted inserted replaced
-1:000000000000 0:dfb7c4ff071f
       
     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 // This file implements a logging facility for the loopback.csy.  The loopback
       
    15 // file created by this log is C:\LOGS\ETEL\LOOPBACK.TXT.
       
    16 // To turn on logging, create the folder C:\LOGS\ETEL.
       
    17 // Note that this logging facility uses the Thread Local Storage for a thread.
       
    18 // 
       
    19 //
       
    20 
       
    21 /**
       
    22  @file
       
    23 */
       
    24 
       
    25 #include "SLOGGER.H"
       
    26 
       
    27 #ifdef __LOGGER__
       
    28 #ifdef __EXE__
       
    29 CETelLogger* ScriptLoggerContext=NULL;
       
    30 #endif
       
    31 
       
    32 _LIT(KLogFileName,"C:\\LOGS\\ETEL\\LOOPBACK.TXT");
       
    33 _LIT(KLogFolder,"C:\\LOGS\\ETEL\\");
       
    34 
       
    35 const TUint KLogBufferSize=500;
       
    36 
       
    37 CETelLogger* CETelLogger::NewL()
       
    38 /**
       
    39  * 2 Phase Constructor
       
    40  *
       
    41  * This method creates an instance of CETelLogger.
       
    42  *
       
    43  * @param      None.
       
    44  * @leave  Leaves if out-of-memory.
       
    45  * @return     pointer to the instance of "CETelLogger".
       
    46  */
       
    47 	{
       
    48 	CETelLogger* logger=new(ELeave) CETelLogger();
       
    49 	CleanupStack::PushL(logger);
       
    50 	logger->ConstructL();
       
    51 	CleanupStack::Pop(logger);
       
    52 	return logger;
       
    53 	}
       
    54 
       
    55 CETelLogger::CETelLogger() : iValid(EFalse)
       
    56 /**
       
    57  * This method is the constructor for CETelLogger.
       
    58  *
       
    59  * @param   None.
       
    60  * @return	None.
       
    61  * @note    Initializes private boolean "iValid" to False.
       
    62  */
       
    63 	{}
       
    64 
       
    65 void CETelLogger::ConstructL()
       
    66 /**
       
    67  * This method is used to implement the 2 Phase Constructor for CETelLogger.
       
    68  * This method sets up the logfile.
       
    69  *
       
    70  * @param     None.
       
    71  * @leave     Leaves if file can not be created.
       
    72  * @return    None.
       
    73  * @note      Logging does not take place if the logging directory has not been created.
       
    74  * \note      In debug mode, the logfile is not deleted at start of new session,
       
    75  * \note      the logging for each session will be appended to the previous logfile.
       
    76  */
       
    77 	{
       
    78 	if (FolderExists())
       
    79 		{
       
    80 		User::LeaveIfError(iFs.Connect());
       
    81 		TInt ret=KErrNone;
       
    82 	#if !(defined (_DEBUG))
       
    83 		ret=iFs.Delete(KLogFileName);
       
    84 		if(ret==KErrNone || ret==KErrNotFound)
       
    85 			ret=iFile.Create(iFs,KLogFileName,EFileShareAny|EFileWrite);
       
    86 	#else
       
    87 		ret=iFile.Open(iFs,KLogFileName,EFileShareAny|EFileWrite);
       
    88 		if(ret!=KErrNone)
       
    89 			ret=iFile.Create(iFs,KLogFileName,EFileShareAny|EFileWrite);
       
    90 	#endif
       
    91 		if(ret==KErrNone)
       
    92 			{
       
    93 			iValid=ETrue;
       
    94 			TInt pos=0;
       
    95 			iFile.Seek(ESeekEnd,pos);
       
    96 			ret=iFile.Write(_L8("----------New Log----------\015\012"));
       
    97 			}
       
    98 		}
       
    99 	}
       
   100 
       
   101 void CETelLogger::Destruct()
       
   102 /**
       
   103  * This method is used to delete the instantion of CETelLogger.
       
   104  *
       
   105  * @param   None.
       
   106  * @return	None.
       
   107  * @note	None.
       
   108  */
       
   109 	{
       
   110 #ifdef __EXE__
       
   111 	CETelLogger* context=ScriptLoggerContext;
       
   112 	delete context;
       
   113 	ScriptLoggerContext=NULL;
       
   114 #else
       
   115 	CETelLogger* context=(CETelLogger*) Dll::Tls();
       
   116 	delete context;
       
   117 	Dll::SetTls(NULL);
       
   118 #endif
       
   119 	}
       
   120 
       
   121 CETelLogger::~CETelLogger()
       
   122 /**
       
   123  * This method is the Destructor for the CETelLogger class and as such, closes
       
   124  * the logfile.
       
   125  *
       
   126  * @param   None.
       
   127  * @return	None.
       
   128  * @note    None.
       
   129  */
       
   130 	{
       
   131 	if(iValid)
       
   132 		iFile.Close();
       
   133 	iFs.Close();
       
   134 	}
       
   135 
       
   136 void CETelLogger::WriteL(const TDesC8& aText)
       
   137 /**
       
   138  * This is a static method that is used to write a record of information 
       
   139  * into the logfile.  This method is used to place a text string without
       
   140  * any variable arguments in the string of input into the logfile.  If an 
       
   141  * instance of the CETelLogger class does not exist, this method is used 
       
   142  * to create it.  This routine only creates one instance of CETelLogger 
       
   143  * for each session. 
       
   144  *
       
   145  * @param   reference to the text string to be written into the logfile.
       
   146  * @leave   Leaves when no memory to create CETelLogger.
       
   147  * @return	None.
       
   148  * @note    This is a static method.
       
   149  * @note    Logging does not take place if the logging directory does not exist.
       
   150  * @note	The logfile is not deleted at start of a new test session, the
       
   151  * @note    logging for each test session will be appended in the logfile.
       
   152  */
       
   153 	{
       
   154 #ifdef __EXE__
       
   155 	CETelLogger* context=ScriptLoggerContext;
       
   156 #else
       
   157 	CETelLogger* context=(CETelLogger*) Dll::Tls();
       
   158 #endif
       
   159 	if(context==NULL)
       
   160 		{
       
   161 		context=CETelLogger::NewL();
       
   162 #ifdef __EXE__
       
   163 		ScriptLoggerContext=context;
       
   164 #else
       
   165 		Dll::SetTls(context);
       
   166 #endif
       
   167 		}
       
   168 	if(context->iValid)
       
   169 		context->WriteRecord(aText);
       
   170 	}
       
   171 
       
   172 void CETelLogger::Write(const TText8* aText)
       
   173 /**
       
   174  * Static method used as the entry point to write information into the logfile.
       
   175  *
       
   176  * @param   pointer to the text string to be written into the logfile.
       
   177  * @return	None.
       
   178  * @note    This is a static method.
       
   179  */
       
   180 	{
       
   181 	TPtrC8 text(aText);
       
   182 	TRAPD(ret,WriteL(text));
       
   183 	ret = ret;	// compiler warning
       
   184 	}
       
   185 
       
   186 void CETelLogger::WriteFormat(TRefByValue<const TDesC8> aFmt,...)
       
   187 /**
       
   188  * This is a static method that is used to write a record of information 
       
   189  * into the logfile.  This method is used to place a text string with 
       
   190  * one or more variable arguments in the string of input into the logfile.
       
   191  * If an instance of the CETelLogger class does not exist, this method is 
       
   192  * used to create it.  This routine only creates one instance of 
       
   193  * CETelLogger for each session. 
       
   194  *
       
   195  * @param   variable argument list for the text and data in the string
       
   196  * @return	None.
       
   197  * @note    This is a static method.
       
   198  * @note    Logging does not take place if the logging directory does not exist.
       
   199  * @note	The logfile is not deleted at start of a new test session, the
       
   200  * @note    logging for each test session will be appended in the logfile.
       
   201  */
       
   202 	{
       
   203 	TBuf8<KLogBufferSize> buf;
       
   204     VA_LIST list;
       
   205     VA_START(list,aFmt);
       
   206     buf.FormatList(aFmt,list);
       
   207 	TChar tmpchar;
       
   208 	for(TInt i=0;i<buf.Length();i++)
       
   209 		{
       
   210 		tmpchar=buf[i];
       
   211 		if(!((tmpchar.IsPrint()) || (tmpchar=='\n') || (tmpchar=='\r') || (tmpchar=='\t')))
       
   212 			buf[i]='.';
       
   213 		}
       
   214 #ifdef __EXE__
       
   215 	CETelLogger* context=ScriptLoggerContext;
       
   216 #else
       
   217 	CETelLogger* context=(CETelLogger*) Dll::Tls();
       
   218 #endif
       
   219 	if(context==NULL)
       
   220 		{
       
   221 		TRAPD(leave,context=CETelLogger::NewL());		// trap but ignore leaves
       
   222 		leave = leave; 		// fix compiler warning about unused variable
       
   223 #ifdef __EXE__
       
   224 		ScriptLoggerContext=context;
       
   225 #else
       
   226 		Dll::SetTls(context);
       
   227 #endif
       
   228 		}
       
   229 	if(context->iValid)
       
   230 		context->WriteRecord(buf);
       
   231 	}
       
   232 
       
   233 void CETelLogger::WriteRecord(const TDesC8& aText)
       
   234 /**
       
   235  * This method is used to add date and time information to the text string 
       
   236  * and input it as a record into the logfile.
       
   237  *
       
   238  * @param   reference to the text string to be written into the logfile.
       
   239  * @return	None.
       
   240  * @note    This is a static method.
       
   241  */
       
   242 	{
       
   243 	if(iValid)
       
   244 		{
       
   245 		TBuf8<KLogBufferSize> buf;
       
   246 		TTime now;
       
   247 		now.HomeTime();
       
   248 		TDateTime dateTime;
       
   249 		dateTime = now.DateTime();
       
   250 		buf.Format(_L8 ("%02d.%02d:%02d:%06d "),dateTime.Hour(),dateTime.Minute(),dateTime.Second(),dateTime.MicroSecond());
       
   251 		buf.AppendFormat(_L8("%S\015\012"),&aText);
       
   252 		iFile.Write(buf);
       
   253 		iFile.Flush();
       
   254 		}
       
   255 	}
       
   256 
       
   257 TBool CETelLogger::FolderExists()
       
   258 /**
       
   259  * This method determines if the folder c:\logs\Etel exists.
       
   260  *
       
   261  * @param   None.
       
   262  * @return	boolean value of ETrue if folder and EFalse if folder does not exist.
       
   263  * @note    This is a static method.
       
   264  */
       
   265 	{
       
   266 	TUint n;
       
   267 	TInt ret=iFs.Connect();
       
   268 	if (ret!=KErrNone)
       
   269 		return EFalse;
       
   270 	
       
   271 	ret=iFs.Att(KLogFolder,n);
       
   272 	iFs.Close();
       
   273 
       
   274 	if (ret==KErrNone)
       
   275 		return ETrue;
       
   276 	return EFalse;
       
   277 	}
       
   278 
       
   279 #endif