add logfunc callback to run_command

logfunc() is called with stdout and stderr.
This commit is contained in:
Dietmar Maurer 2011-10-14 10:48:57 +02:00
parent 75ed54f129
commit 776fbfa8bd

View File

@ -181,6 +181,7 @@ sub run_command {
my $output; my $output;
my $outfunc; my $outfunc;
my $errfunc; my $errfunc;
my $logfunc;
foreach my $p (keys %param) { foreach my $p (keys %param) {
if ($p eq 'timeout') { if ($p eq 'timeout') {
@ -201,6 +202,8 @@ sub run_command {
$outfunc = $param{$p}; $outfunc = $param{$p};
} elsif ($p eq 'errfunc') { } elsif ($p eq 'errfunc') {
$errfunc = $param{$p}; $errfunc = $param{$p};
} elsif ($p eq 'logfunc') {
$logfunc = $param{$p};
} else { } else {
die "got unknown parameter '$p' for run_command\n"; die "got unknown parameter '$p' for run_command\n";
} }
@ -264,12 +267,13 @@ sub run_command {
} }
$select->remove ($h) if !$count; $select->remove ($h) if !$count;
if ($h eq $reader) { if ($h eq $reader) {
if ($outfunc) { if ($outfunc || $logfunc) {
eval { eval {
$outlog .= $buf; $outlog .= $buf;
while ($outlog =~ s/^([^\010\r\n]*)(\r|\n|(\010)+|\r\n)//s) { while ($outlog =~ s/^([^\010\r\n]*)(\r|\n|(\010)+|\r\n)//s) {
my $line = $1; my $line = $1;
&$outfunc($line); &$outfunc($line) if $outfunc;
&$logfunc($line) if $logfunc;
} }
}; };
my $err = $@; my $err = $@;
@ -283,12 +287,13 @@ sub run_command {
*STDOUT->flush(); *STDOUT->flush();
} }
} elsif ($h eq $error) { } elsif ($h eq $error) {
if ($errfunc) { if ($errfunc || $logfunc) {
eval { eval {
$errlog .= $buf; $errlog .= $buf;
while ($errlog =~ s/^([^\010\r\n]*)(\r|\n|(\010)+|\r\n)//s) { while ($errlog =~ s/^([^\010\r\n]*)(\r|\n|(\010)+|\r\n)//s) {
my $line = $1; my $line = $1;
&$errfunc($line); &$errfunc($line) if $errfunc;
&$logfunc($line) if $logfunc;
} }
}; };
my $err = $@; my $err = $@;
@ -306,7 +311,10 @@ sub run_command {
} }
&$outfunc($outlog) if $outfunc && $outlog; &$outfunc($outlog) if $outfunc && $outlog;
&$logfunc($outlog) if $logfunc && $outlog;
&$errfunc($errlog) if $errfunc && $errlog; &$errfunc($errlog) if $errfunc && $errlog;
&$logfunc($errlog) if $logfunc && $errlog;
waitpid ($pid, 0); waitpid ($pid, 0);