Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

MIME::Lite Help

Status
Not open for further replies.

JJ1

Programmer
Apr 19, 2001
104
GB
I'm attempting to send emails at specific times of the day using MIME::Lite and the Windows Task Scheduler. Now, most of the time, when the script is run, no one will be near the machine, so I decided to send all errors to a log file.

Here's some sample code:
[red]
#Open Log file
open ( LOG, ">$log_out" );

#Attach message
eval {
$msg->attach( Type => 'AUTO', Path => $_ );
};
#If errors, call logger
logger ( "Image-attach error: $@\n", 1) if $@;
[/red]

If there are errors, I was hoping that the 'logger' function would be called, as follows:
[red]
sub logger {
my ($message, $quit) = @_;
my ($sec,$min,$hour,$mday,$mon,$yr,$wday,$yday,$isdst) = localtime( time() );
$mon++; #Starts at 0
$yr += 1900; #Add 1900 to start
print LOG "[$hour:$min $mday/$mon/$yr] $message\n";
if ( $quit ){
close (LOG);
exit;
}
}
[/red]


Unfortunately, when an error occurs, such as the msg->attach method failing, the error still displays on stdout and never in the log.

Does anyone know how I could fix this small problem?

Thanks in advance,

James.
 
You can use the forking form of open to catch your own standard error:

[tt]
sub catch_err {
return if $pid = open( STDERR, "|-");
die "$0: Can't fork: $!" unless defined $pid;
open( LOG, ">>/tmp/logfile" ) or die "$0: can't open log: $!";
while(<STDIN>) {
print LOG; # or be cleverer...
}
close( LOG );
exit;
}[/tt]

Call this early in your script and any stderr generated further down will be sent to the log file. You can also filter lines, prepend timestamps, etc within the while loop. (Idea from some Tom Christiansen code.) &quot;As soon as we started programming, we found to our surprise that it wasn't as
easy to get programs right as we had thought. Debugging had to be discovered.
I can remember the exact instant when I realized that a large part of my life
from then on was going to be spent in finding mistakes in my own programs.&quot;
--Maurice Wilk
 
Thanks for the advice! Do you know whether this would work under Windoze?

Also, does this mean I don't need to use the '|| logger' command at the end of every command?

i.e. no need for:
my_method(..) || logger (.....)

Thanks,

James.

 
Ooops. No - my XP box [tt]says C:\perl>'-' is not recognized as an internal or external command, operable program or batch file.[/tt] which is suboptimal. The Active perl documentation doesn't mention this as a problem, which seems strange.

I think that you could achieve almost exactly the same thing with fopen and fdopen calls but it would take a little more effort.

If you get the technique working, there is no limit to the number of filters a program can slip onto it's input and/or output so you could certainly dispense with the command line redirection.

Good luck, &quot;As soon as we started programming, we found to our surprise that it wasn't as
easy to get programs right as we had thought. Debugging had to be discovered.
I can remember the exact instant when I realized that a large part of my life
from then on was going to be spent in finding mistakes in my own programs.&quot;
--Maurice Wilk
 
I've had a read about the eval function in perldoc and I think the following should help me trap the errors and push them into a log file:
[red]
local $SIG{__WARN__} = sub { print LOG &quot;Error: @_ \n&quot;; };

#attempt to attach file
eval { $msg->attach( Type => 'AUTO', Path => $_ ); };
#Call warn if an error is returned
warn () if $@;
[/red]

Unfortunately, if I cause an error, such as providing an invalid file path, control still doesn't read the[red] 'warn () if $@;'[/red] line. I still see the error on the STDOUT, but not in the log file.

Can anyone see what I've done wrong?

Thanks,

James.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top