add manuals to git
[dweather.git] / SMGLog.pm
1 #
2 # the general purpose logging machine
3 #
4 # This module is designed to allow you to log stuff in SMG format
5 #
6 # The idea is that you give it a prefix which is a directory and then 
7 # the system will log stuff to a directory structure which looks like:-
8 #
9 # ./logs/<prefix>/yyyy/mmdd.[log|<optional suffix]
10 #   
11 # Routines are provided to read these files in and to append to them
12
13 # Copyright (c) - 1998-2007 Dirk Koopman G1TLH
14 #
15 # This library is free software; you can redistribute it and/or
16 # modify it under the same terms as Perl itself.
17 #
18
19 package SMGLog;
20
21 use IO::File;
22 use Exporter;
23 use Carp;
24 use File::Path;
25
26 @ISA = qw(Exporter);
27 @EXPORT = qw(Log LogDbg);
28 $VERSION = 1.20;
29
30 use strict;
31
32 use vars qw($log $path);
33 $log = undef;
34 $path = './logs';
35
36 my %open;
37
38 init();
39
40 # make the Log() export use this default file
41 sub init
42 {
43         $log = SMGLog->new("sys_log");
44 }
45
46 # create a log object that contains all the useful info needed
47 # prefix is the main directory off of the data directory
48 # suffix is the suffix after the month/day
49 sub new
50 {
51         my ($pkg, $prefix, $suffix) = @_;
52         my $ref = {};
53         my $dir = "$path/$prefix";
54         $ref->{prefix} = $dir;
55         $ref->{suffix} = $suffix || 'log';
56                 
57         # make sure the directory exists
58         mkpath($dir, 0, 0777) unless -d $dir;
59         die "cannot create or access $dir $!" unless -d $dir;
60         
61         my $self = bless $ref, $pkg;
62         $open{$self} = $self;
63         return $self;
64 }
65
66 sub mode
67 {
68         my $self = shift;
69         $self->{mode} = shift if @_;
70         return $self->{mode};
71 }
72
73 # open the appropriate data file
74 sub open
75 {
76         my ($self, $dayno, $mode) = @_;
77         
78         my ($year, $month, $day) = (gmtime($dayno * 86400))[5,4,3];
79         $year += 1900;
80         $month += 1;
81         
82         # if we are writing, check that the directory exists
83         if (defined $mode) {
84                 my $dir = "$self->{prefix}/$year";
85                 mkdir($dir, 0777) if ! -e $dir;
86         }
87         
88         $self->{fn} = sprintf "$self->{prefix}/$year/%02d%02d", $month, $day;
89         $self->{fn} .= ".$self->{suffix}" if $self->{suffix};
90         
91         $self->{mode} = $mode || 'a+';
92         
93         my $fh = new IO::File $self->{fn}, $mode, 0666;
94         return unless $fh;
95         
96         $fh->autoflush(0) if $mode ne 'r'; # disable autoflushing if writable
97         $self->{fh} = $fh;
98
99         $self->{year} = $year;
100         $self->{month} = $month;
101         $self->{day} = $day;
102         $self->{dayno} = $dayno;
103                 
104 #       DXDebug::dbg("dxlog", "opening $self->{fn}\n");
105         
106         return $self->{fh};
107 }
108
109 # open the previous log file in sequence
110 sub openprev
111 {
112         my $self = shift;
113         return $self->open($self->{dayno} - 1, @_);
114 }
115
116 # open the next log file in sequence
117 sub opennext
118 {
119         my $self = shift;
120         return $self->open($self->{dayno} + 1, @_);
121 }
122
123 # write (actually append) to a file, opening new files as required
124 sub write
125 {
126         my ($self, $dayno, $line) = @_;
127         if ((!$self->{fh} || $dayno != $self->{dayno}) && $self->{mode} ne "r") {
128                 $self->open($dayno, "a+") or confess "can't open $self->{fn} $!";
129         } 
130
131         return $self->{fh} ? $self->{fh}->print("$line\n") : undef;
132 }
133
134 # read a line from an opened file
135 sub read
136 {
137         my $self = shift;
138         confess "can't read $self->{fh} $!" unless $self->{fh};
139         return $self->{fh}->getline;
140 }
141
142 # write (actually append) using the current date to a file, opening new files as required
143 sub writenow
144 {
145         my ($self, $line) = @_;
146         my $dayno = int (time / 86400);
147         return $self->write($dayno, $line);
148 }
149
150 # write (actually append) using a unix time to a file, opening new files as required
151 sub writeunix
152 {
153         my ($self, $t, $line) = @_;
154         my $dayno = int ($t / 86400);
155         return $self->write($dayno, $line);
156 }
157
158 # close the log file handle
159 sub close
160 {
161         my $self = shift;
162         undef $self->{fh};                      # close the filehandle
163         delete $self->{fh};     
164 }
165
166 sub DESTROY
167 {
168         my $self = shift;
169
170         delete $open{$self};
171         undef $self->{fh};                      # close the filehandle
172         delete $self->{fh} if $self->{fh};
173 }
174
175 sub flush
176 {
177         $_[0]->{fh}->flush if $_[0]->{fh};
178 }
179
180 sub flushall
181 {
182         foreach my $v (values %open) {
183                 $v->flush;
184         }
185 }
186
187 sub flush_all { goto &flushall }
188
189
190 sub Log
191 {
192         my $l = ref $_[0] ? shift : $log;
193         return unless $l;
194         my $t = time;
195         my $ts = sprintf("%02d:%02d:%02d", (gmtime($t))[2,1,0]);
196         $l->writeunix($t, "$ts $_") for @_;
197 }
198
199 sub LogDbg
200 {
201     Log(@_);
202     Debug::dbg(@_) if Debug::isdbg('chan');
203 }
204
205 1;