Advertisement
miraage

Untitled

Feb 1st, 2012
442
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.20 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * Debug helper
  5.  */
  6. class Debug
  7. {
  8.     /**
  9.      * Debug initiated?
  10.      *
  11.      * @var boolean
  12.      */
  13.     static private $_initiated = false;
  14.  
  15.     /**
  16.      * Initialize debugging
  17.      */
  18.     static public function main()
  19.     {
  20.         /* Init once */
  21.         if (self::$_initiated)
  22.         {
  23.             return;
  24.         }
  25.         self::$_initiated = true;
  26.  
  27.         /* Setup error/exception handling */
  28.         $handler = array(__CLASS__, IN_DEV ? 'devhdl' : 'prodhdl');
  29.         set_error_handler($handler);
  30.         set_exception_handler($handler);
  31.     }
  32.  
  33.     /**
  34.      * Development mode handler
  35.      */
  36.     static public function devhdl()
  37.     {
  38.         /* Init */
  39.         $msg = self::_parseError(func_get_args());
  40.  
  41.         /* Build output */
  42.         $out = sprintf('<pre>%s</pre>', print_r($msg, 1));
  43.  
  44.         /* Send */
  45.         die($out);
  46.     }
  47.  
  48.     /**
  49.      * Production mode handler
  50.      */
  51.     static public function prodHdl()
  52.     {
  53.         /* Init */
  54.         $msg = self::_parseError(func_get_args());
  55.         $sep = "\n" . str_repeat('-', 60) . "\n";
  56.  
  57.         /* Log message */
  58.         $log = fopen(DEV_LOG, 'a+b');
  59.         flock($log, LOCK_EX);
  60.         fwrite($log, $msg . $sep);
  61.         flock($log, LOCK_UN);
  62.         fclose($log);
  63.  
  64.         /* Send */
  65.         die('System error');
  66.     }
  67.  
  68.     /**
  69.      * Error parser
  70.      *
  71.      * @param array $params <p>Input arguments</p>
  72.      * @return string <p>Error message</p>
  73.      */
  74.     static private function _parseError($params)
  75.     {
  76.         /* Invalid call? */
  77.         if (!(is_array($params) && isset($params[0])))
  78.         {
  79.             die('System error');
  80.         }
  81.  
  82.         /* Exception has been thrown? */
  83.         if ($params[0] instanceof Exception)
  84.         {
  85.             return $params[0]->getMessage() . "\n\n" . $params[0]->getTraceAsString();
  86.         }
  87.  
  88.         /* Process error */
  89.         if (count($params) == 5)
  90.         {
  91.             if (!(error_reporting() & $params[0]))
  92.             {
  93.                 return 'unhandlable_error';
  94.             }
  95.  
  96.             return $params[1] . "\n\n" . $params[2] . ':' . $params[3];
  97.         }
  98.  
  99.         return 'unknown_error';
  100.     }
  101. }
  102.  
  103. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement