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