C
Cosmic Cruizer
I searched, but could not find suggestions on reducing the size of log
files on Windows. Truncate did not work since it keeps to top portion of
the log. I did not try to use tail.
Now that I wrote something that does what I want... what is a cleaner way
of doing this?
### Manage logfile size ###
sub getLogFile {
use File::Copy "move";
my $max_line_count = 50000; # Number of lines for replaced file
my $max_file_size = 3000000; # Maximum logfile size
my $size = (stat("$log"))[7]; # Get current logfile size
my $x; # Incrementer for counter
if($size > $max_file_size) {
# Get the number of lines in the logfile
open my $in, '<', $log or die "cannot open $log: $!";
1 while <$in>;
my $lines = $.;
close $in;
# Number of preceding lines to ignore (not copy)
my $target_size = $lines - $max_line_count;
# Open to read old log and write new temp log
open my $out, '>>', "TEMP.txt" or die "cannot open TEMP.txt: $!";
open my $in, '<', $log or die "cannot open $log: $!";
while (<$in>){
$x+=1;
print $out $_ if($x > $target_size);
}
print $out "\n$time_stamp\tFile size truncated\n\n";
close $in;
close $out;
# Replace old logfile and clean up temp
unlink $log;
move ("TEMP.txt", "$log");
unlink "TEMP.txt";
}
}
Thanks.
files on Windows. Truncate did not work since it keeps to top portion of
the log. I did not try to use tail.
Now that I wrote something that does what I want... what is a cleaner way
of doing this?
### Manage logfile size ###
sub getLogFile {
use File::Copy "move";
my $max_line_count = 50000; # Number of lines for replaced file
my $max_file_size = 3000000; # Maximum logfile size
my $size = (stat("$log"))[7]; # Get current logfile size
my $x; # Incrementer for counter
if($size > $max_file_size) {
# Get the number of lines in the logfile
open my $in, '<', $log or die "cannot open $log: $!";
1 while <$in>;
my $lines = $.;
close $in;
# Number of preceding lines to ignore (not copy)
my $target_size = $lines - $max_line_count;
# Open to read old log and write new temp log
open my $out, '>>', "TEMP.txt" or die "cannot open TEMP.txt: $!";
open my $in, '<', $log or die "cannot open $log: $!";
while (<$in>){
$x+=1;
print $out $_ if($x > $target_size);
}
print $out "\n$time_stamp\tFile size truncated\n\n";
close $in;
close $out;
# Replace old logfile and clean up temp
unlink $log;
move ("TEMP.txt", "$log");
unlink "TEMP.txt";
}
}
Thanks.