die and write to log file

J

justme

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...
 
J

Janek Schleicher

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
 
K

Karel Kubat

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 <[email protected], (e-mail address removed)>
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.
 
B

Ben Morrow

Quoth (e-mail address removed):
(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
 
B

Ben Morrow

Quoth (e-mail address removed):
justme wrote on 18 ÐоÑбрь 2004 10:08:


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
 
A

Andrew Tkachenko

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:
 
G

gumby

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...

Assuming 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.
 
A

Andrew Tkachenko

Ben Morrow wrote on 18 ÐоÑбрь 2004 17:32:
Please don't top-post.
Please indent decently.
Please wrap your lines at 76 characters.

ok, sorry for this, I'll be more polite in further postings :)
$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
};
}

thanks for remark. I've totally missed this
 

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

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,054
Latest member
LucyCarper

Latest Threads

Top