DebugPrinter.pm

filter: Statementstmt 29.0% 9 / 31 Branchbran 0.0% 0 / 22 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 33.3% 2 / 6 Podpod 100% 4 / 4 totaltotal 23.8% 15 / 63 CC 12 SCAR 28.8
CC
12
Cov
17%
dbg
30.0
dbg1
20.0
new
12.0
CRAP 17.9
« Dbh.pm ↑ Summary Delegate/File.pm »
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 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 package Fault::DebugPrinter;
14 1 ▶use vars qw{@ISA};
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 @ISA = qw( UNIVERSAL );
16  
17 #=============================================================================
18 # Class Methods
19 #=============================================================================
20  
21 1 my $DEBUGPRINTER = undef;
22  
23 0 ▶sub new {
Subroutine new: not called CC=3 SCAR=24.8
Pod documented
24 0 my ($class, $debug) = @_;
25 0 ▶ $DEBUGPRINTER || ($DEBUGPRINTER = bless {}, $class);
Branch: 1 branch
BranchTrueFalseTotal
$DEBUGPRINTER or $DEBUGPRINTER = bless({}, $class) 0 0 0
26 0 ▶ $DEBUGPRINTER->{'debug'} = (defined $debug) ? $debug : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $debug ? : 0 0 0
27 0 return $DEBUGPRINTER;
28 }
29  
30 #------------------------------------------------------------------------------
31  
32 0 ▶sub dbg1 {
Subroutine dbg1: not called CC=4 SCAR=30.0
Pod documented
33 0 my ($class,$msg) = @_;
34 0 ▶ defined $msg || ($msg = "<Null diagnostic message>");
Branch: 1 branch
BranchTrueFalseTotal
defined $msg or $msg = "<Null diagnostic message>" 0 0 0
35 0 chomp $msg;
36 0 ▶ $DEBUGPRINTER || $class->new;
Branch: 1 branch
BranchTrueFalseTotal
$DEBUGPRINTER or $class->new 0 0 0
37 0 ▶ if ($DEBUGPRINTER->{'debug'}) {print "$msg\n"; return 1;}
Branch: 1 branch
BranchTrueFalseTotal
if ($DEBUGPRINTER->{"debug"}) 0 0 0
38 0 return 0;
39 }
40  
41 #------------------------------------------------------------------------------
42  
43 0 ▶sub dbg {
Subroutine dbg: not called CC=5 SCAR=34.0
Pod documented
44 0 my ($class,$lvl,$msg) = @_;
45 0 ▶ defined $lvl || ($lvl = 1);
Branch: 1 branch
BranchTrueFalseTotal
defined $lvl or $lvl = 1 0 0 0
46 0 ▶ defined $msg || ($msg = "<Null diagnostic message>");
Branch: 1 branch
BranchTrueFalseTotal
defined $msg or $msg = "<Null diagnostic message>" 0 0 0
47 0 ▶ $DEBUGPRINTER || $class->new;
Branch: 1 branch
BranchTrueFalseTotal
$DEBUGPRINTER or $class->new 0 0 0
48 0 ▶ $DEBUGPRINTER->{'debug'} >= $lvl || (return 0);
Branch: 1 branch
BranchTrueFalseTotal
$DEBUGPRINTER->{"debug"} >= $lvl or return 0 0 0 0
49 0 return print "$msg\n";
50 }
51  
52 #------------------------------------------------------------------------------
53  
54 0 ▶sub level {
Subroutine level: not called CC=3 SCAR=24.8
Pod documented
55 0 my ($class,$v) = @_;
56 0 ▶ $DEBUGPRINTER || $class->new;
Branch: 1 branch
BranchTrueFalseTotal
$DEBUGPRINTER or $class->new 0 0 0
57 0 ▶ defined $v || (return $DEBUGPRINTER->{'debug'});
Branch: 1 branch
BranchTrueFalseTotal
defined $v or return $DEBUGPRINTER->{"debug"} 0 0 0
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-E<gt>new ($level)>
126  
127 =item B<$class_object = $class_object-E<gt>new ($level)>
128  
129 =item B<$class_object = Fault::DebugPrinter-E<gt>new>
130  
131 =item B<$class_object = $class_object-E<gt>new>
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-E<gt>dbg1 ($msg)>
141  
142 =item B<$didprint = $class_object-E<gt>dbg1 ($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 "<Null diagnostic message>" so you
147 at least know it tried.
148  
149 =item B<$didprint = Fault::DebugPrinter-E<gt>dbg ($level,$msg)>
150  
151 =item B<$didprint = $class_object-E<gt>dbg ($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 "<Null diagnostic message>" so you at least know it tried.
158  
159 =item B<$curlvl = Fault::DebugPrinter-E<gt>level ($level)>
160  
161 =item B<$curlvl = $class_object-E<gt>level ($level)>
162  
163 =item B<$curlvl = Fault::DebugPrinter-E<gt>level>
164  
165 =item B<$curlvl = $class_object-E<gt>level>
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 <amon@vnl.com>
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 <amon@vnl.com>
242 # Moved to DMA:: from Archivist::
243 # to make it easier to enforce layers.
244 #
245 # 20030108 Dale Amon <amon@vnl.com>
246 # Created.
247 1 1;
« Dbh.pm ↑ Summary Delegate/File.pm »