| Home Forum RSS PGP Alerts Links (D) |
|
|
|
|
VARARGS and Trace Comment on this articleHaving a syntax similar to good ol' printf can be a good thing at times. It can often be pretty bad, too, since it keeps the compiler from checking arguments in compile time, but sometimes it's worth it. Especially for debugging output. Try to keep it for that use only, if possible. What we want is to be able to use trace statements like in the following example: .
.
TRACE("Bad stuff in %s - msg: %s, %d times", __FILE__, pszMsg, nCount);
.
.
What would be even cuter is if we could do that in release builds as well. And in services and stuff. Using systeminternals dbgview.exe (see www.sysinternals.com) you can. If you use the following macros and functions (I have the habit of declaring global utility functions in the namespace "f"): // Macros.h
// JMW 2000
#pragma once
#define TRACE f::CustomTrace
#define IMPLEMENT_VARARGS(f,b) \
va_list args; \
TCHAR b[512]; \
int nBuf; \
va_start (args, f); \
nBuf = _vsntprintf(b, sizeof(b)/sizeof(b[0]), f, args); \
va_end (args);
// funcs.h
// JMW 2000
#pragma once
#include "macros.h"
namespace f
{
void CustomTrace(LPCTSTR pszFormat,...);
};
// funcs.cpp
// JMW 2000
#include "stdafx.h"
#include "funcs.h"
namespace f
{
void CustomTrace(LPCTSTR pszFormat,...)
{
IMPLEMENT_VARARGS(pszFormat, szBuf);
OutputDebugString(szBuf);
}
};
|
| TOP |