add basic skeleton to git
[dweather.git] / DWeather / lib / DWeather / Log.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
29 use strict;
30
31 use vars qw($log $path);
32 $log = undef;
33 $path = './logs';
34
35 init();
36
37 # make the Log() export use this default file
38 sub init
39 {
40         $log = SMGLog->new("sys_log");
41 }
42
43 # create a log object that contains all the useful info needed
44 # prefix is the main directory off of the data directory
45 # suffix is the suffix after the month/day
46 sub new
47 {
48         my ($pkg, $prefix, $suffix) = @_;
49         my $ref = {};
50         my $dir = "$path/$prefix";
51         $ref->{prefix} = $dir;
52         $ref->{suffix} = $suffix || 'log';
53                 
54         # make sure the directory exists
55         mkpath($dir, 0, 0777) unless -d $dir;
56         die "cannot create or access $dir $!" unless -d $dir;
57         
58         return bless $ref, $pkg;
59 }
60
61 # open the appropriate data file
62 sub open
63 {
64         my ($self, $dayno, $mode) = @_;
65         
66         my ($year, $month, $day) = (gmtime($dayno * 86400))[5,4,3];
67         $year += 1900;
68         $month += 1;
69         
70         # if we are writing, check that the directory exists
71         if (defined $mode) {
72                 my $dir = "$self->{prefix}/$year";
73                 mkdir($dir, 0777) if ! -e $dir;
74         }
75         
76         $self->{fn} = sprintf "$self->{prefix}/$year/%02d%02d", $month, $day;
77         $self->{fn} .= ".$self->{suffix}" if $self->{suffix};
78         
79         $self->{mode} = $mode || 'r';
80         
81         my $fh = new IO::File $self->{fn}, $mode, 0666;
82         return unless $fh;
83         
84         $fh->autoflush(1) if $mode ne 'r'; # make it autoflushing if writable
85         $self->{fh} = $fh;
86
87         $self->{year} = $year;
88         $self->{month} = $month;
89         $self->{day} = $day;
90         $self->{dayno} = $dayno;
91                 
92 #       DXDebug::dbg("dxlog", "opening $self->{fn}\n");
93         
94         return $self->{fh};
95 }
96
97 # open the previous log file in sequence
98 sub openprev
99 {
100         my $self = shift;
101         return $self->open($self->{dayno} - 1, @_);
102 }
103
104 # open the next log file in sequence
105 sub opennext
106 {
107         my $self = shift;
108         return $self->open($self->{dayno} + 1, @_);
109 }
110
111 # write (actually append) to a file, opening new files as required
112 sub write
113 {
114         my ($self, $dayno, $line) = @_;
115         if (!$self->{fh} || 
116                 $self->{mode} ne ">>" || 
117                 $dayno != $self->{dayno}) {
118                 $self->open($dayno, ">>") or confess "can't open $self->{fn} $!";
119         }
120
121         return $self->{fh}->print("$line\n");
122 }
123
124 # read a line from an opened file
125 sub read
126 {
127         my $self = shift;
128         confess "can't read $self->{fh} $!" unless $self->{fh};
129         return $self->{fh}->getline;
130 }
131
132 # write (actually append) using the current date to a file, opening new files as required
133 sub writenow
134 {
135         my ($self, $line) = @_;
136         my $dayno = int (time / 86400);
137         return $self->write($dayno, $line);
138 }
139
140 # write (actually append) using a unix time to a file, opening new files as required
141 sub writeunix
142 {
143         my ($self, $t, $line) = @_;
144         my $dayno = int ($t / 86400);
145         return $self->write($dayno, $line);
146 }
147
148 # close the log file handle
149 sub close
150 {
151         my $self = shift;
152         undef $self->{fh};                      # close the filehandle
153         delete $self->{fh};     
154 }
155
156 sub DESTROY
157 {
158         my $self = shift;
159         undef $self->{fh};                      # close the filehandle
160         delete $self->{fh} if $self->{fh};
161 }
162
163 sub Log
164 {
165         my $l = ref $_[0] ? shift : $log;
166         return unless $l;
167         my $t = time;
168         my $ts = sprintf("%02d:%02d:%02d", (gmtime($t))[2,1,0]);
169         $l->writeunix($t, "$ts $_") for @_;
170 }
171
172 sub LogDbg
173 {
174     Log(@_);
175     Debug::dbg(@_) if Debug::isdbg('chan');
176 }
177
178 1;