Advertisement
OldCode101

Console.class.php

May 10th, 2011
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.68 KB | None | 0 0
  1. <?php // PHP 5.2
  2.  
  3. require_once 'lib/org/firephp/core/FirePHP.class.php';
  4.  
  5. /**
  6.   * Console Class Object
  7.   * Assorted tools to handle error / debug messags
  8.   * @author eric.miller@e-vations.com
  9.   * @version Apr 27, 2011
  10.   */
  11. class Console{
  12.     const EOL = "\r\n";
  13.     const CSS_OPEN = "/*";
  14.     const CSS_CLOSE = "*/";
  15.     const HTML_OPEN = "<!-- ";
  16.     const HTML_CLOSE = " -->";
  17.     const DATESTAMP_FORMAT = 'Y-m-d H:i:s';
  18.  
  19.     const FIREBUG = TRUE;
  20.     const FIREBUG_LOG = 'log';
  21.     const FIREBUG_INFO = 'info';
  22.     const FIREBUG_WARN = 'warn';
  23.     const FIREBUG_ERROR = 'error';
  24.  
  25.     public static function write($msg=NULL, $label=NULL){
  26.         self::toFirebug($msg, self::FIREBUG_LOG, $label);
  27.         return;
  28.     }
  29.  
  30.     public static function error($msg=NULL, $label=NULL){
  31.         self::errorToFirebug($msg);
  32.         return;
  33.     }
  34.  
  35.     public static function exception($e=NULL){
  36.         self::exceptionToFirebug($e);
  37.         return;
  38.     }
  39.  
  40.     public static function debug($msg=NULL, $label=NULL){
  41.         self::infoToFirebug($msg, $label);
  42.         return;
  43.     }
  44.  
  45.     public static function warn($msg=NULL, $label=NULL){
  46.         self::warnToFirebug($msg, $label);
  47.         return;
  48.     }
  49.  
  50.  
  51.     /**
  52.      * Write errors to CSS File
  53.      * @param unknown_type $msg
  54.      * @param unknown_type $label
  55.      */
  56.     public static function cssWrite($msg=NULL, $label=NULL){
  57.         self::write(self::CSS_OPEN . self::getDatestamp() . ' ' .(!is_NULL($label) ? '['.$label.'] ' : ''). $msg . ' ' . self::CSS_CLOSE . self::EOL);
  58.         return;
  59.     }
  60.  
  61.     /**
  62.      * CSS Error Method
  63.      * Add error message to css
  64.      * @param String $msg
  65.      */
  66.     public static function cssError($msg=''){
  67.         Console::cssWrite($msg, 'error');
  68.         return;
  69.     }
  70.  
  71.     /**
  72.      * CSS Debug Method
  73.      * Add Debug information to css
  74.      * @param String $msg
  75.      * @param boolean $debug
  76.      */
  77.     public static function cssDebug($msg='', $label=NULL){
  78.         if(self::isDebug()){
  79.             Console::cssWrite((!is_NULL($label) ? ''.$label.' - ' : '').$msg, 'debug');
  80.         }
  81.     }
  82.  
  83.     /**
  84.      * Debug Check Switch
  85.      * Reads value of $_REQUEST[debug], if any and returns TRUE/FALSE
  86.      * @return boolean TRUE/FALSE
  87.      */
  88.     public static function isDebug(){
  89.         $isDebug = FALSE;
  90.         if(isset($_REQUEST['debug']) && $_REQUEST['debug'] == '1'){
  91.             $isDebug = TRUE;
  92.         }
  93.         return $isDebug;
  94.     }
  95.  
  96.     private static function getDatestamp(){
  97.         return date(self::DATESTAMP_FORMAT);
  98.     }
  99.  
  100.     private static function toFirebug($var=NULL, $type=self::FIREBUG_LOG, $label=NULL){
  101.         if(self::FIREBUG && self::isDebug()){
  102.             ob_start();
  103.             $firephp = FirePHP::getInstance(TRUE);
  104.             $firephp->setEnabled(self::isDebug());
  105.             $firephp->setOptions(array(
  106.                 'includeLineNumbers' => TRUE,
  107.             ));
  108.             switch ($type){
  109.                 case self::FIREBUG_ERROR:
  110.                     $firephp->error($var, label);
  111.                     $firephp->trace('Stacktrace');
  112.                     break;
  113.                 case self::FIREBUG_INFO:
  114.                     $firephp->info($var, $label);
  115.                     break;
  116.                 case self::FIREBUG_WARN:
  117.                     $firephp->warn($var, $label);
  118.                     break;
  119.                 default:
  120.                     $firephp->log($var, $label);
  121.                     break;
  122.             }
  123.         }
  124.         return;
  125.     }
  126.  
  127.     private static function exceptionToFirebug($e=NULL){
  128.         if(get_class($e) == 'Exception'){
  129.             $msg = self::getDatestamp() . ' - ' . $e->getMessage() . '('.$e->getFile().':'.$e->getLine().')';
  130.             self::errorToFirebug($msg);
  131.         }
  132.         return;
  133.     }
  134.  
  135.     private static function errorToFirebug($msg='', $label=NULL){
  136.         self::toFirebug($msg, self::FIREBUG_ERROR, $label);
  137.         return;
  138.     }
  139.  
  140.     private static function infoToFirebug($msg='', $label=NULL){
  141.         self::toFirebug($msg, self::FIREBUG_INFO, $label);
  142.         return;
  143.     }
  144.  
  145.     private static function warnToFirebug($msg='', $label=NULL){
  146.        self::toFirebug($msg, self::FIREBUG_WARN, $label);
  147.        return;
  148.     }
  149.  
  150.     private static function registerFirebug(){
  151.         if(self::FIREBUG && self::isDebug()){
  152.             ob_start();
  153.             $firephp = FirePHP::getInstance(TRUE);
  154.             $firephp->registerErrorHandler( $throwErrorExceptions=FALSE );
  155.             $firephp->registerExceptionHandler();
  156.             $firephp->registerAssertionHandler( $convertAssertionErrorsToExceptions=TRUE, $throwAssertionExceptions=FALSE );
  157.         }
  158.         return;
  159.     }
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement