minor fixups
[lwes-perl.git] / lwes-perl-journal-listener
blob9827df9e7bbe1fdb141b456567a1f95d36195e77
1 #!/usr/bin/env perl
3 use strict;
4 use warnings;
6 use Getopt::Long;
7 use Pod::Usage;
8 use LWES::EventParser;
9 use LWES::Listener;
10 use POSIX qw(strftime);
12 my $help_opt = 0;
13 my $man_opt = 0;
14 my $verbose_opt = 0;
15 my $stats_opt = 0;
17 GetOptions
19 'help' => \$help_opt,
20 'man' => \$man_opt,
21 'stats' => \$stats_opt,
22 'verbose' => \$verbose_opt,
23 ) or pod2usage (2);
25 pod2usage (-exitval => -1, -verbose => 0) if $help_opt;
26 pod2usage (-exitval => -2, -verbose => 2) if $man_opt;
28 my $listener_or_code;
30 # if the first argument is a file, we'll assume it s a journal file, and
31 # use the PrintingListener
32 if (-f $ARGV[0])
34 $listener_or_code = "LWES::Listeners::EventPrintingListener";
36 else
38 $listener_or_code = shift @ARGV;
41 # determine the callback which will process each event
42 my $processEventFunc = getProcessEventFunc ($listener_or_code, @ARGV);
44 foreach my $journal (@ARGV)
46 my $start = time();
47 my $log_date = strftime ("%a %b %e %H:%M:%S %Y", localtime ($start));
48 my $num_events = 0;
50 if ($stats_opt)
52 print "$log_date : $journal ";
55 if (-f $journal)
57 open FH, "zcat $journal |";
59 # read and parse header
60 my $header_bytes;
61 while ( read (FH, $header_bytes, 22) == 22)
63 my $header = bytesToHeader ($header_bytes);
65 # read and parse bytes
66 my $event_bytes;
67 my $n = read (FH, $event_bytes, $header->{'PayloadLength'});
68 if (defined ($n) && $n == $header->{'PayloadLength'})
70 my $event = bytesToEvent ($event_bytes);
72 # merge header into event
73 foreach my $h (keys %{$header})
75 unless (exists ($event->{$h}))
77 $event->{$h} = $header->{$h};
81 # call handler
82 $processEventFunc->($event);
83 $num_events++;
85 else
87 die "malformed or truncated journal";
91 close FH;
94 if ($stats_opt)
96 my $end = time()-$start;
97 print "had $num_events events processed in $end seconds\n";