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