Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jul 5th, 2006, 9:21 PM   #1
myName
Programmer
 
Join Date: Oct 2005
Posts: 48
Rep Power: 0 myName is on a distinguished road
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;
}
myName is offline   Reply With Quote
Old Jul 5th, 2006, 10:55 PM   #2
frankish
Hobbyist Programmer
 
frankish's Avatar
 
Join Date: Oct 2005
Location: Ohio
Posts: 177
Rep Power: 0 frankish is an unknown quantity at this point
DWORD GetCurrentThreadId(void);

This is the subroutine that does what you're asking.
frankish is offline   Reply With Quote
Old Jul 6th, 2006, 1:52 AM   #3
myName
Programmer
 
Join Date: Oct 2005
Posts: 48
Rep Power: 0 myName is on a distinguished road
Thank you very much Frankish. :-)
myName is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:37 AM.

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