write DXLog stuff
[spider.git] / perl / DXLog.pm
1 #
2 # the general purpose logging machine
3 #
4 # This module is designed to allow you to log stuff in specific places
5 # and will rotate logs on a monthly, weekly or daily basis. 
6 #
7 # The idea is that you give it a prefix which is a directory and then 
8 # the system will log stuff to a directory structure which looks like:-
9 #
10 # daily:-
11 #   spots/1998/<julian day no>[.<optional suffix>]
12 #
13 # weekly :-
14 #   log/1998/<week no>[.<optional suffix>]
15 #
16 # monthly
17 #   wwv/1998/<month>[.<optional suffix>]
18 #
19 # Routines are provided to read these files in and to append to them
20
21 # Copyright (c) - 1998 Dirk Koopman G1TLH
22 #
23 # $Id$
24 #
25
26 package DXLog;
27
28 use FileHandle;
29 use DXVars;
30 use DXDebug;
31 use DXUtil;
32 use Julian;
33 use Carp;
34
35 use strict;
36
37 # create a log object that contains all the useful info needed
38 # prefix is the main directory off of the data directory
39 # sort is 'm' for monthly, 'd' for daily 
40 sub new
41 {
42         my ($prefix, $suffix, $sort) = @_;
43         my $ref = {};
44         $ref->{prefix} = "$main::data/$prefix";
45         $ref->{suffix} = $suffix if $suffix;
46         $ref->{sort} = $sort;
47                 
48         # make sure the directory exists
49         mkdir($ref->{prefix}, 0777) if ! -e $ref->{prefix};
50         return bless $ref;
51 }
52
53 # open the appropriate data file
54 sub open
55 {
56         my ($self, $year, $thing, $mode) = @_;
57         
58         # if we are writing, check that the directory exists
59         if (defined $mode) {
60                 my $dir = "$self->{prefix}/$year";
61                 mkdir($dir, 0777) if ! -e $dir;
62                 $self->{mode} = $mode;
63         } else {
64                 delete $self->{mode};
65         }
66         
67         $self->{fn} = sprintf "$self->{prefix}/$year/%03d", $thing;
68         $self->{fn} .= ".$self->{suffix}" if $self->{suffix};
69         
70         $mode = 'r' if !$mode;
71         my $fh = new FileHandle $self->{fn}, $mode;
72         return undef if !$fh;
73         $fh->autoflush(1) if $mode ne 'r'; # make it autoflushing if writable
74         $self->{fh} = $fh;
75
76         $self->{year} = $year;
77         $self->{thing} = $thing;
78         
79         dbg("dxlog", "opening $self->{fn}\n");
80         
81         return $self->{fh};
82 }
83
84 # open the previous log file in sequence
85 sub openprev
86 {
87         my $self = shift;
88         if ($self->{sort} eq 'm') {
89                 ($self->{year}, $self->{thing}) = Julian::subm($self->{year}, $self->{thing}, 1);
90         } elsif ($self->{sort} eq 'd') {
91                 ($self->{year}, $self->{thing}) = Julian::sub($self->{year}, $self->{thing}, 1);
92         }
93         return $self->open($self->{year}, $self->{thing}, @_);
94 }
95
96 # open the next log file in sequence
97 sub opennext
98 {
99         my $self = shift;
100         if ($self->{sort} eq 'm') {
101                 ($self->{year}, $self->{thing}) = Julian::addm($self->{year}, $self->{thing}, 1);
102         } elsif ($self->{sort} eq 'd') {
103                 ($self->{year}, $self->{thing}) = Julian::add($self->{year}, $self->{thing}, 1);
104         }
105         return $self->open($self->{year}, $self->{thing}, @_);
106 }
107
108 # write (actually append) to a file, opening new files as required
109 sub write
110 {
111         my ($self, $year, $thing, $line) = @_;
112         $self->open($year, $thing, ">>") if (!$self->{fh} || 
113                                                                                  $self->{mode} ne ">>" || 
114                                                                                  $year != $self->{year} || 
115                                                                                  $thing != $self->{thing})
116                 or confess "can't open $self->{fn} $!";
117
118         $self->{fh}->print("$line\n");
119         return $self;
120 }
121
122 # write (actually append) using the current date to a file, opening new files as required
123 sub writenow
124 {
125         my ($self, $line) = @_;
126         my @date = unixtoj(time) if $self->{sort} = 'd';
127         @date = unixtojm(time) if $self->{sort} = 'm';
128         
129         return $self->write(@date, $line);
130 }
131
132 # close the log file handle
133 sub close
134 {
135         my $self = shift;
136         undef $self->{fh};                      # close the filehandle
137         delete $self->{fh};
138         delete $self->{mode};
139 }
140
141 sub DESTROY                                             # catch undefs and do what is required further do the tree
142 {
143         my $self = shift;
144         dbg("dxlog", "closing $self->{fn}\n");
145         undef $self->{fh} if defined $self->{fh};
146
147 1;