Created
March 18, 2011 10:33
-
-
Save wchristian/875874 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package TeeToFile; | |
use FileHandle; | |
use PerlIO::Util; | |
use base 'Exporter'; | |
our @EXPORT_OK = qw( tee_child tee ); | |
sub tee { | |
my ( $log_file, $code ) = @_; | |
my $log_fh = FileHandle->new( $log_file, 'a+' ); | |
for ( *STDOUT, *STDERR ) { | |
$_->autoflush; | |
$_->push_layer( tee => $log_fh ); | |
} | |
my @return = $code->(); | |
$_->pop_layer for *STDOUT, *STDERR; | |
return @return; | |
} | |
sub tee_child { | |
my ( $cmd ) = @_; | |
open my $MAKE, "$cmd 2>&1 |" or die; | |
while ( <$MAKE> ) { print } | |
close $MAKE; # to get $? | |
return $?; | |
} | |
1; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment