Why not write the messages you want to capture to stderr and redirect that appropriately? One thin you should consider is that stdout is [usually] buffered and stderr is not.
You're looking for shell redirection and piping. Assuming you're using a standard-ish shell like shell, bash, csh, etc. ...
command >foo.txt
will redirect stdout to the file foo.txt
. Any existing content in the fill will be replaced.
command >>foo.txt
will redirect stdout, appending it to the file foo.txt
to the existing content, if any, of foot.txt
.
foo | bar
will pipe the content of command foo
's stdout
through command bar
's stdin.
A *nix process has three standard i/o handles open:
- stdin is standard input. It is file handle 0.
- stdout is standard output. It is filehandle 1.
- stderr is standard error. It is filehandle 2.
The redirection and pipe operators can hook specific file handles as well. So...
command >foo.txt 2>&1
does the following:
- Redirects standard output to the file
foo.txt
- Redirects stderr to file handle 1, stderr, which is now opened as
foo.txt
.
Other shells can (and do) use different, but generally similar syntax.
You should also learn about some useful commands:
tee(1)
.
This takes the stdin given it and writes it to file specified on the command line as well as to stdout, so a chain like this:
some-command 2>&1 | tee some-command.log.2013-05-31.txt | less
runs some-command
, which presumably produces a goodly amount of output, redirecting its stderr to stdout, which is then piped through tee(1)
, producing a logfile, some-commmand.log.2013-05-31.txt
and piping it through less(1)
to page it on your console.
less(1)
more(1)
more
is usually a synonym for less(1)
these days. both of these are pagers which buffer output and dispaly it a screen at a time, letting you page forwards and backward through it.
man(1)
apropos(1)
This is *nix's online help system. To learn more about them, try running these commands:
* `man man`
* `man apropos`
To get the man page for the man
and apropos
commands. You might also try man tee
, or man [your-shell-name]
for the specific shell you're using (e.g., man csh
will get you the man page for the csh
shell, whilst man sh
will get you the Bourne shell (sh
) man page and man bash
will get you the bash
man page.
This page describes the usual piping/redirection operators for the C and Bourne shell families: http://www.mathinfo.u-picardie.fr/asch/f/MeCS/courseware/users/help/general/unix/redirection.html
You should also read the appropriate O'Reilly Nutshell
book:
Edited To Note:
As I mentioned in the comment below, you can replace System.Out
and System.Err
with your own PrintStream
, along these lines:
PrintStream originalStdOut = System.Out ;
PrintStream newStdout = CreateNewOutputSink() ;
System.Out.flush() ;
System.Out.setOut( newStdout ) ;
invokeSome3rdPartyCode() ;
System.Out.flush() ;
System.Out.setOut( originalStdout ) ;
This will let you hook the output from your 3rd party library and redirect it to the data sink of your choice, whilst not affecting your ordinary output. If you hook both System.Out
and System.Err
and redirect them to the same PrintStream
you'll catch both standard and error output in the process.