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