Connecting Tech Pros Worldwide Forums | Help | Site Map

Preprocessor: convert __LINE__ to const char*

Torsten Wiebesiek
Guest
 
Posts: n/a
#1: Aug 29 '08
Hi,

I have a problem with the preprocessor. I have written my own little
assert macro. This is supposed to log a message (with log4cxx):

#define LogAssert(Expression) \
if (Expression) { \
LOG4CXX_FATAL(log4cxx::Logger::getRootLogger(), "LogAssert: " \
#Expression " in file " __FILE__ ", line " #__LINE__ "."); \
::exit(1); \
}

Unfortunately, the preprocessor is only quoting marcro arguments. In my
case, Expression is quoted with #Expression. __LINE__ expands to an
integer literal. Is there an way to convert __LINE__ to a const char* at
compile time?

Thanks for you help,

Torsten

Marco Manfredini
Guest
 
Posts: n/a
#2: Aug 29 '08

re: Preprocessor: convert __LINE__ to const char*


Torsten Wiebesiek wrote:
Quote:
Hi,
>
I have a problem with the preprocessor. I have written my own little
assert macro. This is supposed to log a message (with log4cxx):
>
#define LogAssert(Expression) \
if (Expression) { \
LOG4CXX_FATAL(log4cxx::Logger::getRootLogger(), "LogAssert: " \
#Expression " in file " __FILE__ ", line " #__LINE__ "."); \
::exit(1); \
}
>
Unfortunately, the preprocessor is only quoting marcro arguments. In
So turn the expansion of __LINE__ into a macro argument.
#define STR2(X) #X
#define STR(X) STR2(X)
#define LOG(X) logger(X " happend here:" __FILE__ STR(__LINE__))

STR(X) expands __LINE__ to the actual line number, and STR2 does the
quoting.

Torsten Wiebesiek
Guest
 
Posts: n/a
#3: Aug 29 '08

re: Preprocessor: convert __LINE__ to const char*


Just found the solution at an unexpected place :-)
http://en.wikipedia.org/wiki/C_prepr...acro_arguments

1: #define _QUOTE(x) #x
2: #define QUOTE(x) _QUOTE(x)
3:
4: _QUOTE(__LINE__) // expands to "__LINE__"
5: QUOTE(__LINE__) // expands to "5"
Torsten Wiebesiek
Guest
 
Posts: n/a
#4: Aug 29 '08

re: Preprocessor: convert __LINE__ to const char*


>I have a problem with the preprocessor. I have written my own little
Quote:
Quote:
>assert macro. This is supposed to log a message (with log4cxx):
>>
>#define LogAssert(Expression) \
> if (Expression) { \
> LOG4CXX_FATAL(log4cxx::Logger::getRootLogger(), "LogAssert: " \
> #Expression " in file " __FILE__ ", line " #__LINE__ "."); \
> ::exit(1); \
> }
>>
>Unfortunately, the preprocessor is only quoting marcro arguments. In
>
So turn the expansion of __LINE__ into a macro argument.
#define STR2(X) #X
#define STR(X) STR2(X)
#define LOG(X) logger(X " happend here:" __FILE__ STR(__LINE__))
>
STR(X) expands __LINE__ to the actual line number, and STR2 does the
quoting.
Thanks, a lot and have a nice weekend. :-)
red floyd
Guest
 
Posts: n/a
#5: Aug 29 '08

re: Preprocessor: convert __LINE__ to const char*


On Aug 29, 7:02*am, Torsten Wiebesiek <wiebes...@tnt.uni-hannover.de>
wrote:
Quote:
Just found the solution at an unexpected place :-)http://en.wikipedia.org/wiki/C_prepr...quoting_macro_...
>
1: #define _QUOTE(x) *#x
2: #define QUOTE(x) *_QUOTE(x)
3:
4: _QUOTE(__LINE__) * *// expands to "__LINE__"
5: QUOTE(__LINE__) * * // expands to "5"
This is bad. Identifiers with leading underscore followed by an
uppercase letter are reserved to the implementation. That is, your
implementation may be using _QUOTE for its own purposes.

Either use a trailing underscore, or some other convention (i.e.
QUOTE_() or QUOTE2()).
Closed Thread