![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Oct 2005
Posts: 48
Rep Power: 0
![]() |
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;
} |
|
|
|
|
|
#2 |
|
Hobbyist Programmer
Join Date: Oct 2005
Location: Ohio
Posts: 177
Rep Power: 0
![]() |
DWORD GetCurrentThreadId(void);
This is the subroutine that does what you're asking. |
|
|
|
|
|
#3 |
|
Programmer
Join Date: Oct 2005
Posts: 48
Rep Power: 0
![]() |
Thank you very much Frankish. :-)
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|