Click here to Skip to main content
6,596,602 members and growing! (18,748 online)
Email Password   helpLost your password?
Development Lifecycle » Debug Tips » Trace     Intermediate

Getting around the need for a vararg #define just to automatically use __FILE__ and __LINE__ in a TRACE macro

By Paul Mclachlan

Ever wrote #define TRACE, #define TRACE1, #define TRACE2, etc., just so you could use the __FILE__ macro and printf style formatting in a macro at the same time?
VC7Win2K, WinXP, Dev, QA
Posted:9 Jan 2003
Views:61,826
Bookmarked:30 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
26 votes for this article.
Popularity: 6.49 Rating: 4.59 out of 5

1

2
1 vote, 3.8%
3
5 votes, 19.2%
4
20 votes, 76.9%
5

Introduction

Someone else has probably done this before, but I haven't seen it, so I had to make this up.

If you want to create a preprocessor macro that outputs something to a log, you generally want one that works something like this:

MYTRACE( "something something: %s (%d)", str, i );

It's a little tricky, because you want it to accept variable arguments, but I often see something like:

#define MYTRACE MyTrace
static inline MyTrace( const char* msg, ... )
{
    // ...

}

__FILE__ and __LINE__

But, if you wanted MYTRACE to automatically put __FILE__ and __LINE__ into the trace message, you're stuck. You can't use them inside the MyTrace() function because __FILE__ and __LINE__ will always point to the header that defines it instead of the place you call MYTRACE -- and it isn't possible to have a vararg preprocessor macro.

If you're stubborn about such things, you do something like this:

#define MYTRACE1( m, a ) MyTrace( __FILE__, __LINE__, m, a )
#define MYTRACE2( m, a, b ) MyTrace( __FILE__, __LINE__, m, a, b )
#define MYTRACE3( m, a, b, c ) MyTrace( __FILE__, __LINE__, m, a, c )

(And so on). It's horrible to use, though, because you have to keep changing the macro you use when you add an argument.

A solution!

With a little imagination it's possible to work around this, though (at least in C++):

class tracing_output_debug_string
{
private:
    const char* m_file;
    int m_line;

    enum { MY_BUFFER_SIZE = 1024 };

public:
    tracing_output_debug_string( const char* file, int line ) :
        m_file( file ),
        m_line( line )
    {
    }

    void operator()( const char* Format, ... )
    {
        //  vararg stuff

    }
};

#define MYTRACE (tracing_output_debug_string( __FILE__, __LINE__ ))

The downloadable project has an implementation (which writes to ::OutputDebugString()) in a .h file and a little test that shows it, you know, actually works.

I've only tried it on VS.NET (that's what I have), but I'm sure it would work on ... well, just about anything, really.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Paul Mclachlan


Member

Occupation: Web Developer
Location: United States United States

Other popular Debug Tips articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 18 of 18 (Total in Forum: 18) (Refresh)FirstPrevNext
GeneralExcellent, great idea! [modified] PinmemberXia Xiongjun8:29 28 Oct '09  
GeneralWorks as advertised Pinmemberrcc20:37 20 May '08  
GeneralThanks for this--like others noticed, it is useful and widely open to customizations PinmemberCuratica13:22 31 Dec '06  
GeneralVar Args in C - Another approach PinmemberSDX-{5A853460-2944-42f8-84B8-2432DE3657EF}20:46 12 Dec '06  
GeneralVisual Studio 2005 (VC8) supports varags in Macros PinmemberJeremyRemington14:16 2 Mar '06  
Generalvararg preprocessor possible. PinsussAnonymous11:10 19 Aug '05  
GeneralEven simpler solution PinsussAnonymous10:43 31 Jan '05  
GeneralSome MACRO crimes against humanity ... PinmemberAmanjit Gill13:57 23 Oct '04  
GeneralRe: Some MACRO crimes against humanity ... PinmemberRick York11:30 31 Jan '05  
GeneralI didn't see the MYTRACE message. PinmemberWREY11:44 5 Sep '03  
Generalchaining operator() Pinmemberilmcuts22:26 12 Jun '03  
GeneralA C solution PinsussAnonymous15:29 28 Mar '03  
GeneralRe: A C solution - This is not thread-safe PinmemberAndrew Schetinin6:34 30 Mar '03  
GeneralRe: A C solution - This is not thread-safe Pinmemberroel_6:28 13 Feb '04  
GeneralRe: A C solution - This is not thread-safe PinmemberAndrew Schetinin6:15 15 Feb '04  
GeneralNice One. Check also boost PinmemberRamon Casellas8:48 11 Jan '03  
GeneralGood one! PinmemberDalle7:36 11 Jan '03  
GeneralVery nice! PinsitebuilderMichael Dunn21:16 10 Jan '03  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 9 Jan 2003
Editor: Smitha Vijayan
Copyright 2003 by Paul Mclachlan
Everything else Copyright © CodeProject, 1999-2009
Web21 | Advertise on the Code Project