diff --git a/lib/WebGUI.pm b/lib/WebGUI.pm
index 1326a0346..4be32fbf3 100644
--- a/lib/WebGUI.pm
+++ b/lib/WebGUI.pm
@@ -31,36 +31,10 @@ use WebGUI::Utility;
#-------------------------------------------------------------------
sub _generateDebug {
- my ($debug);
if ($session{setting}{showDebug} || ($session{form}{debug}==1 && WebGUI::Privilege::isInGroup(3))) {
- $debug = '
'.$session{debug}{warning}.'
';
- $debug .= ''.$session{debug}{security}.'
';
- $debug .= ''.$session{debug}{audit}.'
';
- $debug .= '';
- while (my ($section, $hash) = each %session) {
- if (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") {
- $debug .= '| '.$section.'.'.$key.': | '.$value.' | ';
- }
- }
- } elsif (ref $hash eq 'ARRAY') {
- my $i = 1;
- foreach (@$hash) {
- $debug .= '
| '.$section.'.'.$i.': | '.$_.' | ';
- $i++;
- }
- }
- $debug .= '
| |
';
- }
- $debug .='
';
+ return WebGUI::ErrorHandler::showDebug();
}
- return $debug;
+ return "";
}
#-------------------------------------------------------------------
diff --git a/lib/WebGUI/ErrorHandler.pm b/lib/WebGUI/ErrorHandler.pm
index 2f1d87e9f..65aeddaea 100644
--- a/lib/WebGUI/ErrorHandler.pm
+++ b/lib/WebGUI/ErrorHandler.pm
@@ -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).'
';
+ print '
'.$WebGUI::Session::session{setting}{companyName};
+ print '
'.$WebGUI::Session::session{setting}{companyEmail};
+ print '
'.$WebGUI::Session::session{setting}{companyURL};
+ } else {
+ print "WebGUI Fatal Error
Something unexpected happened that caused this system to fault.";
+ 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 (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).'
';
- print '
'.$WebGUI::Session::session{setting}{companyName};
- print '
'.$WebGUI::Session::session{setting}{companyEmail};
- print '
'.$WebGUI::Session::session{setting}{companyURL};
- } else {
- print "
WebGUI Fatal Error
Something unexpected happened that caused this system to fault.";
- my $formattedData = $data;
- $formattedData =~ s/\n/\
/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/\
/g;
+ return '
'.$audit.'
';
+}
+
+
+#-------------------------------------------------------------------
+
+=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/\
/g;
+ return ''.$security.'
';
+}
+
+
+#-------------------------------------------------------------------
+
+=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/\
/g;
+ return ''.$data.'
';
+}
+
+
+#-------------------------------------------------------------------
+
+=head2 showStackTrace ( )
+
+Returns an HTML formatted message for displaying the stack trace during debug operations.
+
+=cut
+
+sub showStackTrace {
+ my $st = getStackTrace();
+ $st =~ s/\n/\
/g;
+ return $st;
+}
+
+
+#-------------------------------------------------------------------
+
+=head2 showWarnings ( )
+
+Returns HTML formatted warnings for display during debug operations.
+
+=cut
+
+sub showWarnings {
+ my $warning = getWarnings();
+ $warning =~ s/\n/\
/g;
+ return ''.$warning.'
';
+}
+
+
+#-------------------------------------------------------------------
+
+=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;