total = $this->time = (float)microtime(true); $this->reset(); } public function clock() { return -$this->time + ($this->time = (microtime(true))); } public function elapsed() { return (microtime(true)) - $this->total; } public function reset() { $this->total=$this->time=(microtime(true)); } } class D { protected static $handle; protected static $growlr; protected static $config = array( 'debug' => true, 'warnings' => true, 'logfile' => 'logs/main.log', 'logmode' => 'w+', 'growl' => array( 'host' => 'localhost', 'password' => '' ) ); protected static $timers; static function initialize($config=array()) { self::$config = array_merge(self::$config, (array)$config); self::$handle = fopen(self::$config['logfile'], self::$config['logmode'], true); if(self::$config['debug']) { //"God Mode" //error_reporting(E_ALL | E_DEPRECATED | E_STRICT); error_reporting(E_ALL | E_DEPRECATED); ini_set('display_errors', 1); } else { error_reporting(0); ini_set('display_errors', 0); } } static function log($var=null, $label=null) { if(self::$config['debug']) { if(!self::$handle) { trigger_error('Failed writing to the log. ' . print_r(self::$config, true), E_USER_WARNING); return $var; } fwrite(self::$handle, self::getLogMessage($var, $label)); } return $var; } static function time($timer, $label='') { if(!isset(self::$timers)) { self::$timers = array(); } if(!isset(self::$timers[$timer])) { self::$timers[$timer] = new Timer(); return D::log(0, $label . ' | ' . $timer . ' Started '); } self::$timers[$timer]->clock(); return $label . ' | ' . self::log(round(self::$timers[$timer]->elapsed(), 4) . 's', $timer . ' | ' . $label); } static function growl($var, $label=null) { if(self::$config['debug']) { if(!isset(self::$growlr)) { try { require_once('growl.php'); self::$growlr = new Growl(self::$config['growl']['host'], self::$config['growl']['password']); self::$growlr->addNotification('log'); self::$growlr->register(); self::log(self::$growlr); } catch (Exception $e) { self::warn($a, 'growl failed'); self::$growlr = false; } } self::log($var, $label); if(self::$growlr) { try { self::$growlr->notify('log', $label, print_r($var, true)); } catch (Exception $e) { self::warn($a, 'growl failed'); self::$growlr = false; } } } return $var; } static function copy($var) { /* if(self::$config['debug']) { if(!is_string($var)) { $copy = var_export($var, true); } else { $copy = $var; } D::growl(exec('whoami')); //exec('env '); //escapeshellarg( //$cmd = 'echo "do shell script' . addslashes('echo \"' . escapeshellarg($copy) . '\" | pbcopy') . '" | osascript'; $copy = 'thing to put on your clipboard'; $apple = 'do shell script "' . addcslashes('echo ' . escapeshellarg($copy) . ' | pbcopy', '"') . '"'; $cmd = 'echo ' . escapeshellarg($apple) . ' | osascript'; echo "\n\n" . $cmd . "\n\n" . $apple . "\n\n"; exec($cmd); //exec("echo 'hmmmmmm' | /usr/bin/pbcopy"); } return self::log($var, 'Copied'); */ } static function show($var, $label='') { if(self::$config['debug']) { echo self::getDisplayMessage($var, $label); } return self::log($var, $label); } static function export($var, $label='') { if(self::$config['debug']) { echo self::getDisplayMessage(var_export($var, true), $label); } return $var; } static function getLogMessage($var, $label=null) { return "\n" . (!empty($label) ? '# ' . $label . ': ' : '') . stripcslashes(print_r($var, true)) . "\n\n -~-"; } static function getDisplayMessage($var, $label=null) { return '
' . "\n" . (!empty($label) ? '

' . $label . "

\n" : '') . '
' . htmlentities(print_r($var, true)) . '
' . "\n
"; } static function report($context, $error) { //$temp = "\n There has been an error. Context: " . $context . "\n " . "Error: " . $error . "\n Back trace:" . print_r(stacktrace(), true); //self::log($temp, 'Fatal and kinda expected error'); throw new Exception($context . ' In… ' . $error); exit($temp); } static function error($error='There has been an error') { throw new Exception($error); //exit(self::log("\n There has been an error. Message: \n" . $error . "\n Back trace:" . print_r(stacktrace(), true), 'Fatal and expected error')); } static function warn($warning='Warning') { //D::show('WHAT'); if(self::$config['warnings']) { if(!extension_loaded('xdebug')) { D::show(D::stack(), $warning); } trigger_error($warning, E_USER_WARNING); } } static function stackTrace() { return self::log(stacktrace(), 'Stack Trace'); } static function stack($label='Label') { return self::log( "\n" . join( "\n", array_reverse(array_map( function($v) { //) return ' ' . $v['function'] . '();' . "\n →" . substr(substr(@$v['file'], strlen(realpath(LOC))), 1, -4) . ' | line:' . @$v['line']; }, debug_backtrace() )) ), $label . ' - Stack Trace' ); } static function debug() { self::log(debug_backtrace()); } static function close() { if(self::$config['debug']) { fclose(self::$handle); } } } D::initialize(); A.J. Cates -

Copyright 2023 A.J. Cates - Valid HTML5 - Powered by Wolf CMS