Delegate.pm

filter: Statementstmt 31.4% 11 / 35 Branchbran 0.0% 0 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 30.0% 3 / 10 Podpod 100% 4 / 4 totaltotal 30.5% 18 / 59 CC 6 SCAR 16.9
CC
6
Cov
24%
test
12.0
warn
6.0
new
6.0
CRAP 5.4
« Notepad.pm ↑ Summary Delegate/Stderr.pm »
1 #============================== Delegate.pm ==================================
2 # Filename: Delegate.pm
3 # Description: Abstract Superclass for Logger Delegates.
4 # Original Author: Dale M. Amon
5 # Revised by: $Author: amon $
6 # Date: $Date: 2008-08-28 23:20:19 $
7 # Version: $Revision: 1.8 $
8 # License: LGPL 2.1, Perl Artistic or BSD
9 #
10 #=============================================================================
11 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Fault::Msg;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 package Fault::Delegate;
15 1 ▶use vars qw{@ISA};
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 @ISA = qw( UNIVERSAL );
17  
18 #=============================================================================
19 # INTERNAL METHODS
20 #=============================================================================
21 # Subclass may override.
22  
23 0 ▶sub _write ($) {0;}
Subroutine _write: not called CC=1 SCAR=6.9
24 0 ▶sub _connect ($) {1;}
Subroutine _connect: not called CC=1 SCAR=6.9
25 0 ▶sub _disconnect ($) {1;}
Subroutine _disconnect: not called CC=1 SCAR=6.9
26  
27 #-----------------------------------------------------------------------------
28 # Subclass may use.
29  
30 0 ▶sub test ($) {
Subroutine test: not called CC=3 SCAR=24.8
Pod documented
31 0 my $s = shift;
32 0 my $c = ref $s;
33 0 my $v = 0;
34  
35 0 ▶ if ($s->_connect) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s->_connect) 0 0 0
36 0 $v = $s->log(Fault::Msg->new("Initialized $c logger",'NOTE','info'));
37 }
38 0 ▶ if (!$v) {$s->warn("Failed to fully initialize $c logger");}
Branch: 1 branch
BranchTrueFalseTotal
unless ($v) 0 0 0
39 0 $s->_disconnect;
40 0 return $v;
41 }
42  
43 #-----------------------------------------------------------------------------
44 # Subclass may use.
45  
46 0 ▶sub warn ($$) {
Subroutine warn: not called CC=2 SCAR=17.9
Pod documented
47 0 my ($s,$m) = @_;
48 0 ▶ my $p = (defined $::PROCESS_NAME) ? $::PROCESS_NAME : $0;
Branch: 1 branch
BranchTrueFalseTotal
defined $main::PROCESS_NAME ? : 0 0 0
49 0 Fault::ErrorHandler->warn ("$p: $m\n");
50 }
51  
52 #=============================================================================
53 # CLASS METHODS
54 #=============================================================================
55 # Subclass may override.
56  
57 0 ▶sub new ($) {
Subroutine new: not called CC=2 SCAR=17.9
Pod documented
58 0 my $class = shift;
59 0 my $s = bless {},$class;
60  
61 0 ▶ return ($s->test) ? $s : undef;
Branch: 1 branch
BranchTrueFalseTotal
$s->test ? : 0 0 0
62 }
63  
64 #=============================================================================
65 # SUBCLASS MAY OVERRIDE
66 #=============================================================================
67 # Subclass may override.
68  
69 0 ▶sub log ($$) {
Subroutine log: not called CC=2 SCAR=17.9
Pod documented
70 0 my ($self,$msg) = @_;
71 0 my $val = 0;
72  
73 0 ▶ if ($self->_connect) {$val = $self->_write ($msg);}
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_connect) 0 0 0
74 0 $self->_disconnect;
75 0 return $val;
76 }
77
78 #=============================================================================
79 # POD DOCUMENTATION
80 #=============================================================================
81 # You may extract and format the documention section with the 'perldoc' cmd.
82  
83 =head1 NAME
84  
85 Fault::Delegate - Abstract superclass of all Delegates.
86  
87 =head1 SYNOPSIS
88  
89 use Fault::Delegate::MyDelegate;
90 $self = Fault::Delegate::MyDelegate->new;
91 $bool = $self->log($msg);
92  
93 $bool = $self->test;
94 $self->warn ("A warning message");
95  
96 =head1 Inheritance
97  
98 UNIVERSAL
99 Fault::Delegate
100  
101 =head1 Description
102  
103 This is an abstract superclass from which Logger delegate subclasses may
104 inherit common code.
105  
106 A Logger delegate manages a a logging connection of some sort. It is not
107 used directly by a user; it is passed to Fault::logger for use as a proxy
108 by which output messages are printed, syslogged, placed in a database, sent
109 to a web site or whatever else the delegate does.
110  
111 By doing it this way, the logging behavior of the system may be changed at any
112 time simply by changing the delegate. This could even be done
113 dynamically, switching where log messages go at runtime.
114  
115 =head1 Delegate Protocol
116  
117 A delegate must at the very least impliment the log method to be considered
118 a delegate. Beyond that, if it is to impliment the fault protocol, it should
119 at least impliment the trans01 and trans10 methods and should if possible
120 also impliment the initfaults method.
121  
122 If the delegate has any ivars of its own and must do any special
123 initialization it will also need to override or inherit from the new
124 class method. The subclass is responsible for arg checking all of its args
125 to ensure they exist if required and are precisely the correct type if
126 found. This requirement applies only to the new method since it is the only
127 part of the delegate protocal that is directly exposed to the user. All other
128 methods are called only via Fault::Logger, which does all of the arg checking
129 and defaulting before calling a subclass method.
130  
131 If the delegate has no private arguments, @rest can be ignored; if it does
132 not use the $target that can also be ignored. If $target is used, it is a
133 subclass responsibility to make sure it is not undef, not a scalar and
134 is a member of an acceptable class.
135  
136 The $msg object will always contain a message, type and priority. If one of
137 them was missing from a Fault::Logger method call, reasonable defaults will
138 have been generated before any subclass protocol methods were called.
139 The subclass may ignore or modify them as it choses although it is recommend
140 it either use them or as is or skip them unless there is a really good reason
141 to override the values passed to it from the Logger object.
142  
143 No delegate protocol method should ever allow a 'die' to occur. If it is
144 even concievably possible, the potentially offensive code should be
145 protected by an eval statement. If the eval fails, the method should return
146 false.
147  
148 Under no circumstance should a delegate make an 'up-call' to Fault::Logger.
149 This would have the potential to generate infinite loops. If local error
150 messages or diagnostics need to be generated, use warn or print if you
151 must and preferably Fault:;ErrorHandler or Fault::DebugPrinter if you can.
152  
153 Any method which causes a line to be printed to screen or file should
154 use the Fault::Msg object method $msg->stamped_log_line to generate a
155 line like this:
156  
157 $date $time UTC> $process: $type($priority): $message
158  
159 =over 4
160  
161 =item B<$okay = Delegate::MyDelegate-E<gt>new (@rest)>
162  
163 Subclass may override. It is the class constructor. The default method returns
164 an object of the calling class with no ivars. It should test its logger
165 connection to make sure it is functioning. If there are bad arguments or
166 the connection cannot be made, return undef.
167  
168 =item B<@list = $self-E<gt>initfaults>
169  
170 A subclass may impliment this method if it is able to recover persistant
171 fault table data from a previous program execution. It is a callback
172 used by Logger when it initializes it's in-memory fault table. If this
173 method is not implimented that table will be initialized as empty.
174  
175 =item B<$okay = $self-E<gt>log ($msg,$target,@rest)>
176  
177 =item B<$okay = $self-E<gt>log ($msg,$target)>
178  
179 =item B<$okay = $self-E<gt>log ($msg)>
180  
181 Subclass must override. It is the absolute minimum requirement for a delegate
182 class that it be able to accept a log message and do something with
183 it.
184  
185 It should return true if that something succeeded.
186  
187 =item B<$zero = $self-E<gt>trans00 ($msg,$target,@rest)>
188  
189 =item B<$zero = $self-E<gt>trans00 ($msg,$target)>
190  
191 =item B<$zero = $self-E<gt>trans00 ($msg)>
192  
193 A subclass may impliment this method if it wishes to do something every time
194 a fault clear occurs. It is called when a message clear occurs on a
195 message that is not in the fault table.
196  
197 I have to this date never found a need for it but it is available for
198 completeness.
199  
200 It should always returns 0.
201  
202 =item B<$zero = $self-E<gt>trans01 ($msg,$target,@rest)>
203  
204 =item B<$zero = $self-E<gt>trans01 ($msg,$target)>
205  
206 =item B<$zero = $self-E<gt>trans01 ($msg)>
207  
208 A subclass may impliment this method if it wishes to do something the
209 first time a fault raise occurs. It is called when a fault raise occurs
210 on a message that is not already in the fault table.
211  
212 This method is part of the minimal subset required to impliment fault
213 handling.
214  
215 It should always returns 0.
216  
217 =item B<$zero = $self-E<gt>trans10 ($msg,$target,@rest)>
218  
219 =item B<$zero = $self-E<gt>trans10 ($msg,$target)>
220  
221 =item B<$zero = $self-E<gt>trans10 ($msg)>
222  
223 A subclass may impliment this method if it wishes to do something the
224 first time a fault clear occurs. It is called when a faul clear occurs
225 on a message that exists in the fault table.
226  
227 This method is part of the minimal subset required to impliment fault
228 handling.
229  
230 It should always returns 0.
231  
232 =item B<$zero = $self-E<gt>trans11 ($msg,$target,@rest)>
233  
234 =item B<$zero = $self-E<gt>trans11 ($msg,$target)>
235  
236 =item B<$zero = $self-E<gt>trans11 ($msg)>
237  
238 A subclass may impliment this method if it wishes to do something every time
239 a fault raise occurs. It is called when a message raise occurs on a
240 message that is already in the fault table.
241  
242 I have to this date never found a need for it but it is available for
243 completeness.
244  
245 It should always returns 0.
246  
247 =back 4
248  
249 =head1 Examples
250  
251 None. This is an abstract class. You must use a subclass.
252  
253 [See Fault::Logger for a detailed example.]
254  
255 =head1 Class Variables
256  
257 None.
258  
259 =head1 Instance Variables
260  
261 None.
262  
263 =head1 Class Methods
264  
265 =over 4
266  
267 =item B<$self = Fault::Delegate::LogFile-E<gt>new>
268  
269 Create a Delegate object. Classes without any args can inherit this class
270 as is. Others should override, but may use the low level family methods
271 to simplify coding.
272  
273 =back 4
274  
275 =head1 Instance Methods
276  
277 =over 4
278  
279 =item B<$self = Fault::Delegate::LogFile-E<gt>log($msg)>
280  
281 Log a message using subclass provided overrides to the low level protocol. If
282 a subclass has only the $msg argument, this method can be used. If it must
283 deal with extra args, it will need to override this method but can use it
284 as a template.
285  
286 It returns true if the message was logged successfully.
287  
288 =back 4
289  
290 =head1 Private Class Methods
291  
292 =over 4
293  
294 None.
295  
296 =back 4
297  
298 =head1 Private Instance Methods
299  
300 =over 4
301  
302 =item B<$bool = $self-E<gt>_write ($msg)>
303  
304 =item B<$bool = $self-E<gt>_connect>
305  
306 =item B<$bool = $self-E<gt>_disconnect>
307  
308 Impliments a noop internal family protocol which subclasses may override and
309 use if they wish to default most behavior to this parent class. Study the
310 code to understand how to use them.
311  
312 =item B<$bool = $self-E<gt>test>
313  
314 Executes a _connect, a log write and a _disconnect. It returns true if
315 this succeeds. This is useful in personalized subclass new methods.
316  
317 =item B<$bool = $self-E<gt>warn($line)>
318  
319 Issue a local warn output in a standardized format. Useful anywhere in
320 a subclass where errors are detected. If a subclass has an error it
321 probably cannot successfully log as it is supposed to so it should use
322 this as a fall back so there is a debug information available on the
323 problem.
324  
325 =back 4
326  
327 =head1 Errors and Warnings
328  
329 Local warning messages are printed if the logging mechanism cannot be reached
330 or has any problems whatever. You cannot log to a logger that is not working!
331  
332 =head1 KNOWN BUGS
333  
334 See TODO.
335  
336 =head1 SEE ALSO
337  
338 Fault::Logger
339  
340 =head1 AUTHOR
341  
342 Dale Amon <amon@vnl.com>
343  
344 =cut
345
346 #=============================================================================
347 # CVS HISTORY
348 #=============================================================================
349 # $Log: Delegate.pm,v $
350 # Revision 1.8 2008-08-28 23:20:19 amon
351 # perldoc section regularization.
352 #
353 # Revision 1.7 2008-05-08 20:22:50 amon
354 # Minor bug fixes; shifted fault table and initfault from Logger to List
355 #
356 # Revision 1.6 2008-05-07 18:38:20 amon
357 # Documentation fixes.
358 #
359 # Revision 1.5 2008-05-07 18:30:13 amon
360 # Moved much more inheritable code up from subclasses. More docs.
361 #
362 # Revision 1.4 2008-05-05 19:25:49 amon
363 # Catch any small changes before implimenting major changes
364 #
365 # Revision 1.3 2008-05-04 14:36:44 amon
366 # Tidied up code and docs; get_log_args and get_fault_args reduced to getargs;
367 # beefed up new method and added _connect and _disconnect.
368 #
369 # Revision 1.2 2008-05-03 00:33:14 amon
370 # Changed standard arg list
371 #
372 # Revision 1.1.1.1 2008-05-02 16:58:40 amon
373 # Fault and Log System. Pared off of DMA base lib.
374 #
375 # Revision 1.3 2008-04-25 10:58:13 amon
376 # documentation changes
377 #
378 # Revision 1.2 2008-04-18 14:07:54 amon
379 # Minor documentation format changes
380 #
381 # Revision 1.1 2008-04-18 11:36:20 amon
382 # Wrote logger delegate abstract superclass to simplify the code in all the
383 # delegate classes.
384 #
385 # 20080415 Dale Amon <amon@vnl.com>
386 # Created.
387 # DONE * get_log_args and get_fault_args are now identical. Delete one?
388 # (yes. Just get_args instead.) [DMA20080503-20080504]
389 1 1;
« Notepad.pm ↑ Summary Delegate/Stderr.pm »