| 1 | package Net::SolarWinds::FileRotationBase; | ||||||||||
| 2 | |||||||||||
| 3 | 5 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 5 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 5 | ▶ | use IO::File; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 5 | ▶ | use File::Copy qw(move); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 5 | ▶ | use base qw(Net::SolarWinds::ConstructorHash); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 5 | ▶ | use Carp qw(croak); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | =pod | ||||||||||
| 11 | |||||||||||
| 12 | =head1 NAME | ||||||||||
| 13 | |||||||||||
| 14 | Net::SolarWinds::FileRotationBase - base file rotation framework | ||||||||||
| 15 | |||||||||||
| 16 | =head1 SYNOPSIS | ||||||||||
| 17 | |||||||||||
| 18 | use base qw(Net::SolarWinds::FileRotationBase); | ||||||||||
| 19 | |||||||||||
| 20 | =head1 DESCRIPTION | ||||||||||
| 21 | |||||||||||
| 22 | This modle is intended to be used as a base module for modules that require an auto file rotation framework. Auto file rotation is handled by file size. | ||||||||||
| 23 | |||||||||||
| 24 | |||||||||||
| 25 | =head1 OO Methods | ||||||||||
| 26 | |||||||||||
| 27 | |||||||||||
| 28 | =over 3 | ||||||||||
| 29 | |||||||||||
| 30 | =cut | ||||||||||
| 31 | |||||||||||
| 32 | =item * Object constructor | ||||||||||
| 33 | |||||||||||
| 34 | The object constructor for this class has a collection of optional arguments. Arguments are passed to the constructor in key value pairs. | ||||||||||
| 35 | |||||||||||
| 36 | Constructor useage example: | ||||||||||
| 37 | |||||||||||
| 38 | my $fr=new Net::SolarWinds::FileRotationBase(folder=>'/var/logs/myapp',basefilename=>'someapp'); | ||||||||||
| 39 | |||||||||||
| 40 | Argument list is as follows (default values shown): | ||||||||||
| 41 | |||||||||||
| 42 | # Fully quallified system path to the log file | ||||||||||
| 43 | filename=>'/path/to/some/file.log' | ||||||||||
| 44 | |||||||||||
| 45 | # sets the folder files will be created and rotated under | ||||||||||
| 46 | folder=>'.', | ||||||||||
| 47 | |||||||||||
| 48 | # sets the basefilename | ||||||||||
| 49 | basefilename => 'DefaultFile', | ||||||||||
| 50 | |||||||||||
| 51 | # used to concat the folder and filenames togeather | ||||||||||
| 52 | pathconncat => '/', | ||||||||||
| 53 | |||||||||||
| 54 | # sets thefile extention to be used | ||||||||||
| 55 | ext => 'log', | ||||||||||
| 56 | |||||||||||
| 57 | # sets the number of files to keep in rotation | ||||||||||
| 58 | maxfilecount => 3, | ||||||||||
| 59 | |||||||||||
| 60 | # sets the max file size ( 8mb ) | ||||||||||
| 61 | maxsize => 1024 * 1024 * 8, | ||||||||||
| 62 | |||||||||||
| 63 | # sets the autoflush state ( handy for real time info in the file ) | ||||||||||
| 64 | autoflush => 1, | ||||||||||
| 65 | |||||||||||
| 66 | # sets the file auto rotation flag | ||||||||||
| 67 | autorotate => 1, | ||||||||||
| 68 | |||||||||||
| 69 | # glob ref will be overwritten with the current fh | ||||||||||
| 70 | mirror=>undef|\*STDERR | ||||||||||
| 71 | |||||||||||
| 72 | =cut | ||||||||||
| 73 | |||||||||||
| 74 | 13 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 75 | 13 | my ( $class, %args ) = @_; | |||||||||
| 76 | |||||||||||
| 77 | 13 | my $max_size=1024 * 1024 * 8; | |||||||||
| 78 | 13 | my $self = $class->SUPER::new( | |||||||||
| 79 | folder => '.', | ||||||||||
| 80 | basefilename => 'DefaultFile', | ||||||||||
| 81 | pathconncat => '/', | ||||||||||
| 82 | ext => 'log', | ||||||||||
| 83 | maxfilecount => 3, | ||||||||||
| 84 | maxsize => $max_size, | ||||||||||
| 85 | autoflush => 1, | ||||||||||
| 86 | autorotate => 1, | ||||||||||
| 87 | mirror =>undef, | ||||||||||
| 88 | %args | ||||||||||
| 89 | ); | ||||||||||
| 90 | |||||||||||
| 91 | 13 | return $self; | |||||||||
| 92 | } | ||||||||||
| 93 | |||||||||||
| 94 | =item * $fr->set_mirror(\*STDERR); | ||||||||||
| 95 | |||||||||||
| 96 | Sets a file handle to be mirroed to: | ||||||||||
| 97 | |||||||||||
| 98 | =cut | ||||||||||
| 99 | |||||||||||
| 100 | 1 | ▶ | sub set_mirror { | ||||||||
|
Subroutine
set_mirror: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||
| 101 | 1 | my ($self,$glob)=@_; | |||||||||
| 102 | |||||||||||
| 103 | 1 | $self->{mirror}=$glob; | |||||||||
| 104 | |||||||||||
| 105 | 1 | my $fh=$self->get_fh; | |||||||||
| 106 | 1 | ▶ | return unless $fh; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 107 | 1 | *$glob=$fh; | |||||||||
| 108 | } | ||||||||||
| 109 | |||||||||||
| 110 | =item * my $filename=$fr->generate_filename; | ||||||||||
| 111 | |||||||||||
| 112 | Retuns the filename of the active file. | ||||||||||
| 113 | |||||||||||
| 114 | =cut | ||||||||||
| 115 | |||||||||||
| 116 | 17 | ▶ | sub generate_filename { | ||||||||
|
Subroutine
generate_filename: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 117 | 17 | my ($self) = @_; | |||||||||
| 118 | |||||||||||
| 119 | |||||||||||
| 120 | 17 | ▶ | return $self->{filename} if exists $self->{filename}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 121 | |||||||||||
| 122 | my $filename = | ||||||||||
| 123 | $self->{folder} | ||||||||||
| 124 | . $self->{pathconncat} | ||||||||||
| 125 | . $self->{basefilename} . '.' | ||||||||||
| 126 | 10 | . $self->{ext}; | |||||||||
| 127 | } | ||||||||||
| 128 | |||||||||||
| 129 | =item * my $fh=$fr->get_fh; | ||||||||||
| 130 | |||||||||||
| 131 | Returns the active filehandle. | ||||||||||
| 132 | |||||||||||
| 133 | =cut | ||||||||||
| 134 | |||||||||||
| 135 | 66 | ▶ | sub get_fh { | ||||||||
|
Subroutine
get_fh: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||
| 136 | 66 | my ($self) = @_; | |||||||||
| 137 | |||||||||||
| 138 | # assumes this is defined correctly if it exists.. | ||||||||||
| 139 | # if some one mucks with the internals this can be painful! | ||||||||||
| 140 | 66 | ▶ | return $self->{fh} if exists $self->{fh}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 141 | |||||||||||
| 142 | # if we got here then we have no file handle! | ||||||||||
| 143 | |||||||||||
| 144 | 6 | my $filename = $self->generate_filename; | |||||||||
| 145 | |||||||||||
| 146 | 6 | my $fh = new IO::File(qq{>>$filename}); | |||||||||
| 147 | |||||||||||
| 148 | # failed to open the file? | ||||||||||
| 149 | # well this is pretty much the end of the world.. | ||||||||||
| 150 | # time to blame some one else! | ||||||||||
| 151 | 6 | ▶ | croak qq{Failed to create file "$filename" error was: $!\n} | ||||||||
|
Branch: 1 branch
| |||||||||||
| 152 | unless defined($fh); | ||||||||||
| 153 | |||||||||||
| 154 | 6 | $fh->autoflush( $self->{autoflush} ); | |||||||||
| 155 | |||||||||||
| 156 | 6 | $self->{fh} = $fh; | |||||||||
| 157 | 6 | ▶ | if(defined($self->{mirror})) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 158 | 2 | my $mirror=$self->{mirror}; | |||||||||
| 159 | 2 | *$mirror=$fh; | |||||||||
| 160 | } | ||||||||||
| 161 | |||||||||||
| 162 | 6 | return $fh; | |||||||||
| 163 | } | ||||||||||
| 164 | |||||||||||
| 165 | |||||||||||
| 166 | =item * $fr->rotate_files | ||||||||||
| 167 | |||||||||||
| 168 | Forces log files to be rotated now! | ||||||||||
| 169 | |||||||||||
| 170 | =cut | ||||||||||
| 171 | |||||||||||
| 172 | 3 | ▶ | sub rotate_files { | ||||||||
|
Subroutine
rotate_files: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||
| 173 | 3 | my ($self) = @_; | |||||||||
| 174 | |||||||||||
| 175 | 3 | my $max_files = $self->{maxfilecount}; | |||||||||
| 176 | 3 | my $basefile = $self->generate_filename; | |||||||||
| 177 | 3 | ▶ | ROTATE_LOOP: for ( my $count = $max_files ; $count > 0 ; --$count ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 178 | |||||||||||
| 179 | 6 | my $targetfile = qq{$basefile.$count}; | |||||||||
| 180 | |||||||||||
| 181 | # skip this file if it does not exist | ||||||||||
| 182 | 6 | ▶ | next ROTATE_LOOP unless ( -e $targetfile ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 183 | |||||||||||
| 184 | 3 | my $next_count = 1 + $count; | |||||||||
| 185 | 3 | my $nextfile = $basefile . '.' . ($next_count); | |||||||||
| 186 | |||||||||||
| 187 | # last file check | ||||||||||
| 188 | 3 | ▶ | if ( $next_count > $max_files ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 189 | |||||||||||
| 190 | # this is the last file to manage in the list | ||||||||||
| 191 | # we look for one after it and remove it as well | ||||||||||
| 192 | # out of the goodness of our heart | ||||||||||
| 193 | 1 | ▶ | unlink $nextfile if -e $nextfile; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 194 | |||||||||||
| 195 | # delete the elder file | ||||||||||
| 196 | 1 | unlink $targetfile; | |||||||||
| 197 | 1 | next ROTATE_LOOP; | |||||||||
| 198 | |||||||||||
| 199 | } | ||||||||||
| 200 | |||||||||||
| 201 | 2 | move( $targetfile, $nextfile ); | |||||||||
| 202 | |||||||||||
| 203 | } | ||||||||||
| 204 | 3 | my $nextfile = qq{$basefile.1}; | |||||||||
| 205 | |||||||||||
| 206 | # move the current file to the .1 position | ||||||||||
| 207 | 3 | $self->close_fh; | |||||||||
| 208 | 3 | return move( $basefile, $nextfile ); | |||||||||
| 209 | |||||||||||
| 210 | } | ||||||||||
| 211 | |||||||||||
| 212 | |||||||||||
| 213 | =item * $fr->close_fh; | ||||||||||
| 214 | |||||||||||
| 215 | Closes the current active file handle. | ||||||||||
| 216 | |||||||||||
| 217 | =cut | ||||||||||
| 218 | |||||||||||
| 219 | 4 | ▶ | sub close_fh { | ||||||||
|
Subroutine
close_fh: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 220 | 4 | my ($self) = @_; | |||||||||
| 221 | |||||||||||
| 222 | 4 | ▶ | return 1 unless exists $self->{fh}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 223 | |||||||||||
| 224 | 3 | my $fh = $self->{fh}; | |||||||||
| 225 | |||||||||||
| 226 | 3 | delete $self->{fh}; | |||||||||
| 227 | |||||||||||
| 228 | 3 | return close($fh); | |||||||||
| 229 | } | ||||||||||
| 230 | |||||||||||
| 231 | =item * $fr->write_to_file("Something to write to my file\n"); | ||||||||||
| 232 | |||||||||||
| 233 | Writes the list of arguments to the target file. | ||||||||||
| 234 | |||||||||||
| 235 | =cut | ||||||||||
| 236 | |||||||||||
| 237 | 60 | ▶ | sub write_to_file { | ||||||||
|
Subroutine
write_to_file: called CC=5 SCAR=17.5
Pod
documented
| |||||||||||
| 238 | 60 | my ( $self, @data ) = @_; | |||||||||
| 239 | 60 | my $fh = $self->get_fh; | |||||||||
| 240 | |||||||||||
| 241 | 60 | ▶ | return 0 unless $fh; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 242 | |||||||||||
| 243 | 60 | ▶ | return 0 unless print $fh @data; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 244 | |||||||||||
| 245 | 60 | ▶ | return 1 unless $self->{autorotate}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 246 | |||||||||||
| 247 | #auto rotate file checks | ||||||||||
| 248 | 60 | my $pos = tell($fh); | |||||||||
| 249 | |||||||||||
| 250 | 60 | ▶ | return 1 unless $pos >= $self->{maxsize}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 251 | |||||||||||
| 252 | 0 | return $self->rotate_files; | |||||||||
| 253 | |||||||||||
| 254 | } | ||||||||||
| 255 | |||||||||||
| 256 | =pod | ||||||||||
| 257 | |||||||||||
| 258 | =back | ||||||||||
| 259 | |||||||||||
| 260 | =head1 AUTHOR | ||||||||||
| 261 | |||||||||||
| 262 | Michael Shipper | ||||||||||
| 263 | |||||||||||
| 264 | =cut | ||||||||||
| 265 | |||||||||||
| 266 | 5 | 1; | |||||||||