Skip to content

Commit

Permalink
Add Factory class to ActiveSync package.
Browse files Browse the repository at this point in the history
  • Loading branch information
mrubinsk committed Feb 17, 2017
1 parent eb29c4c commit 5f526a5
Showing 1 changed file with 113 additions and 0 deletions.
113 changes: 113 additions & 0 deletions framework/ActiveSync/lib/Horde/ActiveSync/Log/Factory.php
@@ -0,0 +1,113 @@
<?php
/**
* ActiveSync log factory.
*
* @copyright 2010-2017 Horde LLC (http://www.horde.org/)
* @author Michael J Rubinsky <mrubinsk@horde.org>
* @category Horde
* @license http://www.horde.org/licenses/bsd BSD
* @package ActiveSync
*/

/**
* @copyright 2010-2017 Horde LLC (http://www.horde.org/)
* @author Michael J Rubinsky <mrubinsk@horde.org>
* @category Horde
* @license http://www.horde.org/licenses/bsd BSD
* @package ActiveSync
*/
class Horde_ActiveSync_Log_Factory implements Horde_ActiveSync_Interface_LoggerFactory
{
/**
* Parameter array.
*
* @var array
*/
protected $_params;

/**
* Const'r
*
* @param array $params Factory parameters. Must contain:
* - type: The type of log. ['onefile' | 'perdevice' | 'perrequest']
* - path: The path to either the log file (for 'onefile' type) or
* the logging directory (for 'perdevice' or 'perrequest' types).
* - level: The logging level. Defaults to META. MUST be one of
* [CLIENT | META]. CLIENT logs all WBXML sent/received. META
* will log CLIENT plus additional debug messaging.
*
* @throws InvalidArgumentException
*/
public function __construct(array $params)
{
if (empty($params['type']) || empty($params['path'])) {
throw new InvalidArgumentException('Missing required parameters.');
}
if (empty($params['level'])) {
$params['level'] = 'CLIENT';
}
$this->_params = $params;
}

/**
* Creates and configures the logger object.
*
* @param array $properties The property array. Sould contain:
* - DeviceId: The device id of the current client.
* - Cmd: The current command being handled, if known.
*
* @return Horde_Log_Logger The logger object, correctly configured.
*/
public function create($properties = array())
{
$stream = $logger = false;
$formatter = new Horde_ActiveSync_Log_Formatter();

switch ($this->_params['type']) {
case 'onefile':
if (!empty($properties['DeviceId'])) {
$device_id = Horde_String::upper($properties['DeviceId']);
$stream = @fopen($this->_params['path'], 'a');
}
break;
case 'perdevice':
if (!empty($properties['DeviceId'])) {
$stream = @fopen(
$this->_params['path'] . '/' . Horde_String::upper($properties['DeviceId']) . '.txt',
'a'
);
}
break;
case 'perrequest':
if (!empty($properties['DeviceId'])) {
$dir = sprintf('%s/%s',
$this->_params['path'],
Horde_String::upper($properties['DeviceId'])
);
if (!is_dir($dir)) {
mkdir($dir, 0755, true);
}
$path = sprintf('%s/%s-%s-%s.txt',
$dir,
time(),
getmypid(),
(!empty($properties['Cmd']) ? $properties['Cmd'] : 'UnknownCmd')
);
$stream = fopen($path, 'a');
}
}

if ($stream) {
$handler = new Horde_ActiveSync_Log_Handler($stream, false, $formatter);
$handler->addFilter(constant('Horde_ActiveSync_Log_Logger::' . $this->_params['level']));
$logger = new Horde_ActiveSync_Log_Logger($handler);
}

if (!$logger) {
$logger = new Horde_Log_Logger(new Horde_Log_Handler_Null());
}

return $logger;
}

}

0 comments on commit 5f526a5

Please sign in to comment.