Server IP : 80.87.202.40 / Your IP : 216.73.216.169 Web Server : Apache System : Linux rospirotorg.ru 5.14.0-539.el9.x86_64 #1 SMP PREEMPT_DYNAMIC Thu Dec 5 22:26:13 UTC 2024 x86_64 User : bitrix ( 600) PHP Version : 8.2.27 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : ON Directory : /home/bitrix/ext_www/rospirotorg.ru/bitrix/modules/sproduction.datasync/lib/ |
Upload File : |
<?php /** * Check module state * * @mail support@s-production.online * @link s-production.online */ namespace SProduction\Datasync; use Bitrix\Main, Bitrix\Main\DB\Exception, Bitrix\Main\Localization\Loc, Bitrix\Main\Config\Option; use SProdDatasync; class FileLog { const MAX_SIZE = 104857600; public const LOG_PATH = '/upload/sprod_dsync_log.txt'; public static $LOG_LABEL; public static function getPath() { return $_SERVER['DOCUMENT_ROOT'] . self::LOG_PATH; } public static function put($string, $trunc = true) { self::setLabel(); $log = Option::get(SProdDatasync::MODULE_ID, "filelog"); if ($log == 'Y') { // Shorten long strings if ($trunc) { $string = self::truncLongStrings($string); } // Get memory // $memory = self::getLogMemory(); // $memory_diff = self::getLogMemoryDiff(); // Add info $info = date('d.m.Y H:i:s'); $info .= ' label ' . self::getLabel(); // $info .= ' memory ' . $memory; // $info .= ' memory_diff ' . $memory_diff; $string .= "\n---\n" . $info . "\n\n"; // Write record to log $file_path = self::getPath(); $result = file_put_contents($file_path, $string, FILE_APPEND | LOCK_EX); // Check size if ($result) { $max_size = self::getMaxSize(); if ($max_size > 0) { $cur_size = self::getSize(); if ($cur_size > $max_size) { $f_res = fopen($file_path, 'r'); fseek($f_res, -1 * $max_size, SEEK_END); $file_content = fread($f_res, $max_size); fclose($f_res); file_put_contents($file_path, $file_content); unset($file_content, $f_res); } } } } } public static function setLabel($label = '') { if ($label) { self::$LOG_LABEL = $label; } elseif ( ! self::$LOG_LABEL) { self::$LOG_LABEL = str_replace('.', '', strval(microtime(true))) . mt_rand(1000, 9999); } } public static function getLabel() { return self::$LOG_LABEL; } public static function truncLongStrings($string) { // Find a sequence of characters without spaces larger than 500 characters $pieces = explode(' ', $string); foreach ($pieces as $i => $piece) { $piece_len = strlen($piece); if ($piece_len > 1000) { // Shorten a long line $pieces[$i] = substr($piece, 0, 100) . ' ... (file size: ' . $piece_len . ') ... ' . substr($piece, - 100); } } $result = implode(' ', $pieces); return $result; } public static function getMemoryDiff() { if ( ! SProdDatasync::$MEMORY) { SProdDatasync::$MEMORY = 0; } // Get difference $memory = memory_get_usage(); $memory_diff = ($memory - SProdDatasync::$MEMORY); SProdDatasync::$MEMORY = $memory; // Format difference $i = 0; while (floor($memory_diff / 1024) > 0) { $i ++; $memory_diff /= 1024; } $name = array('b', 'Kb', 'Mb'); $memory_diff_format = round($memory_diff, 2) . ' ' . $name[$i]; return $memory_diff_format; } public static function getMemory() { $memory = memory_get_usage(); // Format difference $i = 0; while (floor($memory / 1024) > 0) { $i ++; $memory /= 1024; } $name = array('b', 'Kb', 'Mb'); $memory_format = round($memory, 2) . ' ' . $name[$i]; return $memory_format; } /** * Get log filesize */ public static function getSize() { $file_path = self::getPath(); return is_file($file_path) ? filesize($file_path) : 0; } /** * Get log max size (in bytes) */ public static function getMaxSize() { $opt_size = Settings::get('file_log_max_size'); return $opt_size ? : self::MAX_SIZE; } }