overloading a variable argument list function
-
Newbie question here. I have a bunch of text that I want to write both to the stdout and to a logfile using fprintf. Currently, I'm using the primitive method of just printing out everything twice, once to the stdout and once to the logfile. I thought about an elegant way of doing this, and the best I could come up with is to overload fprintf so it'll take two FILE* arguments, like fprintf(stdout, logfile "text %d", x, ...) etc. While trying to overload fprintf, however, I ran into difficulties since it takes a variable argument list and the overloading seems to be a lot more complicated. I played around with va_list, va_arg and such but ended up getting nowhere. Is there an easy way of doing this, or can anyone offer some assistance in coding this up? Thanks a lot in advance.
-
Newbie question here. I have a bunch of text that I want to write both to the stdout and to a logfile using fprintf. Currently, I'm using the primitive method of just printing out everything twice, once to the stdout and once to the logfile. I thought about an elegant way of doing this, and the best I could come up with is to overload fprintf so it'll take two FILE* arguments, like fprintf(stdout, logfile "text %d", x, ...) etc. While trying to overload fprintf, however, I ran into difficulties since it takes a variable argument list and the overloading seems to be a lot more complicated. I played around with va_list, va_arg and such but ended up getting nowhere. Is there an easy way of doing this, or can anyone offer some assistance in coding this up? Thanks a lot in advance.
-
Newbie question here. I have a bunch of text that I want to write both to the stdout and to a logfile using fprintf. Currently, I'm using the primitive method of just printing out everything twice, once to the stdout and once to the logfile. I thought about an elegant way of doing this, and the best I could come up with is to overload fprintf so it'll take two FILE* arguments, like fprintf(stdout, logfile "text %d", x, ...) etc. While trying to overload fprintf, however, I ran into difficulties since it takes a variable argument list and the overloading seems to be a lot more complicated. I played around with va_list, va_arg and such but ended up getting nowhere. Is there an easy way of doing this, or can anyone offer some assistance in coding this up? Thanks a lot in advance.
Have you considered:
void MyPrintf( const char *pFormat, ... )
{
va_list marker;va\_start(marker, pFormat); vprintf(pFormat, marker); vfprintf(stderr, pFormat, marker); va\_end(marker);
}
void main( void )
{
MyPrintf("%s\n", "Hello World");
MyPrintf("%d\n", 123);
}
"Money talks. When my money starts to talk, I get a bill to shut it up." - Frank
"Judge not by the eye but by the heart." - Native American Proverb