]> git.proxmox.com Git - pve-common.git/commitdiff
new helper dump_logfile
authorDietmar Maurer <dietmar@proxmox.com>
Tue, 8 Nov 2011 06:37:12 +0000 (07:37 +0100)
committerDietmar Maurer <dietmar@proxmox.com>
Tue, 8 Nov 2011 07:10:41 +0000 (08:10 +0100)
data/PVE/Tools.pm

index b991ba5afef2a2592d6519fe05057f6d5fffbaf7..b42a5bb5f603b4ff5b3d13a9503aabb9067f2a64 100644 (file)
@@ -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;