snappymail/rainloop/v/0.0.0/app/libraries/MailSo/Log/Driver.php

348 lines
7.3 KiB
PHP
Raw Normal View History

2013-09-25 03:04:44 +08:00
<?php
2014-10-17 18:15:19 +08:00
/*
* This file is part of MailSo.
*
* (c) 2014 Usenko Timur
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
2013-09-25 03:04:44 +08:00
namespace MailSo\Log;
/**
* @category MailSo
* @package Log
*/
abstract class Driver
{
/**
* @var string
*/
protected $sDatePattern;
/**
* @var string
*/
protected $sName;
/**
* @var array
*/
protected $aPrefixes;
2014-06-07 00:11:38 +08:00
/**
* @var bool
*/
protected $bGuidPrefix;
2013-09-25 03:04:44 +08:00
/**
* @var bool
*/
protected $bTimePrefix;
/**
* @var bool
*/
protected $bTypedPrefix;
/**
* @var int
*/
private $iWriteOnTimeoutOnly;
2013-09-25 03:04:44 +08:00
/**
* @var bool
*/
private $bWriteOnErrorOnly;
/**
* @var bool
*/
private $bWriteOnPhpErrorOnly;
2013-09-25 03:04:44 +08:00
/**
* @var bool
*/
private $bFlushCache;
/**
* @var array
*/
private $aCache;
/**
* @access protected
*/
protected function __construct()
{
$this->sDatePattern = 'H:i:s';
$this->sName = 'INFO';
$this->bTimePrefix = true;
$this->bTypedPrefix = true;
2014-06-07 00:11:38 +08:00
$this->bGuidPrefix = true;
2013-09-25 03:04:44 +08:00
$this->iWriteOnTimeoutOnly = 0;
2013-09-25 03:04:44 +08:00
$this->bWriteOnErrorOnly = false;
$this->bWriteOnPhpErrorOnly = false;
2013-09-25 03:04:44 +08:00
$this->bFlushCache = false;
$this->aCache = array();
2013-09-25 03:04:44 +08:00
$this->aPrefixes = array(
\MailSo\Log\Enumerations\Type::INFO => '[DATA]',
\MailSo\Log\Enumerations\Type::SECURE => '[SECURE]',
\MailSo\Log\Enumerations\Type::NOTE => '[NOTE]',
\MailSo\Log\Enumerations\Type::TIME => '[TIME]',
2014-08-28 22:18:07 +08:00
\MailSo\Log\Enumerations\Type::TIME_DELTA => '[TIME]',
2013-09-25 03:04:44 +08:00
\MailSo\Log\Enumerations\Type::MEMORY => '[MEMORY]',
\MailSo\Log\Enumerations\Type::NOTICE => '[NOTICE]',
\MailSo\Log\Enumerations\Type::WARNING => '[WARNING]',
\MailSo\Log\Enumerations\Type::ERROR => '[ERROR]',
\MailSo\Log\Enumerations\Type::NOTICE_PHP => '[NOTICE]',
\MailSo\Log\Enumerations\Type::WARNING_PHP => '[WARNING]',
\MailSo\Log\Enumerations\Type::ERROR_PHP => '[ERROR]',
2013-09-25 03:04:44 +08:00
);
}
2014-06-07 00:11:38 +08:00
/**
* @return \MailSo\Log\Driver
*/
public function DisableGuidPrefix()
{
$this->bGuidPrefix = false;
return $this;
}
2013-09-25 03:04:44 +08:00
/**
* @return \MailSo\Log\Driver
*/
public function DisableTimePrefix()
{
$this->bTimePrefix = false;
return $this;
}
/**
* @param bool $bValue
*
2013-09-25 03:04:44 +08:00
* @return \MailSo\Log\Driver
*/
public function WriteOnErrorOnly($bValue)
{
$this->bWriteOnErrorOnly = !!$bValue;
return $this;
}
/**
* @param bool $bValue
*
* @return \MailSo\Log\Driver
*/
public function WriteOnPhpErrorOnly($bValue)
{
$this->bWriteOnPhpErrorOnly = !!$bValue;
return $this;
}
/**
* @param int $iTimeout
*
* @return \MailSo\Log\Driver
*/
public function WriteOnTimeoutOnly($iTimeout)
{
$this->iWriteOnTimeoutOnly = (int) $iTimeout;
if (0 > $this->iWriteOnTimeoutOnly)
{
$this->iWriteOnTimeoutOnly = 0;
}
return $this;
}
2013-09-25 03:04:44 +08:00
/**
* @return \MailSo\Log\Driver
*/
public function DisableTypedPrefix()
{
$this->bTypedPrefix = false;
return $this;
}
/**
* @param string|array $mDesc
2013-09-25 03:04:44 +08:00
* @return bool
*/
abstract protected function writeImplementation($mDesc);
/**
* @return bool
*/
protected function writeEmptyLineImplementation()
{
return $this->writeImplementation('');
}
/**
* @param string $sTimePrefix
* @param string $sDesc
* @param int $iType = \MailSo\Log\Enumerations\Type::INFO
2013-09-25 03:04:44 +08:00
* @param array $sName = ''
*
* @return string
*/
protected function loggerLineImplementation($sTimePrefix, $sDesc,
$iType = \MailSo\Log\Enumerations\Type::INFO, $sName = '')
2013-09-25 03:04:44 +08:00
{
2014-06-07 00:11:38 +08:00
return \ltrim(
($this->bTimePrefix ? '['.$sTimePrefix.']' : '').
($this->bGuidPrefix ? '['.\MailSo\Log\Logger::Guid().']' : '').
($this->bTypedPrefix ? ' '.$this->getTypedPrefix($iType, $sName) : '')
).$sDesc;
2013-09-25 03:04:44 +08:00
}
/**
* @return bool
*/
protected function clearImplementation()
{
return true;
}
/**
* @return string
*/
protected function getTimeWithMicroSec()
{
$aMicroTimeItems = \explode(' ', \microtime());
return \gmdate($this->sDatePattern, $aMicroTimeItems[1]).'.'.
\str_pad((int) ($aMicroTimeItems[0] * 1000), 3, '0', STR_PAD_LEFT);
}
/**
* @param int $iType
2013-09-25 03:04:44 +08:00
* @param string $sName = ''
*
* @return string
*/
protected function getTypedPrefix($iType, $sName = '')
2013-09-25 03:04:44 +08:00
{
$sName = 0 < \strlen($sName) ? $sName : $this->sName;
return isset($this->aPrefixes[$iType]) ? $sName.$this->aPrefixes[$iType].': ' : '';
2013-09-25 03:04:44 +08:00
}
/**
* @final
* @param string $sDesc
* @param int $iType = \MailSo\Log\Enumerations\Type::INFO
* @param string $sName = ''
2013-09-25 03:04:44 +08:00
*
* @return bool
*/
final public function Write($sDesc, $iType = \MailSo\Log\Enumerations\Type::INFO, $sName = '')
2013-09-25 03:04:44 +08:00
{
$bResult = true;
if (!$this->bFlushCache && ($this->bWriteOnErrorOnly || $this->bWriteOnPhpErrorOnly || 0 < $this->iWriteOnTimeoutOnly))
2013-09-25 03:04:44 +08:00
{
$bErrorPhp = false;
$bError = $this->bWriteOnErrorOnly && \in_array($iType, array(
2013-09-25 03:04:44 +08:00
\MailSo\Log\Enumerations\Type::NOTICE,
\MailSo\Log\Enumerations\Type::NOTICE_PHP,
2013-09-25 03:04:44 +08:00
\MailSo\Log\Enumerations\Type::WARNING,
\MailSo\Log\Enumerations\Type::WARNING_PHP,
\MailSo\Log\Enumerations\Type::ERROR,
\MailSo\Log\Enumerations\Type::ERROR_PHP
));
if (!$bError)
2013-09-25 03:04:44 +08:00
{
$bErrorPhp = $this->bWriteOnPhpErrorOnly && \in_array($iType, array(
\MailSo\Log\Enumerations\Type::NOTICE_PHP,
\MailSo\Log\Enumerations\Type::WARNING_PHP,
\MailSo\Log\Enumerations\Type::ERROR_PHP
));
}
if ($bError || $bErrorPhp)
{
$sFlush = '--- FlushLogCache: '.($bError ? 'WriteOnErrorOnly' : 'WriteOnPhpErrorOnly');
if (isset($this->aCache[0]) && empty($this->aCache[0]))
{
$this->aCache[0] = $sFlush;
array_unshift($this->aCache, '');
}
else
{
array_unshift($this->aCache, $sFlush);
}
2014-06-07 00:11:38 +08:00
$this->aCache[] = '--- FlushLogCache: Trigger';
$this->aCache[] = $this->loggerLineImplementation($this->getTimeWithMicroSec(), $sDesc, $iType, $sName);
2013-09-25 03:04:44 +08:00
$this->bFlushCache = true;
$bResult = $this->writeImplementation($this->aCache);
$this->aCache = array();
2013-09-25 03:04:44 +08:00
}
else if (0 < $this->iWriteOnTimeoutOnly && \time() - APP_START_TIME > $this->iWriteOnTimeoutOnly)
{
$sFlush = '--- FlushLogCache: WriteOnTimeoutOnly ['.(\time() - APP_START_TIME).'sec]';
if (isset($this->aCache[0]) && empty($this->aCache[0]))
{
$this->aCache[0] = $sFlush;
array_unshift($this->aCache, '');
}
else
{
array_unshift($this->aCache, $sFlush);
}
2013-09-25 03:04:44 +08:00
2014-06-07 00:11:38 +08:00
$this->aCache[] = '--- FlushLogCache: Trigger';
$this->aCache[] = $this->loggerLineImplementation($this->getTimeWithMicroSec(), $sDesc, $iType, $sName);
$this->bFlushCache = true;
$bResult = $this->writeImplementation($this->aCache);
$this->aCache = array();
}
else
{
$this->aCache[] = $this->loggerLineImplementation($this->getTimeWithMicroSec(), $sDesc, $iType, $sName);
}
}
else
{
$bResult = $this->writeImplementation(
$this->loggerLineImplementation($this->getTimeWithMicroSec(), $sDesc, $iType, $sName));
2013-09-25 03:04:44 +08:00
}
return $bResult;
2013-09-25 03:04:44 +08:00
}
/**
* @final
* @return bool
*/
final public function Clear()
{
return $this->clearImplementation();
}
/**
* @final
* @return void
*/
final public function WriteEmptyLine()
{
if (!$this->bFlushCache && ($this->bWriteOnErrorOnly || $this->bWriteOnPhpErrorOnly || 0 < $this->iWriteOnTimeoutOnly))
2013-09-25 03:04:44 +08:00
{
$this->aCache[] = '';
}
else
{
$this->writeEmptyLineImplementation();
}
}
}