Add options to thrift transport classes for custom error handlers
authorMark Slee <mcslee@apache.org>
Wed, 3 Jan 2007 19:23:50 +0000 (19:23 +0000)
committerMark Slee <mcslee@apache.org>
Wed, 3 Jan 2007 19:23:50 +0000 (19:23 +0000)
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

lib/php/src/transport/TSocket.php
lib/php/src/transport/TSocketPool.php

index 3dc643c..c193305 100644 (file)
@@ -65,16 +65,28 @@ class TSocket extends TTransport {
   private $debug_ = FALSE;
 
   /**
-   * Socket constructor
+   * Debug handler
    *
-   * @param string $host    Remote hostname
-   * @param int    $port    Remote port
-   * @param bool   $persist Whether to use a persistent socket
+   * @var mixed
    */
-  public function __construct($host='localhost', $port=9090, $persist=FALSE) {
+  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 $debugHandler Function to call for error logging
+   */
+  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 @@ class TSocket extends TTransport {
     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);
     }
index d9c78f8..5edac38 100644 (file)
@@ -64,17 +64,28 @@ class TSocketPool extends TSocket {
    */
   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 @@ class TSocketPool extends TSocket {
         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 @@ class TSocketPool extends TSocket {
         // 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 @@ class TSocketPool extends TSocket {
     $hostlist = implode(',', $hosts);
     $error .= '('.$hostlist.')';
     if ($this->debug_) {
-      error_log($error);
+      $this->debugHandler_($error);
     }
     throw new Exception($error);
   }