base file name from __FILE__

2.6k views Asked by At

I need the file name only where the __FILE__ and __FILEW__ macros return the whole path.

I defined the following:

#define __FILE_NAME_ONLY__ (strrchr(__FILE__, '\\') ? strrchr(__FILE__, '\\') + 1 : __FILE__)

I am using it as follows:

#define MY_TRACE( mask, format, ... )  \            
            GlobalTrace( mask, L"-[" __FILE_NAME_ONLY__ L":" format , __VA_ARGS__ ) 

I get the following error:

error C2064: term does not evaluate to a function taking 1 arguments

and when I try the following macros:

#define __WIDE(_String) L ## _String
#define _WIDE(_String) __WIDE(_String) 

as follows

 #define MY_TRACE( mask, format, ... )  \            
                GlobalTrace( mask, L"-[" _WIDE(__FILE_NAME_ONLY__) L":" format , __VA_ARGS__ ) 

I get : error C2146: syntax error : missing ')' before identifier 'L' when I actually try to use the MY_TRACE macro

what am I missing? Thanks

2

There are 2 answers

8
Mark Ransom On BEST ANSWER

You're depending on string literal concatenation, except that all of the terms aren't string literals.

I assume you were previously doing it like so:

#define MY_TRACE( mask, format, ... )  \            
            GlobalTrace( mask, L"-[" __FILE__ L":" format , __VA_ARGS__ ) 

If __FILE__ and format expand to a string literal, the 4 strings get pasted together into one. "A" "B" "C" "D" is the same as "ABCD".

That doesn't happen when you replace with __FILE_NAME_ONLY__ because it expands to a function call, not a literal.

2
Jay On

If you're using gcc this macro should help:

__BASE_FILE__

Here's a helpful list