Disabling printf, enabling something else

D

D¿ejm

Hello,

We have a code with lot of printfs. Now I would like to introduce special
mode, in which only special messages are displayed and all other are hidden.
No macros or replacements please. I'm looking for good looking in one place
solution :).

Disabling printf is easy:
fclose( stdout );
But then I can't output anything.

I thought about duplicating handle, and opening my own output. But then all
the printfs are visible again somehow:

int fd = dup( _fileno( stdout ) );
fclose( stdout );
printf( "Should NOT be visible. It is not. Good.\n" );
FILE * specialOutput = _fdopen( fd, "w" );
fprintf( specialOutput, "Should be visible. It is. Good.\n" );
printf( "Should NOT be visible, but it is. Bad.\n" );

Any other ideas how to do this ?

PS. There are also cout used. But this probably will do the trick:
cout.clear( ios::badbit );

Dz
 
E

Erik Wikström

Hello,

We have a code with lot of printfs. Now I would like to introduce special
mode, in which only special messages are displayed and all other are hidden.
No macros or replacements please. I'm looking for good looking in one place
solution :).

Disabling printf is easy:
fclose( stdout );
But then I can't output anything.

I thought about duplicating handle, and opening my own output. But then all
the printfs are visible again somehow:

int fd = dup( _fileno( stdout ) );
fclose( stdout );
printf( "Should NOT be visible. It is not. Good.\n" );
FILE * specialOutput = _fdopen( fd, "w" );
fprintf( specialOutput, "Should be visible. It is. Good.\n" );
printf( "Should NOT be visible, but it is. Bad.\n" );

Any other ideas how to do this ?

PS. There are also cout used. But this probably will do the trick:
cout.clear( ios::badbit );

Wrap the printfs in a function which contains some if-statement checking
for whatever condition you want. Or use fprintf and put the special
output in cerr/clog.
 
A

Alan Johnson

D¿ejm said:
Hello,

We have a code with lot of printfs. Now I would like to introduce special
mode, in which only special messages are displayed and all other are hidden.
No macros or replacements please. I'm looking for good looking in one place
solution :).

Disabling printf is easy:
fclose( stdout );
But then I can't output anything.

I thought about duplicating handle, and opening my own output. But then all
the printfs are visible again somehow:

int fd = dup( _fileno( stdout ) );
fclose( stdout );
printf( "Should NOT be visible. It is not. Good.\n" );
FILE * specialOutput = _fdopen( fd, "w" );
fprintf( specialOutput, "Should be visible. It is. Good.\n" );
printf( "Should NOT be visible, but it is. Bad.\n" );

Any other ideas how to do this ?

PS. There are also cout used. But this probably will do the trick:
cout.clear( ios::badbit );

Dz

Some platforms allow you to do:
freopen("filename", "w", stdout);

Probably not portable. Probably not even advisable (though my man page
suggests using freopen for exactly that).
 
A

Alan Johnson

Alan said:
Some platforms allow you to do:
freopen("filename", "w", stdout);

Probably not portable. Probably not even advisable (though my man page
suggests using freopen for exactly that).

Which, upon rereading, appears to not achieve your goal at all. I
should stop posting late at night.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top