Re: Trying to write a log function



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


.



Relevant Pages

  • Re: Trying to write a log function
    ... 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. ... You should have these two lines at the beginning of your program and let perl help you find mistakes: ... sleep 10; ...
    (comp.lang.perl.misc)
  • MSXML 4.0 SP2 Security Update (KB927978) gets Error 0x643
    ... The log file (in the numbered folder on drive c:\) has these lines: ... Verify that you have sufficient access to that key, ...
    (microsoft.public.windowsupdate)
  • Re: modem
    ... There's not much as I still can't send the modem an AT, ... Could you verify the presence of the cuau* file in /dev? ... you're not using the GENERIC kernel; ...
    (freebsd-questions)
  • Re: I want to open a pdf file in richTextBox using C#.Net
    ... I would verify that dependencies of the ... version of adobe acrobat reader installed, ... This library may have a log file that it writes to ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: NEW FREE APP! NTBACKUP Print Log - Dont use on SBS 2003
    ... I tried using this on SBS 2003 and found it is useless. ... prints the wrong log file and prints before the verify has finished. ...
    (microsoft.public.windows.server.active_directory)