2 # The system variables - those indicated will need to be changed to suit your
3 # circumstances (and callsign)
5 # Copyright (c) 1998-2019 - Dirk Koopman G1TLH
7 # Note: Everything is recorded into the ring buffer (in perl terms: a numerically max sized array).
8 # To allow debugging of a category (e.g. 'chan') but not onto disc (just into the ring buffer)
9 # do: set/debug chan nologchan
11 # To print the current contents into the debug log: show/debug_ring
13 # On exit or serious error the ring buffer is printed to the current debug log
16 # Expose a localhost listener on port (default) 27755 to things like watchdbg so that they can carry on
17 # as normal, possibly with a "remember" button to permanently capture stuff observed.
20 # This is likely to be some form of triggering or filtering controlling (some portion
21 # of) ring_buffer dumping.
29 @EXPORT = qw(dbginit dbg dbgadd dbgsub dbglist dbgdump isdbg dbgclose confess croak cluck);
32 use vars qw(%dbglevel $fp $callback $cleandays $keepdays $dbgringlth);
46 our $no_stdout; # set if not running in a terminal
49 # Avoid generating "subroutine redefined" warnings with the following
50 # hack (from CGI::Carp):
51 if (!defined $DB::VERSION) {
53 eval qq( sub confess {
54 \$SIG{__DIE__} = 'DEFAULT';
55 DXDebug::dbgprintring() if DXDebug::isdbg('nologchan');
57 DXDebug::dbg(Carp::shortmess(\@_));
61 \$SIG{__DIE__} = 'DEFAULT';
62 DXDebug::dbgprintring() if DXDebug::isdbg('nologchan');
64 DXDebug::dbg(Carp::longmess(\@_));
68 DXDebug::dbgprintring(25) if DXDebug('nologchan');
69 DXDebug::dbg(Carp::shortmess(\@_));
72 DXDebug::dbgprintring(25) if DXDebug('nologchan');
73 DXDebug::dbg(Carp::longmess(\@_));
76 CORE::die(Carp::shortmess($@)) if $@;
78 eval qq( sub confess { die Carp::longmess(\@_); };
79 sub croak { die Carp::shortmess(\@_); };
80 sub cluck { warn Carp::longmess(\@_); };
81 sub carp { warn Carp::shortmess(\@_); };
86 my $_isdbg; # current dbg level we are processing
95 my @l = split /\n/, $r;
97 s/([\x00-\x08\x0B-\x1f\x7f-\xff])/uc sprintf("%%%02x",ord($1))/eg;
98 print "$_\n" if defined \*STDOUT && !$no_stdout;
99 my $tag = $_isdbg ? "($_isdbg) " : '';
100 my $str = "$t^$tag$_";
101 &$callback($str) if $callback;
103 shift @dbgring while (@dbgring > $dbgringlth);
106 $fp->writeunix($t, $str) unless $dbglevel{"nolog$_isdbg"};
116 # add sig{__DIE__} handling
117 unless (defined $DB::VERSION) {
118 $SIG{__WARN__} = sub {
119 if ($_[0] =~ /Deep\s+recursion/i) {
121 dbg(Carp::longmess(@_));
126 dbg(Carp::shortmess(@_));
130 $SIG{__DIE__} = sub { dbg($@); dbg(Carp::longmess(@_)); };
132 # switch off STDOUT printing if we are not talking to a TTY
133 unless ($^O =~ /^MS/ || $^O =~ /^OS-2/) {
134 unless (isatty(STDOUT->fileno)) {
140 $fp = DXLog::new('debug', 'dat', 'd');
146 $SIG{__DIE__} = $SIG{__WARN__} = 'DEFAULT';
148 dbgprintring() if grep /nolog/, keys %dbglevel;
161 if ($dbglevel{$l} || $l eq 'err') {
163 for (my $o = 0; $o < length $l; $o += 16) {
164 my $c = substr $l, $o, 16;
165 my $h = unpack "H*", $c;
166 $c =~ s/[\x00-\x1f\x7f-\xff]/./g;
167 my $left = 16 - length $c;
168 $h .= ' ' x (2 * $left) if $left > 0;
169 dbg($m . sprintf("%4d:", $o) . "$h $c");
170 $m = ' ' x (length $m);
180 foreach $entry (@_) {
181 $dbglevel{$entry} = 1;
189 foreach $entry (@_) {
190 delete $dbglevel{$entry};
196 return keys (%dbglevel);
202 if ($dbglevel{$_[0]}) {
210 return Carp::shortmess(@_);
215 return Carp::longmess(@_);
224 my $i = defined $count ? @dbgring-$count : 0;
226 for ( ; $i < $count; ++$i) {
227 my ($t, $str) = split /\^/, $dbgring[$i], 2;
231 $fp->writeunix($lt, "$lt^###");
232 $fp->writeunix($lt, "$lt^### RINGBUFFER START at line $i (zero base)");
233 $fp->writeunix($lt, "$lt^###");
236 my $buf = sprintf "%02d:%02d:%02d", (gmtime($t))[2,1,0];
237 $fp->writeunix($lt, "$lt^RING: $buf^$str");
240 $fp->writeunix($et, "$et^###");
241 $fp->writeunix($et, "$et^### RINGBUFFER END");
242 $fp->writeunix($et, "$et^###");
250 # clean out old debug files, stop when you get a gap of more than a month
253 my $date = $fp->unixtoj($main::systime)->sub($keepdays+1);
257 my $fn = $fp->_genfn($date);
265 $date = $date->sub(1);