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.
31 @EXPORT = qw(dbginit dbg dbgadd dbgsub dbglist dbgdump isdbg dbgclose dbgtrace dbgprintring confess croak cluck carp);
34 use vars qw(%dbglevel $fp $callback $cleandays $keepdays $dbgringlth);
48 our $no_stdout; # set if not running in a terminal
51 # Avoid generating "subroutine redefined" warnings with the following
52 # hack (from CGI::Carp):
53 if (!defined $DB::VERSION) {
55 eval qq( sub confess {
56 \$SIG{__DIE__} = 'DEFAULT';
57 DXDebug::dbgprintring() if DXDebug::isdbg('nologchan');
59 # DXDebug::dbg(Carp::shortmess(\@_));
60 DXDebug::longmess(\@_);
64 \$SIG{__DIE__} = 'DEFAULT';
65 DXDebug::dbgprintring() if DXDebug::isdbg('nologchan');
67 # DXDebug::dbg(Carp::longmess(\@_));
68 DXDebug::shortmess(\@_);
72 DXDebug::dbgprintring(25) if DXDebug('nologchan');
73 # DXDebug::dbg(Carp::shortmess(\@_));
74 DXDebug::longmess(\@_);
77 DXDebug::dbgprintring(25) if DXDebug('nologchan');
78 # DXDebug::dbg(Carp::longmess(\@_));
79 DXDebug::longmess(\@_);
82 CORE::die(Carp::shortmess($@)) if $@;
84 eval qq( sub confess { die Carp::longmess(\@_); };
85 sub croak { die Carp::shortmess(\@_); };
86 sub cluck { warn Carp::longmess(\@_); };
87 sub carp { warn Carp::shortmess(\@_); };
92 my $_isdbg = ''; # current dbg level we are processing
97 # say "*** in dbgtrace";
100 for (my $i = 1; (my ($pkg, $fn, $l, $subr) = caller($i)); ++$i) {
101 # say "*** in dbgtrace $i";
102 next if $pkg eq 'DXDebug';
103 # say "*** in dbgtrace after package";
104 last if $pkg =~ /Mojo/;
105 # say "*** in dbgtrace $i after mojo";
107 dbg("Stack ($i): $pkg::$subr in $fn line: $l");
119 my @l = split /\n/, $r;
121 $l =~ s/([\x00-\x08\x0B-\x1f\x7f-\xff])/sprintf("%%%02X",ord($1))/eg;
122 my $tag = $_isdbg ? "($_isdbg) " : '(*) ';
123 print "$tag$l\n" if defined \*STDOUT && !$no_stdout;
124 my $str = "$t^$tag$l";
125 &$callback($str) if $callback;
127 shift @dbgring while (@dbgring > $dbgringlth);
130 $fp->writeunix($t, $str) unless !$fp || $dbglevel{"nolog$_isdbg"} ;
138 my $basename = shift || 'debug';
141 # add sig{__DIE__} handling
142 unless (defined $DB::VERSION) {
143 $SIG{__WARN__} = sub {
144 if ($_[0] =~ /Deep\s+recursion/i) {
146 dbg(Carp::longmess(@_));
151 dbg(Carp::shortmess(@_));
155 $SIG{__DIE__} = sub { dbg($@); dbg(Carp::longmess(@_)); };
157 # switch off STDOUT printing if we are not talking to a TTY
158 unless ($^O =~ /^MS/ || $^O =~ /^OS-2/) {
159 unless (isatty(STDOUT->fileno)) {
165 $fp = DXLog::new($basename, 'dat', 'd');
171 $SIG{__DIE__} = $SIG{__WARN__} = 'DEFAULT';
173 dbgprintring() if grep /nolog/, keys %dbglevel;
186 if ($dbglevel{$l} || $l eq 'err') {
189 for (my $o = 0; $o < length $l; $o += 16) {
190 my $c = substr $l, $o, 16;
191 my $h = unpack "H*", $c;
192 $c =~ s/[\x00-\x1f\x7f-\xff]/./g;
193 my $left = 16 - length $c;
194 $h .= ' ' x (2 * $left) if $left > 0;
195 push @out, $m . sprintf("%4d:", $o) . "$h $c";
196 $m = ' ' x (length $m);
199 dbg(@out) if isdbg($l); # yes, I know, I have my reasons;
207 foreach $entry (@_) {
208 $dbglevel{$entry} = 1;
216 foreach $entry (@_) {
217 delete $dbglevel{$entry};
223 return keys (%dbglevel);
229 if ($dbglevel{$_[0]}) {
249 my $count = @dbgring;
250 $i = @dbgring-$i if $i;
251 return 0 unless $i < $count; # do nothing if there is nothing to print
255 for ( ; $i < $count; ++$i) {
256 my ($t, $str) = split /\^/, $dbgring[$i], 2;
260 $fp->writeunix($lt, "$lt^###");
261 $fp->writeunix($lt, "$lt^### RINGBUFFER START at line $i (zero base)");
262 $fp->writeunix($lt, "$lt^###");
265 my $buf = sprintf "%02d:%02d:%02d", (gmtime($t))[2,1,0];
266 $fp->writeunix($lt, "$lt^RING: $buf^$str");
270 $fp->writeunix($et, "$et^###");
271 $fp->writeunix($et, "$et^### RINGBUFFER END $l debug lines written");
272 $fp->writeunix($et, "$et^###");
281 # clean out old debug files, stop when you get a gap of more than a month
284 my $date = $fp->unixtoj($main::systime)->sub($keepdays+1);
288 my $fn = $fp->_genfn($date);
296 $date = $date->sub(1);