Catch system error
Sometimes libraries that you use output error message to your standard error but you have no idea when it happened or how it happened. These two functions (using the fact that Perl allows us the ability to redefine STDERR) watch if something was printed to STDERR, log the error to the original STDERR with additional info.
sub startCaptureError {
my $self = shift;
# Return if startCaptureError() has already been called
return if ($self->{"capturedErrorTextRef"} ne "");
# Save the current STDERR handle in $olderr
open my $olderr,">&STDERR" || return;
close(STDERR);
# Keep the old STDERR handle for restoring later
$self->{"olderr"} = $olderr;
# Re-open STDERR as a filehandle to a scalar reference
my $errorText = "";
open(STDERR,'>',\$errorText) || return;
# Store the scalar ref for later usage
$self->{"capturedErrorTextRef"} = \$errorText;
} #startCaptureError
sub endCaptureError {
my $self = shift;
my $logger = get_logger("Proxy::endCaptureError");
# TODO shouldn't STDERR always be restored?
# Return if no error text has been captured
return if ($self->{'capturedErrorTextRef'} eq "");
# Restore the old STDERR handle
close(STDERR);
my $olderr = $self->{"olderr"};
open(STDERR,">&",$olderr) || print $olderr "Can't restore original STDERR handle\n";
# Print the error text that was captured
if (${$self->{'capturedErrorTextRef'}} ne "") {
$logger->warn(${$self->{'capturedErrorTextRef'}});
}
} #endCaptureError
page revision: 0, last edited: 26 Sep 2008 23:26