Trace.pm

filter: Statementstmt 98.7% 235 / 238 Branchbran 88.6% 39 / 44 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 51 / 51 Podpod 100% 8 / 8 totaltotal 97.6% 333 / 341 CC 36 SCAR 13.4
CC
36
Cov
97%
new
11.1
_parse_args
9.0
__ANON__
5.0
CRAP 3.8
« Trace/Postfix.pm ↑ Summary
1 #!/usr/bin/perl
2  
3  
4 package Mail::Log::Trace;
5 {
6 =head1 NAME
7  
8 Mail::Log::Trace - Trace an email through the mailsystem logs.
9  
10 =head1 SYNOPSIS
11  
12 use Mail::Log::Trace;
13
14 my $tracer = Mail::Log::Trace::SUBCLASS->new({log_file => 'path/to/log'});
15 $tracer->set_message_id('message_id');
16 $tracer->find_message();
17 my $from_address = $tracer->get_from_address();
18
19 etc.
20  
21 =head1 DESCRIPTION
22  
23 This is the root-level class for a mail tracer: It allows you to search for
24 and find messages in maillogs. Accessors are provided for info common to
25 most maillogs: Specific subclasses may have further accessors depending on their
26 situation.
27  
28 Probably the two methods most commonly used (and sort of the point of this
29 module) are C<find_message> and C<find_message_info>. Both are simply stubs
30 for subclasses to implement: The first is defined to find the first (or first
31 from current location...) mention of the specified message in the log.
32 Depending on the log format that may or may not be the only mention, and there
33 may be information missing/incomplete at that point.
34  
35 C<find_message_info> should find I<all> information about a specific message
36 in the log. (Well, all information about a specific instance of the message:
37 If there are multiple messages that would match the info provided it must
38 find info on the first found.) That may mean searching through the log for
39 other information.
40  
41 If you just need to find if the message exists, use C<find_message>: it will
42 be faster (or at the least, the same speed. It should never be slower.)
43  
44 =head1 USAGE
45  
46 This is a an object-orientend module, with specific methods documented below.
47  
48 The string coersion is overloaded to return the class name, and the file
49 we are working with. Boolean currently checks to see if we were able to
50 open the file. (Which is kinda silly, as we'd throw an error if we couldn't.)
51  
52 All times are expected to be in Unix epoc-time format.
53  
54 =cut
55  
56 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
57 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
58 4 ▶use Scalar::Util qw(refaddr blessed reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
59 4 ▶use Mail::Log::Exceptions 1.0100;
Subroutine BEGIN: called CC=1 SCAR=0.0
60 4 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
61  
62 4 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
63 4 ▶ use Exporter ();
Subroutine BEGIN: called CC=1 SCAR=0.0
64 4 ▶ use vars qw($VERSION @EXPORT @EXPORT_OK %EXPORT_TAGS);
Subroutine BEGIN: called CC=1 SCAR=0.0
65 4 $VERSION = '1.0101';
66 #Give a hoot don't pollute, do not export more than needed by default
67 4 @EXPORT = qw();
68 4 @EXPORT_OK = qw();
69 4 %EXPORT_TAGS = ();
70 }
71  
72 #
73 # Define class variables. Note that they are hashes...
74 #
75  
76 4 my %message_info;
77 my %log_info;
78 4 my %message_raw_info;
79  
80 # Accessors.
81 4 my %public = ( from_address => undef,
82 message_id => undef,
83 recieved_time => undef,
84 sent_time => undef,
85 relay => undef,
86 subject => undef,
87 );
88 4 my %public_set_only = ();
89 4 my %public_get_only = ( connect_time => undef, disconnect_time => undef, delay => undef );
90 4 my %array_accessors = ( to_address => undef );
91 4 my @valid_parameters;
92 4 my @checked_parameters = qw(from_address message_id recieved_time sent_time relay
93 subject to_address);
94 4 my %all_setters;
95 my %all_getters;
96  
97 4 my @cleared_parameters = qw(from_address message_id recieved_time sent_time
98 relay to_address subject connect_time disconnect_time
99 delay);
100  
101 #
102 # DESTROY class variables.
103 #
104 ### IF NOT DONE THERE IS A MEMORY LEAK. ###
105  
106 38 ▶sub DESTROY {
Subroutine DESTROY: called CC=1 SCAR=0.0
107 38 my ($self) = @_;
108
109 38 delete $message_info{$$self};
110 38 delete $log_info{$$self};
111 38 delete $message_raw_info{$$self};
112 38 delete $all_setters{$$self};
113 38 delete $all_getters{$$self};
114
115 38 return;
116 }
117  
118 #
119 # Set the coercions to something useful.
120 #
121  
122 4 ▶use overload (
Subroutine BEGIN: called CC=1 SCAR=0.0
123 # Strings overload to the path and line number.
124 2 ▶ qw{""} => sub { my ($self) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
125 return blessed($self)
126 .' File: '
127 2 .$log_info{$$self}{filename};
128 },
129
130 # Boolean overloads to if we are usable. (Have a filehandle.)
131 2 ▶ qw{bool} => sub { my ($self) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
132 2 return defined($log_info{$$self}{log_parser});
133 },
134
135 # Numeric context just doesn't mean anything. Throw an error.
136 2 ▶ q{0+} => sub { Mail::Log::Exceptions->throw(q{Can't get a numeric value of a Mail::Log::Trace.} );
Subroutine __ANON__: called CC=1 SCAR=0.0
137 },
138
139 # Perl standard for everything else.
140 4 fallback => 1,
141 4 );
142  
143  
144 =head2 new (constructor)
145  
146 The base constructor for the Mail::Log::Trace classes. It takes inital values
147 for the following in a hash: C<from_address>, C<to_address>, C<message_id>,
148 C<log_file>. The only required value is the path to the logfile.
149  
150 use Mail::Log::Trace;
151 my $object = Mail::Log::Trace->new({ from_address => 'from@example.com',
152 to_address => 'to@example.com',
153 message_id => 'messg.id.string',
154 log_file => 'path/to/log',
155 ...
156 });
157  
158 =cut
159  
160 sub new
161 38 ▶{
Subroutine new: called CC=11 SCAR=24.1
Pod documented
162 38 my ($class, $parameters_ref) = @_;
163  
164 38 my $self = bless \do{my $anon}, $class;
165 38 $$self = refaddr $self;
166  
167 # Build accessors
168
169 # Get stuff from the any base classes.
170 38 my @public = $self->_requested_public_accessors();
171 38 my %public_special = $self->_requested_special_accessors();
172 38 my @public_set_only = $self->_requested_public_set_only();
173 38 my @public_get_only = $self->_requested_public_get_only();
174 38 my @array = $self->_requested_array_accessors();
175
176 38 @checked_parameters = ($self->_set_as_message_info(), @checked_parameters);
177 38 ▶ my %checked_parameters = map { $_ => undef if $_ ne ''; } @checked_parameters;
Branch: 1 branch
BranchTrueFalseTotal
if $_ ne "" 404 0 404
178 38 @checked_parameters = keys %checked_parameters;
179  
180 38 foreach my $item ( @public ) {
181 75 $public{$item} = undef;
182 }
183 38 foreach my $item ( @public_set_only ) {
184 0 $public_set_only{$item} = undef;
185 }
186 38 foreach my $item ( @public_get_only ) {
187 0 $public_get_only{$item} = undef;
188 }
189 38 foreach my $item ( @array ) {
190 0 $array_accessors{$item} = undef;
191 }
192
193 # Setters first.
194 38 my %merged_hash = (%public, %public_set_only, %public_special);
195 38 ▶ while ( my ($accessor, $action) = each %merged_hash ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %merged_hash) 328 38 366
196 328 $all_setters{$$self}{$accessor} = $self->_build_setter($accessor, 0, $action);
197 328 push @valid_parameters, $accessor;
198 }
199
200 # Now getters.
201 38 foreach my $accessor ( keys %public, keys %public_get_only, keys %public_special ) {
202 442 $all_getters{$$self}{$accessor} = $self->_build_getter($accessor);
203 }
204  
205 # Now build the private.
206 38 $all_setters{$$self}{$_} = $self->_build_setter($_, 1) foreach ( keys %public_get_only );
207 38 $all_getters{$$self}{$_} = $self->_build_getter($_, 1) foreach ( keys %public_set_only );
208  
209 # And the complex...
210 38 $self->_build_array_accessors($_) foreach ( keys %array_accessors );
211 38 push @valid_parameters, keys %array_accessors;
212  
213 # Get the list of parameters to clear when 'clear' is called.
214 38 my @requested_cleared = $self->_requested_cleared_parameters();
215 38 @requested_cleared = grep { defined($_) } @requested_cleared;
216 38 push @cleared_parameters, @requested_cleared;
217  
218 # Set up any/all passed parameters.
219 # (Only does message info. Note this can only be called after the above!)
220 38 $self->_parse_args($parameters_ref, 0);
221  
222 # Log info.
223 36 $self->set_log($parameters_ref->{log_file}); # Better to keep validation together.
224  
225 31 return $self;
226 }
227  
228 #
229 # The method factories.
230 #
231  
232 442 ▶sub _build_setter {
Subroutine _build_setter: called CC=3 SCAR=11.0
233 442 my ($self, $attribute, $private, $action) = @_;
234
235 # Build the correct name.
236 442 my $sub_name = "set_$attribute";
237 442 ▶ $sub_name = "_$sub_name" if $private;
Branch: 1 branch
BranchTrueFalseTotal
if $private 114 328 442
238
239 # The typeglob below sets off all kinds of warnings.
240 # (The 'redefine' is because this happens for _every_object_.)
241 4 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
242 4 ▶ no warnings qw(redefine);
Subroutine BEGIN: called CC=1 SCAR=0.0
243
244 # Build the actual subroutine.
245 442 ▶ if ( defined($action) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $action) { } 25 417 442
246 # If we do processing or validation, give it a chance to happen.
247 25 ▶ return *{blessed($self)."::$sub_name"} = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
248 4 ▶ use strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
249 13 my ($self, $new_id) = @_;
250
251 # True if they accept the value, false otherwise.
252 # (To make validation easier.)
253 13 $new_id = $action->($self, $new_id);
254 13 ▶ if ( $new_id ne '____INVALID__VALUE____' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($new_id ne '____INVALID__VALUE____') { } 11 2 13
255 11 $message_info{$$self}{$attribute} = $new_id;
256 }
257 else {
258 # If they don't accept the value, tell the user.
259 2 Mail::Log::Exceptions::InvalidParameter->throw("'$new_id' is not a valid value for $attribute.\n");
260 }
261 11 return;
262 }
263 25 }
264 else {
265 # For basic setters, use a speed-optimized version.
266 417 ▶ return *{blessed($self)."::$sub_name"} = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
267 757 $message_info{${$_[0]}}{$attribute} = $_[1];
268 757 return;
269 }
270 417 }
271 }
272  
273 442 ▶sub _build_getter {
Subroutine _build_getter: called CC=2 SCAR=6.9
274 442 my ($self, $attribute, $private) = @_;
275  
276 # Build the correct name.
277 442 my $sub_name = "get_$attribute";
278 442 ▶ $sub_name = "_$sub_name" if $private;
Branch: 1 branch
BranchTrueFalseTotal
if $private 0 442 442
279  
280 # The typeglob below sets off all kinds of warnings.
281 # (The 'redefine' is because this happens for _every_object_.)
282 4 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
283 4 ▶ no warnings qw(redefine);
Subroutine BEGIN: called CC=1 SCAR=0.0
284  
285 # Build the actual subroutine. (As fast as we can make it.)
286 442 ▶ return *{blessed($self)."::$sub_name"} = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
287 1631 return $message_info{${$_[0]}}{$attribute};
288 }
289 442 }
290  
291 38 ▶sub _build_array_accessors {
Subroutine _build_array_accessors: called CC=3 SCAR=11.0
292 38 my ($self, $attribute, $private) = @_;
293  
294 38 my $get_name = "get_$attribute";
295 38 my $set_name = "set_$attribute";
296 38 my $add_name = "add_$attribute";
297 38 my $remove_name = "remove_$attribute";
298  
299 38 foreach my $name ( ($get_name, $set_name, $add_name, $remove_name) ) {
300 152 ▶ $name = "_$name" if ( $private );
Branch: 1 branch
BranchTrueFalseTotal
if $private 0 152 152
301 152 $name = blessed($self)."::$name";
302 }
303  
304 4 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
305 4 ▶ no warnings qw(redefine);
Subroutine BEGIN: called CC=1 SCAR=0.0
306  
307 123 ▶ *$get_name = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
308 123 return $message_info{${$_[0]}}{$attribute};
309 38 };
310 38 $all_getters{$$self}{$attribute} = *$get_name;
311  
312 # Note that strict refs still aren't in effect.
313 # Needed for the call to $add_name below.
314 104 ▶ *$set_name = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
315 104 my ($self, $new_id) = @_;
316 104 ▶ if (defined($new_id) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $new_id) { } 101 3 104
317 101 @{$message_info{$$self}{$attribute}} = ();
318 101 $add_name->($self, $new_id);
319 }
320 else {
321 3 $message_info{$$self}->{$attribute} = undef;
322 }
323 104 return;
324 38 };
325 38 $all_setters{$$self}{$attribute} = *$set_name;
326  
327 191 ▶ *$add_name = sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
328 4 ▶ use strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
329 191 my ($self, $new_id) = @_;
330
331 # If we are given a single element, and we haven't seen it before,
332 # add it to the array.
333 191 ▶ if ( !defined(reftype($new_id)) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined builtin::reftype($new_id)) { } 178 13 191
elsif (builtin::reftype($new_id) eq 'ARRAY') { } 12 1 13
334 178 ▶ unless ( grep { $_ eq $new_id } @{$message_info{$$self}{$attribute}} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$_ eq $new_id;} @{$message_info{$$self}{$attribute};}) 170 8 178
335 170 push @{$message_info{$$self}{$attribute}}, ($new_id);
336 }
337 }
338 # If we are given an array of elements, merge it with our current array.
339 elsif ( reftype($new_id) eq 'ARRAY' ) {
340 12 my %temp_hash;
341 12 foreach my $element (@{$message_info{$$self}{$attribute}}, @{$new_id}) {
342 26 $temp_hash{$element} = undef;
343 }
344 12 @{$message_info{$$self}{$attribute}} = keys %temp_hash;
345 }
346 191 return;
347 38 };
348  
349 1 ▶ *$remove_name = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
350 1 my ($self, $id) = @_;
351 1 @{$message_info{$$self}{$attribute}}
352 1 = grep { $_ ne $id } @{$message_info{$$self}{$attribute}};
353 1 return;
354 38 };
355 }
356  
357 #
358 # Setters.
359 #
360  
361 =head2 SETTERS
362  
363 =head3 set_from_address
364  
365 Sets the from address of the message we are looking for.
366  
367 =head3 set_message_id
368  
369 Sets the message_id of the message we are looking for.
370 (Check with the specific parser class for what that means in a particular
371 log format.)
372  
373 =head3 set_recieved_time
374  
375 Sets the recieved time of the message we are looking for.
376 (The time this machine got the message.)
377  
378 =head3 set_sent_time
379  
380 Sets the sent time of the message we are looking for.
381 (The time this machine sent the message.)
382  
383 =head3 set_relay_host
384  
385 Sets the relay host of the message we are looking for. Commonly either
386 the relay we recieved it from, or the relay we sent it to. (Depending
387 on the logfile.)
388  
389 =head3 set_subject
390  
391 Sets the subject of the message we are looking for.
392  
393 =head3 set_parser_class
394  
395 Sets the parser class to use when searching the log file. A subclass will
396 have a 'default' parser that it will normally use: This is to allow easy
397 site-specific logfile formats based on more common formats. To use you
398 would subclass the default parser for the log file format of the base program
399 to handle the site's specific changes.
400  
401 Takes the name of a class as a string, and will throw an exception
402 (C<Mail::Log::Exceptions::InvalidParameter>) if that class name doesn't start
403 with Mail::Log::Parse.
404  
405 =cut
406  
407 6 ▶sub set_parser_class {
Subroutine set_parser_class: called CC=2 SCAR=6.9
Pod documented
408 6 my ($self, $new_id) = @_;
409 6 ▶ if ( $new_id =~ /Mail::Log::Parse::/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($new_id =~ /Mail::Log::Parse::/) { } 5 1 6
410 5 $log_info{$$self}{parser_class} = $new_id;
411 }
412 else {
413 1 Mail::Log::Exceptions::InvalidParameter->throw('Parser class needs to be a Mail::Log::Parse:: subclass.');
414 }
415 5 return;
416 }
417  
418 =head3 set_log
419  
420 Sets the log file we are searching throuh. Takes a full or relative path.
421 If it doesn't exist, or can't be read by the current user, it will throw an
422 exception. (C<Mail::Log::Exceptions::LogFile>) Note that it does I<not>
423 try to open it immedeately. That will be done at first attempt to read from
424 the logfile.
425  
426 =cut
427  
428 38 ▶sub set_log {
Subroutine set_log: called CC=4 SCAR=13.9
Pod documented
429 38 my ($self, $new_name) = @_;
430  
431 38 ▶ if ( ! defined($new_name) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $new_name) 1 37 38
432 1 Mail::Log::Exceptions::InvalidParameter->throw('No log file specified in call to '.blessed($self).'->new().');
433 }
434  
435 # Check to make sure the file exists,
436 # and then that we can read it, before accpeting the filename.
437 37 ▶ if ( -e $new_name ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $new_name) { } 35 2 37
438 35 ▶ if ( -r $new_name ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-r $new_name) { } 33 2 35
439 33 $log_info{refaddr $self}{'filename'} = $new_name;
440 }
441 else {
442 2 Mail::Log::Exceptions::LogFile->throw("Log file $new_name is not readable.");
443 }
444 }
445 else {
446 2 Mail::Log::Exceptions::LogFile->throw("Log file $new_name does not exist.");
447 }
448  
449 # Reset the parser.
450 33 $self->_set_log_parser(undef);
451  
452 33 return;
453 }
454  
455 =head3 set_to_address
456  
457 Sets the to address of the message we are looking for. Multiple addresses can
458 be specified, they will all be added, with duplicates skipped. This method
459 completely clears the array: there will be no addresses in the list except
460 those given to it. Duplicates will be consolidated: Only one of any particular
461 address will be in the final array.
462  
463 As a special case, passing C<undef> to this will set the array to undef.
464  
465 =head3 add_to_address
466  
467 Adds to the list of to addresses we are looking for. It does I<not> delete the
468 array first.
469  
470 Duplicates will be consolidated, so that the array will only have one of any
471 given address. (No matter the order they are given in.)
472  
473 =head3 remove_to_address
474  
475 Removes a single to address from the array.
476  
477 =cut
478  
479 #
480 # Getters.
481 #
482  
483 =head2 GETTERS
484  
485 =head3 get_from_address
486  
487 Gets the from address. (Either as set using the setter, or as found in the
488 log.)
489  
490 =head3 get_to_address
491  
492 Gets the to address array. (Either as set using the setters, or as found in the
493 log.)
494  
495 Will return a reference to an array, or 'undef' if the to address has not been
496 set/found.
497  
498 =head3 get_message_id
499  
500 Gets the message_id. (Either as set using the setter, or as found in the
501 log.)
502  
503 =head3 get_subject
504  
505 Gets the message subject. (Either as set using the setter, or as found in the
506 log.)
507  
508 =head3 get_recieved_time
509  
510 Gets the recieved time. (Either as set using the setter, or as found in the
511 log.)
512  
513 =head3 get_sent_time
514  
515 Gets the sent time. (Either as set using the setter, or as found in the
516 log.)
517  
518 =head3 get_relay_host
519  
520 Gets the relay host. (Either as set using the setter, or as found in the
521 log.)
522  
523 =head3 get_log
524  
525 Returns the path to the logfile we are reading.
526  
527 =cut
528  
529 28 ▶sub get_log {
Subroutine get_log: called CC=1 SCAR=0.0
Pod documented
530 28 my ($self) = @_;
531 28 return $log_info{$$self}{'filename'};
532 }
533  
534 =head3 get_connect_time
535  
536 Returns the time the remote host connected to this host to send the message.
537  
538 =head3 get_disconnect_time
539  
540 Returns the time the remote host disconnected from this host after sending
541 the message.
542  
543 =head3 get_delay
544  
545 Returns the total delay in this stage in processing the message.
546  
547 =head3 get_all_info
548  
549 Returns message info as returned from the parser, for more direct/complete
550 access.
551  
552 (It's probably a good idea to avoid using this, but it is useful and arguably
553 needed under certain circumstances.)
554  
555 =cut
556  
557 126 ▶sub get_all_info {
Subroutine get_all_info: called CC=1 SCAR=0.0
Pod documented
558 126 my ($self) = @_;
559 126 return $message_raw_info{$$self};
560 }
561  
562 #
563 # To be implemented by the sub-classes.
564 #
565  
566 =head2 Utility subroutines
567  
568 =head3 clear_message_info
569  
570 Clears I<all> known information on the current message, but not on the log.
571  
572 Use to start searching for a new message.
573  
574 =cut
575  
576 3 ▶sub clear_message_info {
Subroutine clear_message_info: called CC=3 SCAR=11.1
Pod documented
577 3 my ($self) = @_;
578  
579 3 foreach my $parameter ( @cleared_parameters ) {
580 66 ▶ $all_setters{$$self}{$parameter}->($self, undef) if defined($all_setters{$$self}{$parameter});
Branch: 1 branch
BranchTrueFalseTotal
if defined $all_setters{$$self}{$parameter} 66 0 66
581 }
582  
583 3 $self->_set_message_raw_info(undef);
584  
585 3 return;
586 }
587  
588 =head3 find_message
589  
590 Finds the first/next occurance of a message in the log. Can be passed any
591 of the above information in a hash format.
592  
593 Default is to search I<forward> in the log: If you have already done a search,
594 this will start searching where the previous search ended. To start over
595 at the beginning of the logfile, set C<from_start> as true in the parameter
596 hash.
597  
598 This method needs to be overridden by the subclass: by default it will throw
599 an C<Mail::Log::Exceptions::Unimplemented> error.
600  
601 =cut
602  
603 1 ▶sub find_message {
Subroutine find_message: called CC=1 SCAR=0.0
Pod documented
604 1 Mail::Log::Exceptions::Unimplemented->throw("Method 'find_message' needs to be implemented by subclass.\n");
605 # return 0; # Return false: The message couldn't be found. This will never be called.
606 }
607  
608 =head3 find_message_info
609  
610 Finds as much information as possible about a specific occurance of a message
611 in the logfile. Acts much the same as find_message, other than the fact that
612 once it finds a message it will do any searching necarry to find all information
613 on that message connection.
614  
615 (Also needs to be implemented by subclasses.)
616  
617 =cut
618  
619 1 ▶sub find_message_info {
Subroutine find_message_info: called CC=1 SCAR=0.0
Pod documented
620 1 Mail::Log::Exceptions::Unimplemented->throw("Method 'find_message_info' needs to be implemented by subclass.\n");
621 # return 0; # Return false: The message couldn't be found. This will never be called.
622 }
623  
624 =head1 SUBCLASSING
625  
626 There are two ways to subclass Mail::Log::Trace: The standard way, and the
627 automatic way. The old way is fairly straightforward: You create the accessors
628 for all the subclass-specific information, and overide C<find_message>,
629 C<find_message_info>, and C<_parse_args>. (Making sure for C<_parse_args> that
630 you call the SUPER version.)
631  
632 Or you can try to let Mail::Log::Trace do as much of that as possible, and only
633 do C<find_message> and C<find_message_info>.
634  
635 To do the latter, you need to override several of the following list of methods:
636  
637 _requested_public_accessors
638 _requested_public_set_only
639 _requested_public_get_only
640 _requested_array_accessors
641 _requested_special_accessors
642 _requested_cleared_parameters
643 _set_as_message_info
644  
645 That looks like a long list, but it is very rare that you'll need to override
646 all of them, and all they need to do is return a static list of keys that you
647 want the relevant action taken on.
648  
649 The first five build accessors for you, of the form C<get_$key>, C<set_$key>
650 for standard public, C<_get_$key> and C<_set_key> for private accessors (note
651 that if you request a private setter, you'll also get a I<public> getter, and
652 vice-versa), and C<get_$key>, C<set_$key>, C<add_$key> and C<remove_$key> for
653 keys which store arrays. All of these have been heavily optimised for speed.
654  
655 The last two set what keys are cleared when you call C<clear_message_info> and
656 what keys will be checked when C<_parse_args> is called. (If none of those are
657 present, an exception will be thrown, saying there is no message-specific data.)
658  
659 C<_requested_special_accessors> requires a little more discussion. Unlike the
660 rest, it expects not an array, but a hash (not a hashref: a hash). The keys of
661 the hash are the keys that will have accessors built for them (public, single,
662 only), and the values are code references to parsing/validation functions.
663  
664 An example:
665  
666 sub _requested_special_accessors {
667 return ( year => sub { my ($self, $year) = @_;
668 return '____INVALID__VALUE____' if $year < 1970;
669 my $maillog = $self->_get_log_parser();
670 if (defined($maillog)) {
671 $maillog->set_year($year);
672 }
673 return $year;
674 },
675 );
676 };
677  
678 The above is from L<Mail::Log::Trace::Postfix>, and is for the key 'year'.
679 The coderef in this case does both validation and some extra action. The action
680 is to call C<$self->_get_log_parser()->set_year()> on the year being passed.
681 (Because in this case the parser needs to have the year to return info
682 correctly.) The validation is to check to make sure the year is greater than
683 1970. (The birth of UNIX, so we are unlikey to handle any logs earlier than
684 that.) If it is not, the special value C<____INVALID__VALUE____> is returned.
685 This will cause an exception to be thrown. If the value is valid, it is
686 returned.
687  
688 The purpose of all the above is to allow subclasses to check values, do any
689 parsing that is needed, and to any other actions that may be needed. (This is
690 in contrast to the normal accessors, which just store the value given blindly.)
691  
692 Note that C<undef> should always be considered a valid value.
693  
694 Normally keys should be in the 'public_accessors' list: those accessors are much
695 faster.
696  
697 These accessors are built at I<run time>, when the object is first created.
698 This means object creation is fairly expensive.
699  
700 Of course, you still need to write C<find_message> and C<find_message_info>...
701  
702 Mail::Log::Trace is a cached inside-out object. If you don't know what that
703 means, you can probably ignore it. However if you need to store object state
704 data (and aren't using the convience accessors), it may be useful to know that
705 C<$$self == refaddr $self>.
706  
707 =cut
708  
709 #
710 # Private to be implemented by the sub-classes...
711 # (If needed.)
712 #
713  
714 13 ▶sub _requested_public_accessors { return (); };
Subroutine _requested_public_accessors: called CC=1 SCAR=0.0
715 38 ▶sub _requested_public_set_only { return (); };
Subroutine _requested_public_set_only: called CC=1 SCAR=0.0
716 38 ▶sub _requested_public_get_only { return (); };
Subroutine _requested_public_get_only: called CC=1 SCAR=0.0
717 38 ▶sub _requested_array_accessors { return (); };
Subroutine _requested_array_accessors: called CC=1 SCAR=0.0
718 13 ▶sub _requested_cleared_parameters { return (); };
Subroutine _requested_cleared_parameters: called CC=1 SCAR=0.0
719 13 ▶sub _requested_special_accessors { return (); };
Subroutine _requested_special_accessors: called CC=1 SCAR=0.0
720 13 ▶sub _set_as_message_info { return (); };
Subroutine _set_as_message_info: called CC=1 SCAR=0.0
721  
722 71 ▶sub _parse_args {
Subroutine _parse_args: called CC=9 SCAR=22.0
723 71 my ($self, $argref, $throw_error) = @_;
724
725 # It is possible for them to pass the message info here.
726 71 my %args;
727 71 foreach my $parameter ( @valid_parameters ) {
728 4198 ▶ $all_setters{$$self}{$parameter}->($self, $argref->{$parameter}) if exists $argref->{$parameter};
Branch: 1 branch
BranchTrueFalseTotal
if exists $argref->{$parameter} 439 3759 4198
729 }
730
731 # Not all parameters are checked...
732 70 foreach my $parameter ( @checked_parameters ) {
733 652 ▶ $args{$parameter} = $all_getters{$$self}{$parameter}->($self) if defined($all_setters{$$self}{$parameter});
Branch: 1 branch
BranchTrueFalseTotal
if defined $all_setters{$$self}{$parameter} 652 0 652
734 }
735 70 ▶ $args{from_start} = $argref->{from_start} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$argref->{'from_start'} ? : 1 69 70
736
737 # And log info.
738 70 ▶ $self->set_parser_class($argref->{parser_class}) if exists $argref->{parser_class};
Branch: 1 branch
BranchTrueFalseTotal
if exists $argref->{"parser_class"} 4 66 70
739
740 # Speed things up a bit, and make it easier to read.
741
742 69 ▶ if ($throw_error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($throw_error) 30 39 69
743 # If none are defined...
744 30 ▶ if ( (grep { defined($args{$_}) } keys %args) == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep({defined $args{$_};} keys %args) == 1) 1 29 30
745 1 Mail::Log::Exceptions::Message->throw("Warning: Trying to search for a message with no message-specific data.\n");
746 }
747 }
748  
749 68 return \%args;
750 }
751  
752 #
753 # Private functions/methods.
754 #
755  
756 =head1 UTILITY SUBROUTINES
757  
758 B<THESE ARE ONLY FOR USE BY SUBCLASSES>
759  
760 There are a few subroutines especially for use by subclasses.
761  
762 =head2 _set_message_raw_info
763  
764 Give this the raw message info, in whatever format the parser gives it. The
765 user should hopefully never want it, but just in case...
766  
767 =cut
768  
769 137 ▶sub _set_message_raw_info {
Subroutine _set_message_raw_info: called CC=1 SCAR=0.0
770 137 my ($self, $new_hash) = @_;
771 137 $message_raw_info{$$self} = $new_hash;
772 137 return;
773 }
774  
775 =head2 _set_log_parser
776  
777 Sets the log parser. Takes a reference to a parser object.
778  
779 =cut
780  
781 51 ▶sub _set_log_parser {
Subroutine _set_log_parser: called CC=1 SCAR=0.0
782 51 my ($self, $log_parser) = @_;
783 51 $log_info{$$self}->{log_parser} = $log_parser;
784 51 return;
785 }
786  
787 =head2 _get_log_parser
788  
789 Returns the log parser object.
790  
791 =cut
792  
793 40 ▶sub _get_log_parser {
Subroutine _get_log_parser: called CC=1 SCAR=0.0
794 40 my ($self) = @_;
795 40 return $log_info{$$self}->{log_parser};
796 }
797  
798 =head2 _get_parser_class
799  
800 Returns the name of the class the user wants you to use to parse the file.
801  
802 Please take it under advisement.
803  
804 =cut
805  
806 25 ▶sub _get_parser_class {
Subroutine _get_parser_class: called CC=1 SCAR=0.0
807 25 my ($self) = @_;
808 25 return $log_info{$$self}->{parser_class};
809 }
810  
811 =head1 BUGS
812  
813 None known at the moment... (I am nervious about the way I'm storing some of
814 these coderefs. So far I haven't run into problems, but I'm not entirely sure
815 there aren't any. If you start getting weird behaviour when using multiple
816 Mail::Log::Trace subclasses at once, please tell me.)
817  
818 =head1 REQUIRES
819  
820 L<Scalar::Util>, L<Mail::Log::Exceptions>.
821  
822 Some subclass, and probably a L<Mail::Log::Parse> class to be useful.
823  
824 =head1 HISTORY
825  
826 1.1.1 Feb 2, 2009 - Fixed a minor issue that could cause problems with multiple
827 subclass objects exisiting at the same time.
828  
829 1.1.0 Dec 23, 2008 - Major re-write to make subclassing easier. Or possibly
830 more confusing.
831  
832 1.00.03 Dec 5, 2208 - Licence clarification.
833  
834 1.00.02 Dec 2, 2008 - I really mean it this time.
835  
836 1.00.01 Dec 1, 2008 - Requirements fix, no code changes.
837  
838 1.00.00 Nov 28, 2008 - original version.
839  
840 =head1 AUTHOR
841  
842 Daniel T. Staal
843 CPAN ID: DSTAAL
844 dstaal@usa.net
845  
846 =head1 COPYRIGHT
847  
848 This program is free software; you can redistribute
849 it and/or modify it under the same terms as Perl itself.
850  
851 This copyright will expire in 30 years, or five years after the author's death,
852 whichever occurs last, at which time the code be released to the public domain.
853  
854 =cut
855  
856 #################### main pod documentation end ###################
857  
858 }
859 4 1;
860 # The preceding line will help the module return a true value
861  
« Trace/Postfix.pm ↑ Summary