webengine/osswebengine/MemoryManager/Inc/MemoryLogger.h
changeset 0 dd21522fd290
child 16 a359256acfc6
equal deleted inserted replaced
-1:000000000000 0:dd21522fd290
       
     1 /*
       
     2 * Copyright (c) 2006 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 the License "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 #ifndef _MEMORYLOGGING_H
       
    19 #define _MEMORYLOGGING_H
       
    20 
       
    21 // INCLUDES
       
    22 
       
    23 #include <e32std.h>
       
    24 #include <e32def.h>
       
    25 #include <flogger.h>
       
    26 #include <f32file.h>
       
    27 
       
    28 #ifdef OOM_LOGGING
       
    29 // CONSTANTS
       
    30 
       
    31 _LIT( KMEMLogDir,               "WebCore");
       
    32 _LIT( KMEMLogFile,              "Memory.log");
       
    33 _LIT8( KFuncMemLogBegin, "BEGIN: %S @ %S/%d InSize -> %d" );
       
    34 _LIT8( KFuncMemLogEnd, "END: Peek -> %d Diff -> %d Accumulated Peek -> %d" );
       
    35 _LIT8( KTab, "\t" );
       
    36 
       
    37 // MACROS
       
    38 
       
    39 #define MEM_LOG(a)		{ _LIT8(temp, a); RFileLogger::Write(KMEMLogDir, KMEMLogFile, EFileLoggingModeAppend, temp); }
       
    40 #define MEM_LOGF		FPrint
       
    41 
       
    42 // FUNCTION DECLARATIONS
       
    43 
       
    44 inline void FPrint(const TRefByValue<const TDesC8> aFmt, ...)
       
    45 {
       
    46     VA_LIST list;
       
    47     VA_START(list,aFmt);
       
    48     RFileLogger::WriteFormat(KMEMLogDir, KMEMLogFile, EFileLoggingModeAppend, aFmt, list);
       
    49 }
       
    50 
       
    51 // CLASS DECLARATION
       
    52 
       
    53 /**
       
    54 *  A utility class to record the memory usage inside a function
       
    55 *  @lib memman.lib
       
    56 *  @since 3.1
       
    57 */
       
    58 class FunctionLogger
       
    59 	{
       
    60 	public:
       
    61 		inline FunctionLogger( const TDesC8& func, const TDesC8& file, TInt line, TInt size );
       
    62 		inline ~FunctionLogger();
       
    63 			
       
    64 		TInt _peek;
       
    65 		TInt _used;
       
    66 		TInt _accum;
       
    67 		TInt _inSize;
       
    68 		static RPointerArray<FunctionLogger> loggers;
       
    69 	};
       
    70 
       
    71 // MEMBER FUNCTIONS
       
    72 
       
    73 //-----------------------------------------------------------------------------
       
    74 // FunctionLogger::FunctionLogger
       
    75 //-----------------------------------------------------------------------------
       
    76 inline FunctionLogger::FunctionLogger( const TDesC8& func, const TDesC8& file, TInt line, TInt size ) 
       
    77 	: _peek( 0 ), _used( 0 ), _accum( 0 )
       
    78 	{
       
    79 		TBuf8<512>	buf;
       
    80 			
       
    81 		// indent
       
    82 		for( TInt i=0; i<loggers.Count(); ++i )	buf.Append( KTab );
       
    83 		
       
    84 		buf.Append( KFuncMemLogBegin );
       
    85 		
       
    86 		// write the logger info
       
    87 		MEM_LOGF( buf, &func, &file, line, size );
       
    88 		_inSize = size;
       
    89 			
       
    90 		// insert this logger to the head of loggers array
       
    91 		loggers.Insert( this, 0 );
       
    92 	}
       
    93 
       
    94 //-----------------------------------------------------------------------------
       
    95 // FunctionLogger::~FunctionLogger
       
    96 //-----------------------------------------------------------------------------
       
    97 inline FunctionLogger::~FunctionLogger()
       
    98 	{
       
    99 		TBuf8<512>	buf;
       
   100 			
       
   101 		// indent
       
   102 		for( TInt i=0; i<loggers.Count()-1; ++i )	buf.Append( KTab );
       
   103 		
       
   104 		buf.Append( KFuncMemLogEnd );
       
   105 		
       
   106 		_accum += _peek;
       
   107 			
       
   108 		MEM_LOGF( buf, _peek, _used, _accum );
       
   109 		
       
   110 		
       
   111 		// remove the logger
       
   112 		loggers.Remove( 0 );
       
   113 		
       
   114 		// update its parents' _accum
       
   115 		if( loggers.Count() > 0 )
       
   116 			for( TInt i=0; i<loggers.Count(); ++i )
       
   117 				loggers[ i ]->_accum += _peek;
       
   118 	}
       
   119 #else // OOM_LOGGING
       
   120 #define MEM_LOG	{}
       
   121 #define MEM_LOGF	{}
       
   122 #endif
       
   123 
       
   124 #endif
       
   125 // END OF FILE