MFC TRACE Macro for Console Applications

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	1024

char 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 %d\n",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.


Comments

  • This macro include any dangurous case.

    Posted by Legacy on 07/27/2003 12:00am

    Originally posted by: tae young park

    This macro include any dangurous case.
    If this macro is used bellow example.

    example:

    if( ... ) {
    } else
    TraceEx( "trace sample.." );

    This example code will be expanded

    if( ... ) {
    } else
    _snprintf(szBuffer, TRACEMAXSTRING, "%s(%d" : ",
    &strchr(__FILE__,'\\')[1], __LINE__);
    _RPT0(_CRT_WARN,szBuffer);
    Trace("trace sample..");

    so last two line will be executed in any case.
    therefore this macro must be used with a parenthesis.

    if( ... ) {
    } else
    {
    TraceEx( "trace sample..");
    }

    Be carefull !

    • Parenthese

      Posted by jjwalters on 02/17/2005 05:38pm

      Wouldn't it be more sensible to modify the macro slightly to allow for your 
      if( ... ) { 
      } else 
      TraceEx( "trace sample.." ); 
      text?
      
      Put the parentheses around the macro
      #define TraceEx {_snprintf(szBuffer,TRACEMAXSTRING,"%s(%d): ", \
                                &strrchr(__FILE__,'\\')[1],__LINE__); \
      _RPT0(_CRT_WARN,szBuffer); \
      Trace}

      Reply
    Reply
  • Useful (-_-)

    Posted by Legacy on 05/13/2003 12:00am

    Originally posted by: Janus.K

    z

    Reply
  • Release build errors

    Posted by Legacy on 02/01/2002 12:00am

    Originally posted by: suma

    I get the following error message on release build

    error C2182: 'Trace' : illegal use of type 'void'
    error C2433: 'Trace' : 'inline' not permitted on data declarations
    error C2182: 'TraceEx' : illegal use of type 'void'
    error C2433: 'TraceEx' : 'inline' not permitted on data declarations

    These error corresponds to the following lines

    #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

    Reply
  • Good job ;-) !!!

    Posted by Legacy on 10/23/2001 12:00am

    Originally posted by: Hartmut

    This was what I really needed. Well done !!!

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: August 14, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT Data protection has long been considered "overhead" by many organizations in the past, many chalking it up to an insurance policy or an extended warranty you may never use. The realities of today makes data protection a must-have, as we live in a data-driven society -- the digital assets we create, share, and collaborate with others on must be managed and protected for many purposes. Check out this upcoming eSeminar and join Seagate Cloud …

  • Corporate e-Learning technology has a long and diverse pedigree. As far back as the 1980s, companies were adopting computer-based training to supplement traditional classroom activities. More recently, rich web-based applications have added streaming audio and video, real-time collaboration and other new tools to the e-Learning mix. At the same time, the growing availability of informal learning tools--a category that includes everything from web searches to social media posts--are having a major impact on …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds