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