Click here to Skip to main content
15,884,762 members
Articles / Desktop Programming / MFC

Simple Trace Format Tips

Rate me:
Please Sign up or sign in to vote.
4.40/5 (9 votes)
4 Oct 2000 83K   599   33  
A simple way to format your TRACE statements so double clicking takes you directly to the source code
#include "Stdafx.h"

#if !defined(__DURATION_H__)
	#include "Duration.h"
#endif

#ifdef _DEBUG
	#define new DEBUG_NEW
	#undef THIS_FILE
	static char THIS_FILE[] = __FILE__;
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CDuration::CDuration(void)
{
	LARGE_INTEGER liFrequency;

	QueryPerformanceFrequency(&liFrequency);
	m_llFrequency = liFrequency.QuadPart;

	// Calibration
	Start();
	Stop();

	m_llCorrection = m_liStop.QuadPart - m_liStart.QuadPart;
}

void CDuration::Start(void)
{
	// Ensure we will not be interrupted by any other thread for a while
	Sleep(0);
	QueryPerformanceCounter(&m_liStart);
}

//////////////////////////////////////////////////////////////////////
// Implementation
//////////////////////////////////////////////////////////////////////

void CDuration::Stop(void)
{
	QueryPerformanceCounter(&m_liStop);
}

double CDuration::GetDuration(void) const
{
	return (double)(m_liStop.QuadPart - m_liStart.QuadPart - m_llCorrection) * 1000000.0 / m_llFrequency;
}

CString CDuration::Format()
{
	long lDays(0);
	int nHours(0);
	int nMins(0);
	int nSecs(0);

	double nDuration;

	CString strRet;

	nDuration = GetDuration() / 1000000.0;

	lDays = long(nDuration / (24. * 60. * 60.));
	nDuration -= lDays * (24. * 60. * 60.);

	nHours = int(nDuration / (60. * 60.));
	nDuration -= nHours * (60. * 60.);

	nMins = int(nDuration / (60.));
	nDuration -= nMins * (60.);

	nSecs = int(nDuration);
	nDuration -= nSecs;

	nDuration *= 1000.0;

	strRet.Format(_T("%u %02u:%02u:%02u.%03u"),lDays,nHours,nMins,nSecs,int(nDuration));

	return strRet;
}

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.


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

Comments and Discussions