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