homescreenpluginsrv/hspstools/src/hspslogbus.cpp
changeset 0 79c6a41cd166
equal deleted inserted replaced
-1:000000000000 0:79c6a41cd166
       
     1 /*
       
     2 * Copyright (c) 2008 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:  Implementation of ChspsLogBus.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "hspslogbus.h"
       
    20 
       
    21 #ifdef HSPS_BUILD_LOG_IMPLEMENTATION
       
    22 const TInt KMessageGranularity = 256; // initial size and grow-by factor. 
       
    23 const TInt KMessageSanityLimit = 16384; // 16kb.
       
    24 _LIT( KLogError, "Warning: Logging failed." );
       
    25 const TInt KDefaultMaxLineLen = 128;
       
    26 #endif
       
    27 
       
    28 //----------------------------------------------------------------------------
       
    29 // ChspsLogBus::~ChspsLogBus
       
    30 // ----------------------------------------------------------------------------
       
    31 //
       
    32 EXPORT_C ChspsLogBus::~ChspsLogBus()
       
    33     {
       
    34     }
       
    35 
       
    36 //----------------------------------------------------------------------------
       
    37 // ChspsLogBus::LogText
       
    38 // ----------------------------------------------------------------------------
       
    39 //
       
    40 #ifdef HSPS_BUILD_LOG_IMPLEMENTATION
       
    41 EXPORT_C void ChspsLogBus::LogText( TRefByValue<const TDesC16> aFmt, ... )
       
    42     {    
       
    43     VA_LIST list;
       
    44     VA_START( list, aFmt );
       
    45        
       
    46     HBufC* formatted = FormatMessage( aFmt, list );    
       
    47     if( formatted )
       
    48         {
       
    49         CropAndLogText( *formatted );
       
    50         delete formatted;
       
    51         formatted = NULL;
       
    52         }
       
    53     else
       
    54         {
       
    55         _LogText( KLogError() );
       
    56         }
       
    57     
       
    58     VA_END( list );    
       
    59     }
       
    60 #else
       
    61 EXPORT_C void ChspsLogBus::LogText( TRefByValue<const TDesC16> /*aFmt*/, ... )
       
    62     {
       
    63     }
       
    64 #endif
       
    65 
       
    66 //----------------------------------------------------------------------------
       
    67 // ChspsLogBus::LogText
       
    68 // ----------------------------------------------------------------------------
       
    69 //
       
    70 #ifdef HSPS_BUILD_LOG_IMPLEMENTATION
       
    71 EXPORT_C void ChspsLogBus::LogText( TRefByValue<const TDesC8> aFmt, ... )
       
    72     {    
       
    73     VA_LIST list;
       
    74     VA_START( list, aFmt );
       
    75        
       
    76     HBufC* formatted = FormatMessage( aFmt, list );    
       
    77     if( formatted )
       
    78         {
       
    79         CropAndLogText( *formatted );
       
    80         delete formatted;
       
    81         formatted = NULL;
       
    82         }
       
    83     else
       
    84         {
       
    85         _LogText( KLogError() );
       
    86         }
       
    87     
       
    88     VA_END( list );    
       
    89     }
       
    90 #else
       
    91 EXPORT_C void ChspsLogBus::LogText( TRefByValue<const TDesC8> /*aFmt*/, ... )
       
    92     {
       
    93     }
       
    94 #endif
       
    95 
       
    96 //----------------------------------------------------------------------------
       
    97 // ChspsLogBus::ChspsLogBus
       
    98 // ----------------------------------------------------------------------------
       
    99 //
       
   100 #ifdef HSPS_BUILD_LOG_IMPLEMENTATION
       
   101 TInt ChspsLogBus::MaxLineLen() const
       
   102     {    
       
   103     return KDefaultMaxLineLen;
       
   104     }
       
   105 #else
       
   106 TInt ChspsLogBus::MaxLineLen() const
       
   107     {
       
   108     return KErrNotSupported;
       
   109     }    
       
   110 #endif
       
   111 
       
   112 //----------------------------------------------------------------------------
       
   113 // ChspsLogBus::ChspsLogBus
       
   114 // ----------------------------------------------------------------------------
       
   115 //
       
   116 EXPORT_C ChspsLogBus::ChspsLogBus()
       
   117     {
       
   118     // No implementation required
       
   119     }
       
   120 
       
   121 //----------------------------------------------------------------------------
       
   122 // ChspsLogBus::FormatMessage
       
   123 // ----------------------------------------------------------------------------
       
   124 //
       
   125 #ifdef HSPS_BUILD_LOG_IMPLEMENTATION
       
   126 HBufC* ChspsLogBus::FormatMessage( TRefByValue<const TDesC16> aFmt, VA_LIST aList )
       
   127     {
       
   128     HBufC* message = NULL;
       
   129     
       
   130     TInt messageSize = KMessageGranularity;    
       
   131     
       
   132     while( ETrue )
       
   133         {        
       
   134         TRAPD( err, message = HBufC::NewL( messageSize ) );        
       
   135         if( err != KErrNone )
       
   136             {
       
   137             delete message;
       
   138             message = NULL;
       
   139 
       
   140             break;
       
   141             }
       
   142 
       
   143         iOverflow = EFalse;
       
   144         message->Des().AppendFormatList( aFmt, aList, this );        
       
   145         if( !iOverflow )
       
   146             {
       
   147             // All ok.
       
   148             break;
       
   149             }
       
   150         else
       
   151             {
       
   152             messageSize *= 2;
       
   153             
       
   154             delete message;
       
   155             message = NULL;
       
   156 
       
   157             // Sanity check.
       
   158             if( messageSize > KMessageSanityLimit )
       
   159                 {
       
   160                 break;
       
   161                 }            
       
   162             }        
       
   163         }
       
   164     
       
   165     return message; 
       
   166     }
       
   167 #else
       
   168 HBufC* ChspsLogBus::FormatMessage( TRefByValue<const TDesC16> /*aFmt*/, VA_LIST /*aList*/ )
       
   169     {
       
   170     return NULL;
       
   171     }
       
   172 #endif
       
   173 
       
   174 //----------------------------------------------------------------------------
       
   175 // ChspsLogBus::Overflow
       
   176 // ----------------------------------------------------------------------------
       
   177 //
       
   178 void ChspsLogBus::Overflow( TDes16& /*aDes*/ )
       
   179     {
       
   180     iOverflow = ETrue;
       
   181     }
       
   182 
       
   183 //----------------------------------------------------------------------------
       
   184 // ChspsLogBus::FormatMessage
       
   185 // ----------------------------------------------------------------------------
       
   186 //
       
   187 #ifdef HSPS_BUILD_LOG_IMPLEMENTATION
       
   188 HBufC* ChspsLogBus::FormatMessage( TRefByValue<const TDesC8> aFmt, VA_LIST aList )
       
   189     {
       
   190     HBufC8* message8bit = NULL;
       
   191     
       
   192     TInt messageSize = KMessageGranularity;    
       
   193     
       
   194     while( ETrue )
       
   195         {        
       
   196         TRAPD( err, message8bit = HBufC8::NewL( messageSize ) );        
       
   197         if( err != KErrNone )
       
   198             {
       
   199             delete message8bit;
       
   200             message8bit = NULL;
       
   201 
       
   202             break;
       
   203             }
       
   204 
       
   205         iOverflow = EFalse;
       
   206         message8bit->Des().AppendFormatList( aFmt, aList, this );        
       
   207         if( !iOverflow )
       
   208             {
       
   209             // All ok.
       
   210             break;
       
   211             }
       
   212         else
       
   213             {
       
   214             messageSize *= 2;
       
   215             
       
   216             delete message8bit;
       
   217             message8bit = NULL;
       
   218 
       
   219             // Sanity check.
       
   220             if( messageSize >= KMessageSanityLimit )
       
   221                 {
       
   222                 break;
       
   223                 }            
       
   224             }        
       
   225         }
       
   226     
       
   227     // Convert 8bit to 16bit for logging.    
       
   228     HBufC* message16bit = NULL;
       
   229     if( message8bit && message8bit->Length() > 0 )
       
   230         {
       
   231         TRAPD( err, message16bit = HBufC::NewL( message8bit->Length() ));
       
   232         if( err == KErrNone )
       
   233             {
       
   234             message16bit->Des().Copy( *message8bit );
       
   235             }
       
   236         else
       
   237             {
       
   238             delete message16bit;
       
   239             message16bit = NULL;
       
   240             }
       
   241         }
       
   242     else if( message8bit && message8bit->Length() == 0 )
       
   243         {
       
   244         // Source is empty.
       
   245         TRAP_IGNORE( message16bit = KNullDesC().AllocL() );
       
   246         }
       
   247     
       
   248     delete message8bit;
       
   249     message8bit = NULL;
       
   250     
       
   251     return message16bit; 
       
   252     }
       
   253 #else
       
   254 HBufC* ChspsLogBus::FormatMessage( TRefByValue<const TDesC8> /*aFmt*/, VA_LIST /*aList*/ )
       
   255     {
       
   256     return NULL;
       
   257     }
       
   258 #endif
       
   259 
       
   260 //----------------------------------------------------------------------------
       
   261 // ChspsLogBus::Overflow
       
   262 // ----------------------------------------------------------------------------
       
   263 //
       
   264 void ChspsLogBus::Overflow( TDes8& /*aDes*/ )
       
   265     {
       
   266     iOverflow = ETrue;
       
   267     }
       
   268 
       
   269 #ifdef HSPS_BUILD_LOG_IMPLEMENTATION
       
   270 void ChspsLogBus::CropAndLogText( const TDesC& aText )
       
   271     {
       
   272     const TInt KLineMax = MaxLineLen();
       
   273     
       
   274     // Fits to one line.
       
   275     if( aText.Length() <= KLineMax )
       
   276         {
       
   277         _LogText( aText );
       
   278         return;
       
   279         }
       
   280     
       
   281     // Does not fit. Log cropped.    
       
   282     TPtrC ptr = aText;    
       
   283     while( ETrue )
       
   284         {                
       
   285         const TInt KRemainingLen = ptr.Length();
       
   286         if( KRemainingLen == 0 )
       
   287             {
       
   288             break;
       
   289             }
       
   290         
       
   291         if( KRemainingLen <= KLineMax )
       
   292             {
       
   293             _LogText( ptr );
       
   294             break;
       
   295             }
       
   296         else
       
   297             {
       
   298             _LogText( ptr.Left( KLineMax ) );
       
   299             ptr.Set( ptr.Right( ptr.Length() - KLineMax ) );
       
   300             }
       
   301         }
       
   302     }
       
   303 #else
       
   304 void ChspsLogBus::CropAndLogText( const TDesC& /*aText*/ )
       
   305     {
       
   306     }
       
   307 #endif