Enhanced debug for developers.
This commit is contained in:
parent
f7b44d12bd
commit
66555e648b
2 changed files with 258 additions and 80 deletions
|
|
@ -30,11 +30,28 @@ 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:
|
||||
|
|
@ -43,23 +60,6 @@ These functions are available from this package:
|
|||
|
||||
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
sub _log {
|
||||
if (my $log = FileHandle->new(">>".$WebGUI::Session::session{config}{logfile})) {
|
||||
return $log;
|
||||
} else {
|
||||
print STDOUT "Can't open log file: ".$WebGUI::Session::session{config}{logfile}." Check your WebGUI configuration file to set the path of the log file, and check to be sure the web server has the privileges to write to the log file.";;
|
||||
WebGUI::Session::close();
|
||||
exit;
|
||||
}
|
||||
}
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
sub _stamp {
|
||||
return localtime(time)." ".$0." ".$_[0].": ";
|
||||
}
|
||||
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
=head2 audit ( message )
|
||||
|
|
@ -77,14 +77,13 @@ Whatever message you wish to insert into the log.
|
|||
=cut
|
||||
|
||||
sub audit {
|
||||
my $data = _stamp("AUDIT").$WebGUI::Session::session{user}{username}
|
||||
my $data = stamp("AUDIT").$WebGUI::Session::session{user}{username}
|
||||
." (".$WebGUI::Session::session{user}{userId}.") ".$_[0]."\n";
|
||||
my $log = _log();
|
||||
print $log $data;
|
||||
$log->close;
|
||||
writeLog($data);
|
||||
$WebGUI::Session::session{debug}{audit} .= $data;
|
||||
}
|
||||
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
=head2 fatalError ( )
|
||||
|
|
@ -102,13 +101,66 @@ sub fatalError {
|
|||
$cgi = CGI->new;
|
||||
}
|
||||
print $cgi->header;
|
||||
my $data = _stamp("FATAL").$_[0]."\n";
|
||||
$data .= "\t".join(",",caller(1))."\n";
|
||||
$data .= "\t".join(",",caller(2))."\n";
|
||||
$data .= "\t".join(",",caller(3))."\n";
|
||||
$data .= "\t".join(",",caller(4))."\n";
|
||||
my $toLog = stamp("FATAL").$_[0]."\n";
|
||||
$toLog .= getStackTrace();
|
||||
$toLog .= getSessionVars();
|
||||
writeLog($toLog);
|
||||
unless ($WebGUI::Session::session{setting}{showDebug}) {
|
||||
print WebGUI::International::get(416).'<br>';
|
||||
print '<br>'.$WebGUI::Session::session{setting}{companyName};
|
||||
print '<br>'.$WebGUI::Session::session{setting}{companyEmail};
|
||||
print '<br>'.$WebGUI::Session::session{setting}{companyURL};
|
||||
} else {
|
||||
print "<h1>WebGUI Fatal Error</h1>Something unexpected happened that caused this system to fault.<p>";
|
||||
print $_[0]."<p>";
|
||||
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 (ref $hash eq 'HASH') {
|
||||
if ($section eq "debug") {
|
||||
next;
|
||||
} elsif (ref $hash eq 'HASH') {
|
||||
while (my ($key, $value) = each %$hash) {
|
||||
if (ref $value eq 'ARRAY') {
|
||||
$value = '['.join(', ',@$value).']';
|
||||
|
|
@ -127,24 +179,40 @@ sub fatalError {
|
|||
}
|
||||
}
|
||||
}
|
||||
unless ($WebGUI::Session::session{setting}{showDebug}) {
|
||||
print WebGUI::International::get(416).'<br>';
|
||||
print '<br>'.$WebGUI::Session::session{setting}{companyName};
|
||||
print '<br>'.$WebGUI::Session::session{setting}{companyEmail};
|
||||
print '<br>'.$WebGUI::Session::session{setting}{companyURL};
|
||||
} else {
|
||||
print "<h1>WebGUI Fatal Error</h1>Something unexpected happened that caused this system to fault.<p>";
|
||||
my $formattedData = $data;
|
||||
$formattedData =~ s/\n/\<br\>/g;
|
||||
print $formattedData;
|
||||
}
|
||||
my $log = _log();
|
||||
print $log $data;
|
||||
$log->close();
|
||||
WebGUI::Session::close();
|
||||
exit;
|
||||
return $data;
|
||||
}
|
||||
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
=head2 getStackTrace ( )
|
||||
|
||||
Returns a text formatted message containing the current stack trace.
|
||||
|
||||
=cut
|
||||
|
||||
sub getStackTrace {
|
||||
my $data = "\t".join(",",caller(2))."\n";
|
||||
$data .= "\t".join(",",caller(3))."\n";
|
||||
$data .= "\t".join(",",caller(4))."\n";
|
||||
$data .= "\t".join(",",caller(5))."\n";
|
||||
return $data;
|
||||
}
|
||||
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
=head2 getWarnings ( )
|
||||
|
||||
Returns a text formatted message containing the warnings.
|
||||
|
||||
=cut
|
||||
|
||||
sub getWarnings {
|
||||
return $WebGUI::Session::session{debug}{warning};
|
||||
}
|
||||
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
=head2 security ( message )
|
||||
|
|
@ -162,15 +230,126 @@ The message you wish to add to the log.
|
|||
=cut
|
||||
|
||||
sub security {
|
||||
my $data = _stamp("SECURITY").$WebGUI::Session::session{user}{username}
|
||||
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";
|
||||
my $log = _log();
|
||||
print $log $data;
|
||||
$log->close;
|
||||
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/\<br\>/g;
|
||||
return '<div style="background-color: #ffffdd;color: #000000;">'.$audit.'</div>';
|
||||
}
|
||||
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
=head2 showDebug ( )
|
||||
|
||||
Creates an HTML formatted string containing the most common debug information.
|
||||
|
||||
=cut
|
||||
|
||||
sub showDebug {
|
||||
return showWarnings()
|
||||
.showSecurity()
|
||||
.showAudit()
|
||||
.showSessionVars();
|
||||
}
|
||||
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
=head2 showSecurity ( )
|
||||
|
||||
Returns an HTML formatted message with the security messages for display during debug operations.
|
||||
|
||||
=cut
|
||||
|
||||
sub showSecurity {
|
||||
my $security = getSecurity();
|
||||
$security =~ s/\n/\<br\>/g;
|
||||
return '<div style="background-color: #800000;color: #ffffff;">'.$security.'</div>';
|
||||
}
|
||||
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
=head2 showSessionVars ( )
|
||||
|
||||
Returns an HTML formatted list of the session variables for display during debug operations.
|
||||
|
||||
=cut
|
||||
|
||||
sub showSessionVars {
|
||||
my $data = getSessionVars();
|
||||
$data =~ s/\n/\<br\>/g;
|
||||
return '<div bgcolor="#ffffff" style="color: #000000; font-size: 10pt; font-family: helvetica;">'.$data.'</div>';
|
||||
}
|
||||
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
=head2 showStackTrace ( )
|
||||
|
||||
Returns an HTML formatted message for displaying the stack trace during debug operations.
|
||||
|
||||
=cut
|
||||
|
||||
sub showStackTrace {
|
||||
my $st = getStackTrace();
|
||||
$st =~ s/\n/\<br\>/g;
|
||||
return $st;
|
||||
}
|
||||
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
=head2 showWarnings ( )
|
||||
|
||||
Returns HTML formatted warnings for display during debug operations.
|
||||
|
||||
=cut
|
||||
|
||||
sub showWarnings {
|
||||
my $warning = getWarnings();
|
||||
$warning =~ s/\n/\<br\>/g;
|
||||
return '<div style="background-color: #ffdddd;color: #000000;">'.$warning.'</div>';
|
||||
}
|
||||
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
=head2 stamp ( type )
|
||||
|
||||
Generates a stamp to be added to the log file. Use this in conjunction with your message for writeLog().
|
||||
|
||||
=over
|
||||
|
||||
=item type
|
||||
|
||||
The type of message this is. You may use whatever type you wish. WebGUI currently uses AUDIT, WARNING, FATAL, and SECURITY.
|
||||
|
||||
=back
|
||||
|
||||
=cut
|
||||
|
||||
sub stamp {
|
||||
return localtime(time)." ".$0." ".$_[0].": ";
|
||||
}
|
||||
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
=head2 warn ( message )
|
||||
|
|
@ -188,13 +367,38 @@ The message you wish to add to the log.
|
|||
=cut
|
||||
|
||||
sub warn {
|
||||
my ($log);
|
||||
my $data = _stamp("WARNING").$_[0]."\n";
|
||||
$log = _log();
|
||||
print $log $data;
|
||||
$log->close;
|
||||
my $data = stamp("WARNING").$_[0]."\n";
|
||||
writeLog($data);
|
||||
$WebGUI::Session::session{debug}{warning} .= $data;
|
||||
}
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
=head2 writeLog ( message )
|
||||
|
||||
Writes a message to the log.
|
||||
|
||||
=over
|
||||
|
||||
=item message
|
||||
|
||||
The message you wish to write to the log.
|
||||
|
||||
=back
|
||||
|
||||
=cut
|
||||
|
||||
sub writeLog {
|
||||
if (my $log = FileHandle->new(">>".$WebGUI::Session::session{config}{logfile})) {
|
||||
print $log $_[0];
|
||||
$log->close;
|
||||
} else {
|
||||
print STDOUT "Can't open log file: ".$WebGUI::Session::session{config}{logfile}." Check your WebGUI configuration file to set the path of the log file, and check to be sure the web server has the privileges to write to the log file.";;
|
||||
WebGUI::Session::close();
|
||||
exit;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
1;
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue