vtengines/videoteleng/Logger/Src/CVtLogger.cpp
changeset 0 ed9695c8bcbe
child 4 6dc066157ed4
equal deleted inserted replaced
-1:000000000000 0:ed9695c8bcbe
       
     1 /*
       
     2 * Copyright (c) 2004 - 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:  Video telephony logger
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #pragma CTC SKIP
       
    20 // INCLUDE FILES
       
    21 #include    "cvtlogger.h"
       
    22 
       
    23 
       
    24 // LOCAL CONSTANTS
       
    25 _LIT( KVtEngLogConfig, "c:\\Logs\\VT\\VTLOGCONF.txt" );
       
    26 _LIT( KVtEngLogFolder, "c:\\Logs\\VT" );
       
    27 _LIT( KVtEngLogFolderLogger, "VT" );
       
    28 _LIT( KVtEngLogPrefix, "VT: " );
       
    29 _LIT( KVtEngLogExtension, ".txt" );
       
    30 _LIT( KVtLoggerEnterFunc, "<" );
       
    31 _LIT( KVtLoggerExitFunc, ">" );
       
    32 
       
    33 
       
    34 // ============================ MEMBER FUNCTIONS ===============================
       
    35 
       
    36 // -----------------------------------------------------------------------------
       
    37 // CVtLogger::CVtLogger
       
    38 // C++ default constructor can NOT contain any code, that
       
    39 // might leave.
       
    40 // -----------------------------------------------------------------------------
       
    41 //
       
    42 CVtLogger::CVtLogger() : CActive( CActive::EPriorityStandard - 1 ),
       
    43     iLogLevel( ELogLogEverything )
       
    44     {
       
    45     }
       
    46 
       
    47 // -----------------------------------------------------------------------------
       
    48 // CVtLogger::ConstructL
       
    49 // Symbian 2nd phase constructor can leave.
       
    50 // -----------------------------------------------------------------------------
       
    51 //
       
    52 void CVtLogger::ConstructL( )
       
    53     {
       
    54     User::LeaveIfError( iFs.Connect() );
       
    55     TFindFile find( iFs );
       
    56     TUint logLevel( 0 );
       
    57     TInt ret = iFs.Att(KVtEngLogFolder, logLevel );
       
    58     if ( ret == KErrNone )
       
    59         {
       
    60         if ( find.FindByPath( KVtEngLogConfig, NULL ) == KErrNone )
       
    61             {
       
    62             CActiveScheduler::Add( this );
       
    63             ReadCongigFile();
       
    64             }
       
    65         if ( iLogLevel & ELogToFile )
       
    66             {
       
    67             RThread thread;
       
    68             iFileName.Copy( thread.Name() );
       
    69             iFileName.Append( KVtEngLogExtension );
       
    70             }
       
    71         }
       
    72     Dll::SetTls( this );
       
    73     }
       
    74 
       
    75 // -----------------------------------------------------------------------------
       
    76 // CVtLogger::NewL
       
    77 // Two-phased constructor.
       
    78 // -----------------------------------------------------------------------------
       
    79 //
       
    80 CVtLogger* CVtLogger::NewL( )
       
    81     {
       
    82     CVtLogger* self = new( ELeave ) CVtLogger;
       
    83 
       
    84     CleanupStack::PushL( self );
       
    85     self->ConstructL( );
       
    86     CleanupStack::Pop();
       
    87 
       
    88     return self;
       
    89     }
       
    90 
       
    91 // -----------------------------------------------------------------------------
       
    92 // CVtLogger::~CVtLogger
       
    93 // Destructor
       
    94 // -----------------------------------------------------------------------------
       
    95 //
       
    96 CVtLogger::~CVtLogger()
       
    97     {
       
    98     Cancel();
       
    99     iFs.Close();
       
   100     Dll::SetTls( NULL );
       
   101     }
       
   102 
       
   103 // -----------------------------------------------------------------------------
       
   104 // CVtLogger::Print
       
   105 // Writes to log.
       
   106 // -----------------------------------------------------------------------------
       
   107 //
       
   108 void CVtLogger::Print( TDesC& aDes )
       
   109     {
       
   110     if ( iLogLevel & ELogTime && !(iLogLevel & ELogToFile) )
       
   111         {
       
   112         TTime time;
       
   113         time.HomeTime();
       
   114         const TDateTime dt= time.DateTime();
       
   115 
       
   116         iTempBuf.Zero();
       
   117         iTempBuf.AppendNum( dt.Minute() );
       
   118         iTempBuf.Append(':');
       
   119         iTempBuf.AppendNum( dt.Second() );
       
   120         iTempBuf.Append(':');
       
   121         iTempBuf.AppendNum( dt.MicroSecond() );
       
   122         iTempBuf.Append(' ');
       
   123 
       
   124         const TInt currentLength( iTempBuf.Length() );
       
   125 
       
   126         iTempBuf.Append( aDes.Left( Min( iTempBuf.MaxLength() - currentLength, aDes.Length() ) ) );
       
   127         RDebug::Print( iTempBuf );
       
   128         }
       
   129     else
       
   130         {
       
   131         if ( iLogLevel & ELogToFile )
       
   132             {
       
   133             RFileLogger::Write(
       
   134                 KVtEngLogFolderLogger,
       
   135                 iFileName,
       
   136                 EFileLoggingModeAppend,
       
   137                 aDes );
       
   138             }
       
   139         else
       
   140             {
       
   141             RDebug::Print( aDes );
       
   142             }
       
   143         }
       
   144     }
       
   145 
       
   146 // -----------------------------------------------------------------------------
       
   147 // CVtLogger::Print
       
   148 // Writes to log.
       
   149 // -----------------------------------------------------------------------------
       
   150 //
       
   151 EXPORT_C void CVtLogger::Print(
       
   152     TInt aLevel,
       
   153     TPtrC aBuffer )
       
   154     {
       
   155     CVtLogger* logger = CVtLogger::Logger();
       
   156     if ( logger && logger->CheckLevels( aLevel ) )
       
   157         {
       
   158         logger->iBuffer.Copy( KVtEngLogPrefix );
       
   159         logger->iBuffer.Append( aBuffer);
       
   160         logger->Print( logger->iBuffer );
       
   161         }
       
   162     }
       
   163 
       
   164 // -----------------------------------------------------------------------------
       
   165 // CVtLogger::Print
       
   166 // Writes to log.
       
   167 // -----------------------------------------------------------------------------
       
   168 //
       
   169 EXPORT_C void CVtLogger::Print(
       
   170     TInt aLevel,
       
   171     TPtrC aBuffer,
       
   172     TInt aValue )
       
   173     {
       
   174     CVtLogger* logger = CVtLogger::Logger();
       
   175     if ( logger && logger->CheckLevels( aLevel ) )
       
   176         {
       
   177         logger->iBuffer.Zero();
       
   178         logger->iBuffer.AppendNum( aValue );
       
   179         logger->iBuffer.Format( aBuffer, aValue );
       
   180         logger->iBuffer.Insert( 0, KVtEngLogPrefix );
       
   181         logger->Print( logger->iBuffer );
       
   182         }
       
   183     }
       
   184 
       
   185 // -----------------------------------------------------------------------------
       
   186 // CVtLogger::Print
       
   187 // Writes to log.
       
   188 // -----------------------------------------------------------------------------
       
   189 //
       
   190 EXPORT_C void CVtLogger::Print(
       
   191     TInt aLevel,
       
   192     TPtrC aBuffer,
       
   193     TInt aValue1,
       
   194     TInt aValue2 )
       
   195     {
       
   196     CVtLogger* logger = CVtLogger::Logger();
       
   197     if ( logger && logger->CheckLevels( aLevel ) )
       
   198         {
       
   199         logger->iBuffer.Format( aBuffer, aValue1, aValue2 );
       
   200         logger->iBuffer.Insert( 0, KVtEngLogPrefix );
       
   201         logger->Print( logger->iBuffer );
       
   202         }
       
   203     }
       
   204 
       
   205 // -----------------------------------------------------------------------------
       
   206 // CVtLogger::Print
       
   207 // Writes to log.
       
   208 // -----------------------------------------------------------------------------
       
   209 //
       
   210 EXPORT_C void CVtLogger::Print(
       
   211             TInt aLevel,
       
   212             const TBool aEntry,
       
   213             TPtrC aBuffer,
       
   214             TInt aRetVal )
       
   215     {
       
   216     CVtLogger* logger = CVtLogger::Logger();
       
   217     if ( logger && logger->CheckLevels( aLevel ) )
       
   218         {
       
   219         logger->iBuffer.Copy( KVtEngLogPrefix );
       
   220         if ( aEntry )
       
   221             {
       
   222             logger->iBuffer.Append( aBuffer );
       
   223             logger->iBuffer.Append( KVtLoggerEnterFunc );
       
   224             }
       
   225         else
       
   226             {
       
   227             if ( aRetVal != KVtDebugReturnValNotApplicable )
       
   228                 {
       
   229                 logger->iTempBuf.Format( aBuffer, aRetVal );
       
   230                 logger->iBuffer.Append( logger->iTempBuf );
       
   231                 }
       
   232             else
       
   233                 {
       
   234                 logger->iBuffer.Append( aBuffer );
       
   235                 }
       
   236             logger->iBuffer.Append( KVtLoggerExitFunc );
       
   237             }
       
   238         logger->Print( logger->iBuffer );
       
   239         }
       
   240     }
       
   241 
       
   242 // -----------------------------------------------------------------------------
       
   243 // CVtLogger::Logger
       
   244 // Returns logger.
       
   245 // -----------------------------------------------------------------------------
       
   246 //
       
   247 CVtLogger* CVtLogger::Logger()
       
   248     {
       
   249     TAny* tls = Dll::Tls();
       
   250     if ( tls )
       
   251         {
       
   252         return reinterpret_cast<CVtLogger*>( tls );
       
   253         }
       
   254     return NULL;
       
   255     }
       
   256 
       
   257 // -----------------------------------------------------------------------------
       
   258 // CVtLogger::Initialize
       
   259 // Initialize logger.
       
   260 // -----------------------------------------------------------------------------
       
   261 //
       
   262 EXPORT_C void CVtLogger::Initialize()
       
   263     {
       
   264     CVtLogger* logger = NULL;
       
   265     if ( Dll::Tls() == NULL )
       
   266         {
       
   267         TRAPD( err, logger = CVtLogger::NewL() );
       
   268         if ( err == KErrNone )
       
   269             {
       
   270             Dll::SetTls( logger );
       
   271             }
       
   272         }
       
   273     else
       
   274         {
       
   275         logger = CVtLogger::Logger();
       
   276         }
       
   277     if ( logger )
       
   278         {
       
   279         (logger->iAccessCount)++;
       
   280         }
       
   281 
       
   282     }
       
   283 
       
   284 // -----------------------------------------------------------------------------
       
   285 // CVtLogger::Uninitialize
       
   286 // Uninitialize logger.
       
   287 // -----------------------------------------------------------------------------
       
   288 //
       
   289 EXPORT_C void CVtLogger::Uninitialize()
       
   290     {
       
   291     TAny* tls = Dll::Tls();
       
   292     if ( tls )
       
   293         {
       
   294         CVtLogger* logger = CVtLogger::Logger();
       
   295         TInt count( --(logger->iAccessCount) );
       
   296         if ( !count )
       
   297             {
       
   298             delete reinterpret_cast<CVtLogger*>( tls );
       
   299             Dll::SetTls( NULL );
       
   300             }
       
   301         }
       
   302 
       
   303     }
       
   304 
       
   305 void CVtLogger::ReadCongigFile()
       
   306     {
       
   307 #ifdef _DEBUG
       
   308     RDebug::Print(_L(" VT: ReadConfigFile" ) );
       
   309 #endif
       
   310     RFile config;
       
   311     if ( config.Open( iFs, KVtEngLogConfig, EFileShareAny ) == KErrNone )
       
   312         {
       
   313         TBuf8<16> buf;
       
   314         if ( config.Read( buf ) == KErrNone )
       
   315             {
       
   316             TLex8 lex( buf );
       
   317             TUint logLevel( 0 );
       
   318             if ( lex.Val( logLevel, EHex ) == KErrNone )
       
   319                 {
       
   320                 iLogLevel = logLevel;
       
   321                 }
       
   322             }
       
   323         config.Close();
       
   324         }
       
   325     iFs.NotifyChange( ENotifyEntry, iStatus, KVtEngLogConfig() );
       
   326     SetActive();
       
   327     }
       
   328 
       
   329 void CVtLogger::RunL()
       
   330     {
       
   331     if ( iStatus == KErrNone )
       
   332         {
       
   333         ReadCongigFile();
       
   334         }
       
   335     }
       
   336 
       
   337 void CVtLogger::DoCancel()
       
   338     {
       
   339     iFs.NotifyChangeCancel();
       
   340     }
       
   341 
       
   342 // -----------------------------------------------------------------------------
       
   343 // CVtLogger::Flags
       
   344 // Returns flags.
       
   345 // -----------------------------------------------------------------------------
       
   346 //
       
   347 EXPORT_C TInt CVtLogger::Flags()
       
   348     {
       
   349     CVtLogger* logger = CVtLogger::Logger();
       
   350     if ( logger )
       
   351         {
       
   352         return logger->iLogLevel;
       
   353         }
       
   354     return KErrGeneral;
       
   355     }
       
   356 
       
   357 // -----------------------------------------------------------------------------
       
   358 // CVtLogger::CheckLevels
       
   359 // Check debug level.
       
   360 // -----------------------------------------------------------------------------
       
   361 //
       
   362 TBool CVtLogger::CheckLevels( TInt aLevels ) const
       
   363     {
       
   364     if ( aLevels & iLogLevel )
       
   365         {
       
   366         return ETrue;
       
   367         }
       
   368     return EFalse;
       
   369     }
       
   370 #pragma CTC ENDSKIP
       
   371 //  End of File