From 804b104122b78b07f81fdc259db4dd5ab82f016a Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Tue, 8 Nov 2011 07:37:12 +0100 Subject: [PATCH 1/1] new helper dump_logfile --- data/PVE/Tools.pm | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/data/PVE/Tools.pm b/data/PVE/Tools.pm index b991ba5..b42a5bb 100644 --- a/data/PVE/Tools.pm +++ b/data/PVE/Tools.pm @@ -702,4 +702,41 @@ sub split_args { return $str ? [ Text::ParseWords::shellwords($str) ] : []; } +sub dump_logfile { + my ($filename, $start, $limit) = @_; + + my $lines = []; + my $count = 0; + + my $fh = IO::File->new($filename, "r"); + if (!$fh) { + $count++; + push @$lines, { n => $count, t => "unable to open file - $!"}; + return ($count, $lines); + } + + $start = 0 if !$start; + $limit = 50 if !$limit; + + my $line; + while (defined($line = <$fh>)) { + next if $count++ < $start; + next if $limit <= 0; + chomp $line; + push @$lines, { n => $count, t => $line}; + $limit--; + } + + close($fh); + + # HACK: ExtJS store.guaranteeRange() does not like empty array + # so we add a line + if (!$count) { + $count++; + push @$lines, { n => $count, t => "no content"}; + } + + return ($count, $lines); +} + 1; -- 2.39.2