home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / php / PEAR / System / Socket / Debugger.php < prev    next >
Encoding:
PHP Script  |  2008-07-02  |  8.3 KB  |  335 lines

  1. <?php
  2. // +----------------------------------------------------------------------+
  3. // | PEAR :: System :: Socket :: Debugger                                 |
  4. // +----------------------------------------------------------------------+
  5. // | This source file is subject to version 3.0 of the PHP license,       |
  6. // | that is available at http://www.php.net/license/3_0.txt              |
  7. // | If you did not receive a copy of the PHP license and are unable      |
  8. // | to obtain it through the world-wide-web, please send a note to       |
  9. // | license@php.net so we can mail you a copy immediately.               |
  10. // +----------------------------------------------------------------------+
  11. // | Copyright (c) 2004 Michael Wallner <mike@iworks.at>                  |
  12. // +----------------------------------------------------------------------+
  13. //
  14. // $Id: Debugger.php,v 1.1 2004/04/29 15:55:38 mike Exp $
  15.  
  16. /**
  17. * System::Socket::Debugger
  18. * @TODO!        XXX add extended support for PEAR::Log XXX
  19. * @author       Michael Wallner <mike@php.net>
  20. * @package      System_Socket
  21. * @category     System
  22. * @example      simple-debugger.php Simple debugging (echo) example
  23. */
  24.  
  25. if (!defined('SYSTEM_SOCKET_ROOT')) {
  26.     define('SYSTEM_SOCKET_ROOT', dirname(__FILE__) . '/..');
  27. }
  28.  
  29. require_once SYSTEM_SOCKET_ROOT . '/Socket/Observer.php';
  30.  
  31. define('SYSTEM_SOCKET_DEBUG_NONE',          0);
  32. define('SYSTEM_SOCKET_DEBUG_PEAR_LOG',      1);
  33. define('SYSTEM_SOCKET_DEBUG_PEAR_ERROR',    2);
  34. define('SYSTEM_SOCKET_DEBUG_ECHO',          3);
  35. define('SYSTEM_SOCKET_DEBUG_ERRORLOG',      4);
  36. define('SYSTEM_SOCKET_DEBUG_TRIGGERERROR',  5);
  37.  
  38. /** 
  39. * System_Socket_Debugger
  40. * The System_Socket_Debugger class aims to provide a simple and versatile
  41. * option to debug socket implementations in various ways.
  42. * Example 1:
  43. * <code>
  44. * require_once 'System/Socket.php';
  45. * require_once 'System/Socket/Debugger.php';
  46. * $conn = &System_Socket::createConnection(
  47. *   array('address'=>'pear.php.net', 'port'=>80));
  48. * $conn->attach(new System_Socket_Debugger(SYSTEM_SOCKET_DEBUG_ECHO));
  49. * </code>
  50. * Example 2:
  51. * <code>
  52. * // no need to explicitly create a System_Socket_Debugger object
  53. * require_once 'System/Socket.php';
  54. * require_once 'System/Socket/Debugger.php';
  55. * define('SYSTEM_SOCKET_DEBUG', SYSTEM_SOCKET_DEBUG_ECHO);
  56. * $conn = &System_Socket::createConnection(
  57. *   array('address'=>'pear.php.net', 'port'=>80));
  58. * // a debugger object gets attached automagically
  59. * </code>
  60. *
  61. * @author   Michael Wallner <mike@php.net>
  62. * @version  $Revision: 1.1 $
  63. * @access   public
  64. */
  65. class System_Socket_Debugger extends System_Socket_Observer
  66. {
  67.     /**
  68.     * Linefeed character(s)
  69.     * 
  70.     * @access   public
  71.     * @var      string
  72.     */
  73.     var $LF = "\n";
  74.     
  75.     /**
  76.     * Type
  77.     * 
  78.     * @access   public
  79.     * @var      int
  80.     */
  81.     var $type = SYSTEM_SOCKET_DEBUG_NONE;
  82.  
  83.     /**
  84.     * Whether to log all method calls
  85.     * 
  86.     * @access   public
  87.     * @var      bool
  88.     */
  89.     var $verboseCalls = true;
  90.     
  91.     /**
  92.     * Whether to log methods arguments
  93.     * 
  94.     * @access   public
  95.     * @var      bool
  96.     */
  97.     var $verboseArgs = true;
  98.     
  99.     /**
  100.     * Whether to log methods results
  101.     * 
  102.     * @access   public
  103.     * @var      bool
  104.     */
  105.     var $verboseResults = true;
  106.     
  107.     /**
  108.     * Whether to echo a date
  109.     * 
  110.     * @access   public
  111.     * @var      bool
  112.     */
  113.     var $echoDate = true;
  114.     
  115.     /**
  116.     * Echoed dates format
  117.     * 
  118.     * @access   public
  119.     * @var      string
  120.     */
  121.     var $dateFormat = 'Y-m-d H:i:s';
  122.     
  123.     /**
  124.     * PEAR::Log object
  125.     * 
  126.     * @readonly
  127.     * @access   public
  128.     * @var      object
  129.     */
  130.     var $PearLog = null;
  131.     
  132.     /**
  133.     * Whether a PEAR::Log object is attached
  134.     * 
  135.     * @readonly
  136.     * @access   public
  137.     * @var      bool
  138.     */
  139.     var $hasPearLog = false;
  140.     
  141.     /**
  142.     * Note stack
  143.     * 
  144.     * @access   protected
  145.     * @var      array
  146.     */
  147.     var $stack = array();
  148.     
  149.     /**
  150.     * Constructor
  151.     * 
  152.     * @access   public
  153.     * @return   object  System_Socket_Debugger
  154.     */
  155.     function System_Socket_Debugger($type = null)
  156.     {
  157.         System_Socket_Debugger::__construct($type);
  158.     }
  159.  
  160.     /**
  161.     * ZE2 Constructor
  162.     * @ignore
  163.     */
  164.     function __construct($type = null)
  165.     {
  166.         parent::__construct();
  167.         
  168.         if (isset($type)) {
  169.             $this->type = $type;
  170.         } elseif (defined('SYSTEM_SOCKET_DEBUG')) {
  171.             $this->type = SYSTEM_SOCKET_DEBUG;
  172.         }
  173.     }
  174.     
  175.     /**
  176.     * Set a configured PEAR_Log object
  177.     * 
  178.     * @access   public
  179.     * @return   bool
  180.     * @param    object Log  $log
  181.     */
  182.     function setPearLog(&$log)
  183.     {
  184.         if ($this->hasPearLog = is_a($log, 'Log')) {
  185.             $this->PearLog = &$log;
  186.             return true;
  187.         }
  188.         return false;
  189.     }
  190.     
  191.     /**
  192.     * Notify
  193.     * 
  194.     * @access   public
  195.     * @return   void
  196.     * @param    array   $note
  197.     */
  198.     function notify($note)
  199.     {
  200.         static $lastVerboseCall;
  201.         
  202.         $this->stack[] = $note;
  203.         list($event, $class, $result, $args) = $note;
  204.         
  205.         if (!$this->verboseCalls) {
  206.             if (isset($lastVerboseCall) && 
  207.                 preg_match('/^'. $lastVerboseCall .'.+/', $event)) {
  208.                 return;
  209.             }
  210.             $lastVerboseCall = $event;
  211.         }
  212.         
  213.         $message = $class . '::' . $event;
  214.         
  215.         if ($this->verboseArgs) {
  216.             $message .= '(';
  217.             if (is_array($args)) {
  218.                 foreach ($args as $arg) {
  219.                     $message .= $this->arg2string($arg) . ', ';
  220.                 }
  221.                 $message = substr($message, 0, -2);
  222.             } else {
  223.                 $message .= $this->arg2string($arg);
  224.             }
  225.             $message .= ')';
  226.         }
  227.         
  228.         if ($this->verboseResults) {
  229.             $message .= ' Result: ' . $this->arg2string($result);
  230.         }
  231.         
  232.         switch ($this->type) {
  233.             
  234.             case SYSTEM_SOCKET_DEBUG_PEAR_LOG:
  235.                 if ($this->hasPearLog) {
  236.                     $this->PearLog->log($message);
  237.                 }
  238.             break;
  239.             
  240.             case SYSTEM_SOCKET_DEBUG_PEAR_ERROR:
  241.                 PEAR::raiseError($message);
  242.             break;
  243.             
  244.             case SYSTEM_SOCKET_DEBUG_ERRORLOG:
  245.                 error_log($message);
  246.             break;
  247.             
  248.             case SYSTEM_SOCKET_DEBUG_TRIGGERERROR;
  249.                 trigger_error($message, E_USER_NOTICE);
  250.             break;
  251.  
  252.             case SYSTEM_SOCKET_DEBUG_ECHO: 
  253.                 if ($this->echoDate) {
  254.                     echo date($this->dateFormat) . ' ';
  255.                 }
  256.                 echo $message, $this->LF;
  257.             break;
  258.         }
  259.     }
  260.     
  261.     /**
  262.     * Get (part of) note stack
  263.     *
  264.     * Each received note gets stored in the notes stack of the debugger.  With
  265.     * this method you can get the whole stack or the <var>$num</var>th part.
  266.     * 
  267.     * @access   public
  268.     * @return   array
  269.     * @param    int     $num
  270.     */
  271.     function getStack($num = null)
  272.     {
  273.         return isset($num, $this->stack[$num]) ? 
  274.             $this->stack[$num] : $this->stack;
  275.     }
  276.     
  277.     /**
  278.     * arg2string
  279.     *
  280.     * Attempts to convert any argument to a representive string.
  281.     * 
  282.     * @access   public
  283.     * @return   string
  284.     * @param    mixed   $arg
  285.     */
  286.     function arg2string($arg)
  287.     {
  288.         switch (strToLower($type = getType($arg)))
  289.         {
  290.             case 'null':
  291.             {
  292.                 return 'NULL';
  293.             }
  294.             
  295.             case 'resource':
  296.             {
  297.                 return get_resource_type($arg) . ' (' . (string) $arg . ')';
  298.             }
  299.             
  300.             case 'double':
  301.             case 'integer':
  302.             {
  303.                 return (string) $arg;
  304.             }
  305.             
  306.             case 'object':
  307.             {
  308.                 return get_class($arg) . ' (' . (method_exists($arg, 'toString')
  309.                     ? $arg->toString() : ('Object' == ($string = (string) $arg)
  310.                     ? '(' . serialize($arg) . ')' : $string));
  311.             }
  312.             
  313.             case 'array':
  314.             {
  315.                 return 'Array (' . serialize($arg) .')';
  316.             }
  317.             
  318.             case 'string':
  319.             {
  320.                 return '"' . addCSlashes($arg, "\n\t\r\"\0") . '"';
  321.             }
  322.             
  323.             default:
  324.             {
  325.                 return "($type) " . (string) $arg;
  326.             }
  327.         }
  328.     }
  329. }
  330. ?>