Click here to Skip to main content
13,248,766 members (56,032 online)
Click here to Skip to main content
Add your own
alternative version


38 bookmarked
Posted 9 Jan 2003

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

, 9 Jan 2003
Rate this:
Please Sign up or sign in to vote.
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?


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:

<PRE lang=c++>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:

<PRE lang=c++>#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:

<PRE lang=c++>#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++):

<PRE lang=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.


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
Web Developer
United States United States
No Biography provided

You may also be interested in...


Comments and Discussions

GeneralExcellent, great idea! [modified] Pin
Xia Xiongjun28-Oct-09 8:29
memberXia Xiongjun28-Oct-09 8:29 
GeneralWorks as advertised Pin
rcc20-May-08 20:37
memberrcc20-May-08 20:37 
GeneralThanks for this--like others noticed, it is useful and widely open to customizations Pin
Curatica31-Dec-06 13:22
memberCuratica31-Dec-06 13:22 
GeneralVar Args in C - Another approach Pin
SDX-{5A853460-2944-42f8-84B8-2432DE3657EF}12-Dec-06 20:46
memberSDX-{5A853460-2944-42f8-84B8-2432DE3657EF}12-Dec-06 20:46 
GeneralVisual Studio 2005 (VC8) supports varags in Macros Pin
JeremyRemington2-Mar-06 14:16
memberJeremyRemington2-Mar-06 14:16 
Generalvararg preprocessor possible. Pin
Anonymous19-Aug-05 11:10
sussAnonymous19-Aug-05 11:10 
GeneralEven simpler solution Pin
Anonymous31-Jan-05 10:43
sussAnonymous31-Jan-05 10:43 
GeneralSome MACRO crimes against humanity ... Pin
Amanjit Gill23-Oct-04 13:57
memberAmanjit Gill23-Oct-04 13:57 
GeneralRe: Some MACRO crimes against humanity ... Pin
Rick York31-Jan-05 11:30
memberRick York31-Jan-05 11:30 
GeneralI didn't see the MYTRACE message. Pin
WREY5-Sep-03 11:44
memberWREY5-Sep-03 11:44 
Generalchaining operator() Pin
ilmcuts12-Jun-03 22:26
memberilmcuts12-Jun-03 22:26 
GeneralA C solution Pin
Anonymous28-Mar-03 15:29
sussAnonymous28-Mar-03 15:29 
GeneralRe: A C solution - This is not thread-safe Pin
Andrew Schetinin30-Mar-03 6:34
memberAndrew Schetinin30-Mar-03 6:34 
GeneralRe: A C solution - This is not thread-safe Pin
roel_13-Feb-04 6:28
memberroel_13-Feb-04 6:28 
GeneralRe: A C solution - This is not thread-safe Pin
Andrew Schetinin15-Feb-04 6:15
memberAndrew Schetinin15-Feb-04 6:15 
GeneralNice One. Check also boost Pin
Ramon Casellas11-Jan-03 8:48
memberRamon Casellas11-Jan-03 8:48 
GeneralGood one! Pin
Dalle11-Jan-03 7:36
memberDalle11-Jan-03 7:36 
GeneralVery nice! Pin
Michael Dunn10-Jan-03 21:16
sitebuilderMichael Dunn10-Jan-03 21:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.171114.1 | Last Updated 10 Jan 2003
Article Copyright 2003 by Paul Mclachlan
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid