vpnengine/kmdserver/src/kmddebuglogger.cpp
changeset 0 33413c0669b9
equal deleted inserted replaced
-1:000000000000 0:33413c0669b9
       
     1 /*
       
     2 * Copyright (c) 2008-2009 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:  Write logs in debug builds
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // CLASS HEADER
       
    20 #include "kmddebuglogger.h"
       
    21 
       
    22 #if defined(_DEBUG)
       
    23 _LIT(KLogFolder,"vpn");
       
    24 _LIT(KLogFile,"kmd.txt");
       
    25 #endif
       
    26 
       
    27 // ======== MEMBER FUNCTIONS ========
       
    28 
       
    29 #ifndef _DEBUG
       
    30 // ---------------------------------------------------------------------------
       
    31 // Two-phased constructor.
       
    32 // ---------------------------------------------------------------------------
       
    33 //
       
    34 CKmdDebugLogger* CKmdDebugLogger::NewL()
       
    35     {
       
    36     CKmdDebugLogger* self = new ( ELeave ) CKmdDebugLogger;
       
    37     return self;        
       
    38     }
       
    39 
       
    40 // ---------------------------------------------------------------------------
       
    41 // Destructor.
       
    42 // ---------------------------------------------------------------------------
       
    43 //
       
    44 CKmdDebugLogger::~CKmdDebugLogger()
       
    45     {    
       
    46     }
       
    47 
       
    48 #else
       
    49 
       
    50 // ---------------------------------------------------------------------------
       
    51 // Two-phased constructor.
       
    52 // ---------------------------------------------------------------------------
       
    53 //
       
    54 CKmdDebugLogger* CKmdDebugLogger::NewL()
       
    55     {
       
    56     CKmdDebugLogger* self = new ( ELeave ) CKmdDebugLogger;
       
    57     self->ConstructL();
       
    58     return self;        
       
    59     }
       
    60 
       
    61 // ---------------------------------------------------------------------------
       
    62 // Destructor.
       
    63 // ---------------------------------------------------------------------------
       
    64 //
       
    65 CKmdDebugLogger::~CKmdDebugLogger()
       
    66     {
       
    67     delete iIkePcapTrace;
       
    68     iFileLogger.Close();
       
    69     }
       
    70 
       
    71 // ---------------------------------------------------------------------------
       
    72 // Second phase construction.
       
    73 // ---------------------------------------------------------------------------
       
    74 //
       
    75 void CKmdDebugLogger::ConstructL()
       
    76     {    
       
    77     User::LeaveIfError( iFileLogger.Connect() );
       
    78     iFileLogger.SetDateAndTime( ETrue, ETrue );
       
    79     iFileLogger.CreateLog( KLogFolder, KLogFile, EFileLoggingModeAppend );	
       
    80    
       
    81     iIkePcapTrace = CIkePcapTrace::NewL(KLogFolder);
       
    82     }
       
    83 
       
    84 // ---------------------------------------------------------------------------
       
    85 // Writes to log.
       
    86 // ---------------------------------------------------------------------------
       
    87 //
       
    88 void CKmdDebugLogger::LogWrite( const TDesC& aText )
       
    89     {
       
    90     const TInt KMaxLineWidth( 100 );
       
    91     const TInt textLength( aText.Length() );
       
    92     TInt charsLeft( textLength );
       
    93     
       
    94     while ( charsLeft )
       
    95         {
       
    96         if ( charsLeft >= KMaxLineWidth )
       
    97             {
       
    98             // Write next KMaxLineWidth chars.
       
    99             iFileLogger.Write( aText.Mid( textLength-charsLeft, KMaxLineWidth ) );
       
   100             charsLeft -= KMaxLineWidth;            
       
   101             }
       
   102         else
       
   103             {
       
   104             // Write remaining chars (<KMaxLineWidth chars).
       
   105             iFileLogger.Write( aText.Mid( textLength-charsLeft, charsLeft ) );
       
   106             charsLeft = 0;            
       
   107             }
       
   108         }
       
   109     }
       
   110 
       
   111 // ---------------------------------------------------------------------------
       
   112 // Writes to log.
       
   113 // ---------------------------------------------------------------------------
       
   114 //
       
   115 void CKmdDebugLogger::LogWrite( const TDesC8& aText )
       
   116     {
       
   117     const TInt KMaxLineWidth( 100 );
       
   118     const TInt textLength( aText.Length() );
       
   119     TInt charsLeft( textLength );
       
   120     
       
   121     while ( charsLeft )
       
   122         {
       
   123         if ( charsLeft >= KMaxLineWidth )
       
   124             {
       
   125             // Write next KMaxLineWidth chars.
       
   126             iFileLogger.Write( aText.Mid( textLength-charsLeft, KMaxLineWidth ) );
       
   127             charsLeft -= KMaxLineWidth;            
       
   128             }
       
   129         else
       
   130             {
       
   131             // Write remaining chars (<KMaxLineWidth chars).
       
   132             iFileLogger.Write( aText.Mid( textLength-charsLeft, charsLeft ) );
       
   133             charsLeft = 0;            
       
   134             }
       
   135         }
       
   136     }
       
   137 
       
   138 // ---------------------------------------------------------------------------
       
   139 // Writes to log.
       
   140 // ---------------------------------------------------------------------------
       
   141 //
       
   142 void CKmdDebugLogger::LogWriteF( TRefByValue<const TDesC> aFmt, ... )
       
   143     {
       
   144     VA_LIST list;
       
   145     VA_START( list, aFmt );
       
   146 
       
   147     iFileLogger.WriteFormat( aFmt, list );    
       
   148     }
       
   149 
       
   150 // ---------------------------------------------------------------------------
       
   151 // Writes to log.
       
   152 // ---------------------------------------------------------------------------
       
   153 //
       
   154 void CKmdDebugLogger::LogWriteF( TRefByValue<const TDesC8> aFmt, ... )
       
   155     {
       
   156     VA_LIST list;
       
   157     VA_START( list,aFmt );
       
   158 
       
   159     iFileLogger.WriteFormat( aFmt, list );    
       
   160     }
       
   161 
       
   162 // ---------------------------------------------------------------------------
       
   163 // Writes array to log.
       
   164 // ---------------------------------------------------------------------------
       
   165 //
       
   166 void CKmdDebugLogger::LogWriteArray( const TUint8* aArray, TInt aLength )
       
   167     {
       
   168     ASSERT(aArray);
       
   169     HBufC *buf = HBufC::New( aLength*4+1 ); // max 3 num and a blank (+1 for /n)
       
   170     if ( !buf )
       
   171         {
       
   172         return;
       
   173         }
       
   174 
       
   175     for ( TInt i=0; i<aLength; i++ )
       
   176         {
       
   177         if ( i%4 == 0 )
       
   178             {
       
   179             buf->Des().AppendFormat(_L(" "));
       
   180             }
       
   181         buf->Des().AppendFormat( _L("%02.2x"), aArray[i] ); // key Data byte2byte
       
   182         }
       
   183 
       
   184     LogWrite( buf->Des() );
       
   185 
       
   186     delete buf;
       
   187     buf = NULL;    
       
   188     }
       
   189 
       
   190 // ---------------------------------------------------------------------------
       
   191 // Writes number to log.
       
   192 // ---------------------------------------------------------------------------
       
   193 //
       
   194 void CKmdDebugLogger::LogWriteNum( TUint aNum )
       
   195     {
       
   196     const TInt KMaxNumLength( 20 );
       
   197     TBuf<KMaxNumLength> buf;
       
   198     buf.AppendFormat( _L("%u\n"), aNum );  // key Data byte2byte
       
   199     LogWrite( buf );
       
   200     }
       
   201 
       
   202 // ---------------------------------------------------------------------------
       
   203 // Writes message to PCap log.
       
   204 // ---------------------------------------------------------------------------
       
   205 //
       
   206 void CKmdDebugLogger::TraceMessage(const TDesC8& aMessage, 
       
   207                                    const TInetAddr& aSourceAddress, 
       
   208                                    const TInetAddr& aDestinationAddress,
       
   209                                    CIkePcapTrace::TEncryptionType aEncryptionType)
       
   210     {
       
   211     iIkePcapTrace->TraceMessage(aMessage, 
       
   212                                 aSourceAddress, aDestinationAddress,
       
   213                                 aEncryptionType);
       
   214     }
       
   215 
       
   216 
       
   217 #endif //_DEBUG