Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

added syslog and firephp hooks (logger interface)

  • Loading branch information...
commit a98d349e002e48364329513982f6de4bef570938 1 parent 5853af5
@mjpearson authored
View
25 config.php
@@ -20,24 +20,33 @@
define('THRIFT_PORT_DEFAULT', 9160);
+// nasty autoloader in the absense of namespace
function _pandraAutoLoad($className) {
+ // just bail if it doesn't look like us
if (!preg_match("/^pandra/i", $className)) return;
+ // seperate classes and interfaces for clarity
+ $fExt = array('.class.php', '.interface.php');
+
+ // strip prefix
$className = preg_replace('/^pandra/i', '', $className);
// class path relative to config
$classPath = dirname(__FILE__)."/lib/";
- if (preg_match('/^(Query|Clause)/', $className)) $classPath .= 'query/';
-
- // class file suffix
- $cSuffix = ".class.php";
-
- $classFile = $classPath.$className.$cSuffix;
+ if (preg_match('/^(Query|Clause)/', $className)) {
+ $classPath .= 'query/';
+ } elseif (preg_match('/^Log/', $className)) {
+ $classPath .= 'logging/';
+ }
- if (file_exists($classFile)) {
- require_once($classFile);
+ foreach ($fExt as $ext) {
+ $classFile = $classPath.$className.$ext;
+ if (file_exists($classFile)) {
+ require_once($classFile);
+ break;
+ }
}
}
spl_autoload_register('_pandraAutoLoad');
View
12 lib/Column.class.php
@@ -4,6 +4,8 @@
*
* For licensing terms, plese see license.txt which should distribute with this source
*
+ * Column is Cassandra's atomic datatype, consisting of a name/value and timestamp
+ *
* @package Pandra
* @link http://www.phpgrease.net/projects/pandra
* @author Michael Pearson <pandra-support@phpgrease.net>
@@ -126,6 +128,10 @@ public function setValue($value, $validate = TRUE) {
if ($this->_parent !== NULL) {
$this->_parent->registerError($this->errors[0]);
}
+
+ if (PandraLog::isRegistered('FirePHP')) {
+ PandraLog::logTo('FirePHP', $this->errors);
+ }
return FALSE;
}
}
@@ -135,7 +141,6 @@ public function setValue($value, $validate = TRUE) {
$this->value = $value;
$this->setModified();
return TRUE;
-
}
/**
@@ -375,6 +380,7 @@ public function save($consistencyLevel = NULL) {
$columnPath,
$this->bindTime(),
PandraCore::getConsistency($consistencyLevel));
+
} else {
$ok = PandraCore::saveColumnPath(
$this->getKeySpace(),
@@ -408,7 +414,9 @@ public function save($consistencyLevel = NULL) {
public function registerError($errorStr) {
if (!empty($errorStr)) {
array_push($this->errors, $errorStr);
- if ($this->_parent !== NULL) $this->_parent->registerError($errorStr);
+ if ($this->_parent !== NULL) {
+ $this->_parent->registerError($errorStr);
+ }
}
}
View
16 lib/ColumnContainer.class.php
@@ -170,6 +170,11 @@ public function getDelete() {
public function registerError($errorStr) {
if (empty($errorStr)) return;
$this->errors[] = $errorStr;
+
+ if (PandraLog::isRegistered('syslog')) {
+ // @todo get_called_class(), 5.3
+ PandraLog::logTo('syslog', '('.get_class($this).') '.$errorStr, $priority);
+ }
}
/**
@@ -539,12 +544,13 @@ public function __get($columnName) {
* @return bool field set ok
*/
public function __set($columnName, $value) {
- if (!$this->_gsMutable($columnName) && $this->getAutoCreate()) {
+ $mutable = $this->_gsMutable($columnName);
+ if (!$mutable && $this->getAutoCreate()) {
$this->addColumn($columnName);
- }
-
- if (!$this->setColumn($columnName, $value)) {
- throw new RuntimeException($columnName.' set but does not exist in container');
+ } elseif ($mutable) {
+ if (!$this->setColumn($columnName, $value)) {
+ throw new RuntimeException($columnName.' set but does not exist in container');
+ }
}
}
View
0  lib/ContainerChild.class.php → lib/ContainerChild.interface.php
File renamed without changes
View
0  lib/ContainerSavable.class.php → lib/ContainerSavable.interface.php
File renamed without changes
View
57 lib/Core.class.php
@@ -49,6 +49,12 @@ class PandraCore {
/* @var int default write mode (active/round/random) */
static private $writeMode = self::MODE_RANDOM;
+ /* @var bool flag whether to log to syslog */
+ static private $_syslogEnabled = FALSE;
+
+ /* @var bool flag whether to log to firebug */
+ static private $_firePHPEnabled = FALSE;
+
/* @var supported modes for this core version */
static private $_supportedModes = array(
self::MODE_ACTIVE,
@@ -199,13 +205,38 @@ static public function connect($connectionID, $host, $poolName = self::DEFAULT_P
self::setActivePool($poolName);
self::setActiveNode($connectionID);
return TRUE;
- } catch (TException $tx) {
- self::$lastError = 'TException: '.$tx->getMessage() . "\n";
+ } catch (TException $te) {
+ self::registerError('TException: '.$te->getMessage());
+
}
return FALSE;
}
+ static public function enableSyslog() {
+ self::$_syslogEnabled = PandraLog::register('Syslog');
+ }
+
+ static public function enableFirePHP() {
+ self::$_firePHPEnabled = PandraLog::register('FirePHP');
+ }
+
+ static public function registerError($errorMsg, $priority = PandraLog::LOG_NOTICE) {
+ $message = '(PandraCore) '.$errorMsg;
+
+ if (self::$_syslogEnabled && PandraLog::isRegistered('Syslog')) {
+ // @todo get_called_class(), 5.3
+ PandraLog::logTo('Syslog', $message, $priority);
+ }
+
+ if (self::$_firePHPEnabled && PandraLog::isRegistered('FirePHP')) {
+ // @todo get_called_class(), 5.3
+ PandraLog::logTo('FirePHP', $message, $priority);
+ }
+
+ self::$lastError = $errorMsg;
+ }
+
/**
* Given a single host, attempts to find other nodes in the cluster and attaches them
* to the pool
@@ -226,8 +257,8 @@ static public function autoDiscover($host, $poolName = self::DEFAULT_POOL_NAME,
$tokenMap = $client->get_string_property('token map');
return TRUE;
- } catch (TException $tx) {
- self::$lastError = 'TException: '.$tx->getMessage() . "\n";
+ } catch (TException $te) {
+ self::registerError( 'TException: '.$te->getMessage());
}
return FALSE;
}
@@ -367,7 +398,7 @@ public function deleteColumnPath($keySpace, $keyID, cassandra_ColumnPath $column
}
$client->remove($keySpace, $keyID, $columnPath, $time, self::getConsistency($consistencyLevel));
} catch (TException $te) {
- self::$lastError = 'TException: '.$te->getMessage() . "\n";
+ self::registerError( 'TException: '.$te->getMessage());
return FALSE;
}
return TRUE;
@@ -391,7 +422,7 @@ public function saveColumnPath($keySpace, $keyID, cassandra_ColumnPath $columnPa
$client->insert($keySpace, $keyID, $columnPath, $value, $time, self::getConsistency($consistencyLevel));
} catch (TException $te) {
- self::$lastError = 'TException: '.$te->getMessage() . "\n";
+ self::registerError( 'TException: '.$te->getMessage());
return FALSE;
}
return TRUE;
@@ -430,7 +461,7 @@ public function saveSuperColumn($keySpace, $keyID, array $superCFName, array $su
$client->batch_insert($keySpace, $keyID, $mutations, self::getConsistency($consistencyLevel));
} catch (TException $te) {
- self::$lastError = 'TException: '.$te->getMessage() . "\n";
+ self::registerError( 'TException: '.$te->getMessage());
return FALSE;
}
return TRUE;
@@ -467,7 +498,7 @@ public function getCFSlice($keySpace, $keyID, $columnFamilyName, $superColumnNam
try {
return $client->get_slice($keySpace, $keyID, $columnParent, $predicate, self::getConsistency($consistencyLevel));
} catch (TException $te) {
- self::$lastError = 'TException: '.$te->getMessage() . "\n";
+ self::registerError( 'TException: '.$te->getMessage());
return NULL;
}
return NULL;
@@ -506,7 +537,7 @@ public function getCFSliceMulti($keySpace, array $keyIDs, $columnFamilyName, $su
try {
return $client->multiget_slice($keySpace, $keyIDs, $columnParent, $predicate, self::getConsistency($consistencyLevel));
} catch (TException $te) {
- self::$lastError = 'TException: '.$te->getMessage() . "\n";
+ self::registerError( 'TException: '.$te->getMessage());
return NULL;
}
}
@@ -530,7 +561,7 @@ public function getCFColumnCount($keySpace, $keyID, $columnFamilyName, $superCol
try {
return $client->get_count($keySpace, $keyID, $columnParent, self::getConsistency($consistencyLevel));
} catch (TException $te) {
- self::$lastError = 'TException: '.$te->getMessage() . "\n";
+ self::registerError( 'TException: '.$te->getMessage());
return NULL;
}
}
@@ -558,7 +589,7 @@ public function getColumn($keySpace, $keyID, $columnFamilyName, $columnName, $su
try {
return $client->get($keySpace, $keyID, $columnPath, self::getConsistency($consistencyLevel));
} catch (TException $te) {
- self::$lastError = 'TException: '.$te->getMessage() . "\n";
+ self::registerError( 'TException: '.$te->getMessage());
return NULL;
}
}
@@ -569,7 +600,7 @@ public function getColumnPath($keySpace, $keyID, cassandra_ColumnPath $columnPat
try {
return $client->get($keySpace, $keyID, $columnPath, self::getConsistency($consistencyLevel));
} catch (TException $te) {
- self::$lastError = 'TException: '.$te->getMessage() . "\n";
+ self::registerError( 'TException: '.$te->getMessage());
return NULL;
}
}
@@ -609,7 +640,7 @@ public function getRangeKeys($keySpace, $columnFamilyName, $columnNames, $superC
try {
return $client->get_range_slice($keySpace, $columnParent, $predicate, $keyStart, $keyFinish, $numRows, self::getConsistency($consistencyLevel));
} catch (TException $te) {
- self::$lastError = 'TException: '.$te->getMessage() . "\n";
+ self::registerError( 'TException: '.$te->getMessage());
return NULL;
}
View
15 lib/SuperColumn.class.php
@@ -4,6 +4,21 @@
*
* For licensing terms, plese see license.txt which should distribute with this source
*
+ * SuperColumns are special kinds of Column Containers which can contain both
+ * Columns and have a ColumnFamily parent (implements ContainerChild)
+ *
+ * 'super column family' => // Super Column Family
+ * 'my supercolumn' => { // Super Column
+ * 'column1', // Column
+ * 'column2', // Column
+ * 'column3' // Column
+ * },
+ * 'my supercolumn2' => { // Super Column
+ * 'column4', // Column
+ * 'column5', // Column
+ * 'column6' // Column
+ * },
+ * }
* @package Pandra
* @author Michael Pearson <pandra-support@phpgrease.net>
*/
View
15 lib/SuperColumnFamily.class.php
@@ -4,7 +4,20 @@
*
* For licensing terms, plese see license.txt which should distribute with this source
*
- * SuperColumnFamily is a container of SuperColumns
+ * SuperColumnFamily is a container of SuperColumns.
+ *
+ * 'super column family' => // Super Column Family
+ * 'my supercolumn' => { // Super Column
+ * 'column1', // Column
+ * 'column2', // Column
+ * 'column3' // Column
+ * },
+ * 'my supercolumn2' => { // Super Column
+ * 'column4', // Column
+ * 'column5', // Column
+ * 'column6' // Column
+ * },
+ * }
*
* @package Pandra
* @author Michael Pearson <pandra-support@phpgrease.net>
View
49 lib/logging/Log.class.php
@@ -0,0 +1,49 @@
+<?php
+
+class PandraLog {
+
+ const LOG_EMERG = 0;
+ const LOG_ALERT = 1;
+ const LOG_CRIT = 2;
+ const LOG_ERR = 3;
+ const LOG_WARNING = 4;
+ const LOG_NOTICE = 5;
+ const LOG_INFO = 6;
+ const LOG_DEBUG = 7;
+
+ static private $_loggers = array();
+
+ static private function getClassFromName($loggerName) {
+ return 'PandraLogger'.ucfirst($loggerName);
+ }
+
+ static public function getRegisteredLoggers() {
+ return array_values(self::$_loggers);
+ }
+
+ static public function register($loggerName, $params = array()) {
+ $lc = self::getClassFromName($loggerName);
+ if (!array_key_exists($lc, self::$_loggers)) {
+ $lObj = new $lc($params);
+ self::$_loggers[$lc] = &$lObj;
+ return TRUE;
+ }
+ return FALSE;
+ }
+
+ static public function logTo($loggerName, $message, $priority = self::LOG_NOTICE) {
+ $lc = self::getClassFromName($loggerName);
+ if (array_key_exists($lc, self::$_loggers)) {
+ self::$_loggers[$lc]->execute($priority, $message);
+ } else {
+ throw new RuntimeException("Logger '$lc' has not been registered");
+ }
+ }
+
+ static public function isRegistered($loggerName) {
+ $lc = self::getClassFromName($loggerName);
+ return array_key_exists($lc, self::$_loggers);
+ }
+
+}
+?>
View
12 lib/logging/Logger.interface.php
@@ -0,0 +1,12 @@
+<?php
+
+interface PandraLogger {
+
+ public function __construct(array $params);
+
+ public function isOpen();
+
+ public function execute($priority, $message);
+
+}
+?>
View
39 lib/logging/LoggerFirePHP.class.php
@@ -0,0 +1,39 @@
+<?php
+class PandraLoggerFirePHP implements PandraLogger {
+
+ private $_isOpen = FALSE;
+
+ private $_fb = NULL;
+
+ public function __construct(array $params) {
+ $this->_isOpen = class_exists('FB');
+ if ($this->_isOpen) {
+ $this->_fb = FirePHP::getInstance(true);
+ }
+ return $this->_isOpen;
+ }
+
+ public function isOpen() {
+ return $this->_isOpen;
+ }
+
+ public function execute($priority, $message) {
+ if ($this->_isOpen) {
+ switch ($priority) {
+ case PandraLog::LOG_NOTICE :
+ $this->_fb->log($message);
+ break;
+ case PandraLog::LOG_INFO :
+ $this->_fb->info($message);
+ break;
+ case PandraLog::LOG_WARNING :
+ $this->_fb->warn($message);
+ break;
+ default:
+ $this->_fb->error($message);
+ }
+ }
+ return FALSE;
+ }
+}
+?>
View
44 lib/logging/LoggerSyslog.class.php
@@ -0,0 +1,44 @@
+<?php
+class PandraLoggerSyslog implements PandraLogger {
+
+ private $_isOpen = FALSE;
+
+ public function __construct(array $params) {
+ if (empty($params)) {
+ $params = array('ident' => 'pandra', 'option' => LOG_ODELAY, 'facility' => LOG_SYSLOG);
+ }
+
+ return $this->open($params['ident'], $params['option'], $params['facility']);
+ }
+
+ public function isOpen() {
+ return $this->_isOpen;
+ }
+
+ public function open($ident, $option = LOG_ODELAY, $facility = LOG_SYSLOG) {
+ $this->_isOpen = openlog($ident, $option, $facility);
+ return $this->_isOpen;
+ }
+
+ public function execute($priority, $message) {
+ if (!empty($message)) {
+ if ($this->_isOpen) {
+ if (is_array($message)) {
+ foreach ($message as $msg) {
+ syslog($priority, $message);
+ }
+ } else {
+ syslog($priority, $message);
+ }
+ return TRUE;
+ }
+ }
+ return FALSE;
+ }
+
+ public function close() {
+ if ($this->_isOpen) return closelog();
+ return FALSE;
+ }
+}
+?>
Please sign in to comment.
Something went wrong with that request. Please try again.