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.
32 @EXPORT = qw(dbginit dbg dbgadd dbgsub dbglist dbgdump isdbg dbgclose dbgtrace dbgprintring dbgsetcat confess croak cluck carp);
35 use vars qw(%dbglevel $fp $callback $cleandays $keepdays $dbgringlth);
49 our $no_stdout; # set if not running in a terminal
52 # Avoid generating "subroutine redefined" warnings with the following
53 # hack (from CGI::Carp):
54 if (!defined $DB::VERSION) {
56 eval qq( sub confess {
57 \$SIG{__DIE__} = 'DEFAULT';
58 DXDebug::dbgprintring() if DXDebug::isdbg('nologchan');
60 # DXDebug::dbg(Carp::shortmess(\@_));
61 DXDebug::longmess(\@_);
65 \$SIG{__DIE__} = 'DEFAULT';
66 DXDebug::dbgprintring() if DXDebug::isdbg('nologchan');
68 # DXDebug::dbg(Carp::longmess(\@_));
69 DXDebug::shortmess(\@_);
73 DXDebug::dbgprintring(5) if DXDebug::isdbg('nologchan');
74 # DXDebug::dbg(Carp::shortmess(\@_));
75 DXDebug::longmess(\@_);
78 DXDebug::dbgprintring(5) if DXDebug::isdbg('nologchan');
79 # DXDebug::dbg(Carp::longmess(\@_));
80 DXDebug::longmess(\@_);
83 CORE::die(Carp::shortmess($@)) if $@;
85 eval qq( sub confess { die Carp::longmess(\@_); };
86 sub croak { die Carp::shortmess(\@_); };
87 sub cluck { warn Carp::longmess(\@_); };
88 sub carp { warn Carp::shortmess(\@_); };
93 my $_isdbg = ''; # current dbg level we are processing
98 # say "*** in dbgtrace";
101 for (my $i = 1; (my ($pkg, $fn, $l, $subr) = caller($i)); ++$i) {
102 # say "*** in dbgtrace $i";
103 next if $pkg eq 'DXDebug';
104 # say "*** in dbgtrace after package";
105 last if $pkg =~ /Mojo/;
106 # say "*** in dbgtrace $i after mojo";
108 dbg("Stack ($i): ${pkg}::$subr in $fn line: $l");
120 my @l = split /\n/, $r;
122 $l =~ s/([\x00-\x08\x0B-\x1f\x7f-\xff])/sprintf("%%%02X",ord($1))/eg;
123 my $tag = $_isdbg ? "($_isdbg) " : '(*) ';
124 print "$tag$l\n" if defined \*STDOUT && !$no_stdout;
125 my $str = "$t^$tag$l";
126 &$callback($str) if $callback;
128 shift @dbgring while (@dbgring > $dbgringlth);
131 $fp->writeunix($t, $str) unless !$fp || $dbglevel{"nolog$_isdbg"} ;
139 my $basename = shift || 'debug';
142 # add sig{__DIE__} handling
143 unless (defined $DB::VERSION) {
144 $SIG{__WARN__} = sub {
145 if ($_[0] =~ /Deep\s+recursion/i) {
147 dbg(Carp::longmess(@_));
152 dbg(Carp::shortmess(@_));
156 $SIG{__DIE__} = sub { dbg($@); dbg(Carp::longmess(@_)); };
158 # switch off STDOUT printing if we are not talking to a TTY
159 unless ($^O =~ /^MS/ || $^O =~ /^OS-2/) {
160 unless (isatty(STDOUT->fileno)) {
166 $fp = DXLog::new($basename, 'dat', 'd');
172 $SIG{__DIE__} = $SIG{__WARN__} = 'DEFAULT';
174 dbgprintring() if grep /nolog/, keys %dbglevel;
187 if ($dbglevel{$l} || $l eq 'err') {
190 for (my $o = 0; $o < length $l; $o += 16) {
191 my $c = substr $l, $o, 16;
192 my $h = unpack "H*", $c;
193 $c =~ s/[\x00-\x1f\x7f-\xff]/./g;
194 my $left = 16 - length $c;
195 $h .= ' ' x (2 * $left) if $left > 0;
196 push @out, $m . sprintf("%4d:", $o) . "$h $c";
197 $m = ' ' x (length $m);
200 dbg(@out) if isdbg($l); # yes, I know, I have my reasons;
208 foreach $entry (@_) {
209 $dbglevel{$entry} = 1;
217 foreach $entry (@_) {
218 delete $dbglevel{$entry};
224 return keys (%dbglevel);
230 if ($dbglevel{$_[0]}) {
250 my $count = @dbgring;
251 $i = @dbgring-$i if $i;
252 return 0 unless $i < $count; # do nothing if there is nothing to print
256 for ( ; $i < $count; ++$i) {
257 my ($t, $str) = split /\^/, $dbgring[$i], 2;
261 $fp->writeunix($lt, "$lt^###");
262 $fp->writeunix($lt, "$lt^### RINGBUFFER START at line $i (zero base)");
263 $fp->writeunix($lt, "$lt^###");
266 my $buf = sprintf "%02d:%02d:%02d", (gmtime($t))[2,1,0];
267 $fp->writeunix($lt, "$lt^RING: $buf^$str");
271 $fp->writeunix($et, "$et^###");
272 $fp->writeunix($et, "$et^### RINGBUFFER END $l debug lines written");
273 $fp->writeunix($et, "$et^###");
282 # clean out old debug files, stop when you get a gap of more than a month
285 my $date = $fp->unixtoj($main::systime)->sub($keepdays+1);
289 my $fn = $fp->_genfn($date);
297 $date = $date->sub(1);
301 # force a category for the next (unconditional) dbg message (replace (*) with (<something>))