//TESTING include coming from tumblr.php //debuging class… uber helpful. /* @todo write out timing functions in order to find slow spots in code. @todo write out a pesudo languge for transfering debugging messages to javascript(json?) @todo Find a way to support some sort of json logging functionality… with support to pushing it to js console of some sort. Maybe allow the js console to pause scripts. Make the js console open up with a bookmarklet. Make the design look kinda like if webinspector and tweetie had a kid. A counter function so i can count how many times something is being called */ class Timer { public $total; public $time; public function __construct() { //$this->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 '
' . htmlentities(print_r($var, true)) . '' . "\n