Просмотр исходного кода

Made Logfile accept wildcards, sanitized NFiles behaviour on multiple LogDirs.

Christoph Berg 21 лет назад
Родитель
Сommit
49d98af7ef
4 измененных файлов с 71 добавлено и 73 удалено
  1. 2 0
      docs/Changelog
  2. 9 7
      docs/pisg-doc.xml
  3. 4 4
      modules/Pisg.pm
  4. 56 62
      modules/Pisg/Parser/Logfile.pm

+ 2 - 0
docs/Changelog

@@ -10,6 +10,8 @@ pisg (0.64) - ??
      also for <a href>s (SF tracker #1088382 by Jeroen - tux2020).
      also for <a href>s (SF tracker #1088382 by Jeroen - tux2020).
    * Applied patch by Torbjörn Svensson for using alternate CSS files - thanks
    * Applied patch by Torbjörn Svensson for using alternate CSS files - thanks
      (AltColorScheme option).
      (AltColorScheme option).
+   * Made Logfile accept wildcards, sanitized NFiles behaviour on multiple
+     LogDirs.
 
 
 pisg (0.63) - Wed Jan, 12th 2005
 pisg (0.63) - Wed Jan, 12th 2005
    Morten:
    Morten:

+ 9 - 7
docs/pisg-doc.xml

@@ -591,6 +591,8 @@
     you want to parse a directory full of logfiles, you should use the
     you want to parse a directory full of logfiles, you should use the
     <xref linkend="LogDir" /> option instead. Providing this option multiple
     <xref linkend="LogDir" /> option instead. Providing this option multiple
     times will parse multiple files in the order the statements appear.
     times will parse multiple files in the order the statements appear.
+    Wildcards (* ? []) will be expanded.
+    See also <xref linkend="LogDir" /> and <xref linkend="NFiles" />.
     </para>
     </para>
     </refsect1>
     </refsect1>
     <refsect1>
     <refsect1>
@@ -625,9 +627,11 @@
     <para>
     <para>
     When <command>LogDir</command> is defined to valid path to a directory,
     When <command>LogDir</command> is defined to valid path to a directory,
     then pisg will run through that directory, parse all logfiles in it and
     then pisg will run through that directory, parse all logfiles in it and
-    create 1 HTML from it. Useful with for example eggdrop logs. Providing
+    create one HTML page from it. Useful with for example eggdrop logs. Providing
     this option multiple times will parse all the files in multiple
     this option multiple times will parse all the files in multiple
     directories in the order the statements appear.
     directories in the order the statements appear.
+    See also <xref linkend="NFiles" />, <xref linkend="LogPrefix" />, and
+    <xref linkend="LogSuffix" />.
     </para>
     </para>
     </refsect1>
     </refsect1>
     <refsect1>
     <refsect1>
@@ -662,12 +666,10 @@
     <title>Description</title>
     <title>Description</title>
     <para>
     <para>
     When <command>NFiles</command> is set to a positive integer, pisg will
     When <command>NFiles</command> is set to a positive integer, pisg will
-    process at most that much files from <xref linkend="LogDir" />. Useful to
-    create statistics that cover the last week or month. Note that if multiple
-    <xref linkend="LogDir" /> options are provided, <command>NFiles</command>
-    refers to the number of files which will be processed from each
-    <xref linkend="LogDir" />
-    </para>
+    process only the last that much logfiles from <xref linkend="Logfile" />
+    and <xref linkend="LogDir" /> options. Useful to create statistics that
+    cover the last week or month (assuming there are separate logfile per
+    day/week/etc.). </para>
     </refsect1>
     </refsect1>
     <refsect1>
     <refsect1>
     <title>Default</title>
     <title>Default</title>

+ 4 - 4
modules/Pisg.pm

@@ -427,10 +427,10 @@ sub init_pisg
         print STDERR "Warning: $self->{cfg}->{configfile}, line $.: Invalid UserPics setting\n";
         print STDERR "Warning: $self->{cfg}->{configfile}, line $.: Invalid UserPics setting\n";
     }
     }
 
 
-    print "Using language template: $self->{cfg}->{lang}\n\n" if ($self->{cfg}->{lang} ne 'EN' && !$self->{cfg}->{silent});
-
-    print "Statistics for channel $self->{cfg}->{channel} \@ $self->{cfg}->{network} by $self->{cfg}->{maintainer}\n\n"
-        unless ($self->{cfg}->{silent});
+    unless ($self->{cfg}->{silent}) {
+        print "Statistics for channel $self->{cfg}->{channel} \@ $self->{cfg}->{network} by $self->{cfg}->{maintainer}\n\n";
+        print "Using language template: $self->{cfg}->{lang}\n\n" if $self->{cfg}->{lang} ne 'EN';
+    }
 }
 }
 
 
 sub do_channel
 sub do_channel

+ 56 - 62
modules/Pisg/Parser/Logfile.pm

@@ -81,60 +81,72 @@ sub analyze
     my $self = shift;
     my $self = shift;
     my (%stats, %lines);
     my (%stats, %lines);
 
 
-    if (defined $self->{parser}) {
-        my $starttime = time();
+    unless (defined $self->{parser}) {
+        print STDERR "Skipping channel '$self->{cfg}->{channel}' due to lack of parser.\n";
+        return undef
+    }
 
 
-        # Just initialize these to 0
-        $stats{days} = 0;
-        $stats{parsedlines} = 0;
-        $stats{totallines} = 0;
+    my $starttime = time();
 
 
-        if (scalar(@{$self->{cfg}->{logdir}}) > 0) {
-            # Run through all files in dir
-            $self->_parse_dir(\%stats, \%lines);
-        } else {
-            my %state = (
-                linecount  => 0,
-                lastnick   => '',
-                monocount  => 0,
-                lastnormal => '',
-                oldtime    => 24
-            );
-            foreach my $logfile (@{$self->{cfg}->{logfile}}) {
-                # Run through the whole logfile
-                $self->_parse_file(\%stats, \%lines, $logfile, \%state);
-            }
-        }
+    # Just initialize these to 0
+    $stats{days} = 0;
+    $stats{parsedlines} = 0;
+    $stats{totallines} = 0;
 
 
-        $self->_pick_random_lines(\%stats, \%lines);
-        _uniquify_nicks(\%stats);
+    if (scalar(@{$self->{cfg}->{logdir}}) > 0) {
+        $self->_parse_dir(); # get all files in dir
+    }
 
 
-        my ($sec,$min,$hour) = gmtime(time() - $starttime);
-        my $processtime = sprintf('%02d hours, %02d minutes and %02d seconds', $hour, $min, $sec);
+    my @logfiles = @{$self->{cfg}->{logfile}};
 
 
-        $stats{processtime}{hours} = sprintf('%02d', $hour);
-        $stats{processtime}{mins} = sprintf('%02d', $min);
-        $stats{processtime}{secs} = sprintf('%02d', $sec);
+    # expand wildcards
+    @logfiles = map { if(/[\[*?]/) { glob; } else { $_; } } @logfiles;
+    my $count = @logfiles;
 
 
-        print "Channel analyzed successfully in $processtime on ",
-        scalar localtime(time()), "\n"
-            unless ($self->{cfg}->{silent});
+    my $shift = 0;
+    if($self->{cfg}->{nfiles} > 0) { # chop list to maximal length
+        $shift = @logfiles - $self->{cfg}->{nfiles};
+        splice(@logfiles, 0, $shift) if $shift > 0;
+    }
 
 
-        return \%stats;
+    unless ($self->{cfg}->{silent}) {
+        my $msg = "";
+        $msg = ", parsing the last $self->{cfg}->{nfiles}" if ($shift > 0);
+        print "$count logfile(s) found$msg, using $self->{cfg}->{format} format...\n\n"
+    }
 
 
-    } else {
-        print STDERR "Skipping channel '$self->{cfg}->{channel}' due to lack of parser.\n";
-        return undef
+    my %state = (
+        lastnick   => '',
+        monocount  => 0,
+        lastnormal => '',
+        oldtime    => 24
+    );
+
+    foreach my $logfile (@logfiles) {
+        # Run through the logfile
+        $self->_parse_file(\%stats, \%lines, $logfile, \%state);
     }
     }
 
 
-    # Shouldn't get here.
-    return undef;
+    $self->_pick_random_lines(\%stats, \%lines);
+    _uniquify_nicks(\%stats);
+
+    my ($sec,$min,$hour) = gmtime(time() - $starttime);
+    my $processtime = sprintf('%02d hours, %02d minutes and %02d seconds', $hour, $min, $sec);
+
+    $stats{processtime}{hours} = sprintf('%02d', $hour);
+    $stats{processtime}{mins} = sprintf('%02d', $min);
+    $stats{processtime}{secs} = sprintf('%02d', $sec);
+
+    print "Channel analyzed successfully in $processtime on ",
+    scalar localtime(time()), "\n\n"
+        unless ($self->{cfg}->{silent});
+
+    return \%stats;
 }
 }
 
 
 sub _parse_dir
 sub _parse_dir
 {
 {
     my $self = shift;
     my $self = shift;
-    my ($stats, $lines) = @_;
 
 
     # Loop through each logdir we were given
     # Loop through each logdir we were given
     foreach my $logdir (@{$self->{cfg}->{logdir}}) {
     foreach my $logdir (@{$self->{cfg}->{logdir}}) {
@@ -142,11 +154,7 @@ sub _parse_dir
         $logdir =~ s/([^\/])$/$1\//;
         $logdir =~ s/([^\/])$/$1\//;
 
 
         unless ($self->{cfg}->{silent}) {
         unless ($self->{cfg}->{silent}) {
-            unless ($self->{cfg}->{nfiles} > 0) {
-                print "Going into $logdir and parsing all files there...\n\n"
-            } else {
-                print "Going into $logdir and parsing the last $self->{cfg}->{nfiles} file(s) there...\n\n"
-            }
+            print "Looking for logfiles in $logdir...\n\n"
         }
         }
         my @filesarray;
         my @filesarray;
         opendir(LOGDIR, $logdir) or
         opendir(LOGDIR, $logdir) or
@@ -157,11 +165,6 @@ sub _parse_dir
         die("No files in \"$logdir\" matched prefix \"$self->{cfg}->{logprefix}\"");
         die("No files in \"$logdir\" matched prefix \"$self->{cfg}->{logprefix}\"");
         closedir(LOGDIR);
         closedir(LOGDIR);
 
 
-        my %state = (
-            lastnick   => '',
-            monocount  => 0,
-            oldtime    => 24
-        );
         if ($self->{cfg}->{logsuffix} ne '') {
         if ($self->{cfg}->{logsuffix} ne '') {
             my @temparray;
             my @temparray;
             my %months = (
             my %months = (
@@ -220,27 +223,18 @@ sub _parse_dir
                     }
                     }
                 }
                 }
             }
             }
-            my @newarray = @filesarray[ sort {
+            @filesarray = @filesarray[ sort {
                                         $year[$a] <=> $year[$b]
                                         $year[$a] <=> $year[$b]
                                                 ||
                                                 ||
                                         $month[$a] <=> $month[$b]
                                         $month[$a] <=> $month[$b]
                                                 ||
                                                 ||
                                         $day[$a] <=> $day[$b]
                                         $day[$a] <=> $day[$b]
                                     } 0..$#filesarray ];
                                     } 0..$#filesarray ];
-            @filesarray = @newarray;
         } else {
         } else {
             @filesarray = sort {lc($a) cmp lc($b)} @filesarray;
             @filesarray = sort {lc($a) cmp lc($b)} @filesarray;
         }
         }
 
 
-        if($self->{cfg}->{nfiles} > 0) {
-            my $shift = @filesarray - $self->{cfg}->{nfiles};
-            splice(@filesarray, 0, $shift) if $shift > 0;
-        }
-
-        foreach my $file (@filesarray) {
-            $file = $logdir . $file;
-            $self->_parse_file($stats, $lines, $file, \%state);
-        }
+        push @{$self->{cfg}->{logfile}}, map { "$logdir$_" } @filesarray;
     }
     }
 }
 }
 
 
@@ -250,7 +244,7 @@ sub _parse_file
     my $self = shift;
     my $self = shift;
     my ($stats, $lines, $file, $state) = @_;
     my ($stats, $lines, $file, $state) = @_;
 
 
-    print "Analyzing log $file in '$self->{cfg}->{format}' format...\n"
+    print "Analyzing log $file... "
         unless ($self->{cfg}->{silent});
         unless ($self->{cfg}->{silent});
 
 
     if ($file =~ /.bz2?$/ && -f $file) {
     if ($file =~ /.bz2?$/ && -f $file) {
@@ -544,7 +538,7 @@ sub _parse_file
 
 
     close(LOGFILE);
     close(LOGFILE);
 
 
-    print "Finished analyzing log, $stats->{days} days, $stats->{parsedlines} lines total\n"
+    print "$stats->{days} days, $stats->{parsedlines} lines total\n"
         unless ($self->{cfg}->{silent});
         unless ($self->{cfg}->{silent});
 }
 }