I am trying to run a Perl script in Linux and log all output to STDOUT and STDERR to a file using:
open (STDOUT, "| tee -i $transcript_file");
open (STDERR, "| tee -ai $transcript_file");
The script that uses this works roughly as follows:
print, warn and possibly die statements.system command). This produces a lot of output which I want to appear on STDOUT, but not in the logfile (The tool creates its own logfile).print, warn and possibly die statements.Everything works correctly except I would like to exclude the output of step 2 from the log. Is there a simple way to achieve this?
Thanks,
PS: This is my first question on stackoverflow. Please help me in asking questions correctly if I have not done so.
I agree with Sobrique's advice to use a special function print_and_log. But if you really want to do it the way you set out to do, you can dup STDOUT and STDERR, redirect them to your log and then use open3 to run your tool with the dup'ed original standard output and error file descriptors
use IPC::Open3;
# dup the old standard output and error
open(OLDOUT, ">&STDOUT") or die "Can't dup STDOUT: $!\n";
open(OLDERR, ">&STDERR") or die "Can't dup STDERR: $!\n";
# reopen stdout and stderr
open (STDOUT, "|tee $transcript_file") or die "Can't reopen STDOUT: $!\n";
open (STDERR, ">&STDOUT") or die "Can't reopen STDERR: $!\n";
# print statements now write to log
print "Logging important info: blah!\n";
print STDERR "OOPS!\n";
# run your command; output will go to original stdout
# this will die() instead of returning say -1, so use eval() to catch errors
my $pid = open3(">&OLDOUT", "<&STDIN", ">&OLDERR", $command);
# wash those dishes....
waitpid( $pid, 0 );
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With