473,796 Members | 2,536 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(Expre ssion) \
if (Expression) { \
LOG4CXX_FATAL(l og4cxx::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 3183
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(Expre ssion) \
if (Expression) { \
LOG4CXX_FATAL(l og4cxx::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(Expre ssion) \
if (Expression) { \
LOG4CXX_FATAL(l og4cxx::Logger: :getRootLogger( ), "LogAssert: " \
#Expression " in file " __FILE__ ", line " #__LINE__ "."); \
::exit(1); \
}

Unfortunatel y, 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
1701
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 DEP_PROJECT. pseudo preprocessor code would look like this: 1. Lib1 version header is included: #define PROJ_NAME "Lib1" #define PROJ_VERSION 102
5
22068
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 example). int foo(const char* argv) { return 0; } int main(int argc, char* argv) {
2
1511
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 isn't, does anyone know of a good list for those defines under Gcc (3.4 <=)?
13
3595
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 amount of preprocessor directives used in these code. Can anyone please tell me how to actually use these preprocessor directives in a more professional way like selecting particular lines of code suited for some particular hardware etc...
5
12094
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 welcome. This will print the file name and line number followed by a format string and a variable number of arguments. The key here is that you MUST have a space between __LINE__ and the last comma, otherwise __LINE__ gets eaten by the ## if...
3
13958
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, const void *args, OSCTimeTag when, NetworkReturnAddressPtr returnAddr) { const char *typeArgs = (const char *)args;
5
3370
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 and line number. Here I dont
1
2062
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) into a string-fragment that can be added/embedded to an new string literal. This string-fragment shall consist of two characters, both defined as octal numbers: the first octal number \001 (acting as an escape symbol) and the second octal number...
5
2863
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
9680
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9528
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10174
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10012
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7548
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6788
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5442
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5575
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4118
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.