The ellipsis & defines

Posted by Nick Cash on Sat 18 Jun 2005 05:50 AM — 7 posts, 24,149 views.

USA #0
Alright, I've been messing around with more stuff and decided to make an XML-style log generator. It works perfectly, but I'm lazy and so I would like to know the answer to this. All of this is in C by the way.

I have this function:

void log_entry( char *source_file, char *function, int line, const char *message, ... );


It is the main basis of the log as all log entries go through it. Anyways, typing out all of those paramaters is kind of annoying but I want them to be there. So, I made a macro:

#define L( function, linetext ) \
 log_entry( __FILE__, (function), __LINE__, (linetext) )


This is a great time saver, however, it doesnt support the ellipsis. So I guess the question is this, can I make the define use/emulate the ellipsis? Or should I just go change the parameters of the function?

Also, if I change the paramaters, won't I lose the actual file and line # that the function call is comming from?

Thanks in advance :)
USA #1
According to this page,
http://docs.hp.com/en/B3901-90007/ch10s15.html
you can use varargs in your macros under GNU like so:
#define foo(f, s...) printf(f, s)
USA #2
Awesome, that did the trick. It turned out like:

#define L( function, linetext, s... ) \
 log_entry( __FILE__, (function), __LINE__, (linetext), s )


Works perfectly. Thanks :)
USA #3
I made a static library out of this so I don't really have to bother with it again. It currently is Win32 only, but I'll port it over to *nix eventually.

Let me know if you would be interested in it at all :P


EDIT: I found the original version of this post rather vague. What I meant to say is I combined my logging functions that I use for nearly all of my programs (including the new XML-style one) into a library. If you want to see the code, use the library, or something else just send me an e-mail or something. If there is interest I can post it here too :)
Amended on Mon 20 Jun 2005 06:25 PM by Nick Cash
USA #4
Hmm... what exactly does this do? What kind of functionality does your library provide? :)
USA #5
It is a very small but useful library. It contains the primary logging functions I've used in basically all of my programs that need logging (save my mud, it already had lots of logging stuff :P).

Here is a little synopsis, taken from the README file:

The library sports two different but useful ways of logging information. The first way is basic. It provides a
chronological output of logs by number in a text document. It will append, putting several spaces and dash's between
each "new" block every time the program is run. All of your logs will be in one file.

The second is a little more complicated, but a bit more useful, organized, and has potential for easier analysis. It will tell the line number, function, file and message for each log entry. It will also display when the log was created, and list the time when each log was entered. They will all be put in seperate files, with an increasing numerical prefix. It is all formated in XML format.

To avoid crashes, if you log directory doesnt happen to exist, it will create it for you. Each logging function uses an ellipsis (...) to accept a variable number of arguments and slap them into your message for you, taking out the trouble of printing your own strings.

-------------

The code may at least be worth looking at, but I'm sure you all have your own logging functions and wouldn't really want to look as such an amature's code. :P

In any event, you can view the code exported as html at:
http://ew.xidus.net/download/xlib

Or, you can download the code, readme, and compiled -WIN32- library at:
http://ew.xidus.net/download/xlib/xlib.zip

I already see a few things I want to change, but for the most part thats it. If someone would like to compile the library on a *nix system and send me the code/lib that would be fantastic, as I have no idea if what I have in there works or not.
Amended on Wed 22 Jun 2005 03:04 AM by Nick Cash
USA #6
The project is now updated a bit and hosted on source forge. You can find it at www.sourceforge.net/projects/xlib