App-nrun
view release on metacpan or search on metacpan
lib/NRun/Filters/FilterSync.pm view on Meta::CPAN
# Branch: master
#
# Changelog:--reverse --grep '^tags.*relevant':-1:%an : %ai : %s
#
# Timo Benk : 2013-06-13 13:59:01 +0200 : process output handling refined
# Timo Benk : 2013-06-13 20:32:17 +0200 : using __PACKAGE__ is less error-prone
# Timo Benk : 2013-06-14 17:38:58 +0200 : --no-hostname option removed
#
###
# this filter provides synchronious command output.
###
package NRun::Filters::FilterSync;
use strict;
use warnings;
use File::Basename;
use NRun::Filter;
our @ISA = qw(NRun::Filter);
BEGIN {
NRun::Filter::register ( {
'FILTER' => "sync",
'DESC' => "dump the command output synchroniously",
'NAME' => __PACKAGE__,
} );
}
###
# create a new object.
#
# <- the new object
sub new {
my $_pkg = shift;
my $_obj = shift;
my $self = {};
bless $self, $_pkg;
return $self;
}
###
# initialize this filter module.
sub init {
my $_self = shift;
}
###
# handle one line of data written on stdout.
#
# expected data format:
#
# HOSTNAME;[stdout|stderr];TSTAMP;PID;PID(CHILD);[debug|error|exit|output|end];"OUTPUT"
#
# $_data - the data to be handled
sub stdout {
my $_self = shift;
my $_data = shift;
my @data = split(/;/, $_data);
my ($message) = ($_data =~ m/[^"]"(.*)"[^"]*/);
if ($data[5] =~ /output|error|info/) {
push(@{$_self->{data}->{$data[0]}}, $message);
} elsif ($data[5] eq "end") {
$_self->{end_stdout}->{$data[0]} = 1;
if (defined($_self->{end_stderr}->{$data[0]})) {
$_self->end($data[0]);
}
}
}
###
# handle one line of data written on stderr.
#
# expected data format:
#
# HOSTNAME;[stdout|stderr];TSTAMP;PID;PID(CHILD);[debug|error|exit|output|end];"OUTPUT"
#
# $_data - the data to be handled
sub stderr {
my $_self = shift;
my $_data = shift;
my @data = split(/;/, $_data);
my ($message) = ($_data =~ m/[^"]"(.*)"[^"]*/);
if ($data[5] =~ /output|error|info/) {
push(@{$_self->{data}->{$data[0]}}, $message);
} elsif ($data[5] eq "end") {
$_self->{end_stderr}->{$data[0]} = 1;
if (defined($_self->{end_stdout}->{$data[0]})) {
$_self->end($data[0]);
}
}
}
###
# when both stderr and stdout have signaled end for $_host, dump
# the collected data for this host.
#
# $_host - the host that has finished execution
sub end {
my $_self = shift;
my $_host = shift;
my $output = delete($_self->{data}->{$_host});
if (defined($output)) {
print STDOUT "$_host: " . join("\n$_host: ", @$output) . "\n";
} else {
print STDOUT "$_host:\n";
}
}
1;
( run in 0.521 second using v1.01-cache-2.11-cpan-d8267643d1d )