0

Possible Duplicate:
How to redirect cin and cout to files?

I'm debugging some spaghetti code which writes to the terminal via std::cout and via a third party library called tecla:

http://www.astro.caltech.edu/~mcs/tecla/

I would like to hook myself into the stdout so that I can redirect it to a file. Before saving to file I can convert the strings to hex representations so that I can more easily identify all the control characters they contain.

I can also log to this file various events within the program so as to better understand how the program is working.

Any suggestions on how I might do this?

UPDATE

My program also receives commands on the terminal via std input. It seems doing:

myprogram 9889 > output.txt

or even

myprogram 9889 | xxd

disrupts the program in some manner such that I cant seem to get the program to respond to inputted commands.

The prompt (>) does not appear in output.txt.

Community
  • 1
  • 1
Baz
  • 12,713
  • 38
  • 145
  • 268
  • You can just save the output of any process in the terminal / windows command line by saying `process > filename` – leemes Jan 18 '13 at 16:10

5 Answers5

1

If you are on a UNIX-oid system, apart from directly storing into a file (as seen in the other answers), you can view it in HEX-encoding and browse it with the cursor keys by combining hexdump and less:

./myProgram | hexdump -C | less

The output then looks like:

00000000  73 6f 6d 65 20 63 72 79  70 74 69 63 20 6f 75 74  |some cryptic out|
00000010  70 75 74 20 66 72 6f 6d  20 6d 79 20 70 72 6f 63  |put from my proc|
00000020  65 73 73 0a                                       |ess.|
00000024

To view (browse using less) + save in a file simutanously, just add a tee command in this command pipe:

./myProgram | hexdump -C | tee output.txt | less    #for hex-encoded file content

./myProgram | tee output.txt | hexdump -C | less    #for 1:1 file content

To also include the stderr output in the file / view, merge the two channels before processing it by prepending the process command line with 2>&1:

./myProgram 2>&1 | ...
leemes
  • 44,967
  • 21
  • 135
  • 183
  • 1
    On windows you could use cygwin to gain access to hexdump and less with the same syntax as above even when executing windows console applications. – drescherjm Jan 18 '13 at 16:32
  • @Baz Did you try the `tee` command? This will output the `stdout` of your process still on the terminal and allows you to do some input to `stdin`. Should be seamless (it logs as a *copy* in the specified filename, not *redirect*) – leemes Jan 18 '13 at 23:25
1

Writing to std::cout is very bad practice, except in small test programs. But since you're stuck with it, in code you cannot change: it's possible to change the streambuf to which the stream writes, with something like the following:

class WrappedOutputFilebuf : public std::filebuf
{
    std::ostream& mySavedStream;
    std::streambuf* mySavedStreambuf;
public:
    WrappedOutputFilebuf( std::ostream& stream, std::string filename )
        : mySavedStream( stream)
        , mySavedStreambuf( stream.rdbuf() )
    {
        open( filename.c_str(), std::ios_base::out );
        if ( ! is_open() ) {
            throw some_error;
        }
        stream.rdbuf( this );
    }
    ~WrappedOutputFilebuf()
    {
        mySavedStream.rdbuf( mySavedStreambuf );
    }
};

Then just declare it somewhere:

WrappedOutputFilebuf w( std::cout, fileName );

and as long as the variable is in scope, std::cout will output to the file you've specified.

James Kanze
  • 150,581
  • 18
  • 184
  • 329
0

Redirecting output to a file is something you usually do on the command line, not inside your code itself. For example:

myProgram > output.txt
Matt Kline
  • 10,149
  • 7
  • 50
  • 87
0

Just run your program by typing:

myprogram > output.txt

Then view the output.txt file using a hex editor such as WinHex or ghex2.

David Grayson
  • 84,103
  • 24
  • 152
  • 189
0

If the program you're debugging is sensitive to whether it's talking to a terminal, you might look at screen, in particular its logging capabilities.

jthill
  • 55,082
  • 5
  • 77
  • 137