Redirect a file pointer to multiple destinations?

Deeko

Lifer
Jun 16, 2000
30,213
12
81
I have a C++ application that redirects stdout to a file using _wfreopen(). However, I'd like that output to still display on the console as well. Is this a possibility?

I have another process that wraps this one and captures stdout - my other option I can think of is to have that process actually write to the file in addition, rather than redirecting the output in the first place, but I'd like to avoid a major change to the code like that.

Thanks
 

Cogman

Lifer
Sep 19, 2000
10,284
138
106
check out stderr. output there is *almost* always displayed (unless you redirect that as well :))
 

Deeko

Lifer
Jun 16, 2000
30,213
12
81
stderr is also redirected.

A little more background - the application uses almost exclusively oldschool printf statements, hence using _wfreopen instead of cout.rdbuf. If a flag is passed in, stdout/stderr are redirected to a file. I'd like to alter it to write to both - with as minimal a change as possible.
 

Ken g6

Programming Moderator, Elite Member
Moderator
Dec 11, 1999
16,610
4,530
75
Any chance you can use tee in between your two processes?

Edit: The Perl-like way to do it would be to fork a process, open a pipe between the processes, redirect stdout to the pipe's input, and either implement or exec tee on the output.

Otherwise, I once wrote a tee-like extension of OutputStream in Java; but I don't know if C++'s streams are that flexible.
 

Deeko

Lifer
Jun 16, 2000
30,213
12
81
Can't use tee - that's the behavior I want to mimic though :)

If the application were using cout - I could do what you suggested by writing a streambuffer class to do this and changing rdbuf - but I don't know if you can do that with printf.
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
What happens if you use a fprintf and corral all your output print statements into a function(s) where you can pass in the output file handle that you want the output to be directed to.
Then call the output function handler with as many output file handles as needed.
 

degibson

Golden Member
Mar 21, 2008
1,389
0
0
Originally posted by: Common Courtesy
What happens if you use a fprintf and corral all your output print statements into a function(s) where you can pass in the output file handle that you want the output to be directed to.
Then call the output function handler with as many output file handles as needed.

I'm not going to compile this before I post...FYI:

/* Header -- included by everything except printf-override.c */
#ifdef printf
#undef printf
#endif

int printf(const char * format, ... );

/* printf-override.c */
#include <stdarg.h>

int printf(const char * format, ... ) {
va_list args;
va_start(args,format);
/* iterate over all FILE* here to do printing */
vfprintf(redirect_file_1,format,args);
vfprintf(redirect_file_2,format,args);
/* etc */
va_end(args);
}
 
Sep 29, 2004
18,656
67
91
Can I ask why you are not being OO and making a logging class. Try to mimic log4j for Java. Its got well defined severity levels for logging.
 

Dimmu

Senior member
Jun 24, 2005
890
0
0
Originally posted by: IHateMyJob2004
Can I ask why you are not being OO and making a logging class. Try to mimic log4j for Java. Its got well defined severity levels for logging.

Or, for the sake of simplicity, just use this: log4cpp

:D