By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,312 Members | 1,877 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,312 IT Pros & Developers. It's quick & easy.

Preprocessor: convert __LINE__ to const char*

P: n/a
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
Aug 29 '08 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Torsten Wiebesiek wrote:
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.

Aug 29 '08 #2

P: n/a
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"
Aug 29 '08 #3

P: n/a
>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.
Thanks, a lot and have a nice weekend. :-)
Aug 29 '08 #4

P: n/a
On Aug 29, 7:02*am, Torsten Wiebesiek <wiebes...@tnt.uni-hannover.de>
wrote:
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()).
Aug 29 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.