Solved

using C macros to print line number and function name

Posted on 2013-12-24
1
341 Views
Last Modified: 2014-02-04
Hi there,
I am using log4c library and have been using the following macro for my logging purposes.

#define mylog(A,B, ...) log4c_category_log(A, B, __VA_ARGS__)

It works fine, but I want to also use __LINE__, __FUNC__ directives so I can have the line number and function name printed.

For example, if I am using the mylog( ) in my_function() in line 25.

void my_function()
{
    mylog(MYCATEGORY, LOG4C_PRIORITY_ERROR, "some message");
}

I want the log to be printed like :
>>>>>>some message, my_function, 25
I don't want to edit the logging but rather using the macro definition be able to print these details.  Is it possible?

I tried the following, but it does not work when the log is something like:
 mylog(MYCATEGORY, LOG4C_PRIORITY_ERROR, "some message %s  %s", "value1", "value2");

#define mylog(A,B, ...) log4c_category_log(A, B,"%s %s %d ", __VA_ARGS__, __FUNCTION__, __LINE__)
0
Comment
Question by:ambuli
1 Comment
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
ID: 39738341
#define mylog(A,B, ...) log4c_category_log(A, B, __VA_ARGS__,__LINE__)
#define log4c_category_log(A,B,C,...) printf(">>>>>>" C ", " __FILE__ ", %d\n", __VA_ARGS__)
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
Google Drive is extremely cheap offsite storage, and it's even possible to get extra storage for free for two years.  You can use the free account 15GB, and if you have an Android device..when you install Google Drive for the first time it will give…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question