← Index
NYTProf Performance Profile   « line view »
For ./view
  Run on Fri Jul 31 18:42:36 2015
Reported on Fri Jul 31 18:48:15 2015

Filename/var/www/foswikidev/core/lib/Foswiki/Iterator/EventIterator.pm
StatementsExecuted 12 statements in 817µs
Subroutines
Calls P F Exclusive
Time
Inclusive
Time
Subroutine
11114µs26µsFoswiki::Iterator::EventIterator::::BEGIN@4Foswiki::Iterator::EventIterator::BEGIN@4
11114µs47µsFoswiki::Iterator::EventIterator::::BEGIN@29Foswiki::Iterator::EventIterator::BEGIN@29
11113µs17µsFoswiki::Iterator::EventIterator::::BEGIN@5Foswiki::Iterator::EventIterator::BEGIN@5
11110µs33µsFoswiki::Iterator::EventIterator::::BEGIN@6Foswiki::Iterator::EventIterator::BEGIN@6
1114µs4µsFoswiki::Iterator::EventIterator::::BEGIN@8Foswiki::Iterator::EventIterator::BEGIN@8
0000s0sFoswiki::Iterator::EventIterator::::formatDataFoswiki::Iterator::EventIterator::formatData
0000s0sFoswiki::Iterator::EventIterator::::hasNextFoswiki::Iterator::EventIterator::hasNext
0000s0sFoswiki::Iterator::EventIterator::::newFoswiki::Iterator::EventIterator::new
0000s0sFoswiki::Iterator::EventIterator::::nextFoswiki::Iterator::EventIterator::next
0000s0sFoswiki::Iterator::EventIterator::::snoopNextFoswiki::Iterator::EventIterator::snoopNext
Call graph for these subroutines as a Graphviz dot language file.
Line State
ments
Time
on line
Calls Time
in subs
Code
1# See bottom of file for license and copyright information
2package Foswiki::Iterator::EventIterator;
3
4226µs238µ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
use strict;
# spent 26µs making 1 call to Foswiki::Iterator::EventIterator::BEGIN@4 # spent 12µs making 1 call to strict::import
5224µs220µ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
use warnings;
# spent 17µs making 1 call to Foswiki::Iterator::EventIterator::BEGIN@5 # spent 4µs making 1 call to warnings::import
6249µs257µ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
use Assert;
# 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
BEGIN {
915µs if ( $Foswiki::cfg{UseLocale} ) {
10 require locale;
11 import locale();
12 }
13151µs14µs}
# spent 4µs making 1 call to Foswiki::Iterator::EventIterator::BEGIN@8
14
15=begin TML
16
17---++ =Foswiki::Iterator::EventIterator=
18Private 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
25package Foswiki::Iterator::EventIterator;
26166µsrequire Foswiki::LineIterator;
2715µsour @ISA = ('Foswiki::LineIterator');
28
292588µs281µ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
use constant TRACE => 0;
# spent 47µs making 1 call to Foswiki::Iterator::EventIterator::BEGIN@29 # spent 34µs making 1 call to constant::import
30
31sub 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
46Reads records, reassembling them and skipping until a record qualifies per the requested time and levels.
47
48The next matching record is parsed and saved into an instance variable until requested.
49
50Returns true if a cached record is available.
51
52=cut
53
54sub 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
93Returns a hash of the fields in the next available record without
94moving the record pointer. (If the file has not yet been read, the hasNext() method is called,
95which will read the file until it finds a matching record.
96
97=cut
98
99sub 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
109Returns a hash, or an array of the fields in the next available record depending on the API version.
110
111=cut
112
113sub 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
124Used by the EventIterator to assemble the read log record into a hash for the Version 1
125interface, or the array returned for the original Version 0 interface.
126
127=cut
128
129sub 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
17212µs1;
173__END__