Filename | /usr/local/lib/site_perl/Log/Filter/Set.pm |
Statements | Executed 28 statements in 1.03ms |
Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
---|---|---|---|---|---|
1 | 1 | 1 | 13µs | 19µs | BEGIN@2 | Log::Filter::Set::
1 | 1 | 1 | 8µs | 8µs | BEGIN@87 | Log::Filter::Set::
1 | 1 | 1 | 8µs | 42µs | BEGIN@79 | Log::Filter::Set::
1 | 1 | 1 | 7µs | 36µs | BEGIN@105 | Log::Filter::Set::
1 | 1 | 1 | 7µs | 12µs | BEGIN@3 | Log::Filter::Set::
1 | 1 | 1 | 7µs | 40µs | BEGIN@108 | Log::Filter::Set::
1 | 1 | 1 | 7µs | 32µs | BEGIN@106 | Log::Filter::Set::
1 | 1 | 1 | 6µs | 31µs | BEGIN@109 | Log::Filter::Set::
1 | 1 | 1 | 6µs | 33µs | BEGIN@88 | Log::Filter::Set::
1 | 1 | 1 | 6µs | 30µs | BEGIN@111 | Log::Filter::Set::
1 | 1 | 1 | 5µs | 29µs | BEGIN@112 | Log::Filter::Set::
1 | 1 | 1 | 4µs | 4µs | BEGIN@84 | Log::Filter::Set::
1 | 1 | 1 | 2µs | 2µs | END | Log::Filter::Set::
0 | 0 | 0 | 0s | 0s | _field_int | Log::Filter::Set::
0 | 0 | 0 | 0s | 0s | _init | Log::Filter::Set::
0 | 0 | 0 | 0s | 0s | accept | Log::Filter::Set::
Line | State ments |
Time on line |
Calls | Time in subs |
Code |
---|---|---|---|---|---|
1 | package Log::Filter::Set; | ||||
2 | 2 | 22µs | 2 | 25µs | # spent 19µs (13+6) within Log::Filter::Set::BEGIN@2 which was called:
# once (13µs+6µs) by Log::Loger::BEGIN@121 at line 2 # spent 19µs making 1 call to Log::Filter::Set::BEGIN@2
# spent 6µs making 1 call to strict::import |
3 | 2 | 59µs | 2 | 17µs | # spent 12µs (7+5) within Log::Filter::Set::BEGIN@3 which was called:
# once (7µs+5µs) by Log::Loger::BEGIN@121 at line 3 # spent 12µs making 1 call to Log::Filter::Set::BEGIN@3
# spent 5µs making 1 call to warnings::import |
4 | |||||
5 | ################################################################################ | ||||
6 | # | ||||
7 | # DOCUMENTATION SECTION | ||||
8 | # | ||||
9 | ################################################################################ | ||||
10 | |||||
11 | =head1 NAME | ||||
12 | |||||
13 | Log::Filter::Set - Test given message field for values from certain set | ||||
14 | |||||
15 | =head1 SYNOPSIS | ||||
16 | |||||
17 | use Log::Filter::Set; | ||||
18 | |||||
19 | my $filter = new Log::Filter::Set('source','module_a,module_b'); | ||||
20 | |||||
21 | my ($result, $continue) = $filter->accept($source, $severity, $message); | ||||
22 | |||||
23 | =head1 DESCRIPTION | ||||
24 | |||||
25 | This module accepts messages, whose given field`s value is in the given set | ||||
26 | of acceptable values. | ||||
27 | |||||
28 | There are two flags, that are altering the avaluation process: | ||||
29 | |||||
30 | i - Invert the result of the evaluation - | ||||
31 | ACCEPT => REJECT | ||||
32 | REJECT => ACCEPT | ||||
33 | r - By default, module`s evaluation is SUFFICIENT - | ||||
34 | ACCEPT => FINISH | ||||
35 | REJECT => CONTINUE | ||||
36 | By setting the flag 'r', module`s evaluation will be REQUIRED - | ||||
37 | ACCEPT => CONTINUE | ||||
38 | REJECT => FINISH | ||||
39 | |||||
40 | =head1 USAGE | ||||
41 | |||||
42 | =head1 BUGS | ||||
43 | |||||
44 | =head1 SUPPORT | ||||
45 | |||||
46 | =head1 AUTHOR | ||||
47 | |||||
48 | Jan Mach | ||||
49 | Cesnet, z.s.p.o | ||||
50 | jan.mach@cesnet.cz | ||||
51 | http://www.cesnet.cz | ||||
52 | |||||
53 | =head1 COPYRIGHT | ||||
54 | |||||
55 | This program is free software; you can redistribute | ||||
56 | it and/or modify it under the same terms as Perl itself. | ||||
57 | |||||
58 | The full text of the license can be found in the | ||||
59 | LICENSE file included with this module. | ||||
60 | |||||
61 | |||||
62 | =head1 SEE ALSO | ||||
63 | |||||
64 | perl(1). | ||||
65 | |||||
66 | =head1 FUNCTION REFERENCE | ||||
67 | |||||
68 | =over 4 | ||||
69 | |||||
70 | =cut | ||||
71 | |||||
72 | ################################################################################ | ||||
73 | # | ||||
74 | # INITIALIZATION AND CLEANUP SECTION | ||||
75 | # | ||||
76 | ################################################################################ | ||||
77 | |||||
78 | #-- Perl core modules ---------------------------------------------------------# | ||||
79 | 2 | 24µs | 2 | 76µs | # spent 42µs (8+34) within Log::Filter::Set::BEGIN@79 which was called:
# once (8µs+34µs) by Log::Loger::BEGIN@121 at line 79 # spent 42µs making 1 call to Log::Filter::Set::BEGIN@79
# spent 34µs making 1 call to Exporter::import |
80 | |||||
81 | #-- Perl CPAN modules ---------------------------------------------------------# | ||||
82 | |||||
83 | #-- Custom application modules ------------------------------------------------# | ||||
84 | 2 | 32µs | 1 | 4µs | # spent 4µs within Log::Filter::Set::BEGIN@84 which was called:
# once (4µs+0s) by Log::Loger::BEGIN@121 at line 84 # spent 4µs making 1 call to Log::Filter::Set::BEGIN@84 |
85 | |||||
86 | #-- Module initializations ----------------------------------------------------# | ||||
87 | # spent 8µs within Log::Filter::Set::BEGIN@87 which was called:
# once (8µs+0s) by Log::Loger::BEGIN@121 at line 91 | ||||
88 | 2 | 33µs | 2 | 60µs | # spent 33µs (6+27) within Log::Filter::Set::BEGIN@88 which was called:
# once (6µs+27µs) by Log::Loger::BEGIN@121 at line 88 # spent 33µs making 1 call to Log::Filter::Set::BEGIN@88
# spent 27µs making 1 call to vars::import |
89 | 2 | 8µs | $VERSION = '0.01'; | ||
90 | @ISA = ('Log::Filter::Module'); | ||||
91 | 1 | 41µs | 1 | 8µs | } # spent 8µs making 1 call to Log::Filter::Set::BEGIN@87 |
92 | |||||
93 | #-- Module clean-up code (global destructor) ----------------------------------# | ||||
94 | 1 | 2µs | # spent 2µs within Log::Filter::Set::END which was called:
# once (2µs+0s) by main::RUNTIME at line 0 of mentat.storage.mongo.pl | ||
95 | |||||
96 | } | ||||
97 | |||||
98 | ################################################################################ | ||||
99 | # | ||||
100 | # CONSTANTS AND GLOBAL VARIABLES DEFINITION SECTION | ||||
101 | # | ||||
102 | ################################################################################ | ||||
103 | |||||
104 | #-- Constants -----------------------------------------------------------------# | ||||
105 | 2 | 26µs | 2 | 65µs | # spent 36µs (7+29) within Log::Filter::Set::BEGIN@105 which was called:
# once (7µs+29µs) by Log::Loger::BEGIN@121 at line 105 # spent 36µs making 1 call to Log::Filter::Set::BEGIN@105
# spent 29µs making 1 call to constant::import |
106 | 2 | 31µs | 2 | 57µs | # spent 32µs (7+25) within Log::Filter::Set::BEGIN@106 which was called:
# once (7µs+25µs) by Log::Loger::BEGIN@121 at line 106 # spent 32µs making 1 call to Log::Filter::Set::BEGIN@106
# spent 25µs making 1 call to constant::import |
107 | |||||
108 | 2 | 32µs | 2 | 72µs | # spent 40µs (7+33) within Log::Filter::Set::BEGIN@108 which was called:
# once (7µs+33µs) by Log::Loger::BEGIN@121 at line 108 # spent 40µs making 1 call to Log::Filter::Set::BEGIN@108
# spent 33µs making 1 call to constant::import |
109 | 2 | 29µs | 2 | 56µs | # spent 31µs (6+25) within Log::Filter::Set::BEGIN@109 which was called:
# once (6µs+25µs) by Log::Loger::BEGIN@121 at line 109 # spent 31µs making 1 call to Log::Filter::Set::BEGIN@109
# spent 25µs making 1 call to constant::import |
110 | |||||
111 | 2 | 24µs | 2 | 54µs | # spent 30µs (6+24) within Log::Filter::Set::BEGIN@111 which was called:
# once (6µs+24µs) by Log::Loger::BEGIN@121 at line 111 # spent 30µs making 1 call to Log::Filter::Set::BEGIN@111
# spent 24µs making 1 call to constant::import |
112 | 2 | 656µs | 2 | 54µs | # spent 29µs (5+24) within Log::Filter::Set::BEGIN@112 which was called:
# once (5µs+24µs) by Log::Loger::BEGIN@121 at line 112 # spent 29µs making 1 call to Log::Filter::Set::BEGIN@112
# spent 24µs making 1 call to constant::import |
113 | |||||
114 | #-- Static public class variables (our) ---------------------------------------# | ||||
115 | |||||
116 | #-- Static protected class variables (my) -------------------------------------# | ||||
117 | |||||
118 | # Translation table from message fields to indexes | ||||
119 | 1 | 3µs | my %FIELD2INDEX = ( 'SOURCE' => 0, | ||
120 | 'SEVERITY' => 1, | ||||
121 | 'MESSAGE' => 2, | ||||
122 | ); | ||||
123 | # Translation table from indexes to message fields | ||||
124 | 1 | 1µs | my @INDEX2FIELD = ( 'SOURCE', | ||
125 | 'SEVERITY', | ||||
126 | 'MESSAGE', | ||||
127 | ); | ||||
128 | |||||
129 | ################################################################################ | ||||
130 | # | ||||
131 | # IMPLEMENTATION SECTION | ||||
132 | # | ||||
133 | ################################################################################ | ||||
134 | |||||
135 | =item accept($$$) [PUBLIC] | ||||
136 | |||||
137 | Usage : my ($result, $continue) = $filter->accept($source, $severity, $message); | ||||
138 | Purpose : Check, if the filter will accept the given message | ||||
139 | Returns : Array (Log::Filter::Module::ACCEPT|REJECT, Log::Filter::Module::FINISH|CONTINUE) | ||||
140 | Arguments : string $source - Name of the source of the message | ||||
141 | enum $severity - Severity in integer or string format (see Log::Core::Essentials for permited values) | ||||
142 | string $message - Message | ||||
143 | Throws : Croaks, if invoked on class, or if given invalid arguments | ||||
144 | Comments : | ||||
145 | See Also : | ||||
146 | |||||
147 | =cut | ||||
148 | |||||
149 | sub accept($$$) { | ||||
150 | my $self = shift; | ||||
151 | croak ((caller(0))[3] . ": instance method invoked on class") unless ref $self; | ||||
152 | my ($source, $severity, $message) = @_; | ||||
153 | croak ((caller(0))[3] . ": invalid arguments") unless (defined($source) and defined($severity) and defined($message)); | ||||
154 | |||||
155 | my @fields = @_; | ||||
156 | my $resolution; | ||||
157 | my $continue; | ||||
158 | |||||
159 | if ($self->{VALUES}->{@fields[$self->{FIELD}]}) | ||||
160 | { | ||||
161 | unless ($self->{INVERT}) | ||||
162 | { | ||||
163 | $resolution = ACCEPT; | ||||
164 | $continue = ($self->{MODE} eq SUFFICIENT) ? FINISH : CONTINUE; | ||||
165 | } | ||||
166 | else { | ||||
167 | $resolution = REJECT; | ||||
168 | $continue = ($self->{MODE} eq REQUIRED) ? FINISH : CONTINUE; | ||||
169 | } | ||||
170 | return ($resolution, $continue); | ||||
171 | } | ||||
172 | |||||
173 | unless ($self->{INVERT}) | ||||
174 | { | ||||
175 | $resolution = REJECT; | ||||
176 | $continue = ($self->{MODE} eq REQUIRED) ? FINISH : CONTINUE; | ||||
177 | } | ||||
178 | else { | ||||
179 | $resolution = ACCEPT; | ||||
180 | $continue = ($self->{MODE} eq SUFFICIENT) ? FINISH : CONTINUE; | ||||
181 | } | ||||
182 | return ($resolution, $continue); | ||||
183 | } | ||||
184 | |||||
185 | =item _init() [PROTECTED] | ||||
186 | |||||
187 | Usage : Used from constructor as follows: return $self->_init(@_); | ||||
188 | Purpose : Initialize newly created filter instance | ||||
189 | Returns : $self | ||||
190 | Arguments : enum $field - name of the field to test (source | severity | message) | ||||
191 | space separated string|array reference $value_set - set of acceptable values | ||||
192 | string $flags - flags separated with spaces | ||||
193 | i - invert the result | ||||
194 | r - required | ||||
195 | Throws : Croaks, if invoked on class or invalid arguments given | ||||
196 | Comments : Internally used by parent`s new() method | ||||
197 | See Also : | ||||
198 | |||||
199 | =cut | ||||
200 | |||||
201 | sub _init { | ||||
202 | my $self = shift; | ||||
203 | croak ((caller(0))[3] . ": instance method invoked on class") unless ref $self; | ||||
204 | my ($field, $values, $flags) = @_; | ||||
205 | croak ((caller(0))[3] . ": invalid arguments") unless ($field and $values); | ||||
206 | |||||
207 | # First try to parse and init mandatory arguments | ||||
208 | $self->{FIELD} = $self->_field_int($field); | ||||
209 | $self->{VALUES} = {}; | ||||
210 | my @vals; | ||||
211 | if(ref($values) eq 'ARRAY') { @vals = @{$values}; } | ||||
212 | else { @vals = split(/[,; ]+/, scalar $values); } | ||||
213 | foreach my $src (@vals) | ||||
214 | { | ||||
215 | $self->{VALUES}->{$src} = 1; | ||||
216 | } | ||||
217 | croak ((caller(0))[3] . ": invalid arguments") unless (defined($self->{FIELD}) and scalar keys(%{$self->{VALUES}})); | ||||
218 | |||||
219 | # Set the engine flags to default falues | ||||
220 | $self->{MODE} = SUFFICIENT; | ||||
221 | $self->{INVERT} = 0; | ||||
222 | |||||
223 | # Setup the aditional engine flags | ||||
224 | if ($flags) | ||||
225 | { | ||||
226 | foreach my $flag (split(/[,; ]+/, $flags)) | ||||
227 | { | ||||
228 | if ($flag =~ /^r$|^req$/) { $self->{MODE} = REQUIRED; } | ||||
229 | elsif ($flag =~ /^i$|^inv$/) { $self->{INVERT} = 1; } | ||||
230 | else { croak ((caller(0))[3] . ": unknown flag '$flag'"); } | ||||
231 | } | ||||
232 | } | ||||
233 | return $self; | ||||
234 | } | ||||
235 | |||||
236 | =item _field_int($) [PROTECTED] | ||||
237 | |||||
238 | Usage : $self->_field_int($field) | ||||
239 | Purpose : Internal method for converting log field to integer index | ||||
240 | Returns : integer index | ||||
241 | Arguments : enum $field - name of the field to translate (source | severity | message) | ||||
242 | Throws : Croaks, if invoked on class | ||||
243 | Comments : Should be used only internally, used from _init() method | ||||
244 | See Also : | ||||
245 | |||||
246 | =cut | ||||
247 | |||||
248 | sub _field_int { | ||||
249 | my $self = shift; | ||||
250 | croak ((caller(0))[3] . ": instance method invoked on class") unless ref $self; | ||||
251 | my $field = uc(shift); | ||||
252 | |||||
253 | # Return undefined value if field was not specified | ||||
254 | return undef unless (defined($field)); | ||||
255 | |||||
256 | # Perform conversion only if the argument is not a number | ||||
257 | if ($field =~ /^$|[^\d]+/) | ||||
258 | { | ||||
259 | $field = $FIELD2INDEX{uc($field)}; | ||||
260 | } | ||||
261 | |||||
262 | # Return undefined value if the integer is not a valid field value | ||||
263 | return undef unless (defined($field) and defined($INDEX2FIELD[$field])); | ||||
264 | |||||
265 | # Othervise return valid value | ||||
266 | return $field; | ||||
267 | } | ||||
268 | |||||
269 | |||||
270 | =pod | ||||
271 | |||||
272 | =back | ||||
273 | |||||
274 | =cut | ||||
275 | |||||
276 | 1 | 4µs | 1; |