Forum Chat


Mar23,13:31 Johan Marechal
Wees gegroet
Sep20,17:50 Vicente Duque
Kim, Martin, Others :...
Jul07,11:10 Johan Marechal
PGP 9
Jul05,21:13 martin
Fastest in the bush
Jul05,07:48 martin
Spamdexing
Jun28,21:16 martin
New domain / new blog!
Jun28,21:11 martin
On posting etiquette

VARARGS and Trace

Comment on this article

Having 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);
  }
};

Comment on this article

TOP