multimediacommsengine/mmceshared/inc/mcelogs.h
changeset 0 1bce908db942
equal deleted inserted replaced
-1:000000000000 0:1bce908db942
       
     1 /*
       
     2 * Copyright (c) 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 #ifndef MCELOGS_H
       
    24 #define MCELOGS_H
       
    25 
       
    26 #include <e32std.h>
       
    27 
       
    28 // INCLUDES
       
    29 
       
    30 #include <e32base.h>
       
    31 #include <e32svr.h>
       
    32 #include <utf.h>
       
    33 #include <flogger.h>
       
    34 #include <f32file.h>
       
    35 
       
    36 // FORWARD DECLARATIONS
       
    37 
       
    38 // CLASS DECLARATION
       
    39 
       
    40 _LIT( KMceLogPath, "C:\\Logs\\Mce\\" );
       
    41 _LIT( KMceLogDir, "Mce" );
       
    42 _LIT( KMceLogFile, "MceLog.txt" );
       
    43 
       
    44 /**
       
    45  * Class provides logging through RFileLogger or RDebug in debug builds
       
    46  * 'MCE: ' added automatically in fron of every logged line
       
    47  */ 
       
    48 class TMceDebug 
       
    49 	{
       
    50 	public:
       
    51 
       
    52 #ifdef _DEBUG
       
    53 
       
    54 // To disable file logging totally (so that RFileLogger server is not used even
       
    55 // for determining whether file logging is possible), comment line below
       
    56 #define MCE_FILE_LOGGING_ENABLED
       
    57 	    
       
    58 #if ( defined __WINSCW__ ) || ( defined __WINS__ )
       
    59 // No sense to have file logging enabled in wins
       
    60 #undef MCE_FILE_LOGGING_ENABLED
       
    61 #endif
       
    62 
       
    63 	inline static void Print( const TDesC16& aStr, TBool aSafetyChecked = EFalse ) 
       
    64 		{
       
    65 		TChar notAllowed('%');
       
    66 		TBuf<256> str;
       
    67 		_LIT( KMceFormat, "MCE: %S" );
       
    68 		_LIT( KMceWarning, "MCE: Text contains not allowed characters, log ignored");
       
    69 		
       
    70 		if ( aSafetyChecked || aStr.Locate( notAllowed ) == KErrNotFound )
       
    71 			{
       
    72 			str.Format( KMceFormat, &aStr );
       
    73 			}
       
    74 		else
       
    75 			{
       
    76 			str.Copy( KMceWarning );
       
    77 			}	
       
    78 		
       
    79         if ( LogToFile( KMceLogPath ) )
       
    80             {
       
    81             RFileLogger::Write( KMceLogDir, KMceLogFile,
       
    82                 EFileLoggingModeAppend, str );
       
    83             }
       
    84         else
       
    85             {
       
    86             RDebug::Print( str );
       
    87             }
       
    88 		}
       
    89 
       
    90 	inline static void Print( const TDesC16& aStr1, const TDesC16& aStr2 ) 
       
    91 		{
       
    92 		TChar notAllowed('%');
       
    93 		TBuf<256> str;
       
    94 		_LIT( KMceFormat, "MCE: %S, %S" );
       
    95 		_LIT( KMceWarning, "MCE: Text contains not allowed characters, log ignored" );
       
    96 		
       
    97 		if ( aStr1.Locate( notAllowed ) == KErrNotFound  &&
       
    98 		     aStr2.Locate( notAllowed ) == KErrNotFound )
       
    99 			{
       
   100 			str.Format( KMceFormat, &aStr1, &aStr2 );
       
   101 			}
       
   102 		else
       
   103 			{
       
   104 			str.Copy( KMceWarning );
       
   105 			}
       
   106 		
       
   107         if ( LogToFile( KMceLogPath ) )
       
   108             {
       
   109             RFileLogger::Write( KMceLogDir, KMceLogFile,
       
   110                 EFileLoggingModeAppend, str );
       
   111             }
       
   112         else
       
   113             {
       
   114             RDebug::Print( str );
       
   115             }
       
   116 		}
       
   117 
       
   118 	inline static void Print( const TDesC16& aStr, TUint32 aValue ) 
       
   119 		{
       
   120 		TChar notAllowed('%');
       
   121 		TBuf<256> str;
       
   122 		_LIT( KMceFormat, "MCE: %S, %d" );
       
   123 		_LIT( KMceWarning, "MCE: Text contains not allowed characters, log ignored" );
       
   124 		
       
   125 		if ( aStr.Locate( notAllowed ) == KErrNotFound )
       
   126 			{
       
   127 			str.Format( KMceFormat, &aStr, aValue );
       
   128 			}
       
   129 		else
       
   130 			{
       
   131 			str.Copy( KMceWarning );
       
   132 			}
       
   133 			
       
   134 		if ( LogToFile( KMceLogPath ) )
       
   135             {
       
   136             RFileLogger::Write( KMceLogDir, KMceLogFile,
       
   137                 EFileLoggingModeAppend, str );
       
   138             }
       
   139         else
       
   140             {
       
   141             RDebug::Print( str );
       
   142             }
       
   143 		}
       
   144 
       
   145 	inline static void Print( const TDesC16& aStr, TUint32 aValue1, TUint32 aValue2 ) 
       
   146 		{
       
   147 		TChar notAllowed('%');
       
   148 		TBuf<256> str;
       
   149 		_LIT( KMceFormat, "MCE: %S, %d, %d" );
       
   150 		_LIT( KMceWarning, "MCE: Text contains not allowed characters, log ignored" );
       
   151 		
       
   152 		if ( aStr.Locate( notAllowed ) == KErrNotFound )
       
   153 			{
       
   154 			str.Format( KMceFormat, &aStr, aValue1, aValue2 );
       
   155 			}
       
   156 		else
       
   157 			{
       
   158 			str.Copy( KMceWarning );
       
   159 			}
       
   160 		
       
   161         if ( LogToFile( KMceLogPath ) )
       
   162             {
       
   163             RFileLogger::Write( KMceLogDir, KMceLogFile,
       
   164                 EFileLoggingModeAppend, str );
       
   165             }
       
   166         else
       
   167             {
       
   168             RDebug::Print( str );
       
   169             }
       
   170 		}
       
   171 
       
   172 	inline static void Print( const TDesC16& aStr, TUint32 aValue1, 
       
   173 		TUint32 aValue2, TUint32 aValue3 ) 
       
   174 		{
       
   175 		TChar notAllowed('%');
       
   176 		TBuf<256> str;
       
   177 		_LIT( KMceFormat, "MCE: %S, %d, %d, %d" );
       
   178 		_LIT( KMceWarning, "MCE: Text contains not allowed characters, log ignored" );
       
   179 		
       
   180 		if ( aStr.Locate( notAllowed ) == KErrNotFound )
       
   181 			{
       
   182 			str.Format( KMceFormat, &aStr, aValue1, aValue2, aValue3 );
       
   183 			}
       
   184 		else
       
   185 			{
       
   186 			str.Copy( KMceWarning );
       
   187 			}
       
   188 		
       
   189         if ( LogToFile( KMceLogPath ) )
       
   190             {
       
   191             RFileLogger::Write( KMceLogDir, KMceLogFile,
       
   192                 EFileLoggingModeAppend, str );
       
   193             }
       
   194         else
       
   195             {
       
   196             RDebug::Print( str );
       
   197             }
       
   198 		}
       
   199 
       
   200 	inline static void Print( const TDesC16& aStr1, const TDesC8& aStr2 ) 
       
   201 		{
       
   202 		TChar notAllowed('%');
       
   203 		TBuf<256> str;
       
   204 		_LIT( KMceFormat, "MCE: %S, %S" );
       
   205 		_LIT( KMceWarning, "MCE: Text contains not allowed characters, log ignored" );
       
   206 		
       
   207 		if ( aStr1.Locate( notAllowed ) == KErrNotFound  &&
       
   208 		     aStr2.Locate( notAllowed ) == KErrNotFound )
       
   209 			{
       
   210 			TBuf<100> str2;
       
   211 			CnvUtfConverter::ConvertToUnicodeFromUtf8( str2, aStr2 );
       
   212 
       
   213 			str.Format( KMceFormat, &aStr1, &str2 );
       
   214 			}
       
   215 		else
       
   216 			{
       
   217 			str.Copy( KMceWarning );
       
   218 			}
       
   219 		
       
   220         if ( LogToFile( KMceLogPath ) )
       
   221             {
       
   222             RFileLogger::Write( KMceLogDir, KMceLogFile,
       
   223                 EFileLoggingModeAppend, str );
       
   224             }
       
   225         else
       
   226             {
       
   227             RDebug::Print( str );
       
   228             }
       
   229 		
       
   230 		}
       
   231 	
       
   232     private:
       
   233     
       
   234 #ifdef MCE_FILE_LOGGING_ENABLED
       
   235         
       
   236     inline static TBool LogToFile( const TDesC& aFolderName )
       
   237         {
       
   238         TBool exists( EFalse );
       
   239         RFs fs;
       
   240         if ( KErrNone == fs.Connect() )
       
   241             {
       
   242             TEntry entry;
       
   243             exists = ( fs.Entry( aFolderName, entry ) == KErrNone );
       
   244             fs.Close();        
       
   245             }
       
   246         return exists;
       
   247         }
       
   248 #else
       
   249  
       
   250     inline static TBool LogToFile( const TDesC& /*aFolderName*/ )
       
   251         {
       
   252         return EFalse;
       
   253         }
       
   254     
       
   255 #endif
       
   256     
       
   257 #else
       
   258 	
       
   259 	inline static void Print( const TDesC16& /*aStr*/, TBool /*aSafetyChecked*/ = EFalse ) 
       
   260 		{
       
   261 		}
       
   262 
       
   263 	inline static void Print( const TDesC16& /*aStr1*/, const TDesC16& /*aStr1*/ ) 
       
   264 		{
       
   265 		}
       
   266 
       
   267 	inline static void Print( const TDesC16& /*aStr*/, TUint32 /*aValue*/ ) 
       
   268 		{
       
   269 		}
       
   270 
       
   271 	inline static void Print( const TDesC16& /*aStr*/, TUint32 /*aValue1*/, TUint32 /*aValue2*/ ) 
       
   272 		{
       
   273 		}
       
   274 
       
   275 	inline static void Print( const TDesC16& /*aStr*/, TUint32 /*aValue1*/,
       
   276 					  TUint32 /*aValue2*/, TUint32 /*aValue3*/ ) 
       
   277 		{
       
   278 		}
       
   279 
       
   280 	inline static void Print( const TDesC16& /*aStr1*/, const TDesC8& /*aStr2*/ ) 
       
   281 		{
       
   282 		}
       
   283 
       
   284 #endif
       
   285 
       
   286 	};
       
   287 
       
   288 /**
       
   289  * Macro for logging single string
       
   290  * MCE_DEBUG1("Hello")
       
   291  * Note, max length of printed line 256 characters
       
   292  */ 
       
   293 #define MCE_DEBUG1( a ) { TMceDebug::Print( _L( a ) ); }
       
   294 
       
   295 /**
       
   296  * Macro for logging two strings
       
   297  * MCE_DEBUG2("Hello", "Back")
       
   298  * Note, max length of printed line 256 characters
       
   299  */ 
       
   300 #define MCE_DEBUG2( a, b ) { TMceDebug::Print( _L( a ), _L( b ) ); }
       
   301 
       
   302 /**
       
   303  * Macro for logging a string and integer
       
   304  * MCE_DEBUG3("Hello", 1)
       
   305  * Note, max length of printed line 256 characters
       
   306  */ 
       
   307 #define MCE_DEBUG3( a, b ) { TMceDebug::Print( _L( a ), b ); }
       
   308 
       
   309 /**
       
   310  * Macro for logging a string and 2 integers
       
   311  * MCE_DEBUG4("Hello", 1, 2)
       
   312  * Note, max length of printed line 256 characters
       
   313  */ 
       
   314 #define MCE_DEBUG4( a, b, c ) { TMceDebug::Print( _L( a ), b , c); }
       
   315 
       
   316  /**
       
   317  * Macro for logging a string and 3 integers
       
   318  * MCE_DEBUG5("Hello", 1, 2, 3)
       
   319  * Note, max length of printed line 256 characters
       
   320  */ 
       
   321 #define MCE_DEBUG5( a, b, c, d ) { TMceDebug::Print( _L( a ), b , c, d); }
       
   322 
       
   323  /**
       
   324  * Macro for logging a string and variable string
       
   325  * MCE_DEBUG6("Hello", string)
       
   326  * Note, max length of printed line 256 characters
       
   327  */ 
       
   328 #define MCE_DEBUG6( a, b) { TMceDebug::Print( _L( a ), b ); }
       
   329 
       
   330 #endif
       
   331 
       
   332 // End of File