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

https://gitlab.com/wuhang2003/rainloop-webmail · PHP · 403 lines · 223 code · 47 blank · 133 comment · 23 complexity · 70eac38bba514689fbf7df0170145c9c MD5 · raw file

  1. <?php
  2. /*
  3. * This file is part of MailSo.
  4. *
  5. * (c) 2014 Usenko Timur
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace MailSo\Log;
  11. /**
  12. * @category MailSo
  13. * @package Log
  14. */
  15. abstract class Driver
  16. {
  17. /**
  18. * @var string
  19. */
  20. protected $sDatePattern;
  21. /**
  22. * @var string
  23. */
  24. protected $sName;
  25. /**
  26. * @var array
  27. */
  28. protected $aPrefixes;
  29. /**
  30. * @var bool
  31. */
  32. protected $bGuidPrefix;
  33. /**
  34. * @var bool
  35. */
  36. protected $bTimePrefix;
  37. /**
  38. * @var bool
  39. */
  40. protected $bTypedPrefix;
  41. /**
  42. * @var string
  43. */
  44. protected $sNewLine;
  45. /**
  46. * @var int
  47. */
  48. private $iWriteOnTimeoutOnly;
  49. /**
  50. * @var bool
  51. */
  52. private $bWriteOnErrorOnly;
  53. /**
  54. * @var bool
  55. */
  56. private $bWriteOnPhpErrorOnly;
  57. /**
  58. * @var bool
  59. */
  60. private $bFlushCache;
  61. /**
  62. * @var array
  63. */
  64. private $aCache;
  65. /**
  66. * @access protected
  67. */
  68. protected function __construct()
  69. {
  70. $this->sDatePattern = 'H:i:s';
  71. $this->sName = 'INFO';
  72. $this->sNewLine = "\r\n";
  73. $this->bTimePrefix = true;
  74. $this->bTypedPrefix = true;
  75. $this->bGuidPrefix = true;
  76. $this->iTimeOffset = 0;
  77. $this->iWriteOnTimeoutOnly = 0;
  78. $this->bWriteOnErrorOnly = false;
  79. $this->bWriteOnPhpErrorOnly = false;
  80. $this->bFlushCache = false;
  81. $this->aCache = array();
  82. $this->aPrefixes = array(
  83. \MailSo\Log\Enumerations\Type::INFO => '[DATA]',
  84. \MailSo\Log\Enumerations\Type::SECURE => '[SECURE]',
  85. \MailSo\Log\Enumerations\Type::NOTE => '[NOTE]',
  86. \MailSo\Log\Enumerations\Type::TIME => '[TIME]',
  87. \MailSo\Log\Enumerations\Type::TIME_DELTA => '[TIME]',
  88. \MailSo\Log\Enumerations\Type::MEMORY => '[MEMORY]',
  89. \MailSo\Log\Enumerations\Type::NOTICE => '[NOTICE]',
  90. \MailSo\Log\Enumerations\Type::WARNING => '[WARNING]',
  91. \MailSo\Log\Enumerations\Type::ERROR => '[ERROR]',
  92. \MailSo\Log\Enumerations\Type::NOTICE_PHP => '[NOTICE]',
  93. \MailSo\Log\Enumerations\Type::WARNING_PHP => '[WARNING]',
  94. \MailSo\Log\Enumerations\Type::ERROR_PHP => '[ERROR]',
  95. );
  96. }
  97. /**
  98. * @param int $iTimeOffset
  99. *
  100. * @return \MailSo\Log\Driver
  101. */
  102. public function SetTimeOffset($iTimeOffset)
  103. {
  104. $this->iTimeOffset = $iTimeOffset;
  105. return $this;
  106. }
  107. /**
  108. * @return \MailSo\Log\Driver
  109. */
  110. public function DisableGuidPrefix()
  111. {
  112. $this->bGuidPrefix = false;
  113. return $this;
  114. }
  115. /**
  116. * @return \MailSo\Log\Driver
  117. */
  118. public function DisableTimePrefix()
  119. {
  120. $this->bTimePrefix = false;
  121. return $this;
  122. }
  123. /**
  124. * @param bool $bValue
  125. *
  126. * @return \MailSo\Log\Driver
  127. */
  128. public function WriteOnErrorOnly($bValue)
  129. {
  130. $this->bWriteOnErrorOnly = !!$bValue;
  131. return $this;
  132. }
  133. /**
  134. * @param bool $bValue
  135. *
  136. * @return \MailSo\Log\Driver
  137. */
  138. public function WriteOnPhpErrorOnly($bValue)
  139. {
  140. $this->bWriteOnPhpErrorOnly = !!$bValue;
  141. return $this;
  142. }
  143. /**
  144. * @param int $iTimeout
  145. *
  146. * @return \MailSo\Log\Driver
  147. */
  148. public function WriteOnTimeoutOnly($iTimeout)
  149. {
  150. $this->iWriteOnTimeoutOnly = (int) $iTimeout;
  151. if (0 > $this->iWriteOnTimeoutOnly)
  152. {
  153. $this->iWriteOnTimeoutOnly = 0;
  154. }
  155. return $this;
  156. }
  157. /**
  158. * @return \MailSo\Log\Driver
  159. */
  160. public function DisableTypedPrefix()
  161. {
  162. $this->bTypedPrefix = false;
  163. return $this;
  164. }
  165. /**
  166. * @param string|array $mDesc
  167. * @return bool
  168. */
  169. abstract protected function writeImplementation($mDesc);
  170. /**
  171. * @return bool
  172. */
  173. protected function writeEmptyLineImplementation()
  174. {
  175. return $this->writeImplementation('');
  176. }
  177. /**
  178. * @param string $sTimePrefix
  179. * @param string $sDesc
  180. * @param int $iType = \MailSo\Log\Enumerations\Type::INFO
  181. * @param array $sName = ''
  182. *
  183. * @return string
  184. */
  185. protected function loggerLineImplementation($sTimePrefix, $sDesc,
  186. $iType = \MailSo\Log\Enumerations\Type::INFO, $sName = '')
  187. {
  188. return \ltrim(
  189. ($this->bTimePrefix ? '['.$sTimePrefix.']' : '').
  190. ($this->bGuidPrefix ? '['.\MailSo\Log\Logger::Guid().']' : '').
  191. ($this->bTypedPrefix ? ' '.$this->getTypedPrefix($iType, $sName) : '')
  192. ).$sDesc;
  193. }
  194. /**
  195. * @return bool
  196. */
  197. protected function clearImplementation()
  198. {
  199. return true;
  200. }
  201. /**
  202. * @return string
  203. */
  204. protected function getTimeWithMicroSec()
  205. {
  206. $aMicroTimeItems = \explode(' ', \microtime());
  207. return \MailSo\Log\Logger::DateHelper($this->sDatePattern, $this->iTimeOffset, $aMicroTimeItems[1]).'.'.
  208. \str_pad((int) ($aMicroTimeItems[0] * 1000), 3, '0', STR_PAD_LEFT);
  209. }
  210. /**
  211. * @param int $iType
  212. * @param string $sName = ''
  213. *
  214. * @return string
  215. */
  216. protected function getTypedPrefix($iType, $sName = '')
  217. {
  218. $sName = 0 < \strlen($sName) ? $sName : $this->sName;
  219. return isset($this->aPrefixes[$iType]) ? $sName.$this->aPrefixes[$iType].': ' : '';
  220. }
  221. /**
  222. * @param string|array $mDesc
  223. * @param bool $bDiplayCrLf = false
  224. *
  225. * @return string
  226. */
  227. protected function localWriteImplementation($mDesc, $bDiplayCrLf = false)
  228. {
  229. if ($bDiplayCrLf)
  230. {
  231. if (\is_array($mDesc))
  232. {
  233. foreach ($mDesc as &$sLine)
  234. {
  235. $sLine = \strtr($sLine, array("\r" => '\r', "\n" => '\n'.$this->sNewLine));
  236. $sLine = \rtrim($sLine);
  237. }
  238. }
  239. else
  240. {
  241. $mDesc = \strtr($mDesc, array("\r" => '\r', "\n" => '\n'.$this->sNewLine));
  242. $mDesc = \rtrim($mDesc);
  243. }
  244. }
  245. return $this->writeImplementation($mDesc);
  246. }
  247. /**
  248. * @final
  249. * @param string $sDesc
  250. * @param int $iType = \MailSo\Log\Enumerations\Type::INFO
  251. * @param string $sName = ''
  252. * @param bool $bDiplayCrLf = false
  253. *
  254. * @return bool
  255. */
  256. final public function Write($sDesc, $iType = \MailSo\Log\Enumerations\Type::INFO, $sName = '', $bDiplayCrLf = false)
  257. {
  258. $bResult = true;
  259. if (!$this->bFlushCache && ($this->bWriteOnErrorOnly || $this->bWriteOnPhpErrorOnly || 0 < $this->iWriteOnTimeoutOnly))
  260. {
  261. $bErrorPhp = false;
  262. $bError = $this->bWriteOnErrorOnly && \in_array($iType, array(
  263. \MailSo\Log\Enumerations\Type::NOTICE,
  264. \MailSo\Log\Enumerations\Type::NOTICE_PHP,
  265. \MailSo\Log\Enumerations\Type::WARNING,
  266. \MailSo\Log\Enumerations\Type::WARNING_PHP,
  267. \MailSo\Log\Enumerations\Type::ERROR,
  268. \MailSo\Log\Enumerations\Type::ERROR_PHP
  269. ));
  270. if (!$bError)
  271. {
  272. $bErrorPhp = $this->bWriteOnPhpErrorOnly && \in_array($iType, array(
  273. \MailSo\Log\Enumerations\Type::NOTICE_PHP,
  274. \MailSo\Log\Enumerations\Type::WARNING_PHP,
  275. \MailSo\Log\Enumerations\Type::ERROR_PHP
  276. ));
  277. }
  278. if ($bError || $bErrorPhp)
  279. {
  280. $sFlush = '--- FlushLogCache: '.($bError ? 'WriteOnErrorOnly' : 'WriteOnPhpErrorOnly');
  281. if (isset($this->aCache[0]) && empty($this->aCache[0]))
  282. {
  283. $this->aCache[0] = $sFlush;
  284. \array_unshift($this->aCache, '');
  285. }
  286. else
  287. {
  288. \array_unshift($this->aCache, $sFlush);
  289. }
  290. $this->aCache[] = '--- FlushLogCache: Trigger';
  291. $this->aCache[] = $this->loggerLineImplementation($this->getTimeWithMicroSec(), $sDesc, $iType, $sName);
  292. $this->bFlushCache = true;
  293. $bResult = $this->localWriteImplementation($this->aCache, $bDiplayCrLf);
  294. $this->aCache = array();
  295. }
  296. else if (0 < $this->iWriteOnTimeoutOnly && \time() - APP_START_TIME > $this->iWriteOnTimeoutOnly)
  297. {
  298. $sFlush = '--- FlushLogCache: WriteOnTimeoutOnly ['.(\time() - APP_START_TIME).'sec]';
  299. if (isset($this->aCache[0]) && empty($this->aCache[0]))
  300. {
  301. $this->aCache[0] = $sFlush;
  302. \array_unshift($this->aCache, '');
  303. }
  304. else
  305. {
  306. \array_unshift($this->aCache, $sFlush);
  307. }
  308. $this->aCache[] = '--- FlushLogCache: Trigger';
  309. $this->aCache[] = $this->loggerLineImplementation($this->getTimeWithMicroSec(), $sDesc, $iType, $sName);
  310. $this->bFlushCache = true;
  311. $bResult = $this->localWriteImplementation($this->aCache, $bDiplayCrLf);
  312. $this->aCache = array();
  313. }
  314. else
  315. {
  316. $this->aCache[] = $this->loggerLineImplementation($this->getTimeWithMicroSec(), $sDesc, $iType, $sName);
  317. }
  318. }
  319. else
  320. {
  321. $bResult = $this->localWriteImplementation(
  322. $this->loggerLineImplementation($this->getTimeWithMicroSec(), $sDesc, $iType, $sName), $bDiplayCrLf);
  323. }
  324. return $bResult;
  325. }
  326. /**
  327. * @return string
  328. */
  329. public function GetNewLine()
  330. {
  331. return $this->sNewLine;
  332. }
  333. /**
  334. * @final
  335. * @return bool
  336. */
  337. final public function Clear()
  338. {
  339. return $this->clearImplementation();
  340. }
  341. /**
  342. * @final
  343. * @return void
  344. */
  345. final public function WriteEmptyLine()
  346. {
  347. if (!$this->bFlushCache && ($this->bWriteOnErrorOnly || $this->bWriteOnPhpErrorOnly || 0 < $this->iWriteOnTimeoutOnly))
  348. {
  349. $this->aCache[] = '';
  350. }
  351. else
  352. {
  353. $this->writeEmptyLineImplementation();
  354. }
  355. }
  356. }