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