Click here to Skip to main content
15,886,199 members
Articles / Programming Languages / C

Optimized Enter/Leave Logger

Rate me:
Please Sign up or sign in to vote.
4.73/5 (4 votes)
14 Jul 2013CPOL18 min read 27.5K   741   22  
Optimized method to log Enter and Leave information of functions.
/*
Header file of Log.h.
This header is used in all functions to provide enter and leave log.

Usage: Create a local object of LogEnterLeave in your function.
       LogEnterLeave constructor will take the instruction pointer and write to
       log file with some additional information.

       LogEnterLeave destructor will take the instruction pointer and write to
       log file with some additional information.

       These informations( instruction pointers ) can be parsed at a later time by
       LogParser application with the help of pdb files. LogParser will recreate the
       actual log files from the encrypted form of enter and        leave log with the additional informations.

       Additional Information collected by this logger:
       1. Thread ID.
       2. Current System time.

Author Santhosh G.
Send Comments @ santhosh4g@gmail.com
Or http://www.codeproject.com/Members/santhosh4gCode
 */
#ifndef _LOG_HEADER_H_
#define _LOG_HEADER_H_
typedef struct
{
    int  nFunctionAddress;
    DWORD dwThreadID;
    DWORD   nTickCount;
    int bEnterFunction;
}LOG_DETAILS_t;

class LogEnterLeave
{

public:
    LogEnterLeave()
    {
        int nEBPContents;
        __asm mov nEBPContents, EBP;
        int* ptrAddress = (int*)nEBPContents;
        const int nReturnIP = *( ++ptrAddress ); // Here nReturnIP holds the address of caller function.
        WriteToFile( nReturnIP, true ); // Here Enter flag is true
    }
    ~LogEnterLeave()
    {
        int nEBPContents;
        __asm mov nEBPContents, EBP;
        int* ptrAddress = (int*)nEBPContents;
        const int nReturnIP = *( ++ptrAddress ); // Here nReturnIP holds the address of caller function.
        WriteToFile( nReturnIP, false ); // Here Enter flag is false
    }

private:

    void WriteToFile( const int nAddress_i, const bool bEnter_i )
    {
        // Get Thread ID from TIB.
        DWORD dwThreadID;
        __asm {
            mov EAX, FS:[0x24]
            mov [dwThreadID], EAX
        }

        const int nTickCount = GetTickCount();
        const LOG_DETAILS_t stLogDetails = { nAddress_i, dwThreadID, nTickCount, bEnter_i };

        FILE* pFile = fopen( "LogDetails.dat", "ab" );
        fwrite( &stLogDetails, sizeof( stLogDetails ), 1, pFile );
        fclose( pFile );
    }
};
#endif// _LOG_HEADER_H_

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions