diff options
author | Benaka Moorthi <benaka.moorthi@gmail.com> | 2013-09-24 08:47:46 +0400 |
---|---|---|
committer | Benaka Moorthi <benaka.moorthi@gmail.com> | 2013-09-24 08:47:46 +0400 |
commit | 13f27b3b03abb2ffd06b8eeefcb79b8af98f1e9c (patch) | |
tree | 285a3bdae3f87f4d818ccff3d999588e9cd19495 /core | |
parent | 2cb3bca565e0251fa7ecd74246d05f4cbdc1aa61 (diff) |
Fixing ErrorHandler stuff.
Diffstat (limited to 'core')
-rw-r--r-- | core/Error.php | 137 | ||||
-rw-r--r-- | core/ErrorHandler.php | 79 | ||||
-rw-r--r-- | core/Profiler.php | 3 |
3 files changed, 139 insertions, 80 deletions
diff --git a/core/Error.php b/core/Error.php new file mode 100644 index 0000000000..fc5adc919e --- /dev/null +++ b/core/Error.php @@ -0,0 +1,137 @@ +<?php +/** + * Piwik - Open source web analytics + * + * @link http://piwik.org + * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later + * + * @category Piwik + * @package Piwik + */ +namespace Piwik; + +use Piwik\Log; + +/** + * TODO + */ +class Error +{ + /** + * TODO + */ + private $errno; + + /** + * TODO + */ + private $errstr; + + /** + * TODO + */ + private $errfile; + + /** + * TODO + */ + private $errline; + + /** + * TODO + */ + private $backtrace; + + /** + * TODO + */ + public function __construct($errno, $errstr, $errfile, $errline, $backtrace) + { + $this->errno = $errno; + $this->errstr = $errstr; + $this->errfile = $errfile; + $this->errline = $errline; + $this->backgrace = $backtrace; + } + + public static function formatFileAndDBLogMessage(&$message, $level, $pluginName, $datetime) + { + if ($message instanceof Error) { + // TODO + } + } + + public static function formatScreenMessage(&$message, $level, $pluginName, $datetime) + { + if ($message instanceof Error) { + // TODO + } + } + + public static function setErrorHandler() + { + Piwik_AddAction('Log.formatFileMessage', array('Error', 'formatFileAndDBLogMessage')); + Piwik_AddAction('Log.formatDatabaseMessage', array('Error', 'formatFileAndDBLogMessage')); + Piwik_AddAction('Log.formatScreenMessage', array('Error', 'formatScreenMessage')); + + set_error_handler(array('Error', 'errorHandler')); + } + + public static function errorHandler($errno, $errstr, $errfile, $errline) + { + // if the error has been suppressed by the @ we don't handle the error + if (error_reporting() == 0) { + return; + } + + $plugin = false; + + $backtrace = ''; + $bt = @debug_backtrace(); + if ($bt !== null && isset($bt[0])) { + foreach ($bt as $i => $debug) { + $backtrace .= "#$i " + . (isset($debug['class']) ? $debug['class'] : '') + . (isset($debug['type']) ? $debug['type'] : '') + . (isset($debug['function']) ? $debug['function'] : '') + . '(...) called at [' + . (isset($debug['file']) ? $debug['file'] : '') . ':' + . (isset($debug['line']) ? $debug['line'] : '') . ']' . "\n"; + + // try and discern the plugin name + if (empty($plugin)) { + if (preg_match("/^Piwik\\Plugins\\([a-z_]+)\\/", $debug['class'], $matches)) { + $plugin = $matches[1]; + } + } + } + } + + $error = new Error($errno, $errstr, $errfile, $errline, $backtrace); // TODO (also logger_error formatting) + Log::e($plugin ?: 'unknown', $error); + + switch ($errno) { + case E_ERROR: + case E_PARSE: + case E_CORE_ERROR: + case E_CORE_WARNING: + case E_COMPILE_ERROR: + case E_COMPILE_WARNING: + case E_USER_ERROR: + exit; + break; + + case E_WARNING: + case E_NOTICE: + case E_USER_WARNING: + case E_USER_NOTICE: + case E_STRICT: + case E_RECOVERABLE_ERROR: + case E_DEPRECATED: + case E_USER_DEPRECATED: + default: + // do not exit + break; + } + } +}
\ No newline at end of file diff --git a/core/ErrorHandler.php b/core/ErrorHandler.php deleted file mode 100644 index 963d46e3f6..0000000000 --- a/core/ErrorHandler.php +++ /dev/null @@ -1,79 +0,0 @@ -<?php -/** - * Piwik - Open source web analytics - * - * @link http://piwik.org - * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later - * - * @category Piwik - * @package Piwik - */ - -/** - * Error handler used to display nicely errors in Piwik - * - * @param int $errno Error number - * @param string $errstr Error message - * @param string $errfile File name - * @param int $errline Line number - * @return void - */ -function Piwik_ErrorHandler($errno, $errstr, $errfile, $errline) -{ - // if the error has been suppressed by the @ we don't handle the error - if (error_reporting() == 0) { - return; - } - - if (function_exists('debug_backtrace')) { - $backtrace = ''; - $bt = @debug_backtrace(); - if ($bt !== null && isset($bt[0])) { - foreach ($bt as $i => $debug) { - $backtrace .= "#$i " - . (isset($debug['class']) ? $debug['class'] : '') - . (isset($debug['type']) ? $debug['type'] : '') - . (isset($debug['function']) ? $debug['function'] : '') - . '(...) called at [' - . (isset($debug['file']) ? $debug['file'] : '') . ':' - . (isset($debug['line']) ? $debug['line'] : '') . ']' . "\n"; - } - } - } else { - ob_start(); - @debug_print_backtrace(); - $backtrace = ob_get_contents(); - ob_end_clean(); - } - - try { - \Zend_Registry::get('logger_error')->logEvent($errno, $errstr, $errfile, $errline, $backtrace); - } catch (Exception $e) { - // in case the error occurs before the logger creation, we simply display it - print("<pre>$errstr \nin '$errfile' at the line $errline\n\n$backtrace\n</pre>"); - exit; - } - switch ($errno) { - case E_ERROR: - case E_PARSE: - case E_CORE_ERROR: - case E_CORE_WARNING: - case E_COMPILE_ERROR: - case E_COMPILE_WARNING: - case E_USER_ERROR: - exit; - break; - - case E_WARNING: - case E_NOTICE: - case E_USER_WARNING: - case E_USER_NOTICE: - case E_STRICT: - case E_RECOVERABLE_ERROR: - case E_DEPRECATED: - case E_USER_DEPRECATED: - default: - // do not exit - break; - } -} diff --git a/core/Profiler.php b/core/Profiler.php index b959e3990c..2422f5db25 100644 --- a/core/Profiler.php +++ b/core/Profiler.php @@ -17,7 +17,8 @@ namespace Piwik; * enable_sql_profiler = 1 * * [log] - * logger_message[]="screen" + * log_writers[] = file + * log_level=debug * * @package Piwik */ |