blib/lib/POE/Component/SimpleLog.pm

filter: Statementstmt 11.4% 16 / 140 Branchbran 1.3% 1 / 74 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 38.4% 5 / 13 Podpod 0.0% 0 / 7 totaltotal 9.4% 22 / 234 CC 47 SCAR 48.0
CC
47
Cov
8%
UnRegister
240.0
Register
132.0
Log
132.0
CRAP 121.4
↑ Summary
1 # Declare our package
2 package POE::Component::SimpleLog;
3 1 ▶use strict; use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
4
5 # Initialize our version $LastChangedRevision: 16 $
6 1 our $VERSION = '1.05';
7
8 # Import what we need from the POE namespace
9 1 ▶use POE;
Subroutine BEGIN: called CC=1 SCAR=0.0
10
11 # Other miscellaneous modules we need
12 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13
14 # Set some constants
15 1 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=7.2
16 # Debug fun!
17 1 ▶ if ( ! defined &DEBUG ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined &POE::Component::SimpleLog::DEBUG) 1 0 1
18 1 eval "sub DEBUG () { 0 }";
19 }
20 }
21
22 # Set things in motion!
23 0 ▶sub new {
Subroutine new: not called CC=6 SCAR=37.4
Pod undocumented
24 # Get the OOP's type
25 0 my $type = shift;
26
27 # Sanity checking
28 0 ▶ if ( @_ & 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ & 1) 0 0 0
29 0 croak( 'POE::Component::SimpleLog->new needs even number of options' );
30 }
31
32 # The options hash
33 0 my %opt = @_;
34
35 # Our own options
36 0 my ( $ALIAS, $PRECISION );
37
38 # You could say I should do this: $Stuff = delete $opt{'Stuff'}
39 # But, that kind of behavior is not defined, so I would not trust it...
40
41 # Get the session alias
42 0 ▶ if ( exists $opt{'ALIAS'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opt{'ALIAS'}) { } 0 0 0
43 0 $ALIAS = $opt{'ALIAS'};
44 0 delete $opt{'ALIAS'};
45 } else {
46 # Debugging info...
47 0 if ( DEBUG ) {
48 warn 'Using default ALIAS = SimpleLog';
49 }
50
51 # Set the default
52 0 $ALIAS = 'SimpleLog';
53 }
54
55 # Get the precision
56 0 ▶ if ( exists $opt{'PRECISION'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opt{'PRECISION'}) { } 0 0 0
57 0 $PRECISION = $opt{'PRECISION'};
58 0 delete $opt{'PRECISION'};
59
60 # Check if it is defined
61 0 ▶ if ( defined $PRECISION ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $PRECISION) 0 0 0
62 # Use Time::HiRes
63 0 require Time::HiRes;
64 }
65 } else {
66 # Set it to regular
67 0 $PRECISION = undef;
68 }
69
70 # Anything left over is unrecognized
71 0 if ( DEBUG ) {
72 if ( keys %opt > 0 ) {
73 croak 'Unrecognized options were present in POE::Component::SimpleLog->new -> ' . join( ', ', keys %opt );
74 }
75 }
76
77 # Create a new session for ourself
78 POE::Session->create(
79 # Our subroutines
80 'inline_states' => {
81 # Maintenance events
82 '_start' => \&StartLog,
83 0 ▶ '_stop' => sub {},
Subroutine __ANON__: not called CC=1 SCAR=0.0
84
85 # Register a log
86 0 ▶ 'REGISTER' => \&Register,
Branch: 1 branch
BranchTrueFalseTotal
"POE::Session"->create("inline_states", {"_start", \&POE::Component::SimpleLog::StartLog, "_stop", sub { } , "REGISTER", \&POE::Component::SimpleLog::Register, "UNREGISTER", \&POE::Component::SimpleLog::UnRegister, "REMOVESESSION", \&POE::Component::SimpleLog::UnRegisterSession, "LOG", \&POE::Component::SimpleLog::Log, "SHUTDOWN", \&POE::Component::SimpleLog::StopLog}, "heap", {"LOGS", {}, "PRECISION", $PRECISION, "ALLLOGS", {}, "ALIAS", $ALIAS}) or die "Unable to create a new session!" 0 0 0
87
88 # Unregister a log
89 'UNREGISTER' => \&UnRegister,
90 'REMOVESESSION' => \&UnRegisterSession,
91
92 # LOG SOMETHING!
93 'LOG' => \&Log,
94
95 # We are done!
96 'SHUTDOWN' => \&StopLog,
97 },
98
99 # Set up the heap for ourself
100 'heap' => {
101 # The logging relation table
102 'LOGS' => {},
103
104 # Precision
105 'PRECISION' => $PRECISION,
106
107 # Who wants to get *ALL* logs?
108 'ALLLOGS' => {},
109
110 'ALIAS' => $ALIAS,
111 },
112 ) or die 'Unable to create a new session!';
113
114 # Return success
115 0 return 1;
116 }
117
118 # Registers a new log to watch
119 0 ▶sub Register {
Subroutine Register: not called CC=11 SCAR=48.8
Pod undocumented
120 # Get the arguments
121 0 my %args = @_[ ARG0 .. $#_ ];
122
123 # Validation - silently ignore errors
124 0 ▶ if ( ! defined $args{'LOGNAME'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args{"LOGNAME"}) 0 0 0
125 0 if ( DEBUG ) {
126 warn 'Did not get any arguments';
127 }
128 0 return undef;
129 }
130
131 0 ▶ if ( ! defined $args{'SESSION'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $args{'SESSION'}) { } 0 0 0
132 0 if ( DEBUG ) {
133 warn "Did not get a TargetSession for LogName: $args{'LOGNAME'}";
134 }
135 0 return undef;
136 } else {
137 # Convert actual POE::Session objects to their ID
138 0 ▶ if ( UNIVERSAL::isa( $args{'SESSION'}, 'POE::Session') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($args{"SESSION"}, "POE::Session")) 0 0 0
139 0 $args{'SESSION'} = $args{'SESSION'}->ID;
140 }
141 }
142
143 0 ▶ if ( ! defined $args{'EVENT'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args{"EVENT"}) 0 0 0
144 0 if ( DEBUG ) {
145 warn "Did not get an Event for LogName: $args{'LOGNAME'} -> Target Session: $args{'SESSION'}";
146 }
147 0 return undef;
148 }
149
150 # Check if we are registering an *ALL* logger or not
151 0 ▶ if ( $args{'LOGNAME'} eq 'ALL' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'LOGNAME'} eq 'ALL') { } 0 0 0
152 # Put this in ALL
153 0 ▶ if ( ! exists $_[HEAP]->{'ALLLOGS'}->{ $args{'SESSION'} } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $_[3]{"ALLLOGS"}{$args{"SESSION"}}) 0 0 0
154 0 $_[HEAP]->{'ALLLOGS'}->{ $args{'SESSION'} } = {};
155 }
156
157 # Put it in the hash!
158 0 ▶ if ( exists $_[HEAP]->{'ALLLOGS'}->{ $args{'SESSION'} }->{ $args{'EVENT'} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $_[3]{'ALLLOGS'}{$args{'SESSION'}}{$args{'EVENT'}}) { } 0 0 0
159 # Duplicate record...
160 0 if ( DEBUG ) {
161 warn "Tried to register a duplicate! -> LogName: $args{'LOGNAME'} -> Target Session: $args{'SESSION'} -> Event: $args{'EVENT'}";
162 }
163 } else {
164 0 $_[HEAP]->{'ALLLOGS'}->{ $args{'SESSION'} }->{ $args{'EVENT'} } = 1;
165 }
166 } else {
167 # Verify our data structure
168 0 ▶ if ( ! exists $_[HEAP]->{'LOGS'}->{ $args{'LOGNAME'} } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $_[3]{"LOGS"}{$args{"LOGNAME"}}) 0 0 0
169 0 $_[HEAP]->{'LOGS'}->{ $args{'LOGNAME'} } = {};
170 }
171
172 0 ▶ if ( ! exists $_[HEAP]->{'LOGS'}->{ $args{'LOGNAME'} }->{ $args{'SESSION'} } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $_[3]{"LOGS"}{$args{"LOGNAME"}}{$args{"SESSION"}}) 0 0 0
173 0 $_[HEAP]->{'LOGS'}->{ $args{'LOGNAME'} }->{ $args{'SESSION'} } = {};
174 }
175
176 # Finally put it in the hash :)
177 0 ▶ if ( exists $_[HEAP]->{'LOGS'}->{ $args{'LOGNAME'} }->{ $args{'SESSION'} }->{ $args{'EVENT'} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $_[3]{'LOGS'}{$args{'LOGNAME'}}{$args{'SESSION'}}{$args{'EVENT'}}) { } 0 0 0
178 # Duplicate record...
179 0 if ( DEBUG ) {
180 warn "Tried to register a duplicate! -> LogName: $args{'LOGNAME'} -> Target Session: $args{'SESSION'} -> Event: $args{'EVENT'}";
181 }
182 } else {
183 0 $_[HEAP]->{'LOGS'}->{ $args{'LOGNAME'} }->{ $args{'SESSION'} }->{ $args{'EVENT'} } = 1;
184 }
185 }
186
187 # All done!
188 0 return 1;
189 }
190
191 # Delete a watcher
192 0 ▶sub UnRegister {
Subroutine UnRegister: not called CC=15 SCAR=54.8
Pod undocumented
193 # Get the arguments
194 0 my %args = @_[ ARG0 .. $#_ ];
195
196 # Validation - silently ignore errors
197 0 ▶ if ( ! defined $args{'LOGNAME'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args{"LOGNAME"}) 0 0 0
198 0 if ( DEBUG ) {
199 warn 'Did not get any arguments';
200 }
201 0 return undef;
202 }
203
204 0 ▶ if ( ! defined $args{'SESSION'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $args{'SESSION'}) { } 0 0 0
205 0 if ( DEBUG ) {
206 warn "Did not get a TargetSession for LogName: $args{'LOGNAME'}";
207 }
208 0 return undef;
209 } else {
210 # Convert actual POE::Session objects to their ID
211 0 ▶ if ( UNIVERSAL::isa( $args{'SESSION'}, 'POE::Session') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($args{"SESSION"}, "POE::Session")) 0 0 0
212 0 $args{'SESSION'} = $args{'SESSION'}->ID;
213 }
214 }
215
216 0 ▶ if ( ! defined $args{'EVENT'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args{"EVENT"}) 0 0 0
217 0 if ( DEBUG ) {
218 warn "Did not get an Event for LogName: $args{'LOGNAME'} -> Target Session: $args{'SESSION'}";
219 }
220 0 return undef;
221 }
222
223 # Check if this is the special *ALL* log
224 0 ▶ if ( $args{'LOGNAME'} eq 'ALL' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'LOGNAME'} eq 'ALL') { } 0 0 0
225 # Scan it for targetsession
226 0 ▶ if ( exists $_[HEAP]->{'ALLLOGS'}->{ $args{'SESSION'} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $_[3]{"ALLLOGS"}{$args{"SESSION"}}) 0 0 0
227 # Scan for the proper event!
228 0 foreach my $evnt ( keys %{ $_[HEAP]->{'ALLLOGS'}->{ $args{'SESSION'} } } ) {
229 0 ▶ if ( $evnt eq $args{'EVENT'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($evnt eq $args{"EVENT"}) 0 0 0
230 # Found a match, delete it!
231 0 delete $_[HEAP]->{'ALLLOGS'}->{ $args{'SESSION'} }->{ $evnt };
232 0 ▶ if ( scalar keys %{ $_[HEAP]->{'ALLLOGS'}->{ $args{'SESSION'} } } == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %{$_[3]{"ALLLOGS"}{$args{"SESSION"}};} == 0) 0 0 0
233 0 delete $_[HEAP]->{'ALLLOGS'}->{ $args{'SESSION'} };
234 }
235
236 # Return success
237 0 return 1;
238 }
239 }
240 }
241 } else {
242 # Search through the logs for this specific one
243 0 ▶ if ( exists $_[HEAP]->{'LOGS'}->{ $args{'LOGNAME'} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $_[3]{"LOGS"}{$args{"LOGNAME"}}) 0 0 0
244 # Scan it for targetsession
245 0 ▶ if ( exists $_[HEAP]->{'LOGS'}->{ $args{'LOGNAME'} }->{ $args{'SESSION'} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $_[3]{"LOGS"}{$args{"LOGNAME"}}{$args{"SESSION"}}) 0 0 0
246 # Scan for the proper event!
247 0 foreach my $evnt ( keys %{ $_[HEAP]->{'LOGS'}->{ $args{'LOGNAME'} }->{ $args{'SESSION'} } } ) {
248 0 ▶ if ( $evnt eq $args{'EVENT'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($evnt eq $args{"EVENT"}) 0 0 0
249 # Found a match, delete it!
250 0 delete $_[HEAP]->{'LOGS'}->{ $args{'LOGNAME'} }->{ $args{'SESSION'} }->{ $evnt };
251 0 ▶ if ( scalar keys %{ $_[HEAP]->{'LOGS'}->{ $args{'LOGNAME'} }->{ $args{'SESSION'} } } == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %{$_[3]{"LOGS"}{$args{"LOGNAME"}}{$args{"SESSION"}};} == 0) 0 0 0
252 0 delete $_[HEAP]->{'LOGS'}->{ $args{'LOGNAME'} }->{ $args{'SESSION'} };
253 }
254
255 # Return success
256 0 return 1;
257 }
258 }
259 }
260 }
261 }
262
263 # Found nothing...
264 0 return undef;
265 }
266
267 # UnRegisters a whole session
268 0 ▶sub UnRegisterSession {
Subroutine UnRegisterSession: not called CC=7 SCAR=40.3
Pod undocumented
269 # ARG0 = Session ID
270 0 my $TargetSession = $_[ARG0];
271
272 # Validation
273 0 ▶ if ( ! defined $TargetSession ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $TargetSession) 0 0 0
274 # Hmpf
275 0 if ( DEBUG ) {
276 warn 'Did not get any arguments!';
277 }
278 }
279
280 # Go through all of the logs, searching for this session
281 0 foreach my $logname ( keys %{ $_[HEAP]->{'LOGS'} } ) {
282 # Another loop!
283 0 foreach my $session ( keys %{ $_[HEAP]->{'LOGS'}->{ $logname } } ) {
284 # Check if they match
285 0 ▶ if ( $session eq $TargetSession ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($session eq $TargetSession) 0 0 0
286 # Remove this!
287 0 delete $_[HEAP]->{'LOGS'}->{ $logname }->{ $TargetSession };
288 }
289 }
290 }
291
292 # Go through the *ALL* logs
293 0 foreach my $session ( keys %{ $_[HEAP]->{'ALLLOGS'} } ) {
294 # Check if they match
295 0 ▶ if ( $session eq $TargetSession ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($session eq $TargetSession) 0 0 0
296 # Remove this!
297 0 delete $_[HEAP]->{'ALLLOGS'}->{ $TargetSession };
298 }
299 }
300
301 # All done!
302 0 return 1;
303 }
304
305 # The core part of this module :)
306 0 ▶sub Log {
Subroutine Log: not called CC=11 SCAR=48.8
Pod undocumented
307 # ARG0 = LogName, ARG1 = Message
308 0 my( $logname, $message ) = @_[ ARG0, ARG1 ];
309
310 # Check if this is an *ALL* log...
311 0 ▶ if ( $logname eq 'ALL' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($logname eq "ALL") 0 0 0
312 # Should not do this!
313 0 if ( DEBUG ) {
314 warn 'Sending a log named ALL is not allowed, read the documentation';
315 }
316 }
317
318 # Figure out the time
319 0 my $time;
320 0 ▶ if ( defined $_[HEAP]->{'PRECISION'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[3]{'PRECISION'}) { } 0 0 0
321 0 $time = [ Time::HiRes::gettimeofday ];
322 } else {
323 0 $time = time();
324 }
325
326 # Search for this log!
327 0 ▶ if ( exists $_[HEAP]->{'LOGS'}->{ $logname } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $_[3]{'LOGS'}{$logname}) { } 0 0 0
328 # Okay, loop over each targetsession, checking if it is valid
329 0 foreach my $TargetSession ( keys %{ $_[HEAP]->{'LOGS'}->{ $logname } } ) {
330 # Find out if this session exists
331 0 ▶ if ( ! $_[KERNEL]->ID_id_to_session( $TargetSession ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $_[2]->ID_id_to_session($TargetSession)) { } 0 0 0
332 # Argh...
333 0 if ( DEBUG ) {
334 warn "TargetSession ID $TargetSession does not exist";
335 }
336 } else {
337 # Fire off all the events
338 0 foreach my $event ( keys %{ $_[HEAP]->{'LOGS'}->{ $logname }->{ $TargetSession } } ) {
339 # We call events with 5 arguments
340 # ARG0 -> CALLER_FILE
341 # ARG1 -> CALLER_LINE
342 # ARG2 -> Time::HiRes [ gettimeofday ] or plain time()
343 # ARG3 -> LOGNAME
344 # ARG4 -> Message
345 0 $_[KERNEL]->post( $TargetSession,
346 $event,
347 $_[CALLER_FILE],
348 $_[CALLER_LINE],
349 $time,
350 $logname,
351 $message,
352 );
353 }
354 }
355 }
356 } else {
357 # Check if we have any *ALL* handlers
358 0 ▶ if ( keys %{ $_[HEAP]->{'ALLLOGS'} } > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %{$_[3]{'ALLLOGS'};} > 0) { } 0 0 0
359 # Oh boy, send the log!
360 0 foreach my $TargetSession ( keys %{ $_[HEAP]->{'ALLLOGS'} } ) {
361 # Find out if this session exists
362 0 ▶ if ( ! $_[KERNEL]->ID_id_to_session( $TargetSession ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $_[2]->ID_id_to_session($TargetSession)) { } 0 0 0
363 # Argh...
364 0 if ( DEBUG ) {
365 warn "TargetSession ID $TargetSession does not exist";
366 }
367 } else {
368 # Get all the events
369 0 foreach my $event ( keys %{ $_[HEAP]->{'ALLLOGS'}->{ $TargetSession } } ) {
370 # We call events with 5 arguments
371 # ARG0 -> CALLER_FILE
372 # ARG1 -> CALLER_LINE
373 # ARG2 -> Time::HiRes [ gettimeofday ] or plain time()
374 # ARG3 -> LOGNAME
375 # ARG4 -> Message
376 0 $_[KERNEL]->post( $TargetSession,
377 $event,
378 $_[CALLER_FILE],
379 $_[CALLER_LINE],
380 $time,
381 $logname,
382 $message,
383 );
384 }
385 }
386 }
387 } else {
388 # Ignore this logname
389 0 if ( DEBUG ) {
390 warn "Got a LogName: $logname -> Ignoring it because it is not registered";
391 }
392 }
393 }
394
395 # All done!
396 0 return 1;
397 }
398
399 # Starts the logger!
400 0 ▶sub StartLog {
Subroutine StartLog: not called CC=1 SCAR=6.9
Pod undocumented
401 # Create an alias for ourself
402 0 $_[KERNEL]->alias_set( $_[HEAP]->{'ALIAS'} );
403
404 # All done!
405 0 return 1;
406 }
407
408 # Stops the logger
409 0 ▶sub StopLog {
Subroutine StopLog: not called CC=1 SCAR=6.9
Pod undocumented
410 # Remove our alias
411 0 $_[KERNEL]->alias_remove( $_[HEAP]->{'ALIAS'} );
412
413 # Clear our data
414 0 delete $_[HEAP]->{'LOGS'};
415 0 delete $_[HEAP]->{'ALLLOGS'};
416
417 # All done!
418 0 return 1;
419 }
420
421 # End of module
422 1 1;
423
424 __END__
↑ Summary