logging function, adding __FILE__ and __LINE_ automagically, how ?
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
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
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
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:
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
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
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!]
THANK YOU !
cheers for the help guys, I''ve managed to get it working !
i''m so happy... :D
cheers,
rich
cheers for the help guys, I''ve managed to get it working !
i''m so happy... :D
cheers,
rich
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.
quote:Original post by Dean Harding
Pfft, how many compilers implement C99?
I won''t argue with that .
[Resist Windows XP''s Invasive Production Activation Technology!]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement