File Coverage

blib/lib/Fault/DebugPrinter.pm
Criterion Covered Total %
statement 6 28 21.4
branch 0 22 0.0
condition n/a
subroutine 2 6 33.3
pod 4 4 100.0
total 12 60 20.0


line stmt bran cond sub pod time code
1             #============================= DebugPrinter.pm ===============================
2             # Filename: DebugPrinter.pm
3             # Description: A Debug print controller.
4             # Original Author: Dale M. Amon
5             # Revised by: $Author: amon $
6             # Date: $Date: 2008-08-28 23:20:19 $
7             # Version: $Revision: 1.5 $
8             # License: LGPL 2.1, Perl Artistic or BSD
9             #
10             #=============================================================================
11 1     1   732 use strict;
  1         2  
  1         93  
12              
13             package Fault::DebugPrinter;
14 1     1   6 use vars qw{@ISA};
  1         1  
  1         11173  
15             @ISA = qw( UNIVERSAL );
16              
17             #=============================================================================
18             # Class Methods
19             #=============================================================================
20              
21             my $DEBUGPRINTER = undef;
22              
23             sub new {
24 0     0 1   my ($class, $debug) = @_;
25 0 0         $DEBUGPRINTER || ($DEBUGPRINTER = bless {}, $class);
26 0 0         $DEBUGPRINTER->{'debug'} = (defined $debug) ? $debug : 0;
27 0           return $DEBUGPRINTER;
28             }
29              
30             #------------------------------------------------------------------------------
31              
32             sub dbg1 {
33 0     0 1   my ($class,$msg) = @_;
34 0 0         defined $msg || ($msg = "");
35 0           chomp $msg;
36 0 0         $DEBUGPRINTER || $class->new;
37 0 0         if ($DEBUGPRINTER->{'debug'}) {print "$msg\n"; return 1;}
  0            
  0            
38 0           return 0;
39             }
40              
41             #------------------------------------------------------------------------------
42              
43             sub dbg {
44 0     0 1   my ($class,$lvl,$msg) = @_;
45 0 0         defined $lvl || ($lvl = 1);
46 0 0         defined $msg || ($msg = "");
47 0 0         $DEBUGPRINTER || $class->new;
48 0 0         $DEBUGPRINTER->{'debug'} >= $lvl || (return 0);
49 0           return print "$msg\n";
50             }
51              
52             #------------------------------------------------------------------------------
53              
54             sub level {
55 0     0 1   my ($class,$v) = @_;
56 0 0         $DEBUGPRINTER || $class->new;
57 0 0         defined $v || (return $DEBUGPRINTER->{'debug'});
58 0           return $DEBUGPRINTER->{'debug'} = $v;
59             }
60            
61             #=============================================================================
62             # Pod Documentation
63             #=============================================================================
64             # You may extract and format the documentation section with the 'perldoc' cmd.
65              
66             =head1 NAME
67              
68             Fault::DebugPrinter - A Debug print controller with levels.
69              
70             =head1 SYNOPSIS
71              
72             use Fault::DebugPrinter;
73             $class_object = Fault::DebugPrinter->new ($level);
74             $class_object = $class_object->new ($level);
75             $class_object = Fault::DebugPrinter->new;
76             $class_object = $class_object->new;
77             $didprint = Fault::DebugPrinter->dbg1 ($msg);
78             $didprint = $class_object->dbg1 ($msg);
79             $didprint = Fault::DebugPrinter->dbg ($level,$msg);
80             $didprint = $class_object->dbg ($level,$msg);
81             $curlvl = Fault::DebugPrinter->level ($level);
82             $curlvl = $class_object->level ($level);
83             $curlvl = Fault::DebugPrinter->level;
84             $curlvl = $class_object->level;
85              
86             =head1 Inheritance
87              
88             UNIVERSAL
89              
90             =head1 Description
91              
92             This Class does not have instance objects, only a single 'Class Object'. It
93             is always referenced under the Class name. It supplies a simple mechanism for
94             run time selection of how much Diagnostic message detail will be displayed.
95             By setting the level to zero, all Diagnostic printouts are disabled. It can
96             be used either in a mode that emulates a simple enable/disable of diagnostics
97             or with multiple levels with more and more detail printed at each higher
98             level. It is entirely at the user's discretion.
99              
100             =head1 Examples
101              
102             use Fault::DebugPrinter;
103             my $classobj = Fault::DebugPrinter->new (1);
104              
105             my $didprint = Fault::DebugPrinter->dbg1 ("This will print");
106             $didprint = Fault::DebugPrinter->dbg (2, "This will not");
107              
108             my $curlvl = Fault::DebugPrinter->level;
109             $curlvl = $classobj->level ($curlvl+1);
110             $didprint = Fault::DebugPrinter->dbg (2, "This will now");
111              
112             $classobj = Fault::DebugPrinter->new;
113             $didprint = Fault::DebugPrinter->dbg1 ("This is Disabled.");
114             $curlvl = Fault::DebugPrinter->level (1);
115             $didprint = Fault::DebugPrinter->dbg1 ("This is Enabled.");
116              
117             =head1 Class Variables
118              
119             level Highest level of Diagnostic message that will be printed.
120              
121             =head1 Class Methods
122              
123             =over 4
124              
125             =item B<$class_object = Fault::DebugPrinter-Enew ($level)>
126              
127             =item B<$class_object = $class_object-Enew ($level)>
128              
129             =item B<$class_object = Fault::DebugPrinter-Enew>
130              
131             =item B<$class_object = $class_object-Enew>
132              
133             Generate the DebugPrinter object if it doesn't already exist; otherwise just
134             return the existing class object.
135              
136             $level will turn diagnostic printing on for messages with a debug level above
137             the specified it or off it is zero. If the argument is not present or undef
138             the current level is set to zero so that, diagnostic printing is disabled.
139              
140             =item B<$didprint = Fault::DebugPrinter-Edbg1 ($msg)>
141              
142             =item B<$didprint = $class_object-Edbg1 ($msg)>
143              
144             Single argument Diagnostic printer method. It prints $msg to stdout and
145             returns true if the current debug level is greater than zero. If the $msg
146             argument was missing or undef, it prints "" so you
147             at least know it tried.
148              
149             =item B<$didprint = Fault::DebugPrinter-Edbg ($level,$msg)>
150              
151             =item B<$didprint = $class_object-Edbg ($level,$msg)>
152              
153             Dual argument Diagnostic printer method. It prints $msg to stdout and returns
154             true if the current debug level is greater than zero and at least equal to the
155             integer value contained in $level. If the $level argument is missing or undef,
156             it is defaulted to Level 1. If the $msg argument was missing or undef, it
157             prints "" so you at least know it tried.
158              
159             =item B<$curlvl = Fault::DebugPrinter-Elevel ($level)>
160              
161             =item B<$curlvl = $class_object-Elevel ($level)>
162              
163             =item B<$curlvl = Fault::DebugPrinter-Elevel>
164              
165             =item B<$curlvl = $class_object-Elevel>
166              
167             Set the current diagnostic level to $level. If the $level argument is
168             missing or undef, the current level is unchanged. The no-argument format
169             thus doubles as a 'read current diagnostic level' command.
170              
171             =back 4
172              
173             =head1 Instance Methods
174              
175             None
176              
177             =head1 Private Class Methods
178              
179             None.
180              
181             =head1 Private Instance Methods
182              
183             None.
184              
185             =head1 Errors and Warnings
186              
187             None.
188              
189             =head1 KNOWN BUGS
190              
191             See TODO.
192              
193             =head1 SEE ALSO
194              
195             None.
196              
197             =head1 AUTHOR
198              
199             Dale Amon
200              
201             =cut
202            
203             #=============================================================================
204             # CVS HISTORY
205             #=============================================================================
206             # $Log: DebugPrinter.pm,v $
207             # Revision 1.5 2008-08-28 23:20:19 amon
208             # perldoc section regularization.
209             #
210             # Revision 1.4 2008-08-17 21:56:37 amon
211             # Make all titles fit CPAN standard.
212             #
213             # Revision 1.3 2008-05-07 17:43:05 amon
214             # Documentation changes
215             #
216             # Revision 1.2 2008-05-04 14:34:12 amon
217             # Tidied up code and docs.
218             #
219             # Revision 1.1.1.1 2008-05-02 16:35:05 amon
220             # Fault and Log System. Pared off of DMA base lib.
221             #
222             # Revision 1.6 2008-04-18 14:07:54 amon
223             # Minor documentation format changes
224             #
225             # Revision 1.5 2008-04-11 22:25:23 amon
226             # Add blank line after cut.
227             #
228             # Revision 1.4 2008-04-11 18:56:35 amon
229             # Fixed quoting problem with formfeeds.
230             #
231             # Revision 1.3 2008-04-11 18:39:15 amon
232             # Implimented new standard for headers and trailers.
233             #
234             # Revision 1.2 2008-04-10 15:01:08 amon
235             # Added license to headers, removed claim that the documentation section still
236             # relates to the old doc file.
237             #
238             # Revision 1.1.1.1 2004-08-30 01:14:44 amon
239             # Dale's library of primitives in Perl
240             #
241             # 20040813 Dale Amon
242             # Moved to DMA:: from Archivist::
243             # to make it easier to enforce layers.
244             #
245             # 20030108 Dale Amon
246             # Created.
247             1;