btrbk: syslog: add syslog hack in print_formatted(); remove print-to-scalar hack

pull/88/head
Axel Burri 2016-04-25 19:40:11 +02:00
parent c424d917b6
commit 47eb14f5fb
1 changed files with 27 additions and 22 deletions

45
btrbk
View File

@ -205,7 +205,7 @@ my $err = "";
my $abrt = ""; # last ABORTED() message my $abrt = ""; # last ABORTED() message
my $output_format; my $output_format;
my $tlog_fh; my $tlog_fh;
my $syslog_facility; my $syslog_enabled = 0;
my $current_transaction; my $current_transaction;
my @transaction_log; my @transaction_log;
my %config_override; my %config_override;
@ -338,14 +338,15 @@ sub init_transaction_log($$)
} }
} }
if(defined($config_syslog_facility) && (not $dryrun)) { if(defined($config_syslog_facility) && (not $dryrun)) {
eval { local $SIG{'__DIE__'}; eval {
local $SIG{'__DIE__'};
require Sys::Syslog; require Sys::Syslog;
}; };
if($@) { if($@) {
WARN "Syslog disabled: $@"; WARN "Syslog disabled: $@";
} else { } else {
$syslog_facility = $config_syslog_facility; $syslog_enabled = 1;
Sys::Syslog::openlog("btrbk", "", $syslog_facility); Sys::Syslog::openlog("btrbk", "", $config_syslog_facility);
} }
} }
action("startup", status => "v$VERSION", message => "$VERSION_INFO"); action("startup", status => "v$VERSION", message => "$VERSION_INFO");
@ -357,7 +358,7 @@ sub close_transaction_log()
DEBUG "Closing transaction log"; DEBUG "Closing transaction log";
close $tlog_fh || ERROR "Failed to close transaction log: $!"; close $tlog_fh || ERROR "Failed to close transaction log: $!";
} }
if(defined($syslog_facility)) { if($syslog_enabled) {
eval { local $SIG{'__DIE__'}; eval { local $SIG{'__DIE__'};
Sys::Syslog::closelog(); Sys::Syslog::closelog();
}; };
@ -373,19 +374,7 @@ sub action($@)
$h->{time} = $time; $h->{time} = $time;
$h->{localtime} = timestamp($time, 'debug-iso'); $h->{localtime} = timestamp($time, 'debug-iso');
print_formatted("transaction", [ $h ], output_format => "tlog", no_header => 1, outfile => $tlog_fh) if($tlog_fh); print_formatted("transaction", [ $h ], output_format => "tlog", no_header => 1, outfile => $tlog_fh) if($tlog_fh);
if (defined($syslog_facility)) { print_formatted("transaction", [ $h ], output_format => "syslog", no_header => 1) if($syslog_enabled); # dirty hack, this calls syslog()
my $msg = q{};
open my $msg_fh, '+<', \$msg;
print_formatted("transaction", [ $h ], output_format => "syslog", no_header => 1, outfile => $msg_fh);
seek($msg_fh, 0, 0);
while (my $line = <$msg_fh>) {
eval { local $SIG{'__DIE__'};
Sys::Syslog::syslog("info", $line);
};
if($@) { WARN "syslog failed: $@"; }
}
close $msg_fh;
}
push @transaction_log, $h; push @transaction_log, $h;
return $h; return $h;
} }
@ -415,6 +404,17 @@ sub end_transaction($$)
$current_transaction = undef; $current_transaction = undef;
} }
sub syslog($)
{
my $line = shift;
return undef unless($syslog_enabled);
eval {
local $SIG{'__DIE__'};
Sys::Syslog::syslog("info", $line);
};
if($@) { WARN "syslog failed: $@"; }
}
sub safe_cmd($) sub safe_cmd($)
{ {
my $aref = shift; my $aref = shift;
@ -3196,14 +3196,19 @@ sub print_formatted(@)
print $fh join(' ', map { "$_=\"" . ($row->{$_} // "") . "\""; } @$keys) . "\n"; print $fh join(' ', map { "$_=\"" . ($row->{$_} // "") . "\""; } @$keys) . "\n";
} }
} }
elsif($format eq "tlog" || $format eq "syslog") elsif(($format eq "tlog") || ($format eq "syslog"))
{ {
# output: value0 value1, ... # output: value0 value1, ...
unless($args{no_header}) { unless($args{no_header}) {
print $fh join(' ', @$keys) . "\n"; print $fh join(' ', @$keys) . "\n";
} }
foreach my $row (@$data) { foreach my $row (@$data) {
print $fh join(' ', map { ((defined($row->{$_}) && ($_ eq "message")) ? '# ' : '') . ($row->{$_} // "-") } @$keys) . "\n"; my $line = join(' ', map { ((defined($row->{$_}) && ($_ eq "message")) ? '# ' : '') . ($row->{$_} // "-") } @$keys);
if($format eq "syslog") { # dirty hack, ignore outfile on syslog format
syslog($line);
} else {
print $fh ($line . "\n");
}
} }
} }
else else