webservices/wslogger/src/senlogger.cpp
changeset 0 62f9d29f7211
child 15 32ab7ae9ec94
equal deleted inserted replaced
-1:000000000000 0:62f9d29f7211
       
     1 /*
       
     2 * Copyright (c) 2002-2005 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:          
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 
       
    21 
       
    22 
       
    23 
       
    24 
       
    25 
       
    26 
       
    27 #include "senlogger.h"
       
    28 //#include <flogger.h>
       
    29 #include <f32file.h>
       
    30 #include <s32file.h> 
       
    31 #include <versit.h>
       
    32 
       
    33 namespace
       
    34     {
       
    35     const TInt KColumnWidth = 100; // used in WriteAll() methods.
       
    36      _LIT(KPath,"C:\\SenLogConfig.txt");
       
    37      _LIT(  KClientIdFmt,  "- Client[%d] %S");
       
    38      
       
    39      _LIT(  KClientIdFmtNextLine,  "Client(%d):");
       
    40      
       
    41      _LIT8( KClientIdFmt8, "- Client[%d] %S");
       
    42      _LIT8( KClientIdFmtNextLine8, "Client(%d):");
       
    43     }
       
    44 
       
    45 EXPORT_C TInt CSenLogger::CreateL(TInt aChannel, TInt aLevel, const TDesC& aLogDir, const TDesC& aLogFileName)
       
    46      {
       
    47 	 return CreateL( aChannel, aLevel, aLogDir, aLogFileName, EFileLoggingModeOverwrite );
       
    48      }
       
    49 EXPORT_C TInt CSenLogger::CreateL(TInt aChannel, TInt aLevel, const TDesC& aLogDir, const TDesC& aLogFileName, TFileLoggingMode aMode)
       
    50     {
       
    51     TInt index(KErrNotFound);
       
    52     //	  CSenLogger* pSenLogger = (CSenLogger*)Dll::Tls();
       
    53     CSenLogger* pSenLogger = NULL;
       
    54     TWsTls* tls = (TWsTls*)Dll::Tls();
       
    55     if( !tls )
       
    56         {
       
    57         tls = new (ELeave) TWsTls();
       
    58         tls->iLogger = NULL;
       
    59         tls->iProxy = NULL;
       
    60         Dll::SetTls( tls );
       
    61         }
       
    62     pSenLogger = tls->iLogger;    
       
    63 	if ( !pSenLogger )
       
    64   		{
       
    65   		pSenLogger = new (ELeave) CSenLogger();
       
    66         if ( pSenLogger )
       
    67             {
       
    68             //      		  Dll::SetTls(pSenLogger);
       
    69             tls->iLogger = pSenLogger;
       
    70       		pSenLogger->iCount = 0;
       
    71       		//reading the configuration file for the logging levels!!!
       
    72       		
       
    73       		/*the file has the  following format
       
    74       		* ==============================================================================
       
    75       		  * LogChannel LogLevel
       
    76       		    1000 0
       
    77       		    2000 1
       
    78       		      .
       
    79       		      .
       
    80       		      .
       
    81       		      .
       
    82       		      .
       
    83       		      ....
       
    84       		    18000 2
       
    85       		   this means first you need to write the channel followed by the corresponding logging level after a space
       
    86       		 
       
    87       		 the file has to be located under C:\Config.txt
       
    88       		 * ==============================================================================*/
       
    89       		 
       
    90       
       
    91       		TInt res;
       
    92       	    RFs fs;
       
    93       	    res=fs.Connect();
       
    94       	      if(res==KErrNone)
       
    95 				{
       
    96       	     
       
    97                 RFileReadStream readStream;
       
    98       	        res=readStream.Open(fs,KPath,EFileRead|EFileStreamText);
       
    99       	   		if(res==KErrNone)
       
   100       	     		{
       
   101       	      		TInt error;
       
   102       	      		TBool result;
       
   103       	      		TInt retVal;
       
   104       	      		
       
   105       	      		CLineReader *readLine = CLineReader::NewL(readStream);
       
   106                     CleanupStack::PushL(readLine);
       
   107       	    		
       
   108                 	do
       
   109       	    		    {
       
   110       	      		    result=readLine->ReadLineL(0,error);
       
   111       	      		    
       
   112       	       	        if(result==CLineReader::ELineHasContent)
       
   113       	       			    {
       
   114       	       			    
       
   115               	      		TInt *key = new (ELeave) TInt;
       
   116               	      	
       
   117           	      		
       
   118       	       			    TLex8 lexBuf(readLine->iBufPtr);
       
   119       	     			    retVal = lexBuf.Val(*key);
       
   120       	     			    
       
   121       	     			    if(retVal!=0)
       
   122       	     			        {
       
   123       	     			        delete key;
       
   124                              
       
   125       	     			        continue;
       
   126       	     			        }
       
   127       	     			    else
       
   128       	     			        {
       
   129                   	      		TInt *value = new (ELeave) TInt;
       
   130                   	      
       
   131       	      		
       
   132   	     			            lexBuf.SkipSpace();
       
   133   	     			            retVal=lexBuf.Val(*value);
       
   134   	     			            if(retVal==0)
       
   135 	  	     			            {
       
   136   	     			    	        retVal = pSenLogger->iMap.Append(key,value);
       
   137   	     			    	        if(retVal != KErrNone)
       
   138   	     			    	            {
       
   139   	     			    	            delete key;
       
   140   	     			    	            delete value;
       
   141       	    		                   
       
   142   	     			    	            }
       
   143   	     			    	   
       
   144   		     			            }
       
   145   		     			         else
       
   146   		     			            {
       
   147   		     			              delete key;
       
   148   	     			    	          delete value;
       
   149   		     			                
       
   150   		     			            }
       
   151       	     			    	
       
   152       	     			        }
       
   153       	       			    }
       
   154       	       			 
       
   155       	    		    }while(error!=KErrEof);
       
   156       	    		 
       
   157       	    		 CleanupStack::PopAndDestroy(readLine);
       
   158       	      	     readStream.Close();
       
   159       	     		}
       
   160       	     
       
   161       	      	fs.Close();
       
   162       	      	}
       
   163             }
       
   164         else
       
   165             {
       
   166             return KErrGeneral;
       
   167             }
       
   168   		}
       
   169   	else
       
   170   	    {
       
   171   	    index = pSenLogger->ChannelIndex(aChannel);
       
   172   	    }	
       
   173   	
       
   174   	if (index == KErrNotFound)
       
   175   	    {
       
   176   	    TInt channelIndex;
       
   177         TLog* pTLog = new (ELeave) TLog;
       
   178         CleanupStack::PushL(pTLog);
       
   179         pTLog->iChannel=aChannel;
       
   180         channelIndex=pSenLogger->iMap.Find(aChannel);
       
   181         if(channelIndex==KErrNotFound)
       
   182         {
       
   183         pTLog->iLevel=aLevel;
       
   184         }
       
   185         else
       
   186         {
       
   187         pTLog->iLevel=*(pSenLogger->iMap.ValueAt(channelIndex));
       
   188         }
       
   189         pTLog->iCount=1;
       
   190         pTLog->iLog.Connect();
       
   191         pTLog->iLog.CreateLog(aLogDir, aLogFileName, aMode);
       
   192         pTLog->iLog.Write(_L("SenTLSLogger - Log file opened"));
       
   193 #ifdef EKA
       
   194         pSenLogger->iLogs.AppendL(pTLog);
       
   195 #else
       
   196         User::LeaveIfError(pSenLogger->iLogs.Append(pTLog));
       
   197 	  
       
   198 #endif
       
   199         CleanupStack::Pop(pTLog);
       
   200   	    }
       
   201   	else
       
   202   	    {
       
   203   	    pSenLogger->iLogs[index]->iCount++;
       
   204   	    }
       
   205   	pSenLogger->iCount++;
       
   206     
       
   207     return KErrNone;
       
   208     }
       
   209 
       
   210 EXPORT_C void CSenLogger::Destroy(TInt aChannel)
       
   211     {
       
   212     //	CSenLogger* pSenLogger = (CSenLogger*)Dll::Tls();
       
   213     CSenLogger* pSenLogger = NULL;
       
   214     TWsTls* tls = (TWsTls*)Dll::Tls();
       
   215     if( tls )
       
   216         {
       
   217         pSenLogger = tls->iLogger;
       
   218         }
       
   219 	if ( pSenLogger )
       
   220 	    {
       
   221 	    
       
   222 	    TInt index = pSenLogger->ChannelIndex(aChannel);
       
   223 	    if (index != KErrNotFound)
       
   224 	        {
       
   225 	        pSenLogger->iLogs[index]->iCount--;
       
   226 	        if (pSenLogger->iLogs[index]->iCount <= 0)
       
   227 	            {
       
   228                 pSenLogger->iLogs[index]->iLog.Write(_L("SenTLSLogger - Log file closed."));
       
   229                 pSenLogger->iLogs[index]->iLog.CloseLog();
       
   230                 pSenLogger->iLogs[index]->iLog.Close();
       
   231                 delete pSenLogger->iLogs[index];
       
   232                 pSenLogger->iLogs.Remove(index);
       
   233 	            }
       
   234             }
       
   235 	        pSenLogger->iCount--;
       
   236 	        if (pSenLogger->iCount <= 0)
       
   237 	            {
       
   238 	            pSenLogger->iLogs.Close();
       
   239 	            delete pSenLogger;
       
   240             tls->iLogger = NULL;
       
   241             if( !tls->iLogger && !tls->iProxy )
       
   242                 {
       
   243 	            delete tls;
       
   244                 Dll::FreeTls();	            
       
   245 	            }
       
   246 	        }
       
   247 	    }
       
   248     }
       
   249 
       
   250 // 16-bit variants    
       
   251     
       
   252 EXPORT_C void CSenLogger::Write(TInt aChannel, TInt aLevel, const TDesC16& aText)
       
   253     {
       
   254     RFileLogger* pLogger = Logger(aChannel, aLevel);
       
   255     if ( pLogger )
       
   256         {
       
   257         pLogger->Write(aText);
       
   258         }
       
   259     }
       
   260 
       
   261 EXPORT_C void CSenLogger::WriteAll(TInt aChannel, TInt aLevel, const TDesC16& aText)
       
   262     {
       
   263     RFileLogger* pLogger = Logger(aChannel, aLevel);
       
   264     if ( pLogger )
       
   265         {
       
   266         TInt i = 0;
       
   267         TInt lineAmount(KColumnWidth);
       
   268         TInt length = aText.Length();
       
   269         while(i<aText.Length())
       
   270             {
       
   271             if ( length - i < KColumnWidth )
       
   272                 {
       
   273                 lineAmount = length - i;    
       
   274                 }
       
   275             pLogger->Write(aText.Mid(i, lineAmount));
       
   276             i += KColumnWidth;
       
   277             }
       
   278         }
       
   279     }
       
   280 
       
   281 EXPORT_C void CSenLogger::WriteFormat(TInt aChannel, TInt aLevel, TRefByValue<const TDesC16> aFmt,...)
       
   282     {
       
   283     RFileLogger* pLogger = Logger(aChannel, aLevel);
       
   284     if (pLogger)
       
   285         {
       
   286         VA_LIST list;
       
   287         VA_START(list,aFmt);
       
   288         pLogger->WriteFormat(aFmt, list);
       
   289         }    
       
   290     }
       
   291     
       
   292 EXPORT_C void CSenLogger::WriteFormat(TInt aChannel, TInt aLevel, TRefByValue<const TDesC16> aFmt, VA_LIST& aList)
       
   293     {
       
   294     RFileLogger* pLogger = Logger(aChannel, aLevel);
       
   295     if (pLogger)
       
   296         {
       
   297         pLogger->WriteFormat(aFmt, aList);
       
   298         }    
       
   299     }
       
   300 
       
   301 
       
   302 // Client ID spesific logging methods    
       
   303 EXPORT_C void CSenLogger::WriteWithClientId(TInt aClientId, TInt aChannel, TInt aLevel, const TDesC16& aText)
       
   304     {
       
   305     WriteFormat( aChannel, aLevel, KClientIdFmt, aClientId, &aText );
       
   306     }
       
   307 
       
   308 EXPORT_C void CSenLogger::WriteAllWithClientId(TInt aClientId, TInt aChannel, TInt aLevel, const TDesC16& aText)
       
   309     {
       
   310     // Write client id on separate line, for clarity & simplicity
       
   311     WriteFormat( aChannel, aLevel, KClientIdFmtNextLine, aClientId);
       
   312     WriteAll( aChannel, aLevel, aText );
       
   313     }
       
   314 
       
   315 EXPORT_C void CSenLogger::WriteFormatWithClientId(TInt aClientId, TInt aChannel, TInt aLevel, TRefByValue<const TDesC16> aFmt, ...)
       
   316     {
       
   317     // Write client id on separate line, for clarity & simplicity
       
   318     WriteFormat( aChannel, aLevel, KClientIdFmtNextLine, aClientId);
       
   319     VA_LIST list;
       
   320     VA_START(list,aFmt);
       
   321     WriteFormat( aChannel, aLevel, aFmt, list);
       
   322     }
       
   323     
       
   324 EXPORT_C void CSenLogger::WriteFormatWithClientId(TInt aClientId, TInt aChannel, TInt aLevel, TRefByValue<const TDesC16> aFmt, VA_LIST& aList)
       
   325     {
       
   326     WriteFormat( aChannel, aLevel, KClientIdFmtNextLine, aClientId);
       
   327     WriteFormat( aChannel, aLevel, aFmt, aList);
       
   328     }
       
   329 
       
   330 EXPORT_C void CSenLogger::WriteFormatWithClientIdToMainLogs(TInt aClientId, TInt aLevel, TRefByValue<const TDesC16> aFmt,...)
       
   331     {
       
   332     VA_LIST list;
       
   333     VA_START(list,aFmt);
       
   334     WriteFormatWithClientId( aClientId, KSenClientSessionLogChannelBase+aClientId, aLevel, aFmt, list );
       
   335     WriteFormatWithClientId( aClientId, KSenCoreServiceManagerLogChannelBase, aLevel, aFmt, list );
       
   336     }
       
   337 
       
   338 EXPORT_C void CSenLogger::WriteFormatWithClientIdToMainLogs(TInt aClientId, TInt aLevel, TRefByValue<const TDesC16> aFmt, VA_LIST& aList)
       
   339     {
       
   340     WriteFormatWithClientId( aClientId, KSenClientSessionLogChannelBase+aClientId, aLevel, aFmt, aList );
       
   341     WriteFormatWithClientId( aClientId, KSenCoreServiceManagerLogChannelBase, aLevel, aFmt, aList );
       
   342     }
       
   343     
       
   344 // 8-bit variants    
       
   345 
       
   346 EXPORT_C void CSenLogger::Write(TInt aChannel, TInt aLevel, const TDesC8& aText)
       
   347     {
       
   348     RFileLogger* pLogger = Logger(aChannel, aLevel);
       
   349     if (pLogger)
       
   350         {
       
   351         pLogger->Write(aText);
       
   352         }
       
   353     }
       
   354 
       
   355 EXPORT_C void CSenLogger::WriteAll(TInt aChannel, TInt aLevel, const TDesC8& aText)
       
   356     {
       
   357     RFileLogger* pLogger = Logger(aChannel, aLevel);
       
   358     if ( pLogger )
       
   359         {
       
   360         TInt i = 0;
       
   361         TInt lineAmount(KColumnWidth);
       
   362         TInt length = aText.Length();
       
   363         while(i<aText.Length())
       
   364             {
       
   365             if ( length - i < KColumnWidth )
       
   366                 {
       
   367                 lineAmount = length - i;    
       
   368                 }
       
   369             pLogger->Write(aText.Mid(i, lineAmount));
       
   370             i += KColumnWidth;
       
   371             }
       
   372         }
       
   373     }
       
   374 
       
   375 
       
   376 EXPORT_C void CSenLogger::WriteFormat(TInt aChannel, TInt aLevel, TRefByValue<const TDesC8> aFmt,...)
       
   377     {
       
   378     RFileLogger* pLogger = Logger(aChannel, aLevel);
       
   379     if (pLogger)
       
   380         {
       
   381         VA_LIST list;
       
   382         VA_START(list,aFmt);
       
   383         pLogger->WriteFormat(aFmt, list);
       
   384         }    
       
   385     }
       
   386 
       
   387 EXPORT_C void CSenLogger::WriteFormat(TInt aChannel, TInt aLevel, TRefByValue<const TDesC8> aFmt, VA_LIST& aList)
       
   388     {
       
   389     RFileLogger* pLogger = Logger(aChannel, aLevel);
       
   390     if (pLogger)
       
   391         {
       
   392         pLogger->WriteFormat(aFmt, aList);
       
   393         }    
       
   394     }
       
   395 
       
   396 EXPORT_C void CSenLogger::WriteWithClientId(TInt aClientId, TInt aChannel, TInt aLevel, const TDesC8& aText)
       
   397     {
       
   398     WriteFormat( aChannel, aLevel, KClientIdFmt8, aClientId, &aText );    	
       
   399     }
       
   400 
       
   401 EXPORT_C void CSenLogger::WriteAllWithClientId(TInt aClientId, TInt aChannel, TInt aLevel, const TDesC8& aText)
       
   402     {
       
   403     // Write client id on separate line, for clarity & simplicity
       
   404     WriteFormat( aChannel, aLevel, KClientIdFmtNextLine8, aClientId);
       
   405     WriteAll( aChannel, aLevel, aText );
       
   406     }
       
   407 
       
   408 EXPORT_C void CSenLogger::WriteFormatWithClientId(TInt aClientId, TInt aChannel, TInt aLevel, TRefByValue<const TDesC8> aFmt,...)
       
   409     {
       
   410     // Write client id on separate line, for clarity & simplicity
       
   411     WriteFormat( aChannel, aLevel, KClientIdFmtNextLine8, aClientId);
       
   412     VA_LIST list;
       
   413     VA_START(list,aFmt);
       
   414     WriteFormat( aChannel, aLevel, aFmt, list);
       
   415     }
       
   416     
       
   417 EXPORT_C void CSenLogger::WriteFormatWithClientId(TInt aClientId, TInt aChannel, TInt aLevel, TRefByValue<const TDesC8> aFmt, VA_LIST& aList)
       
   418     {
       
   419     WriteFormat( aChannel, aLevel, KClientIdFmtNextLine8, aClientId);
       
   420     WriteFormat( aChannel, aLevel, aFmt, aList);
       
   421     }
       
   422 
       
   423 EXPORT_C void CSenLogger::WriteFormatWithClientIdToMainLogs(TInt aClientId, TInt aLevel, TRefByValue<const TDesC8> aFmt, ...)
       
   424     {
       
   425     VA_LIST list;
       
   426     VA_START(list,aFmt);
       
   427     WriteFormatWithClientId( aClientId, KSenClientSessionLogChannelBase+aClientId, aLevel, aFmt, list );
       
   428     WriteFormatWithClientId( aClientId, KSenCoreServiceManagerLogChannelBase, aLevel, aFmt, list );
       
   429     }
       
   430 
       
   431 EXPORT_C void CSenLogger::WriteFormatWithClientIdToMainLogs(TInt aClientId, TInt aLevel, TRefByValue<const TDesC8> aFmt, VA_LIST& aList)
       
   432     {
       
   433     WriteFormatWithClientId( aClientId, KSenClientSessionLogChannelBase+aClientId, aLevel, aFmt, aList );
       
   434     WriteFormatWithClientId( aClientId, KSenCoreServiceManagerLogChannelBase, aLevel, aFmt, aList );
       
   435     }
       
   436 
       
   437 
       
   438 RFileLogger* CSenLogger::Logger(TInt aChannel, TInt aLevel)
       
   439     {
       
   440 //    CSenLogger* pSenLogger = (CSenLogger*)Dll::Tls();
       
   441     CSenLogger* pSenLogger = NULL;
       
   442     TWsTls* tls = (TWsTls*)Dll::Tls();
       
   443     if( tls )
       
   444         {
       
   445         pSenLogger = tls->iLogger;
       
   446         }
       
   447 	if ( pSenLogger )
       
   448 	    {
       
   449         TInt index = pSenLogger->ChannelIndex(aChannel);
       
   450         if (index != KErrNotFound)
       
   451             {
       
   452             if (pSenLogger->iLogs[index]->iLevel >= aLevel)
       
   453                 {
       
   454                 return &pSenLogger->iLogs[index]->iLog;
       
   455                 }
       
   456             }
       
   457 	    }
       
   458 	return NULL;
       
   459     }
       
   460 
       
   461 TInt CSenLogger::ChannelIndex(TInt aChannel) const
       
   462     {
       
   463     TInt index(KErrNotFound);
       
   464     TInt count=iLogs.Count();
       
   465     for (TInt i=0; i<count; i++)      	    
       
   466         {
       
   467         if (iLogs[i]->iChannel == aChannel)
       
   468             {
       
   469             index = i;
       
   470             break;
       
   471             }
       
   472         }
       
   473     return index;    
       
   474     }
       
   475 
       
   476 EXPORT_C CSenLogger::CSenLogger()
       
   477     : iCount(0),iMap(1,1)
       
   478     {
       
   479     }
       
   480 
       
   481 CSenLogger::~CSenLogger()
       
   482     {
       
   483    
       
   484     iMap.Reset();
       
   485     }
       
   486 
       
   487 // END OF FILE
       
   488