The TRACE macro defined using MFC and ATL can also be used in console or Win32 application, however with some small modifications.
This example will post Messages to the tracer built-in with Visual Studio IDE, TraceEx Also includes source file and code line which can be used as shortcut when double clicked on.
/*
Trace.h
*/
#ifndef __TRACE_H__
#define __TRACE_H__#include <crtdbg.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>#ifdef _DEBUG
#define TRACEMAXSTRING 1024char szBuffer[TRACEMAXSTRING];
inline void Trace(const char* format,…)
{
va_list args;
va_start(args,format);
int nBuf;
nBuf = _vsnprintf(szBuffer,
TRACEMAXSTRING,
format,
args);
va_end(args);_RPT0(_CRT_WARN,szBuffer);
}
#define TraceEx _snprintf(szBuffer,TRACEMAXSTRING,”%s(%d): “,
&strrchr(__FILE__,’\’)[1],__LINE__);
_RPT0(_CRT_WARN,szBuffer);
Trace#else
inline void Trace(0);
inline void TraceEx(0);
#endif#endif // __TRACE_H__
Example
Here’s all you have to do to use this macro!
#include “trace.h”int main(void)
{
TraceEx(“Somethings wrongs here %dn”,1)
}
Running this application will result in the following output:
main.cpp(5): Somethings wrong here 1
Other features that could be included are ThreadID, ProcessID etc. fun debugging.