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

* Multiple LogDirs and LogFiles can now be specified (thanks, Nathan
Poznick)

Morten Brix Pedersen 22 лет назад
Родитель
Сommit
4dec4ca889
6 измененных файлов с 134 добавлено и 105 удалено
  1. 2 0
      docs/CREDITS
  2. 2 0
      docs/Changelog
  3. 9 3
      docs/pisg-doc.sgml
  4. 16 6
      modules/Pisg.pm
  5. 97 92
      modules/Pisg/Parser/Logfile.pm
  6. 8 4
      pisg

+ 2 - 0
docs/CREDITS

@@ -101,6 +101,8 @@ And all the other contributors:
    | perlbot logfile support
  * Christoph Berg <cb@df7cb.de>
    | NFiles and UserPics options, wildcards for random user pictures
+ * Nathan Poznick
+   | Ability to specify multiple LogFiles and LogDirs.
 
 I probably forgot a lot of people here, _PLEASE_ notify me if I left you
 out.. because I have a bad habit of forgetting to maintain this list :)

+ 2 - 0
docs/Changelog

@@ -5,6 +5,8 @@ pisg (x.xx)
      0.37) has been removed.
    * PicWidth and PicHeight apparently got mixed up in the last version,
    fixed.
+   * Multiple LogDirs and LogFiles can now be specified (thanks, Nathan
+   Poznick)
 
 pisg (0.51) - Wed Jan, 21st 2004
    * Fixed a bug in the irssi format where an action could be recognized as a

+ 9 - 3
docs/pisg-doc.sgml

@@ -589,7 +589,8 @@
     <para>
     This defines the filename of the logfile to parse for the channel. If
     you want to parse a directory full of logfiles, you should use the
-    <command>LogDir</command> option instead.
+    <command>LogDir</command> option instead. Providing this option multiple
+    times will parse multiple files in the order the statements appear.
     </para>
     </refsect1>
     <refsect1>
@@ -624,7 +625,9 @@
     <para>
     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
-    create 1 HTML from it. Useful with for example eggdrop logs.
+    create 1 HTML from it. Useful with for example eggdrop logs. Providing
+    this option multiple times will parse all the files in multiple
+    directories in the order the statements appear.
     </para>
     </refsect1>
     <refsect1>
@@ -660,7 +663,10 @@
     <para>
     When <command>NFiles</command> is set to a positive integer, pisg will
     process at most that much files from <command>LogDir</command>. Useful to
-    create statistics that cover the last week or month.
+    create statistics that cover the last week or month. Note that if multiple
+    <command>LogDir</command> options are provided, <command>NFiles</command>
+    referrs to the number of files which will be processed from each
+    <command>LogDir</command>
     </para>
     </refsect1>
     <refsect1>

+ 16 - 6
modules/Pisg.pm

@@ -91,7 +91,7 @@ sub get_default_config_settings
     $self->{cfg} = {
         channel => '',
         logtype => 'Logfile',
-        logfile => '',
+        logfile => [],
         format => 'mIRC',
         network => 'SomeIRCNet',
         outputfile => 'index.html',
@@ -103,7 +103,7 @@ sub get_default_config_settings
         imagepath => '',
         imageglobpath => '',
         defaultpic => '',
-        logdir => '',
+        logdir => [],
         nfiles => 0,
         lang => 'en',
         langfile => 'lang.txt',
@@ -352,14 +352,24 @@ sub init_config
             $self->{cfg}->{chan_done}{$self->{cfg}->{channel}} = 1; # don't parse channel in $self->{cfg}->{channel} if a channel statement is present
             while ($settings =~ s/\s([^=]+)=(["'])(.+?)\2//) {
                 my $var = lc($1);
-                $self->{chans}->{$channel}{$var} = $3;
+                if ($var eq "logdir" || $var eq "logfile") {
+                    push(@{$self->{chans}->{$channel}{$var}}, $3);
+                } else {
+                    $self->{chans}->{$channel}{$var} = $3;
+                }
             }
             while (<$fh>) {
                 last if ($_ =~ /<\/*channel>/i);
                 if ($_ =~ /^\s*(\w+)\s*=\s*(["'])(.+?)\2/) {
                     my $var = lc($1);
-                    unless ($self->{override_cfg}->{$var}) {
-                        $self->{chans}->{$channel}{$var} = $3;
+                    unless ((($var eq "logdir" || $var eq "logfile") && scalar(@{$self->{override_cfg}->{$var}}) > 0) || (($var ne "logdir" && $var ne "logfile") && $self->{override_cfg}->{$var})) {
+
+                        if($var eq "logdir" || $var eq "logfile") {
+                            push @{$self->{chans}->{$channel}{$var}}, $3;
+                        } else {
+                            $self->{chans}->{$channel}{$var} = $3;
+                        }
+
                     }
                 } elsif ($_ !~ /^$/) {
                     print STDERR "Warning: $self->{cfg}->{configfile}, line $.: Unrecognized line\n";
@@ -433,7 +443,7 @@ sub do_channel
     my $self = shift;
     if (!$self->{cfg}->{channel}) {
         print STDERR "No channels defined.\n";
-    } elsif ((!$self->{cfg}->{logfile}) && (!$self->{cfg}->{logdir})) {
+    } elsif ((!@{$self->{cfg}->{logfile}}) && (!@{$self->{cfg}->{logdir}})) {
         print STDERR "No logfile or logdir defined for " . $self->{cfg}->{channel} . "\n";
     } else {
         $self->init_pisg();        # Init some general things

+ 97 - 92
modules/Pisg/Parser/Logfile.pm

@@ -60,11 +60,10 @@ sub analyze
         $stats{parsedlines} = 0;
         $stats{totallines} = 0;
 
-        if ($self->{cfg}->{logdir}) {
+        if (scalar(@{$self->{cfg}->{logdir}}) > 0) {
             # Run through all files in dir
             $self->_parse_dir(\%stats, \%lines);
         } else {
-            # Run through the whole logfile
             my %state = (
                 linecount  => 0,
                 lastnick   => '',
@@ -72,7 +71,10 @@ sub analyze
                 lastnormal => '',
                 oldtime    => 24
             );
-            $self->_parse_file(\%stats, \%lines, $self->{cfg}->{logfile}, \%state);
+            foreach my $logfile (@{$self->{cfg}->{logfile}}) {
+                # Run through the whole logfile
+                $self->_parse_file(\%stats, \%lines, $logfile, \%state);
+            }
         }
 
         $self->_pick_random_lines(\%stats, \%lines);
@@ -105,108 +107,111 @@ sub _parse_dir
     my $self = shift;
     my ($stats, $lines) = @_;
 
-    # Add trailing slash when it's not there..
-    $self->{cfg}->{logdir} =~ s/([^\/])$/$1\//;
+    # Loop through each logdir we were given
+    foreach my $logdir (@{$self->{cfg}->{logdir}}) {
+        # Add trailing slash when it's not there..
+        $logdir =~ s/([^\/])$/$1\//;
 
-    unless ($self->{cfg}->{silent}) {
-        unless ($self->{cfg}->{nfiles} > 0) {
-            print "Going into $self->{cfg}->{logdir} and parsing all files there...\n\n"
-        } else {
-            print "Going into $self->{cfg}->{logdir} and parsing the last $self->{cfg}->{nfiles} file(s) there...\n\n"
+        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"
+            }
         }
-    }
-    my @filesarray;
-    opendir(LOGDIR, $self->{cfg}->{logdir}) or
-    die("Can't opendir $self->{cfg}->{logdir}: $!");
-    @filesarray = grep {
-        /^[^\.]/ && /^$self->{cfg}->{logprefix}/ && -f "$self->{cfg}->{logdir}/$_"
-    } readdir(LOGDIR) or
-    die("No files in \"$self->{cfg}->{logdir}\" matched prefix \"$self->{cfg}->{logprefix}\"");
-    closedir(LOGDIR);
-
-    my %state = (
-        lastnick   => '',
-        monocount  => 0,
-        oldtime    => 24
-    );
-    if ($self->{cfg}->{logsuffix} ne '') {
-        my @temparray;
-        my %months = (
-            'jan' => '0',
-            'feb' => '1',
-            'mar' => '2',
-            'apr' => '3',
-            'may' => '4',
-            'jun' => '5',
-            'jul' => '6',
-            'aug' => '7',
-            'sep' => '8',
-            'oct' => '9',
-            'nov' => '10',
-            'dec' => '11',
+        my @filesarray;
+        opendir(LOGDIR, $logdir) or
+        die("Can't opendir ${logdir}: $!");
+        @filesarray = grep {
+            /^[^\.]/ && /^$self->{cfg}->{logprefix}/ && -f "$logdir/$_"
+        } readdir(LOGDIR) or
+        die("No files in \"$logdir\" matched prefix \"$self->{cfg}->{logprefix}\"");
+        closedir(LOGDIR);
+
+        my %state = (
+            lastnick   => '',
+            monocount  => 0,
+            oldtime    => 24
         );
-        my ($mreg, $dreg, $yreg) = split(/\|\|/, $self->{cfg}->{logsuffix});
-        my (@month, @day, @year);
-        for my $file (@filesarray) {
-            LOOPSTART:
-            if ($file =~ /$mreg/) {
-                my $month = $1;
-                $month = lc $month;
-                $month = $months{$month}
-                    if (defined $months{$month});
-                push @month, $month;
-            } else {
-                splice(@filesarray,$#month + 1, 1);
-                if ($file = $filesarray[$#month + 1]) {
-                    goto LOOPSTART;
+        if ($self->{cfg}->{logsuffix} ne '') {
+            my @temparray;
+            my %months = (
+                'jan' => '0',
+                'feb' => '1',
+                'mar' => '2',
+                'apr' => '3',
+                'may' => '4',
+                'jun' => '5',
+                'jul' => '6',
+                'aug' => '7',
+                'sep' => '8',
+                'oct' => '9',
+                'nov' => '10',
+                'dec' => '11',
+            );
+            my ($mreg, $dreg, $yreg) = split(/\|\|/, $self->{cfg}->{logsuffix});
+            my (@month, @day, @year);
+            for my $file (@filesarray) {
+                LOOPSTART:
+                if ($file =~ /$mreg/) {
+                    my $month = $1;
+                    $month = lc $month;
+                    $month = $months{$month}
+                        if (defined $months{$month});
+                    push @month, $month;
                 } else {
-                    last;
+                    splice(@filesarray,$#month + 1, 1);
+                    if ($file = $filesarray[$#month + 1]) {
+                        goto LOOPSTART;
+                    } else {
+                        last;
+                    }
                 }
-            }
-            if ($file =~ /$dreg/) {
-                push @day, $1;
-            } else {
-                splice(@filesarray,$#day + 1, 1);
-                splice(@month,$#day + 1);
-                if ($file = $filesarray[$#day + 1]) {
-                    goto LOOPSTART;
+                if ($file =~ /$dreg/) {
+                    push @day, $1;
                 } else {
-                    last;
+                    splice(@filesarray,$#day + 1, 1);
+                    splice(@month,$#day + 1);
+                    if ($file = $filesarray[$#day + 1]) {
+                        goto LOOPSTART;
+                    } else {
+                        last;
+                    }
                 }
-            }
-            if ($file =~ /$yreg/) {
-                push @year, $1;
-            } else {
-                splice(@filesarray,$#year + 1, 1);
-                splice(@month,$#year + 1);
-                splice(@day,$#year + 1);
-                if ($file = $filesarray[$#year + 1]) {
-                    goto LOOPSTART;
+                if ($file =~ /$yreg/) {
+                    push @year, $1;
                 } else {
-                    last;
+                    splice(@filesarray,$#year + 1, 1);
+                    splice(@month,$#year + 1);
+                    splice(@day,$#year + 1);
+                    if ($file = $filesarray[$#year + 1]) {
+                        goto LOOPSTART;
+                    } else {
+                        last;
+                    }
                 }
             }
+            my @newarray = @filesarray[ sort {
+                                        $year[$a] <=> $year[$b]
+                                                ||
+                                        $month[$a] <=> $month[$b]
+                                                ||
+                                        $day[$a] <=> $day[$b]
+                                    } 0..$#filesarray ];
+            @filesarray = @newarray;
+        } else {
+            @filesarray = sort {lc($a) cmp lc($b)} @filesarray;
         }
-        my @newarray = @filesarray[ sort {
-                                    $year[$a] <=> $year[$b]
-                                               ||
-                                    $month[$a] <=> $month[$b]
-                                               ||
-                                    $day[$a] <=> $day[$b]
-                                 } 0..$#filesarray ];
-        @filesarray = @newarray;
-    } else {
-        @filesarray = sort {lc($a) cmp lc($b)} @filesarray;
-    }
 
-    if($self->{cfg}->{nfiles} > 0) {
-        my $shift = @filesarray - $self->{cfg}->{nfiles};
-        splice(@filesarray, 0, $shift);
-    }
+        if($self->{cfg}->{nfiles} > 0) {
+            my $shift = @filesarray - $self->{cfg}->{nfiles};
+            splice(@filesarray, 0, $shift);
+        }
 
-    foreach my $file (@filesarray) {
-        $file = $self->{cfg}->{logdir} . $file;
-        $self->_parse_file($stats, $lines, $file, \%state);
+        foreach my $file (@filesarray) {
+            $file = $logdir . $file;
+            $self->_parse_file($stats, $lines, $file, \%state);
+        }
     }
 }
 

+ 8 - 4
pisg

@@ -72,6 +72,8 @@ sub get_cmdline_options
 
     my %cfg = (
         modules_dir => "$script_dir/modules/",     # Module search path
+        logfile => [],
+        logdir => [],
     );
 
     # Commandline options
@@ -84,13 +86,15 @@ Usage: pisg [-ch channel] [-l logfile] [-o outputfile] [-ma maintainer]
 -ch --channel=xxx      : Set channel name
 -cc --cchannels=xxx    : Only do this channel from cfg file, give multiple
                          times to do multiple channels
--l  --logfile=xxx      : Log file to parse
+-l  --logfile=xxx      : Log file to parse, give multiple times to use
+                         multiple log files.
 -o  --outfile=xxx      : Name of HTML file to create
 -t  --tag=xxx          : Replace \%t in --outfile by xxx
 -ma --maintainer=xxx   : Channel/statistics maintainer
 -f  --format=xxx       : Logfile format [see FORMATS file]
 -n  --network=xxx      : IRC network for the channel
--d  --dir=xxx          : Analyze all files in this dir. Ignores logfile
+-d  --dir=xxx          : Analyze all files in this dir. Ignores logfile.
+                         Give multiple times to use multiple directories.
 -nf --nfiles=xxx       : Analyze the last xxx files if used with --dir
 -p  --prefix=xxx       : Analyse only files prefixed by xxx in dir
                          Only works with --dir
@@ -111,13 +115,13 @@ END_USAGE
 
     if (GetOptions('channel=s'    => \$cfg{channel},
                    'cchannels=s@'  => \@{ $cfg{cchannels} },
-                   'logfile=s'    => \$cfg{logfile},
+                   'logfile=s'    => \@{ $cfg{logfile} },
                    'format=s'     => \$cfg{format},
                    'network=s'    => \$cfg{network},
                    'maintainer=s' => \$cfg{maintainer},
                    'outfile=s'    => \$cfg{outputfile},
                    'tag=s'        => \$cfg{outputtag},
-                   'dir=s'        => \$cfg{logdir},
+                   'dir=s'        => \@{ $cfg{logdir} },
                    'nfiles=i'     => \$cfg{nfiles},
                    'prefix=s'     => \$cfg{logprefix},
                    'moduledir=s'  => \$cfg{moduledir},