1 # File: Stem/Log/File.pm
3 # This file is part of Stem.
4 # Copyright (C) 1999, 2000, 2001 Stem Systems, Inc.
6 # Stem is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # Stem is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with Stem; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 # For a license to use the Stem under conditions other than those
21 # described here, to purchase support for this software, or to purchase a
22 # commercial warranty contract, please contact Stem Systems at:
24 # Stem Systems, Inc. 781-643-7504
25 # 79 Everett St. info@stemsystems.com
35 package Stem::Log::File ;
37 #########################
38 #########################
39 # add stuff for file rotation, number suffix, etc.
40 #########################
41 #########################
49 The path for the physical log file
54 'default' => '%Y%m%d%H%M%S',
56 Format passed to strftime to print the log file suffix timestamp
64 Make strftime use gmtime instead of localtime for the suffix timestamp
72 This is a list of option key/value pairs that can be applied to log rotation.
81 my( $class ) = shift ;
83 my $self = Stem::Class::parse_args( $attr_spec_log, @_ ) ;
84 return $self unless ref $self ;
86 if ( my $rotate_options = $self->{'rotate'} ) {
88 if ( ref $rotate_options eq 'ARRAY' ) {
90 $self->{'rotate'} = { @{$rotate_options} } ;
94 $self->{'base_path'} = $self->{'path'} ;
95 ( $self->{'log_dir'}, $self->{'file_name'} ) =
96 File::Basename::fileparse( $self->{'path'} ) ;
98 my $err = $self->_open_file() ;
107 my( $self, $text ) = @_ ;
109 $self->{'fh'}->print( $text ) ;
111 $self->{'size'} += length( $text ) ;
113 my $rotate_options = $self->{'rotate'} ;
115 if ( $rotate_options &&
116 $self->{'size'} >= $rotate_options->{'max_size'} ) {
138 my $fh = $self->{'fh'} ;
142 $self->_open_file() ;
150 my $open_path = $self->{'base_path'} ;
152 if ( $self->{'rotate'} ) {
154 my $suffix = $self->_get_last_suffix() ||
155 $self->_generate_suffix() ;
158 $open_path .= ".$suffix" ;
161 $self->{'open_path'} = $open_path ;
163 my $fh = IO::File->new( ">>$open_path" ) or
164 return "Can't append to log file '$open_path' $!" ;
166 $self->{'size'} = -s $fh ;
168 $fh->autoflush( 1 ) ;
170 $self->{'fh'} = $fh ;
175 sub _get_last_suffix {
179 my $log_dir = $self->{'log_dir'} ;
180 my $file_name = $self->{'file_name'} ;
184 opendir( DH, $log_dir ) || return '' ;
186 my @files = sort grep { /^$file_name/ } readdir(DH) ;
188 # return the latest file suffix
192 return $1 if $files[-1] =~ /\.(\d+$)/ ;
199 sub _generate_suffix {
205 my @time = ( $self->{'use_gmt'} ) ? gmtime : localtime ;
207 return POSIX::strftime( $self->{'strftime'}, @time ) ;