Send wind even if it hasn't changed
[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         $ref->{dayno} = int (time / 86400);
57                 
58         # make sure the directory exists
59         mkpath($dir, 0, 0777) unless -d $dir;
60         die "cannot create or access $dir $!" unless -d $dir;
61         
62         my $self = bless $ref, $pkg;
63         $open{$self} = $self;
64         return $self;
65 }
66
67 sub mode
68 {
69         my $self = shift;
70         $self->{mode} = shift if @_;
71         return $self->{mode};
72 }
73
74 # open the appropriate data file
75 sub open
76 {
77         my ($self, $dayno, $mode) = @_;
78         
79         my ($year, $month, $day) = (gmtime($dayno * 86400))[5,4,3];
80         $year += 1900;
81         $month += 1;
82         
83         # if we are writing, check that the directory exists
84         if (defined $mode) {
85                 my $dir = "$self->{prefix}/$year";
86                 mkdir($dir, 0777) if ! -e $dir;
87         }
88         
89         $self->{fn} = sprintf "$self->{prefix}/$year/%02d%02d", $month, $day;
90         $self->{fn} .= ".$self->{suffix}" if $self->{suffix};
91         
92         $self->{mode} = $mode || 'a+';
93         
94         my $fh = new IO::File $self->{fn}, $mode, 0666;
95         return unless $fh;
96         
97         $fh->autoflush(0) if $mode ne 'r'; # disable autoflushing if writable
98         $self->{fh} = $fh;
99
100         $self->{year} = $year;
101         $self->{month} = $month;
102         $self->{day} = $day;
103         $self->{dayno} = $dayno;
104                 
105 #       DXDebug::dbg("dxlog", "opening $self->{fn}\n");
106         
107         return $self->{fh};
108 }
109
110 # open the previous log file in sequence
111 sub openprev
112 {
113         my $self = shift;
114         return $self->open($self->{dayno} - 1, @_);
115 }
116
117 # open the next log file in sequence
118 sub opennext
119 {
120         my $self = shift;
121         return $self->open($self->{dayno} + 1, @_);
122 }
123
124 # write (actually append) to a file, opening new files as required
125 sub write
126 {
127         my ($self, $dayno, $line) = @_;
128         if ((!$self->{fh} || $dayno != $self->{dayno}) && $self->{mode} ne "r") {
129                 $self->open($dayno, "a+") or confess "can't open $self->{fn} $!";
130         } 
131
132         return $self->{fh} ? $self->{fh}->print("$line\n") : undef;
133 }
134
135 # read a line from an opened file
136 sub read
137 {
138         my $self = shift;
139         confess "can't read $self->{fh} $!" unless $self->{fh};
140         return $self->{fh}->getline;
141 }
142
143 # write (actually append) using the current date to a file, opening new files as required
144 sub writenow
145 {
146         my ($self, $line) = @_;
147         my $dayno = int (time / 86400);
148         return $self->write($dayno, $line);
149 }
150
151 # write (actually append) using a unix time to a file, opening new files as required
152 sub writeunix
153 {
154         my ($self, $t, $line) = @_;
155         my $dayno = int ($t / 86400);
156         return $self->write($dayno, $line);
157 }
158
159 # close the log file handle
160 sub close
161 {
162         my $self = shift;
163         undef $self->{fh};                      # close the filehandle
164         delete $self->{fh};     
165 }
166
167 sub DESTROY
168 {
169         my $self = shift;
170
171         delete $open{$self};
172         undef $self->{fh};                      # close the filehandle
173         delete $self->{fh} if $self->{fh};
174 }
175
176 sub flush
177 {
178         $_[0]->{fh}->flush if $_[0]->{fh};
179 }
180
181 sub flushall
182 {
183         foreach my $v (values %open) {
184                 $v->flush;
185         }
186 }
187
188 sub flush_all { goto &flushall }
189
190
191 sub Log
192 {
193         my $l = ref $_[0] ? shift : $log;
194         return unless $l;
195         my $t = time;
196         my $ts = sprintf("%02d:%02d:%02d", (gmtime($t))[2,1,0]);
197         $l->writeunix($t, "$ts $_") for @_;
198 }
199
200 sub LogDbg
201 {
202     Log(@_);
203     Debug::dbg(@_) if Debug::isdbg('chan');
204 }
205
206 1;