From ad58f952bc0c637a203416eaaf46383f9955ef8e Mon Sep 17 00:00:00 2001 From: Mark Slee Date: Wed, 3 Jan 2007 19:23:50 +0000 Subject: [PATCH] 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 --- lib/php/src/transport/TSocket.php | 24 +++++++++++++----- lib/php/src/transport/TSocketPool.php | 35 ++++++++++++++++++--------- 2 files changed, 41 insertions(+), 18 deletions(-) diff --git a/lib/php/src/transport/TSocket.php b/lib/php/src/transport/TSocket.php index 3dc643ca..c193305d 100644 --- a/lib/php/src/transport/TSocket.php +++ b/lib/php/src/transport/TSocket.php @@ -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); } diff --git a/lib/php/src/transport/TSocketPool.php b/lib/php/src/transport/TSocketPool.php index d9c78f89..5edac383 100644 --- a/lib/php/src/transport/TSocketPool.php +++ b/lib/php/src/transport/TSocketPool.php @@ -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); } -- 2.17.1