Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Debug helper
- */
- class Debug
- {
- /**
- * Debug initiated?
- *
- * @var boolean
- */
- static private $_initiated = false;
- /**
- * Initialize debugging
- */
- static public function main()
- {
- /* Init once */
- if (self::$_initiated)
- {
- return;
- }
- self::$_initiated = true;
- /* Setup error/exception handling */
- $handler = array(__CLASS__, IN_DEV ? 'devhdl' : 'prodhdl');
- set_error_handler($handler);
- set_exception_handler($handler);
- }
- /**
- * Development mode handler
- */
- static public function devhdl()
- {
- /* Init */
- $msg = self::_parseError(func_get_args());
- /* Build output */
- $out = sprintf('<pre>%s</pre>', print_r($msg, 1));
- /* Send */
- die($out);
- }
- /**
- * Production mode handler
- */
- static public function prodHdl()
- {
- /* Init */
- $msg = self::_parseError(func_get_args());
- $sep = "\n" . str_repeat('-', 60) . "\n";
- /* Log message */
- $log = fopen(DEV_LOG, 'a+b');
- flock($log, LOCK_EX);
- fwrite($log, $msg . $sep);
- flock($log, LOCK_UN);
- fclose($log);
- /* Send */
- die('System error');
- }
- /**
- * Error parser
- *
- * @param array $params <p>Input arguments</p>
- * @return string <p>Error message</p>
- */
- static private function _parseError($params)
- {
- /* Invalid call? */
- if (!(is_array($params) && isset($params[0])))
- {
- die('System error');
- }
- /* Exception has been thrown? */
- if ($params[0] instanceof Exception)
- {
- return $params[0]->getMessage() . "\n\n" . $params[0]->getTraceAsString();
- }
- /* Process error */
- if (count($params) == 5)
- {
- if (!(error_reporting() & $params[0]))
- {
- return 'unhandlable_error';
- }
- return $params[1] . "\n\n" . $params[2] . ':' . $params[3];
- }
- return 'unknown_error';
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement