Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   How to get current thread ID? (http://www.programmingforums.org/showthread.php?t=10636)

myName Jul 5th, 2006 10:21 PM

How to get current thread ID?
 
Program developing enviroment: Mircosoft Visual C++, Micosoft Window XP.

My program is multiThreading.

For debugging purose, I created a log file to log the activity of the program into tha file. All thread activity will log into the same file. I wish to log the thread id of the current thread, but i don't know how. Please help. :o

Code For logger.cpp
:

CLog::CLog()
{
       
        m_fLog = NULL;
        memset( m_szLogFileName, 0, sizeof(char) * LOG_FILE_NAME_LEN );
        strcpy(m_szLogFileName, LOG_PATH);
}

CLog::~CLog()
{
        tm                *tmNow;
        time_t        timeNow;

        // free all the memory allocation
        if ( m_fLog )
        {
                time( &timeNow );
                tmNow = localtime( &timeNow );
                fprintf( m_fLog, "\n\nLog closed at [%04d-%02d-%02d %02d:%02d:%02d]\n\n", tmNow->tm_year + 1900, tmNow->tm_mon, tmNow->tm_mday, tmNow->tm_hour, tmNow->tm_min, tmNow->tm_sec );
                fclose ( m_fLog );
        }

}

bool CLog::Log( const char *sz_FileName, int n_LineNum, const char *sz_Log )
{
        bool        bRetCode = true;
        time_t        timeNow;
        tm                *tmNow;
        char        szShortDate[8],
                        szLogFileName[LOG_FILE_NAME_LEN];
        int                iCounter = 0,
                        iFileNameLen = 0;
        string        szFileName = "";

        time( &timeNow );
        tmNow = localtime( &timeNow );

        ///////////////////////
        //Get day for      //
        ///////////////////////
        strftime( szShortDate, sizeof(char) * 8, "%y%m%d\0", tmNow );

        iFileNameLen = strlen( m_szLogFileName );
        iCounter = iFileNameLen - 1;

        while ( ('_' != m_szLogFileName[iCounter]) && ( iCounter > 0 ) )
                iCounter--;

        strncpy( szLogFileName, m_szLogFileName, iCounter );

        sprintf( szLogFileName + iCounter, "_%s.log\0", szShortDate );

        /////////////////////////////////////////////
        // Check if the log file has been created  //
        /////////////////////////////////////////////
        if( strcmp( szLogFileName, m_szLogFileName ) != 0 )
        {
                if( strcmp( m_szLogFileName, LOG_PATH )==0 )
                {
                        m_fLog = fopen( szLogFileName, "a+" );
                        fprintf( m_fLog, "\n\nLog Started at [%04d-%02d-%02d %02d:%02d:%02d]. \n\n", tmNow->tm_year + 1900, tmNow->tm_mon, tmNow->tm_mday, tmNow->tm_hour, tmNow->tm_min, tmNow->tm_sec);
                }
                else
                {
                        fprintf( m_fLog, "\n\nLog closed at [%04d-%02d-%02d %02d:%02d:%02d]\n\n", tmNow->tm_year + 1900, tmNow->tm_mon, tmNow->tm_mday, tmNow->tm_hour, tmNow->tm_min, tmNow->tm_sec );
                        fclose( m_fLog );
                        m_fLog = NULL;
                        m_fLog = fopen( szLogFileName, "a+" );
                        fprintf( m_fLog, "\n\nLog Started at [%04d-%02d-%02d %02d:%02d:%02d]. \n\n", tmNow->tm_year + 1900, tmNow->tm_mon, tmNow->tm_mday, tmNow->tm_hour, tmNow->tm_min, tmNow->tm_sec);
                }
               
                sprintf( m_szLogFileName, "%s\0", szLogFileName );
        }

       
        szFileName = sz_FileName;
        iCounter = szFileName.find_last_of("\\");
        szFileName = szFileName.substr( iCounter+1, strlen(sz_FileName));

        fprintf( m_fLog, "[%04d-%02d-%02d %02d:%02d:%02d]\t", tmNow->tm_year + 1900, tmNow->tm_mon, tmNow->tm_mday, tmNow->tm_hour, tmNow->tm_min, tmNow->tm_sec);

            //???????????????????????????????????????
            //Get the Thread ID and write into file
            //???????????????????????????????????????
          //HOW???????????????????????????????????

        fprintf( m_fLog, "[%s %d]\t", szFileName.c_str(), n_LineNum );
        fprintf( m_fLog, "%s\n", sz_Log );
        fflush( m_fLog );


        return bRetCode;
}


frankish Jul 5th, 2006 11:55 PM

DWORD GetCurrentThreadId(void);

This is the subroutine that does what you're asking.

myName Jul 6th, 2006 2:52 AM

Thank you very much Frankish. :-)


All times are GMT -5. The time now is 12:52 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC