From: Dietmar Maurer Date: Fri, 14 Oct 2011 08:48:57 +0000 (+0200) Subject: add logfunc callback to run_command X-Git-Url: https://git.proxmox.com/?p=pve-common.git;a=commitdiff_plain;h=776fbfa8bde32e28297bcb74d042c8ce7a1a3d7c;ds=sidebyside add logfunc callback to run_command logfunc() is called with stdout and stderr. --- diff --git a/data/PVE/Tools.pm b/data/PVE/Tools.pm index 9ced12a..4f85564 100644 --- a/data/PVE/Tools.pm +++ b/data/PVE/Tools.pm @@ -181,6 +181,7 @@ sub run_command { my $output; my $outfunc; my $errfunc; + my $logfunc; foreach my $p (keys %param) { if ($p eq 'timeout') { @@ -201,6 +202,8 @@ sub run_command { $outfunc = $param{$p}; } elsif ($p eq 'errfunc') { $errfunc = $param{$p}; + } elsif ($p eq 'logfunc') { + $logfunc = $param{$p}; } else { die "got unknown parameter '$p' for run_command\n"; } @@ -264,12 +267,13 @@ sub run_command { } $select->remove ($h) if !$count; if ($h eq $reader) { - if ($outfunc) { + if ($outfunc || $logfunc) { eval { $outlog .= $buf; while ($outlog =~ s/^([^\010\r\n]*)(\r|\n|(\010)+|\r\n)//s) { my $line = $1; - &$outfunc($line); + &$outfunc($line) if $outfunc; + &$logfunc($line) if $logfunc; } }; my $err = $@; @@ -283,12 +287,13 @@ sub run_command { *STDOUT->flush(); } } elsif ($h eq $error) { - if ($errfunc) { + if ($errfunc || $logfunc) { eval { $errlog .= $buf; while ($errlog =~ s/^([^\010\r\n]*)(\r|\n|(\010)+|\r\n)//s) { my $line = $1; - &$errfunc($line); + &$errfunc($line) if $errfunc; + &$logfunc($line) if $logfunc; } }; my $err = $@; @@ -306,7 +311,10 @@ sub run_command { } &$outfunc($outlog) if $outfunc && $outlog; + &$logfunc($outlog) if $logfunc && $outlog; + &$errfunc($errlog) if $errfunc && $errlog; + &$logfunc($errlog) if $logfunc && $errlog; waitpid ($pid, 0);