a1c63407f71f1b2aba38c58782dbe77f0a73e63b
[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 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 = DXLog::new('debug', 'dat', 'd');
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::_store(\$@, Carp::shortmess(\@_));
33             exit(-1); 
34         }
35         sub croak { 
36                 \$SIG{__DIE__} = 'DEFAULT'; 
37         DXDebug::_store(\$@, Carp::longmess(\@_));
38                 exit(-1); 
39         }
40         sub carp    { DXDebug::_store(Carp::shortmess(\@_)); }
41         sub cluck   { DXDebug::_store(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 _store
53 {
54         my $t = time; 
55         for (@_) {
56                 chomp;
57                 my @l = split /\n/;
58                 for (@l) {
59                         print "$_\n" if defined \*STDOUT;
60                         $fp->writeunix($t, "$t^$_"); 
61                 }
62         }
63 }
64
65 sub dbginit
66 {
67         # add sig{__DIE__} handling
68         if (!defined $DB::VERSION) {
69                 $SIG{__WARN__} = sub { _store($@, Carp::shortmess(@_)); };
70                 $SIG{__DIE__} = sub { _store($@, Carp::longmess(@_)); };
71         }
72 }
73
74 sub dbgclose
75 {
76         $SIG{__DIE__} = $SIG{__WARN__} = 'DEFAULT';
77         $fp->close();
78 }
79
80 sub dbg
81 {
82         my $l = shift;
83         if ($dbglevel{$l} || $l eq 'err') {
84             my @in = @_;
85                 my $t = time;
86                 for (@in) {
87                     s/\n$//o;
88                         s/\a//og;   # beeps
89                         print "$_\n" if defined \*STDOUT;
90                         $fp->writeunix($t, "$t^$_");
91                 }
92         }
93 }
94
95 sub dbgadd
96
97         my $entry;
98         
99         foreach $entry (@_) {
100                 $dbglevel{$entry} = 1;
101         }
102 }
103
104 sub dbgsub
105 {
106         my $entry;
107         
108         foreach $entry (@_) {
109                 delete $dbglevel{$entry};
110         }
111 }
112
113 sub dbglist
114 {
115         return keys (%dbglevel);
116 }
117
118 sub isdbg
119 {
120         my $s = shift;
121         return $dbglevel{$s};
122 }
123
124 sub shortmess 
125 {
126         return Carp::shortmess(@_);
127 }
128
129 sub longmess 
130
131         return Carp::longmess(@_);
132 }
133
134 1;
135 __END__
136
137
138
139
140
141
142