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