Filename | /var/www/foswikidev/core/lib/Foswiki/Iterator/EventIterator.pm |
Statements | Executed 12 statements in 817µs |
Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
---|---|---|---|---|---|
1 | 1 | 1 | 14µs | 26µs | BEGIN@4 | Foswiki::Iterator::EventIterator::
1 | 1 | 1 | 14µs | 47µs | BEGIN@29 | Foswiki::Iterator::EventIterator::
1 | 1 | 1 | 13µs | 17µs | BEGIN@5 | Foswiki::Iterator::EventIterator::
1 | 1 | 1 | 10µs | 33µs | BEGIN@6 | Foswiki::Iterator::EventIterator::
1 | 1 | 1 | 4µs | 4µs | BEGIN@8 | Foswiki::Iterator::EventIterator::
0 | 0 | 0 | 0s | 0s | formatData | Foswiki::Iterator::EventIterator::
0 | 0 | 0 | 0s | 0s | hasNext | Foswiki::Iterator::EventIterator::
0 | 0 | 0 | 0s | 0s | new | Foswiki::Iterator::EventIterator::
0 | 0 | 0 | 0s | 0s | next | Foswiki::Iterator::EventIterator::
0 | 0 | 0 | 0s | 0s | snoopNext | Foswiki::Iterator::EventIterator::
Line | State ments |
Time on line |
Calls | Time in subs |
Code |
---|---|---|---|---|---|
1 | # See bottom of file for license and copyright information | ||||
2 | package Foswiki::Iterator::EventIterator; | ||||
3 | |||||
4 | 2 | 26µs | 2 | 38µs | # spent 26µs (14+12) within Foswiki::Iterator::EventIterator::BEGIN@4 which was called:
# once (14µs+12µs) by Foswiki::Logger::PlainFile::BEGIN@36 at line 4 # spent 26µs making 1 call to Foswiki::Iterator::EventIterator::BEGIN@4
# spent 12µs making 1 call to strict::import |
5 | 2 | 24µs | 2 | 20µs | # spent 17µs (13+4) within Foswiki::Iterator::EventIterator::BEGIN@5 which was called:
# once (13µs+4µs) by Foswiki::Logger::PlainFile::BEGIN@36 at line 5 # spent 17µs making 1 call to Foswiki::Iterator::EventIterator::BEGIN@5
# spent 4µs making 1 call to warnings::import |
6 | 2 | 49µs | 2 | 57µs | # spent 33µs (10+24) within Foswiki::Iterator::EventIterator::BEGIN@6 which was called:
# once (10µs+24µs) by Foswiki::Logger::PlainFile::BEGIN@36 at line 6 # spent 33µs making 1 call to Foswiki::Iterator::EventIterator::BEGIN@6
# spent 24µs making 1 call to Exporter::import |
7 | |||||
8 | # spent 4µs within Foswiki::Iterator::EventIterator::BEGIN@8 which was called:
# once (4µs+0s) by Foswiki::Logger::PlainFile::BEGIN@36 at line 13 | ||||
9 | 1 | 5µs | if ( $Foswiki::cfg{UseLocale} ) { | ||
10 | require locale; | ||||
11 | import locale(); | ||||
12 | } | ||||
13 | 1 | 51µs | 1 | 4µs | } # spent 4µs making 1 call to Foswiki::Iterator::EventIterator::BEGIN@8 |
14 | |||||
15 | =begin TML | ||||
16 | |||||
17 | ---++ =Foswiki::Iterator::EventIterator= | ||||
18 | Private subclass of LineIterator that | ||||
19 | * Selects log records that match the requested begin time and levels. | ||||
20 | * reasembles divided records into a single log record | ||||
21 | * splits the log record into fields | ||||
22 | |||||
23 | =cut | ||||
24 | |||||
25 | package Foswiki::Iterator::EventIterator; | ||||
26 | 1 | 66µs | require Foswiki::LineIterator; | ||
27 | 1 | 5µs | our @ISA = ('Foswiki::LineIterator'); | ||
28 | |||||
29 | 2 | 588µs | 2 | 81µs | # spent 47µs (14+34) within Foswiki::Iterator::EventIterator::BEGIN@29 which was called:
# once (14µs+34µs) by Foswiki::Logger::PlainFile::BEGIN@36 at line 29 # spent 47µs making 1 call to Foswiki::Iterator::EventIterator::BEGIN@29
# spent 34µs making 1 call to constant::import |
30 | |||||
31 | sub new { | ||||
32 | my ( $class, $fh, $threshold, $level, $version, $filename ) = @_; | ||||
33 | my $this = $class->SUPER::new($fh); | ||||
34 | $this->{_api} = $version; | ||||
35 | $this->{_threshold} = $threshold; | ||||
36 | $this->{_reqLevel} = $level; | ||||
37 | $this->{_filename} = $filename || 'n/a'; | ||||
38 | |||||
39 | # print STDERR "EventIterator created for $this->{_filename} \n"; | ||||
40 | return $this; | ||||
41 | } | ||||
42 | |||||
43 | =begin TML | ||||
44 | |||||
45 | ---+++ ObjectMethod hasNext() -> $boolean | ||||
46 | Reads records, reassembling them and skipping until a record qualifies per the requested time and levels. | ||||
47 | |||||
48 | The next matching record is parsed and saved into an instance variable until requested. | ||||
49 | |||||
50 | Returns true if a cached record is available. | ||||
51 | |||||
52 | =cut | ||||
53 | |||||
54 | sub hasNext { | ||||
55 | my $this = shift; | ||||
56 | return 1 if defined $this->{_nextEvent}; | ||||
57 | while ( $this->SUPER::hasNext() ) { | ||||
58 | my $ln = $this->SUPER::next(); | ||||
59 | |||||
60 | # Merge records until record ends in | | ||||
61 | while ( substr( $ln, -1 ) ne '|' && $this->SUPER::hasNext() ) { | ||||
62 | $ln .= "\n" . $this->SUPER::next(); | ||||
63 | } | ||||
64 | |||||
65 | my @line = split( /\s*\|\s*/, $ln ); | ||||
66 | shift @line; # skip the leading empty cell | ||||
67 | next unless scalar(@line) && defined $line[0]; | ||||
68 | |||||
69 | if ( | ||||
70 | $line[0] =~ s/\s+($this->{_reqLevel})\s*$// # test the level | ||||
71 | # accept a plain 'old' format date with no level only if reading info (statistics) | ||||
72 | || $line[0] =~ m/^\d{1,2} [a-z]{3} \d{4}/i | ||||
73 | && $this->{_reqLevel} =~ m/info/ | ||||
74 | ) | ||||
75 | { | ||||
76 | $this->{_level} = $1 || 'info'; | ||||
77 | $line[0] = Foswiki::Time::parseTime( $line[0] ); | ||||
78 | next | ||||
79 | unless ( defined $line[0] ); # Skip record if time doesn't decode. | ||||
80 | if ( $line[0] >= $this->{_threshold} ) { # test the time | ||||
81 | $this->{_nextEvent} = \@line; | ||||
82 | $this->{_nextParsed} = $this->formatData(); | ||||
83 | return 1; | ||||
84 | } | ||||
85 | } | ||||
86 | } | ||||
87 | return 0; | ||||
88 | } | ||||
89 | |||||
90 | =begin TML | ||||
91 | |||||
92 | ---+++ ObjectMethod snoopNext() -> $hashref | ||||
93 | Returns a hash of the fields in the next available record without | ||||
94 | moving the record pointer. (If the file has not yet been read, the hasNext() method is called, | ||||
95 | which will read the file until it finds a matching record. | ||||
96 | |||||
97 | =cut | ||||
98 | |||||
99 | sub snoopNext { | ||||
100 | my $this = shift; | ||||
101 | return $this->{_nextParsed}; # if defined $this->{_nextParsed}; | ||||
102 | #return undef unless $this->hasNext(); | ||||
103 | #return $this->{_nextParsed}; | ||||
104 | } | ||||
105 | |||||
106 | =begin TML | ||||
107 | |||||
108 | ---+++ ObjectMethod next() -> \$hash or @array | ||||
109 | Returns a hash, or an array of the fields in the next available record depending on the API version. | ||||
110 | |||||
111 | =cut | ||||
112 | |||||
113 | sub next { | ||||
114 | my $this = shift; | ||||
115 | undef $this->{_nextEvent}; | ||||
116 | return $this->{_nextParsed}[0] if $this->{_api}; | ||||
117 | return $this->{_nextParsed}[1]; | ||||
118 | } | ||||
119 | |||||
120 | =begin TML | ||||
121 | |||||
122 | ---++ PrivateMethod formatData($this) -> ( $hashRef, @array ) | ||||
123 | |||||
124 | Used by the EventIterator to assemble the read log record into a hash for the Version 1 | ||||
125 | interface, or the array returned for the original Version 0 interface. | ||||
126 | |||||
127 | =cut | ||||
128 | |||||
129 | sub formatData { | ||||
130 | my $this = shift; | ||||
131 | my $data = $this->{_nextEvent}; | ||||
132 | my %fhash; # returned hash of identified fields | ||||
133 | $fhash{level} = $this->{_level}; | ||||
134 | $fhash{filename} = $this->{_filename} | ||||
135 | if (TRACE); | ||||
136 | if ( $this->{_level} eq 'info' ) { | ||||
137 | $fhash{epoch} = @$data[0]; | ||||
138 | $fhash{user} = @$data[1]; | ||||
139 | $fhash{action} = @$data[2]; | ||||
140 | $fhash{webTopic} = @$data[3]; | ||||
141 | $fhash{extra} = @$data[4]; | ||||
142 | $fhash{remoteAddr} = @$data[5]; | ||||
143 | } | ||||
144 | elsif ( $this->{_level} =~ m/warning|error|critical|alert|emergency/ ) { | ||||
145 | $fhash{epoch} = @$data[0]; | ||||
146 | $fhash{extra} = join( ' ', @$data[ 1 .. $#$data ] ); | ||||
147 | } | ||||
148 | elsif ( $this->{_level} eq 'debug' ) { | ||||
149 | $fhash{epoch} = @$data[0]; | ||||
150 | $fhash{extra} = join( ' ', @$data[ 1 .. $#$data ] ); | ||||
151 | } | ||||
152 | |||||
153 | return ( | ||||
154 | [ | ||||
155 | \%fhash, | ||||
156 | |||||
157 | ( | ||||
158 | [ | ||||
159 | $fhash{epoch}, | ||||
160 | $fhash{user} || '', | ||||
161 | $fhash{action} || '', | ||||
162 | $fhash{webTopic} || '', | ||||
163 | $fhash{extra} || '', | ||||
164 | $fhash{remoteAddr} || '', | ||||
165 | $fhash{level}, | ||||
166 | ] | ||||
167 | ) | ||||
168 | ] | ||||
169 | ); | ||||
170 | } | ||||
171 | |||||
172 | 1 | 2µs | 1; | ||
173 | __END__ |