pass arguments through to Listeners for processing with Getopt::Long
[lwes-perl.git] / lwes-perl-journal-listener
bloba094e8fb8ad1afd228acb912df75dea94115d341
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 Getopt::Long::Configure ("pass_through");
19 GetOptions
21 'help' => \$help_opt,
22 'man' => \$man_opt,
23 'stats' => \$stats_opt,
24 'verbose' => \$verbose_opt,
25 ) or pod2usage (2);
27 pod2usage (-exitval => -1, -verbose => 0) if $help_opt;
28 pod2usage (-exitval => -2, -verbose => 2) if $man_opt;
30 my $listener_or_code;
32 # if the first argument is a file, we'll assume it s a journal file, and
33 # use the PrintingListener
34 if (-f $ARGV[0])
36 $listener_or_code = "LWES::Listeners::EventPrintingListener";
38 else
40 $listener_or_code = shift @ARGV;
43 # determine the callback which will process each event
44 my $processEventFunc = getProcessEventFunc ($listener_or_code, \@ARGV);
46 foreach my $journal (@ARGV)
48 my $start = time();
49 my $log_date = strftime ("%a %b %e %H:%M:%S %Y", localtime ($start));
50 my $num_events = 0;
52 if ($stats_opt)
54 print "$log_date : $journal ";
57 if (-f $journal)
59 open FH, "zcat $journal |";
61 # read and parse header
62 my $header_bytes;
63 while ( read (FH, $header_bytes, 22) == 22)
65 my $header = bytesToHeader ($header_bytes);
67 # read and parse bytes
68 my $event_bytes;
69 my $n = read (FH, $event_bytes, $header->{'PayloadLength'});
70 if (defined ($n) && $n == $header->{'PayloadLength'})
72 my $event = bytesToEvent ($event_bytes);
74 # merge header into event
75 foreach my $h (keys %{$header})
77 unless (exists ($event->{$h}))
79 $event->{$h} = $header->{$h};
83 # call handler
84 $processEventFunc->($event);
85 $num_events++;
87 else
89 die "malformed or truncated journal";
93 close FH;
96 if ($stats_opt)
98 my $end = time()-$start;
99 print "had $num_events events processed in $end seconds\n";
105 __END__
107 =head1 NAME
109 lwes-perl-journal-listener - listens for events from a journal
111 =head1 SYNOPSIS
113 lwes-perl-journal-listener [options] [<listener> [<args>] | <code>] <journal(s)>
115 Options:
116 --stats Print information about each file processed
117 -help Brief help message
118 -man Full Documentation
120 <listener> is the name of a perl module which extends the base
121 listener LWES::Listener and provides an initialize and processEvent
122 method. The <args> are passed directly to the listener constructor.
124 code is perl code which is embedded in a function which takes one
125 argument, a reference to a perl hash which contains the contents
126 of the event called $event
128 =head1 OPTIONS
130 =over 8
132 =item B<--stats>
134 Print information about how many events were processed and how long it took
135 to process.
137 =item B<-help>
139 Print help information
141 =item B<-man>
143 Print more verbose help information
145 =back
147 =head1 DESCRIPTION
149 The lwes-perl-journal-listener is a tool for inspecting LWES events that
150 are in journal files created by the lwes-journaller.
152 There are several ways to use the tool. To just print out events as they
153 are seen it can be invoked as
155 % lwes-perl-journal-listener <journal>
157 Alternatively you can provide a LWES::Listener module as outlined in
158 the lwes-perl-event-listener man page.
160 =cut