Re: Trying to write a log function
- From: "Simon" <shmh@xxxxxxxxxxxxxx>
- Date: Wed, 04 Jul 2007 10:17:21 GMT
John!
Youre so kind to have spent some time on this for me.
I really appreciate your comments.
Thansk John.
Simon
"John W. Krahn" <dummy@xxxxxxxxxxx> wrote in message
news:i_Fii.26076$xk5.3904@xxxxxxxxxxx
Simon wrote:
Hope you can help.
Im a newbie to this, but have some sample code that Im trying to
understand.
The goal is that I can successfully write a log function that writes
messages to a log file.
Please bare with me as I dont fully understand the below code, which is
where I need your gurus expertise.
Here is the code, which is not working, but Id like to try and find out
how i can get it to work to write messages to a log file.
Any help appreciated.
Thank you.
======================================================================
code
You should have these two lines at the beginning of your program and let
perl help you find mistakes:
use warnings;
use strict;
sub Log {
@g = localtime time;
open(LOG,">>$LogFile");
You should *ALWAYS* verify that your file opened correctly:
open LOG, '>>', $LogFile or die "Cannot open '$LogFile' $!";
This would have caught the error that $LogFile has *no* value here because
it is not assigned a value until later in the program.
printf LOG "%02d%02d/%02d/%02d %02d:%02d:%02d $_[0]\n",
sprintf("%02d",(substr sprintf("%03d",$g[5]), 0, 1) + 19),(substr
sprintf("%03d",$g[5]), 1, 2),$g[4]+1,$g[3],$g[2],$g[1],$g[0];
The correct way to get the year (as described in 'perldoc -f localtime')
is to add 1900 to the sixth element of the list returned from localtime so
that expression could be more concisely written as:
printf LOG "%04d/%02d/%02d %02d:%02d:%02d %s\n", $g[ 5 ] + 1900,
$g[ 4 ] + 1, @g[ 3, 2, 1, 0 ], $_[ 0 ];
printf "%02d%02d/%02d/%02d %02d:%02d:%02d $_[0]\n",
sprintf("%02d",(substr sprintf("%03d",$g[5]), 0, 1) + 19),(substr
sprintf("%03d",$g[5]), 1, 2),$g[4]+1,$g[3],$g[2],$g[1],$g[0];
Same as above.
close(LOG);
}
sub WaitForInstall() {
Log " Waiting for installation to complete";
sleep 10;
$finished = 0;
while (!$finished) {
sleep 5;
$finished = 1; # assume we're finished
open(PS,"$PSCMD|");
You should *ALWAYS* verify that your file opened correctly:
open PS, '-|', $PSCMD or die "Cannot open pipe from '$PSCMD' $!";
while (<PS>) {
if ($_ =~ /$Product Setup/i) {
$finished = 0; # not finished
}
elsif ($_ =~ /$Product/i) {
$finished = 0; # not finished
}
}
close(PS);
And with a pipe you should also verify that it closed correctly:
close PS or warn $! ? "Error closing '$PSCMD' pipe: $!"
: "Exit status $? from '$PSCMD'";
}
}
John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
.
- References:
- Trying to write a log function
- From: Simon
- Re: Trying to write a log function
- From: John W. Krahn
- Trying to write a log function
- Prev by Date: Re: Trying to write a log function
- Next by Date: Error handling
- Previous by thread: Re: Trying to write a log function
- Next by thread: Re: Trying to write a log function
- Index(es):
Relevant Pages
|