package WebGUI::ErrorHandler; =head1 LEGAL ------------------------------------------------------------------- WebGUI is Copyright 2001-2004 Plain Black Corporation. ------------------------------------------------------------------- Please read the legal notices (docs/legal.txt) and the license (docs/license.txt) that came with this distribution before using this software. ------------------------------------------------------------------- http://www.plainblack.com info@plainblack.com ------------------------------------------------------------------- =cut use FileHandle; use strict; use WebGUI::Session; =head1 NAME Package WebGUI::ErrorHandler =head1 DESCRIPTION This package provides simple but effective error handling and logging for WebGUI. =head1 SYNOPSIS use WebGUI::ErrorHandler; WebGUI::ErrorHandler::audit(message); WebGUI::ErrorHandler::fatalError(); WebGUI::ErrorHandler::security(message); WebGUI::ErrorHandler::warn(message); WebGUI::ErrorHandler::getAudit(); WebGUI::ErrorHandler::getSecurity(); WebGUI::ErrorHandler::getSessionVars(); WebGUI::ErrorHandler::getStackTrace(); WebGUI::ErrorHandler::getWarnings(); WebGUI::ErrorHandler::showAudit(); WebGUI::ErrorHandler::showDebug(); WebGUI::ErrorHandler::showSecurity(); WebGUI::ErrorHandler::showSessionVars(); WebGUI::ErrorHandler::showStackTrace(); WebGUI::ErrorHandler::showWarnings(); WebGUI::ErrorHandler::stamp($type); WebGUI::ErrorHandler::writeLog($message); =head1 METHODS These functions are available from this package: =cut #------------------------------------------------------------------- =head2 audit ( message ) Inserts an AUDIT type message into the WebGUI log. =head3 message Whatever message you wish to insert into the log. =cut sub audit { my $data = stamp("AUDIT").$WebGUI::Session::session{user}{username} ." (".$WebGUI::Session::session{user}{userId}.") ".$_[0]."\n"; writeLog($data); $WebGUI::Session::session{debug}{audit} .= $data; } #------------------------------------------------------------------- =head2 fatalError ( ) Outputs an error message to the user and logs an error. Should only be called if the system cannot recover from an error, or if it would be unsafe to attempt to recover from an error (like compile errors or database errors). =cut sub fatalError { my $cgi; if (exists $WebGUI::Session::session{cgi}) { $cgi = $WebGUI::Session::session{cgi}; } else { use CGI; $cgi = CGI->new; } print $cgi->header; my $toLog = stamp("FATAL").$_[0]."\n"; $toLog .= getStackTrace(); $toLog .= getSessionVars(); writeLog($toLog); unless ($WebGUI::Session::session{setting}{showDebug}) { #NOTE: You can't internationalize this because with some types of errors that would cause an infinite loop. print "
"; print $_[0]."
";
print showStackTrace();
print showDebug();
}
WebGUI::Session::close();
exit;
}
#-------------------------------------------------------------------
=head2 getAudit ( )
Returns a text formatted message containing the audit messages.
=cut
sub getAudit {
return $WebGUI::Session::session{debug}{audit};
}
#-------------------------------------------------------------------
=head2 getSecurity ( )
Returns a text formatted message containing the security messages.
=cut
sub getSecurity {
return $WebGUI::Session::session{debug}{security};
}
#-------------------------------------------------------------------
=head2 getSessionVars ( )
Returns a text message containing all of the session variables.
=cut
sub getSessionVars {
my $data;
while (my ($section, $hash) = each %WebGUI::Session::session) {
if ($section eq "debug") {
next;
} elsif (ref $hash eq 'HASH') {
while (my ($key, $value) = each %$hash) {
if (ref $value eq 'ARRAY') {
$value = '['.join(', ',@$value).']';
} elsif (ref $value eq 'HASH') {
$value = '{'.join(', ',map {"$_ => $value->{$_}"} keys %$value).'}';
}
unless (lc($key) eq "password" || lc($key) eq "identifier") {
$data .= "\t".$section.'.'.$key.' = '.$value."\n";
}
}
} elsif (ref $hash eq 'ARRAY') {
my $i = 1;
foreach (@$hash) {
$data .= "\t".$section.'.'.$i.' = '.$_."\n";
$i++;
}
}
}
return $data;
}
#-------------------------------------------------------------------
=head2 getStackTrace ( )
Returns a text formatted message containing the current stack trace.
=cut
sub getStackTrace {
my $i = 2;
my $output;
while (my @data = caller($i)) {
$output .= "\t".join(",",@data)."\n";
$i++;
}
return $output;
}
#-------------------------------------------------------------------
=head2 getWarnings ( )
Returns a text formatted message containing the warnings.
=cut
sub getWarnings {
return $WebGUI::Session::session{debug}{warning};
}
#-------------------------------------------------------------------
=head2 security ( message )
Adds a SECURITY type message to the log.
=head3 message
The message you wish to add to the log.
=cut
sub security {
my $data = stamp("SECURITY").$WebGUI::Session::session{user}{username}
." (".$WebGUI::Session::session{user}{userId}
.") connecting from ".$WebGUI::Session::session{env}{REMOTE_ADDR}." attempted to ".$_[0]."\n";
writeLog($data);
$WebGUI::Session::session{debug}{security} .= $data;
}
#-------------------------------------------------------------------
=head2 showAudit ( )
Returns an HTML formatted message with the audit messages for display during debug operations.
=cut
sub showAudit {
my $audit = getAudit();
$audit =~ s/\n/\
\n/g;
return '