btrbk: add output file support for print_formatted() function

pull/57/head
Axel Burri 2015-10-20 20:16:34 +02:00
parent bfda14358e
commit cea3781ed2
1 changed files with 19 additions and 20 deletions

39
btrbk
View File

@ -273,7 +273,7 @@ sub action($@)
$h->{type} = $type; $h->{type} = $type;
$h->{time} = $time; $h->{time} = $time;
$h->{localtime} = strftime("%FT%T%z", localtime($time)); $h->{localtime} = strftime("%FT%T%z", localtime($time));
print_formatted("transaction", [ $h ], output_format => "tlog", no_header => 1, outfile => $tlog_fh); print_formatted("transaction", [ $h ], output_format => "tlog", no_header => 1, outfile => $tlog_fh) if($tlog_fh);
push @transaction_log, $h; push @transaction_log, $h;
return $h; return $h;
} }
@ -1940,6 +1940,7 @@ sub print_formatted(@)
my $format = $args{output_format} || $output_format || $default_format; my $format = $args{output_format} || $output_format || $default_format;
my $keys = $table_formats{$format_key}->{$format}; my $keys = $table_formats{$format_key}->{$format};
my $ralign = $table_formats{$format_key}->{RALIGN} // {}; my $ralign = $table_formats{$format_key}->{RALIGN} // {};
my $fh = $args{outfile} // *STDOUT;
my $table_spacing = 2; my $table_spacing = 2;
unless($keys) { unless($keys) {
@ -1948,25 +1949,23 @@ sub print_formatted(@)
$format = $default_format; $format = $default_format;
} }
print "$title\n" if($title); print $fh "$title\n" if($title);
if($format eq "raw") if($format eq "raw")
{ {
# output: key0="value0" key1="value1" ... # output: key0="value0" key1="value1" ...
foreach my $row (@$data) { foreach my $row (@$data) {
print "format=\"$format_key\" "; print $fh "format=\"$format_key\" ";
print join(' ', map { "$_=\"" . ($row->{$_} // "") . "\""; } @$keys) . "\n"; print $fh join(' ', map { "$_=\"" . ($row->{$_} // "") . "\""; } @$keys) . "\n";
} }
} }
elsif($format eq "tlog") elsif($format eq "tlog")
{ {
# output: value0 value1, ... # output: value0 value1, ...
if($tlog_fh) { unless($args{no_header}) {
unless($args{no_header}) { print $fh join(' ', @$keys) . "\n";
print $tlog_fh join(' ', @$keys) . "\n"; }
} foreach my $row (@$data) {
foreach my $row (@$data) { print $fh join(' ', map { ((defined($row->{$_}) && ($_ eq "message")) ? '# ' : '') . ($row->{$_} // "-") } @$keys) . "\n";
print $tlog_fh join(' ', map { ((defined($row->{$_}) && ($_ eq "message")) ? '# ' : '') . ($row->{$_} // "-") } @$keys) . "\n";
}
} }
} }
else else
@ -1994,33 +1993,33 @@ sub print_formatted(@)
# print keys (headings) # print keys (headings)
my $fill = 0; my $fill = 0;
foreach (@$keys) { foreach (@$keys) {
print ' ' x $fill; print $fh ' ' x $fill;
$fill = $maxlen{$_} - length($_); $fill = $maxlen{$_} - length($_);
if($ralign->{$_}) { if($ralign->{$_}) {
print ' ' x $fill; print $fh ' ' x $fill;
$fill = 0; $fill = 0;
} }
print $_; print $fh $_;
$fill += $table_spacing; $fill += $table_spacing;
} }
print "\n"; print $fh "\n";
print join(' ' x $table_spacing, map { '-' x ($maxlen{$_}) } @$keys) . "\n"; print $fh join(' ' x $table_spacing, map { '-' x ($maxlen{$_}) } @$keys) . "\n";
# print values # print values
foreach my $row (@$data) { foreach my $row (@$data) {
my $fill = 0; my $fill = 0;
foreach (@$keys) { foreach (@$keys) {
my $val = $row->{$_}; my $val = $row->{$_};
print ' ' x $fill; print $fh ' ' x $fill;
$fill = $maxlen{$_} - length($val); $fill = $maxlen{$_} - length($val);
if($ralign->{$_}) { if($ralign->{$_}) {
print ' ' x $fill; print $fh ' ' x $fill;
$fill = 0; $fill = 0;
} }
print $val; print $fh $val;
$fill += $table_spacing; $fill += $table_spacing;
} }
print "\n"; print $fh "\n";
} }
} }
} }