3535184b3461918e1d853ea9a946f04de2417d1e
[dweather.git] / DWeather / lib / DWeather / Debug.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: Debug.pm,v 1.1 2001/05/18 14:02:10 djk Exp $
8 #
9 # This library is free software; you can redistribute it and/or
10 # modify it under the same terms as Perl itself.
11 #
12
13 package DWeather::Debug;
14
15 require Exporter;
16 @ISA = qw(Exporter);
17 @EXPORT = qw(dbginit dbg dbgadd dbgsub dbglist dbgdump isdbg dbgclose confess croak cluck carp);
18 $VERSION = sprintf( "%d.%02d", q$Revision: 1.1 $ =~ /(\d+)\.(\d+)/ );
19
20 use strict;
21 use vars qw(%dbglevel $fp);
22
23 use DWeather::Logger;
24 use Carp qw(cluck);
25
26 %dbglevel = ();
27 $fp = undef;
28
29 # Avoid generating "subroutine redefined" warnings with the following
30 # hack (from CGI::Carp):
31 if (!defined $DB::VERSION) {
32         local $^W=0;
33         eval qq( sub confess { 
34             \$SIG{__DIE__} = 'DEFAULT'; 
35         Debug::dbg(\$@, Carp::shortmess(\@_));
36             exit(-1); 
37         }
38         sub croak { 
39                 \$SIG{__DIE__} = 'DEFAULT'; 
40         Debug::dbg(\$@, Carp::longmess(\@_));
41                 exit(-1); 
42         }
43         sub carp    { Debug::dbg(Carp::shortmess(\@_)); }
44         sub cluck   { Debug::dbg(Carp::longmess(\@_)); } 
45         );
46
47     CORE::die(Carp::shortmess($@)) if $@;
48 } else {
49     eval qq( sub confess { Carp::confess(\@_); }
50         sub cluck { Carp::cluck(\@_); } 
51         sub carp { Carp::cluck(\@_); } 
52    );
53
54
55 sub dbg
56 {
57         my $t = time; 
58         my $ts = sprintf("%02d:%02d:%02d", (gmtime($t))[2,1,0]);
59         for (@_) {
60                 my $r = $_;
61                 chomp $r;
62                 my @l = split /\n/, $r;
63                 for (@l) {
64                         s/([\x00-\x08\x0B-\x1f\x7f-\xff])/uc sprintf("%%%02x",ord($1))/eg;
65 #                       print "$_\n" if defined \*STDOUT;
66                         $fp->writeunix($t, "$ts $_"); 
67                 }
68         }
69 }
70
71 sub dbginit
72 {
73         # add sig{__DIE__} handling
74         if (!defined $DB::VERSION) {
75                 $SIG{__WARN__} = sub { dbg($@, Carp::shortmess(@_)); };
76                 $SIG{__DIE__} = sub { dbg($@, Carp::longmess(@_)); };
77         }
78
79         $fp = DWeather::Logger->new('debug', 'log', 'd') unless $fp;
80 }
81
82 sub dbgclose
83 {
84         $SIG{__DIE__} = $SIG{__WARN__} = 'DEFAULT';
85         $fp->close() if $fp;
86         undef $fp;
87 }
88
89 sub dbgdump
90 {
91         my $m = shift;
92
93         foreach my $l (@_) {
94                 my $p = $m;
95                 for (my $o = 0; $o < length $l; $o += 16) {
96                         my $c = substr $l, $o, 16;
97                         my $h = unpack "H*", $c;
98                         $c =~ s/[\x00-\x1f\x7f-\xff]/./g;
99                         my $left = 16 - length $c;
100                         $h .= ' ' x (2 * $left) if $left > 0;
101                         dbg($p . sprintf("%4d:", $o) . "$h $c");
102                         $p = ' ' x (length $p);
103                 }
104         }
105 }
106
107 sub dbgadd
108
109         my $entry;
110         
111         foreach $entry (@_) {
112                 $dbglevel{$entry} = 1;
113         }
114 }
115
116 sub dbgsub
117 {
118         my $entry;
119         
120         foreach $entry (@_) {
121                 delete $dbglevel{$entry};
122         }
123 }
124
125 sub dbglist
126 {
127         return keys (%dbglevel);
128 }
129
130 sub isdbg
131 {
132         return undef unless $fp;
133         return $dbglevel{$_[0]};
134 }
135
136 sub shortmess 
137 {
138         return Carp::shortmess(@_);
139 }
140
141 sub longmess 
142
143         return Carp::longmess(@_);
144 }
145
146 1;
147 __END__
148
149
150
151
152
153
154