App-nrun
view release on metacpan or search on metacpan
lib/NRun/Loggers/LoggerOutput.pm view on Meta::CPAN
use File::Basename;
use NRun::Logger;
our @ISA = qw(NRun::Logger);
BEGIN {
NRun::Logger::register ( {
'LOGGER' => "output",
'DESC' => "log the command output",
'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 logger module.
#
# $_cfg - parameter hash where
# {
# 'log_directory' - the base directory the logfile should be created in
# }
# <- the new object
sub init {
my $_self = shift;
my $_cfg = shift;
$_self->{log_directory} = $_cfg->{log_directory};
$_self->{logfile} = "$_self->{log_directory}/output.log";
select(LOG);
$| = 1;
select(STDOUT);
open(LOG, ">>$_self->{logfile}") or die("$_self->{logfile}: $!");
$_self->{LOG} = \*LOG;
}
###
# 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, save
# 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 {$_self->{LOG}} "$_host: " . join("\n$_host: ", @$output) . "\n";
} else {
print {$_self->{LOG}} "$_host:\n";
}
}
DESTROY {
my $_self = shift;
close($_self->{LOG});
};
1;
( run in 1.063 second using v1.01-cache-2.11-cpan-d8267643d1d )