Archived

This topic is now archived and is closed to further replies.

logging function, adding __FILE__ and __LINE_ automagically, how ?

This topic is 5853 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

ello, Ive wrriten a few logging functions: log_open() log_close() log_write(char *text, ...) What i''m trying to do is have some sort of macro or something where __LINE__ and __FILE__ get added to functions'' arguments automatically. And a bool verbose flag. But I have no idea how to do this. The c book I have doesnt really explain macros at all. so log_write would be: log_write(char *text, bool verbose, ...); so calling log_write("error: some_func() failed\n", true); would print error: some_func() failed LINE: 34 FILE: some_file.cpp but calling log_write("error: some_func() failed\n", false); would not print the __LINE__ and __FILE__ I hope this makes sense to someone cheers for any help, rich

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
How about something like
log_write(const char *filename, int line, bool showInfo, char *text, ...)
{
// Expand args
// If showInfo is true
// Tack on (or prefix) file/line information
};


I know the GNU compilers support variable number of arguments to macros, so you could clean it up by creating a macro:

#define LOG_WRITE(showInfo, text, args...) log_write(__FILE__, __LINE__, showInfo, text, #args);

I think that should work, haven''t tried it though

Share this post


Link to post
Share on other sites
Variable number of arguments to a macro isn't standard C/C++, and only GCC (that I know of) supports it. Perhaps another way would be like this:

    
log_set_file( char *file, int line );
log_do_write( bool verbose, char *text, ... );
//

// Now, log_set_file sets a global variable which is the file/line

// and log_print uses that global.

// next, create a macro like this:

#define log_write log_set_file( __FILE__, __LINE__ ); log_do_write

//

// you use it like this:

log_write( true, "blah, blah, blah...\n" ):


It would still call the log_set_file() function even if you set verbose, but you don't usually expect your logging functions to be super-fast anyway.


codeka.com - Just click it.

Edited by - Dean Harding on December 9, 2001 1:33:45 AM

Share this post


Link to post
Share on other sites
quote:
Original post by Dean Harding
Variable number of arguments to a macro isn''t standard C/C++, and only GCC (that I know of) supports it.

The C99 Draft disagrees with you. Here''s an example it gives:
  
#define debug(...) fprintf(stderr, __VA_ARGS__)


[Resist Windows XP''s Invasive Production Activation Technology!]

Share this post


Link to post
Share on other sites
quote:
Original post by Null and Void
The C99 Draft disagrees with you. Here''s an example it gives:

    
#define debug(...) fprintf(stderr, __VA_ARGS__)



Pfft, how many compilers implement C99?

GCC is still the only compiler that I know of that implements it (and it''s done differently to the standard as well )


codeka.com - Just click it.

Share this post


Link to post
Share on other sites