Solved

using C macros to print line number and function name

Posted on 2013-12-24
1
328 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
Comment Utility
#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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Join & Write a Comment

How many times have you wanted to quickly do the same thing to a list but found yourself typing it again and again? I first figured out a small time saver with the up arrow to recall the last command but that can only get you so far if you have a bi…
Using 'screen' for session sharing, The Simple Edition Step 1: user starts session with command: screen Step 2: other user (logged in with same user account) connects with command: screen -x Done. Both users are connected to the same CLI sessio…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

744 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now