]> dxcluster.net Git - spider.git/blob - perl/DXDebug.pm
make sure that the actual line is printed in grepdbg & watchdbg esp. with
[spider.git] / perl / DXDebug.pm
1 #
2 # The system variables - those indicated will need to be changed to suit your
3 # circumstances (and callsign)
4 #
5 # Copyright (c) 1998 - Dirk Koopman G1TLH
6 #
7 # $Id$
8 #
9
10 package DXDebug;
11
12 require Exporter;
13 @ISA = qw(Exporter);
14 @EXPORT = qw(dbginit dbg dbgadd dbgsub dbglist dbgdump isdbg dbgclose confess croak cluck);
15
16 use strict;
17 use vars qw(%dbglevel $fp $callback);
18
19 use DXUtil;
20 use DXLog ();
21 use Carp ();
22
23 %dbglevel = ();
24 $fp = undef;
25 $callback = undef;
26
27 # Avoid generating "subroutine redefined" warnings with the following
28 # hack (from CGI::Carp):
29 if (!defined $DB::VERSION) {
30         local $^W=0;
31         eval qq( sub confess { 
32             \$SIG{__DIE__} = 'DEFAULT'; 
33         DXDebug::dbg(\$@);
34                 DXDebug::dbg(Carp::shortmess(\@_));
35             exit(-1); 
36         }
37         sub croak { 
38                 \$SIG{__DIE__} = 'DEFAULT'; 
39         DXDebug::dbg(\$@);
40                 DXDebug::dbg(Carp::longmess(\@_));
41                 exit(-1); 
42         }
43         sub carp    { DXDebug::dbg(Carp::shortmess(\@_)); }
44         sub cluck   { DXDebug::dbg(Carp::longmess(\@_)); } 
45         );
46
47     CORE::die(Carp::shortmess($@)) if $@;
48 } else {
49     eval qq( sub confess { die Carp::longmess(\@_); }; 
50                          sub croak { die Carp::shortmess(\@_); }; 
51                          sub cluck { warn Carp::longmess(\@_); }; 
52                          sub carp { warn Carp::shortmess(\@_); }; 
53    );
54
55
56
57 sub dbg($)
58 {
59         return unless $fp;
60         my $t = time; 
61         for (@_) {
62                 my $r = $_;
63                 chomp $r;
64                 my @l = split /\n/, $r;
65                 for (@l) {
66                         s/([\x00-\x08\x0B-\x1f\x7f-\xff])/uc sprintf("%%%02x",ord($1))/eg;
67                         print "$_\n" if defined \*STDOUT;
68                         my $str = "$t^$_";
69                         &$callback($str) if $callback;
70                         $fp->writeunix($t, $str); 
71                 }
72         }
73 }
74
75 sub dbginit
76 {
77         $callback = shift;
78         
79         # add sig{__DIE__} handling
80         if (!defined $DB::VERSION) {
81                 $SIG{__WARN__} = sub { 
82                         if ($_[0] =~ /Deep\s+recursion/i) {
83                                 dbg($@);
84                                 dbg(Carp::longmess(@_)); 
85                                 CORE::die;
86                         } else { 
87                                 dbg($@);
88                                 dbg(Carp::shortmess(@_));
89                         }
90                 };
91                 
92                 $SIG{__DIE__} = sub { dbg($@); dbg(Carp::longmess(@_)); };
93         }
94
95         $fp = DXLog::new('debug', 'dat', 'd');
96 }
97
98 sub dbgclose
99 {
100         $SIG{__DIE__} = $SIG{__WARN__} = 'DEFAULT';
101         $fp->close() if $fp;
102         undef $fp;
103 }
104
105 sub dbgdump
106 {
107         my $l = shift;
108         my $m = shift;
109         if ($fp && ($dbglevel{$l} || $l eq 'err')) {
110                 foreach my $l (@_) {
111                         for (my $o = 0; $o < length $l; $o += 16) {
112                                 my $c = substr $l, $o, 16;
113                                 my $h = unpack "H*", $c;
114                                 $c =~ s/[\x00-\x1f\x7f-\xff]/./g;
115                                 my $left = 16 - length $c;
116                                 $h .= ' ' x (2 * $left) if $left > 0;
117                                 dbg($m . sprintf("%4d:", $o) . "$h $c");
118                                 $m = ' ' x (length $m);
119                         }
120                 }
121         }
122 }
123
124 sub dbgadd
125
126         my $entry;
127         
128         foreach $entry (@_) {
129                 $dbglevel{$entry} = 1;
130         }
131 }
132
133 sub dbgsub
134 {
135         my $entry;
136         
137         foreach $entry (@_) {
138                 delete $dbglevel{$entry};
139         }
140 }
141
142 sub dbglist
143 {
144         return keys (%dbglevel);
145 }
146
147 sub isdbg($)
148 {
149         return unless $fp;
150         return $dbglevel{$_[0]};
151 }
152
153 sub shortmess 
154 {
155         return Carp::shortmess(@_);
156 }
157
158 sub longmess 
159
160         return Carp::longmess(@_);
161 }
162
163 1;
164 __END__
165
166
167
168
169
170
171