Implemented Tracy/Debugger from the Nette framework.

This debugger allows for advanced debugging for both development and production environments.
This did however require the implementation of an ENVIRONMENT constant. This has been put in the index file. If no constant is set it will be set by the core class and the default is PRODUCTION.
Chances are that the location for this constant will move in the future.

There is also a proper way now that fatal errors will lead to a 500 error on the screen. This is either done by Tracy or FuzeWorks, depending on if Tracy is available.
This commit is contained in:
Abel Hoogeveen 2016-06-08 14:57:41 +02:00
parent 525e77203d
commit 440ead5784
10 changed files with 105 additions and 28 deletions

1
.gitignore vendored
View File

@ -24,3 +24,4 @@ Modules/admin/themes/adminlte2.1/bootstrap/
doc
nbproject
Application/Cache
Application/Logs

View File

@ -1,5 +1,4 @@
<?php return array (
'debug' => false,
'error_reporting' => true,
'log_to_file' => false,
'logger_template' => 'logger_default',

View File

View File

@ -70,6 +70,15 @@ class Core
*/
public static function init()
{
// Set the CWD for usage in the shutdown function+
self::$cwd = getcwd();
// If the environment is not yet defined, use production settings
if (!defined('ENVIRONMENT'))
{
define('ENVIRONMENT', 'PRODUCTION');
}
// Defines the time the framework starts. Used for timing functions in the framework
if (!defined('STARTTIME')) {
define('STARTTIME', microtime(true));
@ -112,9 +121,6 @@ class Core
if ($event->isCancelled()) {
return true;
}
// Set the CWD for usage in the shutdown function+
self::$cwd = getcwd();
}
/**
@ -177,6 +183,7 @@ class Core
if ($event->isCancelled() === false)
{
// If the output should be displayed, send the final render and parse the logger
Logger::shutdownError();
Factory::getInstance()->output->_display();
Logger::shutdown();
}
@ -194,6 +201,7 @@ class Core
if (file_exists($file)) {
include $file;
Logger::log('Loaded Composer');
Logger::loadComposer();
return true;
}

View File

@ -477,8 +477,8 @@ class Layout
// Load the engines provided in this file
self::registerEngine(new PHPEngine(), 'PHP', array('php'));
self::registerEngine(new SmartyEngine(), 'Smarty', array('tpl'));
self::registerEngine(new JsonEngine(), 'JSON', array('json'));
self::registerEngine(new SmartyEngine(), 'Smarty', array('tpl'));
self::$engines_loaded = true;
}
}

View File

@ -32,6 +32,7 @@
*/
namespace FuzeWorks;
use Tracy\Debugger;
/**
* Logger Class.
@ -115,6 +116,13 @@ class Logger {
*/
public static $markPoints = array();
/**
* Whether to use the Tracy debugger instead of FuzeWorks Logger
*
* @var bool
*/
public static $useTracy = false;
/**
* Initiates the Logger.
*
@ -127,18 +135,60 @@ class Logger {
set_Exception_handler(array('\FuzeWorks\Logger', 'exceptionHandler'));
error_reporting(false);
}
self::$debug = Config::get('error')->debug;
self::$debug = (ENVIRONMENT === 'DEVELOPMENT');
self::$log_to_file = Config::get('error')->log_to_file;
self::$logger_template = Config::get('error')->logger_template;
self::newLevel('Logger Initiated');
}
/**
* Try to load Trace Debugger when available
*
* @return void
*/
public static function loadComposer()
{
if (class_exists('\Tracy\Debugger', true))
{
if (ENVIRONMENT === 'DEVELOPMENT')
{
Debugger::enable(Debugger::DEVELOPMENT, realpath('Application'.DS.'Logs'));
}
else
{
Debugger::enable(Debugger::PRODUCTION, realpath('Application'.DS.'Logs'));
}
self::$useTracy = true;
}
}
/**
* Function to be run upon FuzeWorks shutdown.
*
* Logs data to screen when requested to do so
*/
public static function shutdown() {
// And finally stop the Logging
self::stopLevel();
if (self::$debug === true || self::$print_to_screen) {
self::log('Parsing debug log');
self::logToScreen();
}
if (self::$log_to_file == true)
{
self::logToFile();
}
}
/**
* Function to be run upon FuzeWorks shutdown.
*
* Logs a fatal error and outputs the log when configured or requested to do so
*/
public static function shutdown() {
public static function shutdownError()
{
// Load last error if thrown
$errfile = 'Unknown file';
$errstr = 'shutdown';
@ -153,20 +203,13 @@ class Logger {
$errstr = $error['message'];
// Log it!
Factory::getInstance()->output->set_output('');
self::errorHandler($errno, $errstr, $errfile, $errline);
}
// And finally stop the Logging
self::stopLevel();
if (self::$debug == true || self::$print_to_screen) {
self::log('Parsing debug log');
self::logToScreen();
}
if (self::$log_to_file == true)
{
self::logToFile();
if (self::$useTracy === false)
{
self::http_error('500');
}
}
}
@ -314,6 +357,12 @@ class Logger {
self::$infoErrors[] = $LOG;
self::$Logs[] = $LOG;
// Use Tracy when we can
if (self::$useTracy === true)
{
Debugger::log($msg, 'info');
}
}
/**
@ -334,6 +383,12 @@ class Logger {
self::$debugErrors[] = $LOG;
self::$Logs[] = $LOG;
// Use Tracy when we can
if (self::$useTracy === true)
{
Debugger::log($msg, 'debug');
}
}
/**
@ -354,6 +409,12 @@ class Logger {
self::$criticalErrors[] = $LOG;
self::$Logs[] = $LOG;
// Use Tracy when we can
if (self::$useTracy === true)
{
Debugger::log($msg, 'error');
}
}
/**
@ -374,6 +435,12 @@ class Logger {
self::$warningErrors[] = $LOG;
self::$Logs[] = $LOG;
// Use Tracy when we can
if (self::$useTracy === true)
{
Debugger::log($msg, 'warning');
}
}
/**
@ -393,6 +460,12 @@ class Logger {
'runtime' => round(self::getRelativeTime(), 4),);
self::$Logs[] = $LOG;
// Use Tracy when we can
if (self::$useTracy === true)
{
Debugger::log($msg, 'info');
}
}
/**

View File

@ -385,7 +385,7 @@ class Modules
if ($cache)
{
// Retrieve the cache if possible
$cache = Libraries::getDriver('cache');
$cache = Factory::getInstance()->libraries->getDriver('cache');
$cacheData = $cache->$cachingMethod->get('moduleRegisters');
if ( ! is_bool($cacheData) )

View File

@ -3,7 +3,8 @@
"php": ">=5.4.0",
"ext-curl": "*",
"ext-json": "*",
"smarty/smarty": "~3.1"
"smarty/smarty": "~3.1",
"tracy/tracy": "*"
},
"require-dev": {
"phpunit/phpunit": "5.3.*",

View File

@ -32,6 +32,8 @@
use FuzeWorks\Core;
use FuzeWorks\Factory;
define('ENVIRONMENT', 'PRODUCTION');
// Include framework
require_once dirname(__FILE__).'/Core/System/class.core.php';

View File

@ -41,13 +41,6 @@ require_once 'Core/System/class.core.php';
ob_start();
Core::init();
// Disable debugger
$cfg = Config::get('error');
$cfg->debug = false;
$cfg->error_reporting = false;
$cfg->log_to_file = false;
$cfg->commit();
restore_error_handler();
restore_exception_handler();