Add options to thrift transport classes for custom error handlers

Summary: So we can pass debug_rlog (facebook custom) in as a handler for errors to thrift (generic open source)

Reviewed By: lucas


git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@664903 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/lib/php/src/transport/TSocket.php b/lib/php/src/transport/TSocket.php
index 3dc643c..c193305 100644
--- a/lib/php/src/transport/TSocket.php
+++ b/lib/php/src/transport/TSocket.php
@@ -65,16 +65,28 @@
   private $debug_ = FALSE;
 
   /**
+   * Debug handler
+   *
+   * @var mixed
+   */
+  private $debugHandler_ = null;
+
+  /**
    * Socket constructor
    *
-   * @param string $host    Remote hostname
-   * @param int    $port    Remote port
-   * @param bool   $persist Whether to use a persistent socket
+   * @param string $host         Remote hostname
+   * @param int    $port         Remote port
+   * @param bool   $persist      Whether to use a persistent socket
+   * @param string $debugHandler Function to call for error logging
    */
-  public function __construct($host='localhost', $port=9090, $persist=FALSE) {
+  public function __construct($host='localhost',
+                              $port=9090,
+                              $persist=FALSE,
+                              $debugHandler=null) {
     $this->host_ = $host;
     $this->port_ = $port;
     $this->persist_ = $persist;
+    $this->debugHandler_ = $debugHandler ? $debugHandler : 'error_log';
   }
 
   /**
@@ -135,7 +147,7 @@
     if ($this->handle_ === FALSE) {
       $error = 'TSocket: Could not connect to '.$this->host_.':'.$this->port_;
       if ($this->debug_) {
-        error_log($error);
+        $this->debugHandler_($error);
       }
       throw new Exception($error);
     }
diff --git a/lib/php/src/transport/TSocketPool.php b/lib/php/src/transport/TSocketPool.php
index d9c78f8..5edac38 100644
--- a/lib/php/src/transport/TSocketPool.php
+++ b/lib/php/src/transport/TSocketPool.php
@@ -65,16 +65,27 @@
   private $alwaysTryLast_ = TRUE;
 
   /**
+   * User can supply their own debug handler instead of error_log
+   *
+   * @var mixed
+   */
+  private $debugHandler_ = null;
+
+  /**
    * Socket pool constructor
    *
-   * @param array  $hosts   List of remote hostnames
-   * @param mixed  $ports   Array of remote ports, or a single common port
-   * @param bool   $persist Whether to use a persistent socket
+   * @param array  $hosts        List of remote hostnames
+   * @param mixed  $ports        Array of remote ports, or a single common port
+   * @param bool   $persist      Whether to use a persistent socket
+   * @param mixed  $debugHandler Function for error logging
    */
   public function __construct($hosts=array('localhost'),
                               $ports=array(9090),
-                              $persist=FALSE) {
-    parent::__construct(null, 0, $persist);
+                              $persist=FALSE,
+                              $debugHandler=null) {
+    parent::__construct(null, 0, $persist, $debugHandler);
+
+    $this->debugHandler_ = $debugHandler ? $debugHandler : 'error_log';
 
     if (!is_array($ports)) {
       $port = $ports;
@@ -171,9 +182,9 @@
         if ($elapsed > $retryInterval) {
           $retryIntervalPassed = TRUE;
           if ($this->debug_) {
-            error_log('TSocketPool: retryInterval '.
-                      '('.$this->retryInterval_.') '.
-                      'has passed for host '.$host.':'.$port);
+            $this->debugHandler_('TSocketPool: retryInterval '.
+                                 '('.$this->retryInterval_.') '.
+                                 'has passed for host '.$host.':'.$port);
           }
         }
       }
@@ -227,9 +238,9 @@
         // Log and cache this failure
         if ($consecfails >= $this->maxConsecutiveFailures_) {
           if ($this->debug_) {
-            error_log('TSocketPool: marking '.$host.':'.$port.
-                      ' as down for '.$this->retryInterval.' seconds '.
-                      'after '.$consecfails.' failed connect attempts.');
+            $this->debugHandler_('TSocketPool: marking '.$host.':'.$port.
+                                 ' as down for '.$this->retryInterval.' secs '.
+                                 'after '.$consecfails.' failed attempts.');
           }
           // Store the failure time
           apc_store($failtimeKey, time());
@@ -251,7 +262,7 @@
     $hostlist = implode(',', $hosts);
     $error .= '('.$hostlist.')';
     if ($this->debug_) {
-      error_log($error);
+      $this->debugHandler_($error);
     }
     throw new Exception($error);
   }