File Coverage

blib/lib/Paranoid/Debug.pm
Criterion Covered Total %
statement 97 103 94.1
branch 20 28 71.4
condition 2 2 100.0
subroutine 27 27 100.0
pod 7 7 100.0
total 153 167 91.6


line stmt bran cond sub pod time code
1             # Paranoid::Debug -- Debug support for paranoid programs
2             #
3             # $Id: lib/Paranoid/Debug.pm, 2.08 2020/12/31 12:10:06 acorliss Exp $
4             #
5             # This software is free software. Similar to Perl, you can redistribute it
6             # and/or modify it under the terms of either:
7             #
8             # a) the GNU General Public License
9             # as published by the
10             # Free Software Foundation ; either version 1
11             # , or any later version
12             # , or
13             # b) the Artistic License 2.0
14             # ,
15             #
16             # subject to the following additional term: No trademark rights to
17             # "Paranoid" have been or are conveyed under any of the above licenses.
18             # However, "Paranoid" may be used fairly to describe this unmodified
19             # software, in good faith, but not as a trademark.
20             #
21             # (c) 2005 - 2020, Arthur Corliss (corliss@digitalmages.com)
22             # (tm) 2008 - 2020, Paranoid Inc. (www.paranoid.com)
23             #
24             #####################################################################
25              
26             #####################################################################
27             #
28             # Environment definitions
29             #
30             #####################################################################
31              
32             package Paranoid::Debug;
33              
34 49     49   6822 use strict;
  49         456  
  49         1385  
35 49     49   239 use warnings;
  49         75  
  49         1358  
36 49     49   232 use vars qw($VERSION @EXPORT @EXPORT_OK %EXPORT_TAGS);
  49         107  
  49         2993  
37 49     49   297 use base qw(Exporter);
  49         102  
  49         3538  
38 49     49   349 use Paranoid;
  49         89  
  49         7255  
39              
40             ($VERSION) = ( q$Revision: 2.08 $ =~ /(\d+(?:\.\d+)+)/sm );
41              
42             @EXPORT = qw(PDEBUG pdebug pIn pOut
43             PDEBUG1 PDEBUG2 PDEBUG3 PDEBUG4 PDEBUG5 PDEBUG6 PDEBUG7 PDEBUG8);
44             @EXPORT_OK = (
45             @EXPORT,
46             qw(pderror PDPREFIX PDLEVEL1 PDLEVEL2 PDLEVEL3 PDLEVEL4 PDMAXINDENT),
47             );
48             %EXPORT_TAGS = (
49             all => [@EXPORT_OK],
50             constants => [
51             qw(PDEBUG1 PDEBUG2 PDEBUG3 PDEBUG4 PDEBUG5 PDEBUG6
52             PDEBUG7 PDEBUG8)
53             ],
54             );
55              
56 49     49   404 use constant PDLEVEL1 => 9;
  49         107  
  49         3088  
57 49     49   322 use constant PDLEVEL2 => 10;
  49         90  
  49         2636  
58 49     49   747 use constant PDLEVEL3 => 11;
  49         132  
  49         2371  
59 49     49   518 use constant PDLEVEL4 => 12;
  49         119  
  49         2601  
60              
61 49     49   299 use constant PDMAXIND => 60;
  49         94  
  49         2801  
62              
63 49     49   322 use constant PDEBUG1 => 1;
  49         105  
  49         2753  
64 49     49   310 use constant PDEBUG2 => 2;
  49         81  
  49         2661  
65 49     49   341 use constant PDEBUG3 => 3;
  49         174  
  49         2593  
66 49     49   294 use constant PDEBUG4 => 4;
  49         341  
  49         2345  
67 49     49   278 use constant PDEBUG5 => 5;
  49         90  
  49         2728  
68 49     49   283 use constant PDEBUG6 => 6;
  49         93  
  49         2357  
69 49     49   299 use constant PDEBUG7 => 7;
  49         99  
  49         2643  
70 49     49   291 use constant PDEBUG8 => 8;
  49         102  
  49         27950  
71              
72             #####################################################################
73             #
74             # Module code follows
75             #
76             #####################################################################
77              
78             {
79             my $dlevel = 0; # Start with no debug level
80             my $ilevel = 0; # Start with no identation
81             my $pdebug = 0; # Start with debug output disabled
82             my $maxLevel = PDMAXIND; # Start with normal max indentation
83             my $indIgnored = 0; # Start without ignoring indentation
84              
85             my $defprefix = sub {
86              
87             # Default Prefix to use with debug messages looks like:
88             #
89             # [PID - $dlevel] Subroutine:
90             #
91             my $caller = shift;
92             my $prefix = ' ' x $ilevel . "[$$-$dlevel] $caller: ";
93              
94             return $prefix;
95             };
96             my $pdprefix = $defprefix;
97              
98             sub PDEBUG : lvalue {
99 13854     13854 1 50814 $pdebug;
100             }
101              
102             sub PDPREFIX : lvalue {
103 13854     13854 1 21439 $pdprefix;
104             }
105              
106             sub PDMAXINDENT : lvalue {
107 5283     5283 1 10034 $maxLevel;
108             }
109              
110             sub pderror ($) {
111              
112             # Purpose: Print passed string to STDERR
113             # Returns: Return value from print function
114             # Usage: $rv = pderror("Foo!");
115              
116 1     1 1 86 my $msg = shift;
117              
118 1         4 $@ = $msg;
119              
120 1         32 return print STDERR "$msg\n";
121             }
122              
123             sub pdebug ($;$@) {
124              
125             # Purpose: Calls pderror() if the message level is less than or equal
126             # to the value of PDBEBUG, after prepending the string
127             # returned by the PDPREFIX routine, if defined
128             # Returns: Always returns the passed message, regardless of PDEBUG's
129             # value
130             # Usage: pdebug($message, $level);
131              
132 13854     13854 1 26295 my $msg = shift;
133 13854   100     26656 my $level = shift || 1;
134 13854         29998 my @pfargs = @_;
135 13854         27238 my $prefix = PDPREFIX;
136 13854         21374 my ( $ci, @crec, $caller, $n, $np );
137              
138 13854 50       26317 $msg = '' unless defined $msg;
139              
140             # If called with a negative level it merely means we
141             # need to go a little bit deeper in the call stack to find the
142             # true initiator of the message. This provides the mechanism for
143             # Paranoid::Log::plog to pass indirect debug messages
144 13854 100       25252 $ci = $level < 0 ? 2 : 1;
145 13854 100       24375 $level *= -1 if $level < 0;
146              
147             # Get the call stack info
148 13854         89416 @crec = caller $ci;
149 13854 50       34982 $caller =
    100          
150             defined $crec[3] ? $crec[3]
151             : defined $crec[1] ? "$crec[1]/$crec[2]"
152             : 'undef';
153              
154             # Filter message through sprintf if args were passed
155 13854         75692 $n = [ $msg =~ m#(%[\w.]+)#sg ];
156 13854         29819 $np = $n = scalar @$n;
157 13854 100       28669 if ($n) {
158              
159             # Adjust n upwards if we were given more list items than
160             # we see placeholders for in the messsage string
161 12632 100       24751 $n = scalar @pfargs if @pfargs > $n;
162              
163             # Make sure the requisite number of args are translated for undefs
164 12632         24147 while ( $n > 0 ) {
165 24481         30228 $n--;
166 24481 100       60891 $pfargs[$n] = 'undef' unless defined $pfargs[$n];
167             }
168              
169             # Consolidate extra args into the last placeholder's spot
170 12632 100       23049 if ( scalar @pfargs > $np ) {
171 595         844 $n = $np - 1;
172 595         4840 @pfargs =
173             ( @pfargs[ 0 .. ( $n - 1 ) ], "@pfargs[$n .. $#pfargs]" );
174             }
175              
176             # Filter through sprintf
177             {
178 49     49   390 no warnings;
  49         94  
  49         5248  
  12632         16371  
179 12632         49849 $msg = sprintf( $msg, @pfargs );
180             }
181             }
182              
183 13854 50       27215 return $msg if $level > PDEBUG;
184              
185             # Execute the code block, if that's what it is
186 0 0       0 $prefix = &$prefix($caller) if ref($prefix) eq 'CODE';
187              
188             {
189 49     49   347 no warnings;
  49         93  
  49         10933  
  0         0  
190 0         0 pderror("$prefix$msg");
191             }
192              
193 0         0 return $msg;
194             }
195              
196             sub pIn () {
197              
198             # Purpose: Increases indentation level
199             # Returns: Always True (1)
200             # Usage: pIn();
201              
202 5283 50   5283 1 8778 if ( $ilevel < PDMAXINDENT ) {
203 5283         8701 $ilevel++;
204             } else {
205 0         0 $indIgnored = 1;
206             }
207 5283         6908 $dlevel++;
208              
209 5283         9250 return 1;
210             }
211              
212             sub pOut () {
213              
214             # Purpose: Decreases indentation level
215             # Returns: Always True (1)
216             # Usage: pOut();
217              
218 5283 50   5283 1 10813 if ($indIgnored) {
219 0         0 $indIgnored = 0;
220             } else {
221 5283 50       12690 $ilevel-- if $ilevel > 0;
222             }
223 5283         6972 $dlevel--;
224              
225 5283         8835 return 1;
226             }
227              
228             }
229              
230             1;
231              
232             __END__