/usr/share/perl5/Paranoid/Debug.pm is in libparanoid-perl 0.31-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 | # Paranoid::Debug -- Debug support for paranoid programs
#
# (c) 2005, Arthur Corliss <corliss@digitalmages.com>
#
# $Id: Debug.pm,v 0.93 2010/06/03 18:58:30 acorliss Exp $
#
# This software is licensed under the same terms as Perl, itself.
# Please see http://dev.perl.org/licenses/ for more information.
#
#####################################################################
#####################################################################
#
# Environment definitions
#
#####################################################################
package Paranoid::Debug;
use strict;
use warnings;
use vars qw($VERSION @EXPORT @EXPORT_OK %EXPORT_TAGS);
use base qw(Exporter);
use Paranoid;
($VERSION) = ( q$Revision: 0.93 $ =~ /(\d+(?:\.(\d+))+)/sm );
@EXPORT = qw(PDEBUG pdebug perror pIn pOut psetDebug PDPREFIX);
@EXPORT_OK = qw(PDEBUG pdebug perror pIn pOut psetDebug PDPREFIX
PDLEVEL1 PDLEVEL2 PDLEVEL3 PDLEVEL4 PDMAXINDENT);
%EXPORT_TAGS = (
all => [
qw(PDEBUG pdebug perror pIn pOut psetDebug PDPREFIX
PDLEVEL1 PDLEVEL2 PDLEVEL3 PDLEVEL4 PDMAXINDENT)
],
);
use constant PDLEVEL1 => 9;
use constant PDLEVEL2 => 10;
use constant PDLEVEL3 => 11;
use constant PDLEVEL4 => 12;
use constant PDMAXIND => 60;
#####################################################################
#
# Module code follows
#
#####################################################################
{
my $dlevel = 0; # Start with no debug level
my $ilevel = 0; # Start with no identation
my $pdebug = 0; # Start with debug output disabled
my $maxLevel = PDMAXIND; # Start with normal max indentation
my $indIgnored = 0; # Start without ignoring indentation
my $defprefix = sub {
# Default Prefix to use with debug messages looks like:
#
# [PID - $dlevel] Subroutine:
#
my $caller = ( caller 2 )[3];
my $prefix;
$caller = defined $caller ? $caller : 'undef';
$prefix = ' ' x $ilevel . "[$$-$dlevel] $caller: ";
return $prefix;
};
my $pdprefix = $defprefix;
sub PDEBUG : lvalue {
$pdebug;
}
sub PDPREFIX : lvalue {
$pdprefix;
}
sub PDMAXINDENT : lvalue {
$maxLevel;
}
sub perror ($) {
# Purpose: Print passed string to STDERR
# Returns: Return value from print function
# Usage: $rv = perror("Foo!");
my $msg = shift;
$@ = $msg;
return print STDERR "$msg\n";
}
sub pdebug ($;$) {
# Purpose: Calls perror() if the message level is less than or equal
# to the value of PDBEBUG, after prepending the string
# returned by the PDPREFIX routine, if defined
# Returns: Always returns the passed message, regardless of PDEBUG's
# value
# Usage: pdebug($message, $level);
my $msg = shift;
my $level = shift || 1;
my $prefix = PDPREFIX;
return $msg if $level > PDEBUG;
# Execute the code block, if that's what it is
$prefix = &$prefix() if ref($prefix) eq 'CODE';
perror("$prefix$msg");
return $msg;
}
sub pIn () {
# Purpose: Increases indentation level
# Returns: Always True (1)
# Usage: pIn();
if ( $ilevel < PDMAXINDENT ) {
$ilevel++;
} else {
$indIgnored = 1;
}
$dlevel++;
return 1;
}
sub pOut () {
# Purpose: Decreases indentation level
# Returns: Always True (1)
# Usage: pOut();
if ($indIgnored) {
$indIgnored = 0;
} else {
$ilevel-- if $ilevel > 0;
}
$dlevel--;
return 1;
}
}
# TODO: Kill this freaking thing (psetDebug)
sub psetDebug (@) {
# Purpose: Set PDEBUG equal to the number of 'v's in '-v...'
# Returns: PDEBUG (after counting v's)
# Usage: psetDebug(@ARGV);
my @args = @_;
my $v;
# Extract all ^-v+$ arguments
$v = join '', grep /^-v+$/sm, @args;
$v =~ s/-//smg;
# Set debug level
PDEBUG = length $v;
return PDEBUG;
}
1;
__END__
=head1 NAME
Paranoid::Debug - Trace message support for paranoid programs
=head1 VERSION
$Id: Debug.pm,v 0.93 2010/06/03 18:58:30 acorliss Exp $
=head1 SYNOPSIS
use Paranoid::Debug;
PDEBUG = 1;
PDMAXINDENT = 40;
PDPREFIX = sub { scalar localtime };
pdebug("starting program", 1);
foo();
sub foo {
pdebug("entering foo()", 2);
pIn();
pdebug("someting happened!", 2);
pOut();
pdebug("leaving w/rv: $rv", 2):
}
perror("error msg");
# Deprecated
psetDebug(@ARGV);
=head1 DESCRIPTION
The purpose of this module is to provide a barely useful framework to produce
debugging output. With this module you can assign a level of detail to pdebug
statements, and they'll only be displayed when PDEBUG is set to that level or
higher. This allows you to have your program produce varying levels of
debugging output.
Using the B<pIn> and B<pOut> functions at the beginning and end of each
function will cause debugging output to be indented appropriately so you can
visually see the level of recursion.
B<NOTE:> This module provides a function called B<perror> which conflicts with
a similar function provided by the B<POSIX> module. If you use this module
you should avoid using or importing POSIX's version of this function.
B<NOTE:> All modules within the Paranoid framework use this module. Their
debug levels range from 9 and up. You should use 1 - 8 for your own modules
or code.
=head1 SUBROUTINES/METHODS
=head2 PDEBUG
B<PDEBUG> is an lvalue subroutine which is initially set to 0, but can be
set to any positive integer. The higher the number the higher the level
of pdebug statements are printed.
=head2 PDMAXINDENT
B<PDMAXINDENT> is an lvalue subroutine which is initially set to 60, but can
be set to any integer. This controls the max indentation of the debug
messages. Obviously, it wouldn't help to indent a debug message by a hundred
columns on an eighty column terminal just because your stack depth gets that
deep.
=head2 PDPREFIX
B<PDPREFIX> is also an lvalue subroutien and is set by default to a
subroutine that returns as a string the standard prefix for debug
messages:
[PID - DLEVEL] Subroutine:
Assigning another subroutine reference to a subroutine can override this
behavior.
=head2 perror
perror("error msg");
This function prints the passed message to STDERR.
=head2 pdebug
pdebug("debug statement", 3);
This function is called with one mandatory argument (the string to be
printed), and an optional integer. This integer is compared against B<PDEBUG>
and the debug statement is printed if PDEBUG is equal to it or higher.
The return value is always the debug statement itself. This allows for a
single statement to produce debug output and set variables. For instance:
Paranoid::ERROR = pdebug("Something bad happened!", 3);
=head2 pIn
pIn();
This function causes all subsequent pdebug messages to be indented by one
additional space.
=head2 pOut
pOut();
This function causes all subsequent pdebug messages to be indented by one
less space.
=head2 psetDebug
psetDebug(@ARGV);
This function extracts all ^-v+$ arguments from the passed list and counts the
number of 'v's that result, and sets B<PDEBUG> to that count. You would
typically use this by passing @ARGV for command-line programs.
B<NOTE>: This was a dumb idea of incredible proportions. As soons as it is
safe to do so I will kill this function and perform my penance before the gods
of bitrot. Consider this deprecated.
=head1 DEPENDENCIES
L<Paranoid>
=head1 BUGS AND LIMITATIONS
B<perror> (and by extension, B<pdebug>) will generate errors if STDERR is
closed elsewhere in the program.
There is also no upper limit on how much indentation will be used by the
program, so if you're using B<pIn> in deeply recursive call stacks you can
expect some overhead due some rather large strings being bandied about.
=head1 AUTHOR
(c) 2005 Arthur Corliss (corliss@digitalmages.com)
=head1 LICENSE AND COPYRIGHT
This software is licensed under the same terms as Perl, itself.
Please see http://dev.perl.org/licenses/ for more information.
(c) 2005, Arthur Corliss (corliss@digitalmages.com)
|