Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Factory class to ActiveSync package.
- Loading branch information
Showing
1 changed file
with
113 additions
and
0 deletions.
There are no files selected for viewing
113 changes: 113 additions & 0 deletions
113
framework/ActiveSync/lib/Horde/ActiveSync/Log/Factory.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
|
||
} |