473,394 Members | 1,866 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Preprocessor: convert __LINE__ to const char*

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
4 3149
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
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
>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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Jakob Simon-Gaarde | last post by:
Some project includes files from different libraries lib1,lib2 and lib3 all having each there own version header file. I need to be able to pick up these values in a single define value...
5
by: Brad Moore | last post by:
Hey all, I'm getting the following compiler error from my code. I was wondering if anyone could help me understand the concept behind it (I actually did try and compile this degenerate...
2
by: Pelle Beckman | last post by:
Hi, This might be OT... Are there preprocessor defines in the C++ standard, such as __FILE__, __LINE__, __NAMESPACE__, __FUNCTION__, ec? If there is, were can I find a list? If there...
13
by: seemanta dutta | last post by:
Greetings C gurus, I have used preprocessor directives since a very long time. But whenever I see some professional piece of C code, the linux kernel for example, I get literally confused by the...
5
by: jake1138 | last post by:
I couldn't find an example of this anywhere so I post it in the hope that someone finds it useful. I believe this is compiler specific (I'm using gcc), as C99 defines __VA_ARGS__. Comments are...
3
by: bg_ie | last post by:
Hi, I am using a API I downloaded from the internet programmed in C. I need the function below which works with this api in my c++ file - void StoreNoteCallback(void *context, int arglen,...
5
by: Neo | last post by:
Hie, Can I put __FILE__ and __LINE__ macros inside the class function which may not be inline. And void function() { classobject::LogInfo(...); } Internally LogInfo should log file name...
1
by: Klaus Siegesleitner | last post by:
Hi all! sorry, my first posting to this newsgroup - hope everything works fine... I'm looking for a macro that converts an octal number assigned to a preprocessor symbol (via a simple #define)...
5
by: kRyszard | last post by:
how to make the following code work: template<class TYPE> void f2(char *buffer, TYPE *outer) { // do something ... // ... #if (TYPE == int) *outer = atoi(buffer); #endif #if (TYPE == double)
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.