2 # The system variables - those indicated will need to be changed to suit your
3 # circumstances (and callsign)
5 # Copyright (c) 1998 - Dirk Koopman G1TLH
14 @EXPORT = qw(dbginit dbg dbgadd dbgsub dbglist dbgdump isdbg dbgclose confess croak cluck);
17 use vars qw(%dbglevel $fp $callback $cleandays $keepdays);
30 our $no_stdout; # set if not running in a terminal
32 # Avoid generating "subroutine redefined" warnings with the following
33 # hack (from CGI::Carp):
34 if (!defined $DB::VERSION) {
36 eval qq( sub confess {
37 \$SIG{__DIE__} = 'DEFAULT';
39 DXDebug::dbg(Carp::shortmess(\@_));
43 \$SIG{__DIE__} = 'DEFAULT';
45 DXDebug::dbg(Carp::longmess(\@_));
48 sub carp { DXDebug::dbg(Carp::shortmess(\@_)); }
49 sub cluck { DXDebug::dbg(Carp::longmess(\@_)); }
52 CORE::die(Carp::shortmess($@)) if $@;
54 eval qq( sub confess { die Carp::longmess(\@_); };
55 sub croak { die Carp::shortmess(\@_); };
56 sub cluck { warn Carp::longmess(\@_); };
57 sub carp { warn Carp::shortmess(\@_); };
69 my @l = split /\n/, $r;
71 s/([\x00-\x08\x0B-\x1f\x7f-\xff])/uc sprintf("%%%02x",ord($1))/eg;
72 print "$_\n" if defined \*STDOUT && !$no_stdout;
74 &$callback($str) if $callback;
75 $fp->writeunix($t, $str);
84 # add sig{__DIE__} handling
85 unless (defined $DB::VERSION) {
86 $SIG{__WARN__} = sub {
87 if ($_[0] =~ /Deep\s+recursion/i) {
89 dbg(Carp::longmess(@_));
93 dbg(Carp::shortmess(@_));
97 $SIG{__DIE__} = sub { dbg($@); dbg(Carp::longmess(@_)); };
99 # switch off STDOUT printing if we are not talking to a TTY
100 unless ($^O =~ /^MS/ || $^O =~ /^OS-2/) {
101 unless (isatty(STDOUT->fileno)) {
107 $fp = DXLog::new('debug', 'dat', 'd');
112 $SIG{__DIE__} = $SIG{__WARN__} = 'DEFAULT';
123 if ($dbglevel{$l} || $l eq 'err') {
125 for (my $o = 0; $o < length $l; $o += 16) {
126 my $c = substr $l, $o, 16;
127 my $h = unpack "H*", $c;
128 $c =~ s/[\x00-\x1f\x7f-\xff]/./g;
129 my $left = 16 - length $c;
130 $h .= ' ' x (2 * $left) if $left > 0;
131 dbg($m . sprintf("%4d:", $o) . "$h $c");
132 $m = ' ' x (length $m);
142 foreach $entry (@_) {
143 $dbglevel{$entry} = 1;
151 foreach $entry (@_) {
152 delete $dbglevel{$entry};
158 return keys (%dbglevel);
164 return $dbglevel{$_[0]};
169 return Carp::shortmess(@_);
174 return Carp::longmess(@_);
177 # clean out old debug files, stop when you get a gap of more than a month
180 my $date = $fp->unixtoj($main::systime)->sub($keepdays+1);
184 my $fn = $fp->_genfn($date);
191 $date = $date->sub(1);