rotate items in log file

R

Robert Wallace

in my code i'm trying to keep a log file of say 10 lines.
if a line is added and the log file is more than 10 lines, then the last
line is removed before a new line is added.
if the log file is less than 10 lines, then the new line will be added
and no lines will be removed.
here's my first attempt:


#append and clean logfile
my(@logFile);
open (READ, $logFile);
chomp(@logFile=<READ>);
close (READ);
my($size)=@logFile;
if ($size > 10){
unshift @logFile, $search;
pop @logFile;
}

my($logFileWrite)=join "\n", @logFile;
open (WRITE, ">$logFile");
print WRITE $logFileWrite;
close (WRITE);

the problem with unshift/pop here is that if there's only one line it'll
still rotate.
so I added the if statement. but if there are say 19 lines, it'll rotate
the 19 lines instead of trimming off 9.

is there a way perhaps to move 10 items from one array to another
like psudo code:
@arrayClean=@array [item 1], [item 2] ... [item 10]...throw out the
rest;
 
T

Tad McClellan

Robert Wallace said:
in my code i'm trying to keep a log file of say 10 lines.


Why don't you just use an existing wheel, such as Logfile::Rotate ?

if a line is added and the log file is more than 10 lines, then the last
line is removed before a new line is added.
open (READ, $logFile);


You should always, yes *always*, check the return value from open():

open (READ, $logFile) or die "could not open '$logFile' $!";

chomp(@logFile=<READ>);
close (READ);
my($size)=@logFile;


That does NOT get the size of the @logFile array, that gets the
1st element, same as:

my($size) = $logFile[0];

You need the name of the array in _scalar_ context if you want
to get the size:

my $size = @logFile;

if ($size > 10){


Here you already have a scalar context, so you don't even need the $size
temporary variable:

if (@logFile > 10) {

my($logFileWrite)=join "\n", @logFile;


Why did you chomp() them above only to put the newlines back in?

(and why no newline on the last element?)


I don't see any file locking going on. Is logging going on while
you are rotating the log?

is there a way perhaps to move 10 items from one array to another


You can also do it from one array back to the _same_ array.

like psudo code:
@arrayClean=@array [item 1], [item 2] ... [item 10]...throw out the
rest;


Yes, by using an "array slice":

@array = @array[ 0 .. 9 ];

or, in keeping with your write-only style:

@array=@array[0..9];


Space characters are not a scarce resource, feel free to use as many
as you like to make your code easier to read and understand.
 
G

Gunnar Hjalmarsson

Robert said:
in my code i'm trying to keep a log file of say 10 lines. if a line
is added and the log file is more than 10 lines, then the last line
is removed before a new line is added. if the log file is less than
10 lines, then the new line will be added and no lines will be
removed. here's my first attempt:

my($size)=@logFile;
--------^-----^
Remove those parentheses, or else $size is assigned the first line
rather than the number of lines.
if ($size > 10){
unshift @logFile, $search;
pop @logFile;
}

Try this instead:

unshift @logFile, $search;
splice @logFile, 10 if $size >= 10;
 
R

Robert Wallace

thanks folks. here's what I did:

Tad said:
Robert Wallace said:
in my code i'm trying to keep a log file of say 10 lines.
.....


That does NOT get the size of the @logFile array, that gets the
1st element, same as:

my($size) = $logFile[0];

You need the name of the array in _scalar_ context if you want
to get the size:

my $size = @logFile;
if ($size > 10){
got rid of the $size thing.

Here you already have a scalar context, so you don't even need the $size
temporary variable:

if (@logFile > 10) {


Why did you chomp() them above only to put the newlines back in?

(and why no newline on the last element?)
done.

I don't see any file locking going on. Is logging going on while
you are rotating the log?
I thought I keep it simple.
you think it'll be a problem without locking?
 
T

Tad McClellan

Robert Wallace said:
Tad McClellan wrote:
you think it'll be a problem without locking?


If you answer my question, then I will answer your question.


(the reason for asking the question was to determine whether
there might be a problem. But you did not answer it, so I
don't know if it will be a problem or not...
)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top