Some more optimisations
[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 #
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 $cleandays $keepdays);
18
19 use DXUtil;
20 use DXLog ();
21 use Carp ();
22 use POSIX qw(isatty);
23
24 %dbglevel = ();
25 $fp = undef;
26 $callback = undef;
27 $keepdays = 10;
28 $cleandays = 100;
29
30 our $no_stdout;                                 # set if not running in a terminal
31
32 # Avoid generating "subroutine redefined" warnings with the following
33 # hack (from CGI::Carp):
34 if (!defined $DB::VERSION) {
35         local $^W=0;
36         eval qq( sub confess { 
37             \$SIG{__DIE__} = 'DEFAULT'; 
38         DXDebug::dbg(\$@);
39                 DXDebug::dbg(Carp::shortmess(\@_));
40             exit(-1); 
41         }
42         sub croak { 
43                 \$SIG{__DIE__} = 'DEFAULT'; 
44         DXDebug::dbg(\$@);
45                 DXDebug::dbg(Carp::longmess(\@_));
46                 exit(-1); 
47         }
48         sub carp    { DXDebug::dbg(Carp::shortmess(\@_)); }
49         sub cluck   { DXDebug::dbg(Carp::longmess(\@_)); } 
50         );
51
52     CORE::die(Carp::shortmess($@)) if $@;
53 } else {
54     eval qq( sub confess { die Carp::longmess(\@_); }; 
55                          sub croak { die Carp::shortmess(\@_); }; 
56                          sub cluck { warn Carp::longmess(\@_); }; 
57                          sub carp { warn Carp::shortmess(\@_); }; 
58    );
59
60
61
62 sub dbg($)
63 {
64         return unless $fp;
65         my $t = time; 
66         for (@_) {
67                 my $r = $_;
68                 chomp $r;
69                 my @l = split /\n/, $r;
70                 for (@l) {
71                         s/([\x00-\x08\x0B-\x1f\x7f-\xff])/uc sprintf("%%%02x",ord($1))/eg;
72                         print "$_\n" if defined \*STDOUT && !$no_stdout;
73                         my $str = "$t^$_";
74                         &$callback($str) if $callback;
75                         $fp->writeunix($t, $str); 
76                 }
77         }
78 }
79
80 sub dbginit
81 {
82         $callback = shift;
83         
84         # add sig{__DIE__} handling
85         unless (defined $DB::VERSION) {
86                 $SIG{__WARN__} = sub { 
87                         if ($_[0] =~ /Deep\s+recursion/i) {
88                                 dbg($@);
89                                 dbg(Carp::longmess(@_)); 
90                                 CORE::die;
91                         } else { 
92                                 dbg($@);
93                                 dbg(Carp::shortmess(@_));
94                         }
95                 };
96                 
97                 $SIG{__DIE__} = sub { dbg($@); dbg(Carp::longmess(@_)); };
98
99                 # switch off STDOUT printing if we are not talking to a TTY
100                 unless ($^O =~ /^MS/ || $^O =~ /^OS-2/) {
101                         unless (isatty(STDOUT->fileno)) {
102                                 ++$no_stdout;
103                         }
104                 }
105         }
106
107         $fp = DXLog::new('debug', 'dat', 'd');
108 }
109
110 sub dbgclose
111 {
112         $SIG{__DIE__} = $SIG{__WARN__} = 'DEFAULT';
113         $fp->close() if $fp;
114         undef $fp;
115 }
116
117 sub dbgdump
118 {
119         my $l = shift;
120         my $m = shift;
121         if ($fp && ($dbglevel{$l} || $l eq 'err')) {
122                 foreach my $l (@_) {
123                         for (my $o = 0; $o < length $l; $o += 16) {
124                                 my $c = substr $l, $o, 16;
125                                 my $h = unpack "H*", $c;
126                                 $c =~ s/[\x00-\x1f\x7f-\xff]/./g;
127                                 my $left = 16 - length $c;
128                                 $h .= ' ' x (2 * $left) if $left > 0;
129                                 dbg($m . sprintf("%4d:", $o) . "$h $c");
130                                 $m = ' ' x (length $m);
131                         }
132                 }
133         }
134 }
135
136 sub dbgadd
137
138         my $entry;
139         
140         foreach $entry (@_) {
141                 $dbglevel{$entry} = 1;
142         }
143 }
144
145 sub dbgsub
146 {
147         my $entry;
148         
149         foreach $entry (@_) {
150                 delete $dbglevel{$entry};
151         }
152 }
153
154 sub dbglist
155 {
156         return keys (%dbglevel);
157 }
158
159 sub isdbg($)
160 {
161         return unless $fp;
162         return $dbglevel{$_[0]};
163 }
164
165 sub shortmess 
166 {
167         return Carp::shortmess(@_);
168 }
169
170 sub longmess 
171
172         return Carp::longmess(@_);
173 }
174
175 # clean out old debug files, stop when you get a gap of more than a month
176 sub dbgclean
177 {
178         my $date = $fp->unixtoj($main::systime)->sub($keepdays+1);
179         my $i = 0;
180
181         while ($i < 31) {
182                 my $fn = $fp->_genfn($date);
183                 if (-e $fn) {
184                         unlink $fn;
185                         $i = 0;
186                 } else {
187                         $i++;
188                 }
189                 $date = $date->sub(1);
190         }
191 }
192
193 1;
194 __END__
195
196
197
198
199
200
201