Drop \MailSo\Base\Loader and just use $_SERVER['REQUEST_TIME_FLOAT'] in \MailSo\Log\Logger

This commit is contained in:
djmaze 2021-08-31 15:14:01 +02:00
parent 1b497ae239
commit 2518881d10
12 changed files with 15 additions and 165 deletions

View file

@ -28,8 +28,6 @@ if (!\defined('RAINLOOP_APP_LIBRARIES_PATH'))
if (\class_exists('RainLoop\Api'))
{
\MailSo\Base\Loader::Init();
if (!empty($_ENV['RAINLOOP_INCLUDE_AS_API']))
{
if (!\defined('APP_API_STARTED'))

View file

@ -1,121 +0,0 @@
<?php
/*
* 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.
*/
namespace MailSo\Base;
/**
* @category MailSo
* @package Base
*/
class Loader
{
/**
* @var bool
*/
public static $StoreStatistic = true;
/**
* @var int
*/
public static $InitTime = 0;
/**
* @var float
*/
public static $InitMicrotimeTime = 0;
/**
* @var array
*/
private static $aIncStatistic = array();
/**
* @var array
*/
private static $aSetStatistic = array();
/**
* @staticvar bool $bIsInited
*/
public static function Init() : void
{
static $bIsInited = false;
if (!$bIsInited)
{
$bIsInited = true;
self::$InitTime = \time();
self::$InitMicrotimeTime = \microtime(true);
self::SetStatistic('Inited', self::$InitMicrotimeTime);
}
}
public static function IncStatistic(string $sName, int $iIncSize = 1) : void
{
if (self::$StoreStatistic)
{
self::$aIncStatistic[$sName] = isset(self::$aIncStatistic[$sName])
? self::$aIncStatistic[$sName] + $iIncSize : $iIncSize;
}
}
/**
* @param mixed $mValue
*/
public static function SetStatistic(string $sName, $mValue) : void
{
if (self::$StoreStatistic)
{
self::$aSetStatistic[$sName] = $mValue;
}
}
/**
* @return mixed
*/
public static function GetStatistic(string $sName)
{
return self::$StoreStatistic && isset(self::$aSetStatistic[$sName]) ? self::$aSetStatistic[$sName] : null;
}
public static function Statistic() : ?array
{
$aResult = null;
if (self::$StoreStatistic)
{
$aResult = array(
'php' => array(
'phpversion' => PHP_VERSION,
'ssl' => (int) \function_exists('openssl_open'),
'iconv' => (int) \function_exists('iconv')
));
if (\MailSo\Base\Utils::FunctionExistsAndEnabled('memory_get_usage') &&
\MailSo\Base\Utils::FunctionExistsAndEnabled('memory_get_peak_usage'))
{
$aResult['php']['memory_get_usage'] =
Utils::FormatFileSize(\memory_get_usage(true), 2);
$aResult['php']['memory_get_peak_usage'] =
Utils::FormatFileSize(\memory_get_peak_usage(true), 2);
}
$iTimeDelta = \microtime(true) - self::GetStatistic('Inited');
self::SetStatistic('TimeDelta', $iTimeDelta);
$aResult['statistic'] = self::$aSetStatistic;
$aResult['counts'] = self::$aIncStatistic;
$aResult['time'] = $iTimeDelta;
}
return $aResult;
}
}

View file

@ -38,7 +38,6 @@ abstract class ResourceRegistry
{
if (\is_resource(static::$Resources[$sKey]))
{
\MailSo\Base\Loader::IncStatistic('CloseMemoryResource');
\fclose(static::$Resources[$sKey]);
}
static::$Resources[$sKey] = null;
@ -60,7 +59,6 @@ abstract class ResourceRegistry
$oResult = \fopen('php://temp/maxmemory:'.($iMemoryMaxInMb * 1024 * 1024), 'r+b');
if (\is_resource($oResult))
{
\MailSo\Base\Loader::IncStatistic('CreateMemoryResource');
static::$Resources[(string) $oResult] = $oResult;
return $oResult;
}
@ -96,7 +94,6 @@ abstract class ResourceRegistry
\fclose(static::$Resources[$sKey]);
static::$Resources[$sKey] = null;
unset(static::$Resources[$sKey]);
\MailSo\Base\Loader::IncStatistic('CloseMemoryResource');
}
if (\is_resource($rResource))

View file

@ -202,8 +202,6 @@ class Binary
self::$aStreams[$sHashName] =
array($rStream, $sUtilsDecodeOrEncodeFunctionName, $sFromEncoding, $sToEncoding);
\MailSo\Base\Loader::IncStatistic('CreateStream/Binary');
return \fopen(self::STREAM_NAME.'://'.$sHashName, 'rb');
}

View file

@ -59,8 +59,6 @@ class Literal
self::$aStreams[$sHashName] = array($rStream, $iLiteralLen);
\MailSo\Base\Loader::IncStatistic('CreateStream/Literal');
return fopen(self::STREAM_NAME.'://'.$sHashName, 'rb');
}

View file

@ -70,8 +70,6 @@ class SubStreams
\MailSo\Base\ResourceRegistry::CreateMemoryResourceFromString($mItem);
}, $aSubStreams);
\MailSo\Base\Loader::IncStatistic('CreateStream/SubStreams');
return \fopen(self::STREAM_NAME.'://'.$sHashName, 'rb');
}

View file

@ -75,8 +75,6 @@ class TempFile
self::$aStreams[$sHashName] = $this->rSream;
$bResult = true;
\MailSo\Base\Loader::IncStatistic('CreateStream/TempFile');
}
}

View file

@ -52,8 +52,6 @@ class Test
self::$aStreams[$sHashName] = $rConnect;
\MailSo\Base\Loader::IncStatistic('CreateStream/Test');
return fopen(self::STREAM_NAME.'://'.$sHashName, 'r+b');
}

View file

@ -832,7 +832,7 @@ END;
public static function ResetTimeLimit(int $iTimeToReset = 15, int $iTimeToAdd = 120) : bool
{
$iTime = \time();
if ($iTime < Loader::$InitTime + 5)
if ($iTime < $_SERVER['REQUEST_TIME_FLOAT'] + 5)
{
// do nothing first 5s
return true;

View file

@ -1216,7 +1216,6 @@ class ImapClient extends \MailSo\Net\NetClient
if (false !== $sLiteral)
{
$iLiteralSize = \strlen($sLiteral);
\MailSo\Base\Loader::IncStatistic('NetRead', $iLiteralSize);
if ($iLiteralLen !== $iLiteralSize)
{
$this->writeLog('Literal stream read warning "read '.$iLiteralSize.' of '.
@ -1685,8 +1684,6 @@ class ImapClient extends \MailSo\Net\NetClient
\MailSo\Log\Enumerations\Type::WARNING);
}
\MailSo\Base\Loader::IncStatistic('NetRead', $iLiteralLen);
$this->bRunningCallback = false;
return true;

View file

@ -59,7 +59,7 @@ class Logger extends \MailSo\Base\Collection
/**
* @param mixed $mData
*/
public static function SystemLog($mData, int $iType = \MailSo\Log\Enumerations\Type::INFO)
public static function SystemLog($mData, int $iType = Enumerations\Type::INFO)
{
if (\MailSo\Config::$SystemLogger instanceof Logger)
{
@ -127,14 +127,14 @@ class Logger extends \MailSo\Base\Collection
public function __phpErrorHandler(int $iErrNo, string $sErrStr, string $sErrFile, int $iErrLine) : bool
{
if (\error_reporting() & $iErrNo) {
$iType = \MailSo\Log\Enumerations\Type::NOTICE_PHP;
$iType = Enumerations\Type::NOTICE_PHP;
switch ($iErrNo)
{
case E_USER_ERROR:
$iType = \MailSo\Log\Enumerations\Type::ERROR_PHP;
$iType = Enumerations\Type::ERROR_PHP;
break;
case E_USER_WARNING:
$iType = \MailSo\Log\Enumerations\Type::WARNING_PHP;
$iType = Enumerations\Type::WARNING_PHP;
break;
}
@ -147,18 +147,11 @@ class Logger extends \MailSo\Base\Collection
public function __loggerShutDown() : void
{
if ($this->bUsed && $aStatistic = \MailSo\Base\Loader::Statistic())
if ($this->bUsed)
{
if (isset($aStatistic['php']['memory_get_peak_usage']))
{
$this->Write('Memory peak usage: '.$aStatistic['php']['memory_get_peak_usage'],
\MailSo\Log\Enumerations\Type::MEMORY);
}
if (isset($aStatistic['time']))
{
$this->Write('Time delta: '.$aStatistic['time'], \MailSo\Log\Enumerations\Type::TIME_DELTA);
}
$this->Write('Memory peak usage: '.\MailSo\Utils::FormatFileSize(\memory_get_peak_usage(true), 2),
Enumerations\Type::MEMORY);
$this->Write('Time delta: '.(\microtime(true) - $_SERVER['REQUEST_TIME_FLOAT']), Enumerations\Type::TIME_DELTA);
}
}
@ -174,7 +167,7 @@ class Logger extends \MailSo\Base\Collection
return (bool) $iResult;
}
public function Write(string $sDesc, int $iType = \MailSo\Log\Enumerations\Type::INFO,
public function Write(string $sDesc, int $iType = Enumerations\Type::INFO,
string $sName = '', bool $bSearchSecretWords = true, bool $bDiplayCrLf = false) : bool
{
if (isset($this->aForbiddenTypes[$iType]) && true === $this->aForbiddenTypes[$iType])
@ -204,13 +197,13 @@ class Logger extends \MailSo\Base\Collection
/**
* @param mixed $oValue
*/
public function WriteDump($oValue, int $iType = \MailSo\Log\Enumerations\Type::INFO, string $sName = '',
public function WriteDump($oValue, int $iType = Enumerations\Type::INFO, string $sName = '',
bool $bSearchSecretWords = false, bool $bDiplayCrLf = false) : bool
{
return $this->Write(\print_r($oValue, true), $iType, $sName, $bSearchSecretWords, $bDiplayCrLf);
}
public function WriteException(\Throwable $oException, int $iType = \MailSo\Log\Enumerations\Type::NOTICE, string $sName = '',
public function WriteException(\Throwable $oException, int $iType = Enumerations\Type::NOTICE, string $sName = '',
bool $bSearchSecretWords = true, bool $bDiplayCrLf = false) : bool
{
if ($oException instanceof \Throwable)
@ -228,7 +221,7 @@ class Logger extends \MailSo\Base\Collection
return false;
}
public function WriteExceptionShort(\Throwable $oException, int $iType = \MailSo\Log\Enumerations\Type::NOTICE, string $sName = '',
public function WriteExceptionShort(\Throwable $oException, int $iType = Enumerations\Type::NOTICE, string $sName = '',
bool $bSearchSecretWords = true, bool $bDiplayCrLf = false) : bool
{
if ($oException instanceof \Throwable)
@ -252,12 +245,12 @@ class Logger extends \MailSo\Base\Collection
public function WriteMixed($mData, int $iType = null, string $sName = '',
bool $bSearchSecretWords = true, bool $bDiplayCrLf = false) : bool
{
$iType = null === $iType ? \MailSo\Log\Enumerations\Type::INFO : $iType;
$iType = null === $iType ? Enumerations\Type::INFO : $iType;
if (\is_array($mData) || \is_object($mData))
{
if ($mData instanceof \Throwable)
{
$iType = null === $iType ? \MailSo\Log\Enumerations\Type::NOTICE : $iType;
$iType = null === $iType ? Enumerations\Type::NOTICE : $iType;
return $this->WriteException($mData, $iType, $sName, $bSearchSecretWords, $bDiplayCrLf);
}
else

View file

@ -402,8 +402,6 @@ abstract class NetClient
}
else
{
\MailSo\Base\Loader::IncStatistic('NetWrite', $mResult);
if ($bWriteToLog)
{
$this->writeLogWithCrlf('> '.($bFake ? $sFakeRaw : $sRaw), //.' ['.$iWriteSize.']',
@ -484,8 +482,6 @@ abstract class NetClient
$this->writeLog('Received '.$iReadedLen.'/'.$iReadLen.' bytes.',
\MailSo\Log\Enumerations\Type::INFO);
}
\MailSo\Base\Loader::IncStatistic('NetRead', $iReadedLen);
}
}