BxDolLogsStorageFolder.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php defined('BX_DOL') or defined('BX_DOL_INSTALL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup UnaCore UNA Core
  7. * @{
  8. */
  9. class BxDolLogsStorageFolder extends BxDolFactory implements iBxDolSingleton
  10. {
  11. protected function __construct()
  12. {
  13. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  14. trigger_error ('Multiple instances are not allowed for the class: ' . get_class($this), E_USER_ERROR);
  15. parent::__construct();
  16. }
  17. /**
  18. * Prevent cloning the instance
  19. */
  20. public function __clone()
  21. {
  22. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  23. trigger_error('Clone is not allowed for the class: ' . get_class($this), E_USER_ERROR);
  24. }
  25. /**
  26. * Get singleton instance of the class
  27. */
  28. public static function getInstance()
  29. {
  30. if(!isset($GLOBALS['bxDolClasses'][__CLASS__]))
  31. $GLOBALS['bxDolClasses'][__CLASS__] = new BxDolLogsStorageFolder();
  32. return $GLOBALS['bxDolClasses'][__CLASS__];
  33. }
  34. /**
  35. * Write to log
  36. * @param $oObject logs object
  37. * @param $mixed string or array to log
  38. * @return true on success or false on error
  39. */
  40. public function add($oObject, $mixed, $iLevel)
  41. {
  42. if (!$mixed)
  43. return true;
  44. $sFile = BX_DIRECTORY_PATH_LOGS . $oObject->getObjectName() . '.log';
  45. $s = $this->formatLogString($oObject, $mixed, false, $iLevel);
  46. $bNewFile = !file_exists($sFile);
  47. $bRet = file_put_contents($sFile, $s, FILE_APPEND) ? true : false;
  48. if ($bNewFile && $bRet)
  49. chmod($sFile, BX_DOL_FILE_RIGHTS);
  50. return $bRet;
  51. }
  52. protected function formatLogString($oObject, $mixed, $bIncludeObjectName = false, $iLevel = BX_LOG_DEBUG)
  53. {
  54. $aLevelNames = BxDolLogs::getLevel2NameMap();
  55. $s = date('M d H:i:s');
  56. $s .= " [" . $aLevelNames[$iLevel] . "]";
  57. if ($bIncludeObjectName)
  58. $s .= ' ' . $oObject->getObjectName();
  59. if (!function_exists('bx_get_logged_profile_id'))
  60. require_once(BX_DIRECTORY_PATH_INC . "profiles.inc.php");
  61. $s .= ' [' . (int)bx_get_logged_profile_id() . '] ';
  62. if (!empty($_SERVER['REQUEST_URI']))
  63. $s .= $_SERVER['REQUEST_URI'];
  64. elseif (defined('BX_DOL_CRON_EXECUTE'))
  65. $s .= 'CRON';
  66. else
  67. $s .= 'EMPTY';
  68. $s .= ' ' . trim(is_array($mixed) ? print_r($mixed, true) : $mixed);
  69. $s .= "\n";
  70. return $s;
  71. }
  72. /**
  73. * Get logs
  74. * @param $oObject logs object
  75. * @param $iLines number of lines from the tail
  76. * @param $sFilter filter lines which include this phrase
  77. * @return array of strings
  78. */
  79. public function get($oObject, $iLines, $sFilter = false)
  80. {
  81. $sFile = BX_DIRECTORY_PATH_LOGS . $oObject->getObjectName() . '.log';
  82. $iSize = filesize($sFile);
  83. if (!($fd = fopen($sFile, 'r+')))
  84. return false;
  85. $iPos = $iSize;
  86. $n = 0;
  87. while ($n < $iLines+1 && $iPos > 0) {
  88. if (-1 == fseek($fd, $iPos))
  89. break;
  90. if (false === ($s = fread($fd, 1)))
  91. break;
  92. if ($s === "\n")
  93. ++$n;
  94. $iPos--;
  95. }
  96. $a = array();
  97. for ($i = 0; $i < $iLines; $i++) {
  98. if (false === ($s = fgets($fd)))
  99. break;
  100. if (!$sFilter || false !== stripos($s, $sFilter))
  101. array_push($a, $s);
  102. }
  103. fclose($fd);
  104. return $a;
  105. }
  106. /**
  107. * Check if filtering supported in `get` method
  108. */
  109. public function isFilterAvail()
  110. {
  111. return true;
  112. }
  113. /**
  114. * Check if `get` method available
  115. */
  116. public function isGetAvail()
  117. {
  118. return true;
  119. }
  120. }
  121. /** @} */