contentpublishingsrv/contentpublishingutils/contentpublishingdebug/src/cpdebug.cpp
branchRCL_3
changeset 114 a5a39a295112
child 122 6cadd6867c17
equal deleted inserted replaced
113:0efa10d348c0 114:a5a39a295112
       
     1 /*
       
     2 * Copyright (c)  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 #include "cpdebug.h"
       
    20 
       
    21 const TUint KMemDataSize( 100 );
       
    22 const TUint KBufMaxSize( 512 );
       
    23 const TUint KThousand( 1000 );
       
    24 
       
    25 // ======== MEMBER FUNCTIONS ========
       
    26 
       
    27 // ---------------------------------------------------------------------------
       
    28 // 
       
    29 // ---------------------------------------------------------------------------
       
    30 //
       
    31 void CCPDebug::ConstructL( const TDesC& aFile )
       
    32     {
       
    33     iData = new (ELeave) DebugData();
       
    34     iData->iLogEnabled = EFalse;
       
    35     iData->iFirstUpdateTime.UniversalTime( );
       
    36     iData->iFileName.Create( aFile.Length( ) );
       
    37     iData->iFileName = aFile;
       
    38     Dll::SetTls( iData );
       
    39     User::LeaveIfError( iData->iFs.Connect( ) );
       
    40     EnableLogging( EFalse );
       
    41     }
       
    42 
       
    43 // ---------------------------------------------------------------------------
       
    44 // 
       
    45 // ---------------------------------------------------------------------------
       
    46 //
       
    47 EXPORT_C CCPDebug* CCPDebug::NewL( const TDesC& aFile )
       
    48     {
       
    49     CCPDebug* self = NewLC( aFile );
       
    50     CleanupStack::Pop(self);
       
    51     return self;
       
    52     }
       
    53 
       
    54 // ---------------------------------------------------------------------------
       
    55 // 
       
    56 // ---------------------------------------------------------------------------
       
    57 //
       
    58 EXPORT_C CCPDebug* CCPDebug::NewLC( const TDesC& aFile )
       
    59     {
       
    60     CCPDebug* self = new (ELeave) CCPDebug();
       
    61     CleanupStack::PushL(self);
       
    62     self->ConstructL( aFile );
       
    63     return self;
       
    64     }
       
    65 
       
    66 // ---------------------------------------------------------------------------
       
    67 //
       
    68 // ---------------------------------------------------------------------------
       
    69 //
       
    70 CCPDebug::CCPDebug()
       
    71     {
       
    72 
       
    73     }
       
    74 
       
    75 // ---------------------------------------------------------------------------
       
    76 // 
       
    77 // ---------------------------------------------------------------------------
       
    78 //	
       
    79 CCPDebug::~CCPDebug()
       
    80     {
       
    81     if ( iData )
       
    82         {
       
    83         iData->iFileName.Close( );
       
    84         iData->iLogFile.Close( );
       
    85         iData->iFs.Close( );
       
    86         }
       
    87     Dll::FreeTls( );
       
    88     delete iData;
       
    89     }
       
    90 
       
    91 // ---------------------------------------------------------------------------
       
    92 // 
       
    93 // ---------------------------------------------------------------------------
       
    94 //
       
    95 EXPORT_C TBool CCPDebug::Enable()
       
    96     {
       
    97     DebugData* data = Data();
       
    98     if(!data)
       
    99         {
       
   100         return false;
       
   101         }
       
   102     else
       
   103         {
       
   104         return true;
       
   105         }
       
   106     }
       
   107 
       
   108 // ---------------------------------------------------------------------------
       
   109 // 
       
   110 // ---------------------------------------------------------------------------
       
   111 //
       
   112 EXPORT_C void CCPDebug::EnableLogging(TBool aEnable)
       
   113     {
       
   114     DebugData* data = Data();
       
   115     if(aEnable && !data->iLogEnabled)
       
   116         {
       
   117         if( data->iLogFile.Replace(data->iFs, data->iFileName, EFileWrite) == KErrNone )
       
   118             {
       
   119             data->iLogEnabled = ETrue;
       
   120             Printf(_L8("CCPDebug::EnableLogging()"));
       
   121             }
       
   122         }
       
   123     else if(!aEnable && data->iLogEnabled)
       
   124         {
       
   125         data->iLogFile.Close();
       
   126         data->iLogEnabled = EFalse;
       
   127         }
       
   128     }
       
   129 
       
   130 // ---------------------------------------------------------------------------
       
   131 // 
       
   132 // ---------------------------------------------------------------------------
       
   133 //
       
   134 EXPORT_C void CCPDebug::Printf(TRefByValue<const TDesC8> aFormat, ...)
       
   135     {
       
   136     DebugData* data = Data();
       
   137     if(!data || !data->iLogEnabled)
       
   138         {
       
   139         return;
       
   140         }
       
   141     TTime now;
       
   142     now.UniversalTime();
       
   143     TInt32 elapsed = (TInt32)(now.MicroSecondsFrom(data->iFirstUpdateTime).Int64() / KThousand);
       
   144 
       
   145     TBuf8<KMemDataSize> memData;
       
   146     memData.Format(_L8("% 2d,%03d "), elapsed / KThousand, elapsed % KThousand);
       
   147 
       
   148     TBuf8<KBufMaxSize> buf;
       
   149     VA_LIST list;
       
   150     VA_START(list, aFormat);
       
   151     buf.FormatList(aFormat, list);
       
   152     VA_END(list);
       
   153     buf.Insert(0, memData);
       
   154     buf.Append(_L8("\n"));
       
   155 
       
   156     data->iLogFile.Write(buf);
       
   157     data->iLogFile.Flush();
       
   158     }
       
   159 
       
   160 // ---------------------------------------------------------------------------
       
   161 // 
       
   162 // ---------------------------------------------------------------------------
       
   163 //
       
   164 EXPORT_C void CCPDebug::ExtendedPrint( const char* aStringParam,
       
   165                                        const CLiwGenericParamList& aInParamList )
       
   166     {
       
   167     RDebug::Printf( "CPS Client::Request %s Parameters: \n", aStringParam );
       
   168     for ( TInt i = 0; i < aInParamList.Count( ); i++ )
       
   169         {
       
   170         TLiwGenericParam tempParam;
       
   171         tempParam.PushL();
       
   172         TRAP_IGNORE( aInParamList.AtL(i ,tempParam) );
       
   173         Dump( tempParam.Value() );
       
   174         CleanupStack::Pop(&tempParam);
       
   175         tempParam.Reset();
       
   176         }
       
   177     }
       
   178 
       
   179 // ---------------------------------------------------------------------------
       
   180 //
       
   181 // ---------------------------------------------------------------------------
       
   182 //
       
   183 EXPORT_C DebugData* CCPDebug::Data()
       
   184     {
       
   185     return static_cast<DebugData*>(Dll::Tls());
       
   186     }
       
   187 
       
   188 // End of File