> Programming Languages > Perl
Various Topics Home | Disclaimer | Report Adult Posts

Various Topics on Perl



Perl - "die and write to log file" in Programming Languages


Old 11-18-2004   #1
..st..
 
Default die and write to log file

hi

usually when opening a file, we should check whether it can be opened
open(FILE,"textfile.txt") or die "Cannot open for reading:$!\n";

how can i also pipe a error message to a log file ? Something like "tee" ?

thanks...
 
Old 11-18-2004   #2
..n.. ..hleich..
 
Default Re: die and write to log file

On Thu, 18 Nov 2004 02:08:24 -0800, justme wrote:

> usually when opening a file, we should check whether it can be opened
> open(FILE,"textfile.txt") or die "Cannot open for reading:$!\n";
>
> how can i also pipe a error message to a log file ? Something like "tee" ?


Have you already looked for the modules provided at cpan, e.g.
http://search.cpan.org/search?query=tee
especially the CPAN module
IO::Tee
looks like it would it solve your problem.


Greetings,
Janek
 
Old 11-18-2004   #3
..d ..Clell..
 
Default Re: die and write to log file

justme <eight02645999@yahoo.com> wrote:

> how can i also pipe a error message to a log file ? Something like "tee" ?



How do I print to more than one file at once?


--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
 
Old 11-18-2004   #4
..r.. ..b..
 
Default Re: die and write to log file

Hi,

> usually when opening a file, we should check whether it can be opened
> open(FILE,"textfile.txt") or die "Cannot open for reading:$!\n";
> how can i also pipe a error message to a log file ? Something like "tee" ?


(1) Appending to a logfile from your Perl program:
sub handle_error {
# Try to append to the logfile. If it fails, show a msg on stderr.
if (open (my $of, ">>logfile")) {
print $of (@_);
close ($of);
} else {
print STDERR ("ERROR HANDLE: cannot append to logfile: $!\n");
}
# Now abort.
die (@_);
}

# Sample usage:
open (my $input, "input.txt")
or handle_error ("Cannot read input.txt: $!\n");

(2) Appending from the OS: use die() just as you would, but run your script
as:
perl myscript.pl 2>logfile
Unix-ish only. I don't think DOS allows for stderr-redirection while stdout
remains un-redirected.

Hope that helps.
--
Karel Kubat <karel@e-tunity.com, karel@qbat.org>
Phone: mobile (+31) 6 2956 4861, office (+31) (0)38 46 06 125
PGP fingerprint: D76E 86EC B457 627A 0A87 0B8D DB71 6BCD 1CF2 6CD5

From the Small Ads File:
Have several very old dresses from grandmother in beautiful condition.

 
Old 11-18-2004   #5
..n ..rr..
 
Default Re: die and write to log file


Quoth karel@e-tunity.com:
> (2) Appending from the OS: use die() just as you would, but run your script
> as:
> perl myscript.pl 2>logfile
> Unix-ish only. I don't think DOS allows for stderr-redirection while stdout
> remains un-redirected.


Not strictly Perl-related, but recent versions of NT (at least since
win2k) with cmd.exe do allow this, with the usual Bourne shell syntax.

Ben

--
You poor take courage, you rich take care:
The Earth was made a common treasury for everyone to share
All things in common, all people one.
'We come in peace'---the order came to cut them down. [ben@morrow.me.uk]
 
Old 11-18-2004   #6
..n ..rr..
 
Default Re: die and write to log file


Quoth and11@rol.ru:
> justme wrote on 18 Ноябрь 2004 10:08:
>
> > usually when opening a file, we should check whether it can be opened
> > open(FILE,"textfile.txt") or die "Cannot open for reading:$!\n";
> >
> > how can i also pipe a error message to a log file ? Something like "tee" ?

>
> You may also try to catch __DIE__ signal and replace its handler with your custom one.
>
> {


> local $SIG{__DIE__} = sub { open FH, ">>log.txt"; print FH "@_\n";
> close FH; print STDERR "@_\n"; }

^^ ;

> open FH, "somefile" or die "err: $!"

^^ ; not strictly required, but I
would anyway
> }


Please don't top-post.
Please indent decently.
Please wrap your lines at 76 characters.

$SIG{__DIE__} has been broken since it was first introduced, in that it
is called whenever die is called inside an eval, including for parse
errors. This means you need to be careful of this:

{
local $SIG{__DIE__} = sub {
die $_[0] if $^S or not defined $^S;
# whetever else you want
};
}

This is, of course, mentioned in perlvar, under $SIG{__DIE__} and $^S.

Ben

--
It will be seen that the Erwhonians are a meek and long-suffering people,
easily led by the nose, and quick to offer up common sense at the shrine of
logic, when a philosopher convinces them that their institutions are not based
on the strictest morality. [Samuel Butler, paraphrased] ben@morrow.me.uk
 
Old 11-18-2004   #7
..dr.. ..achen..
 
Default Re: die and write to log file

You may also try to catch __DIE__ signal and replace its handler with your custom one.

{
local $SIG{__DIE__} = sub { open FH, ">>log.txt"; print FH "@_\n"; close FH; print STDERR "@_\n"; }
open FH, "somefile" or die "err: $!"
}


Regards, Andrew

justme wrote on 18 Ноябрь 2004 10:08:

> hi
>
> usually when opening a file, we should check whether it can be opened
> open(FILE,"textfile.txt") or die "Cannot open for reading:$!\n";
>
> how can i also pipe a error message to a log file ? Something like "tee" ?
>
> thanks...


--
Andrew
 
Old 11-18-2004   #8
..m..
 
Default Re: die and write to log file

eight02645999@yahoo.com (justme) wrote in message news:<c0837966.0411180208.69ca8ac1@posting.google. com>...
> hi
>
> usually when opening a file, we should check whether it can be opened
> open(FILE,"textfile.txt") or die "Cannot open for reading:$!\n";
>
> how can i also pipe a error message to a log file ? Something like "tee" ?
>
> thanks...


***uming the user can access the log file:

unless(open FILE, "textfile.txt")
{
my $error = $!;
appendErrorLog($error);
}

################
sub appendErrorLog
{
my $error = @_;

unless(open ERRORFILE ">>errorLog.txt")
{
#Cant even access the errorLog.
}

print ERRORFILE "$error\n";
close ERRORFILE;

}

You will want to customize this a bit. For example getting the userid
would be a start. Also there are cpan things that will do the same
thing.
 

Thread Tools
Display Modes





Powered by vBulletin®
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0