From e2648d4c08794c7843a9e89dc445f4ae1e28fed8 Mon Sep 17 00:00:00 2001 From: Ben Roberts Date: Mon, 25 Apr 2011 00:43:46 +0100 Subject: [PATCH] Add custom error handler to log through new logging code --- private/config.php.dist | 9 ++++ source/lib/SihnonFramework/LogEntry.class.php | 5 +++ source/lib/SihnonFramework/Main.class.php | 45 +++++++++++++++++++ 3 files changed, 59 insertions(+) diff --git a/private/config.php.dist b/private/config.php.dist index 7b1784e..cfe3bfd 100644 --- a/private/config.php.dist +++ b/private/config.php.dist @@ -71,4 +71,13 @@ define('Sihnon_ConfigTable', 'settings'); */ define('Sihnon_ConfigFile', '../private/settings.txt'); +/** + * Sihnon Development Mode + * + * Specifies whether or not the Framework should operate in debug mode + * + * @var bool + */ +define('Sihnon_Dev', true); + ?> \ No newline at end of file diff --git a/source/lib/SihnonFramework/LogEntry.class.php b/source/lib/SihnonFramework/LogEntry.class.php index 444165d..f93e25f 100644 --- a/source/lib/SihnonFramework/LogEntry.class.php +++ b/source/lib/SihnonFramework/LogEntry.class.php @@ -117,6 +117,11 @@ class SihnonFramework_LogEntry { $logger->log($entry); } + public static function logInternal($logger, $severity, $file, $line, $message, $category = SihnonFramework_Log::CATEGORY_DEFAULT) { + $entry = new self($severity, $category, time(), getmypid(), $file, $line, $message); + $logger->log($entry); + } + public static function debug($logger, $message, $category = SihnonFramework_Log::CATEGORY_DEFAULT) { static::log($logger, SihnonFramework_Log::LEVEL_DEBUG, $message, $category); } diff --git a/source/lib/SihnonFramework/Main.class.php b/source/lib/SihnonFramework/Main.class.php index 47d33c3..d236422 100644 --- a/source/lib/SihnonFramework/Main.class.php +++ b/source/lib/SihnonFramework/Main.class.php @@ -99,7 +99,37 @@ class SihnonFramework_Main { } public static function initialise() { + // Provide a means to load framework classes autonomously spl_autoload_register(array('SihnonFramework_Main','autoload')); + + // Handle error messages using custom error handler + set_error_handler(array(get_called_class(), 'errorHandler')); + } + + public static function errorHandler($errno, $errstr, $errfile = '', $errline = 0, $errcontext = array()) { + $severity = ''; + switch ($errno) { + case E_NOTICE: + case E_USER_NOTICE: + $severity = SihnonFramework_Log::LEVEL_INFO; + break; + case E_WARNING: + case E_USER_WARNING: + $severity = SihnonFramework_Log::LEVEL_WARNING; + break; + case E_ERROR: + case E_USER_ERROR: + $severity = SihnonFramework_Log::LEVEL_ERROR; + break; + default: + $severity = SihnonFramework_Log::LEVEL_INFO; + break; + } + + SihnonFramework_LogEntry::logInternal(static::instance()->log(), $severity, $errfile, $errline, $errstr, SihnonFramework_Log::CATEGORY_DEFAULT); + + // If dev mode is enabled, fail here to enable the normal PHP error handling + return ! Sihnon_Dev; } public static function autoload($classname) { @@ -227,6 +257,21 @@ class SihnonFramework_Main { return true; } + public static function rmdir_recursive($dir) { + if (is_dir($dir)) { + $objects = scandir($dir); + foreach ($objects as $object) { + if ($object != "." && $object != "..") { + if (filetype($dir."/".$object) == "dir") self::rmdir_recursive($dir."/".$object); else unlink($dir."/".$object); + } + } + reset($objects); + rmdir($dir); + } + + return true; + } + public static function issetelse($var, $default = null) { if (isset($var)) { return $var;