From 07f5140771388c9e0c8a99b0dd2e5d950bdb173b Mon Sep 17 00:00:00 2001 From: Yuchen Pei Date: Thu, 14 Oct 2021 15:16:42 +1100 Subject: moving h-source subdir out. --- admin/Library/Files/Log.php | 97 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 admin/Library/Files/Log.php (limited to 'admin/Library/Files/Log.php') diff --git a/admin/Library/Files/Log.php b/admin/Library/Files/Log.php new file mode 100644 index 0000000..895c26d --- /dev/null +++ b/admin/Library/Files/Log.php @@ -0,0 +1,97 @@ +splFile = new SplFileObject($path,'a+'); + //change the permission of the file + @chmod($path,self::$logPermission); + } + + // The singleton method + // $instanceName: name of the key of self::$instance. It is also the name of the log file to open + public static function getInstance($instanceName) + { + if (!isset(self::$instance[$instanceName])) { + $className = __CLASS__; + self::$instance[$instanceName] = new $className($instanceName); + } + + return self::$instance[$instanceName]; + } + + //write the string $string at the end of the file + public function writeString($string,$format = 'Y-m-d H:i:s') + { + $date = date($format); + $this->splFile->fwrite("[$date]\t".$string."\n"); + } + + //get the date string of the line $line + public function getDateString($line) + { + if (preg_match('/^[\[]{1}([a-zA-Z0-9:\-\s])*[\]]{1}/',$line,$match)) + { + $match[0] = str_replace('[',null,$match[0]); + $match[0] = str_replace(']',null,$match[0]); + return $match[0]; + } + else + { + return false; + } + } + + //delete all the lines older than a number of days equal to $days + public function clearBefore($days = 30) + { + $tempArray = array(); + $newTime = time() - (int)$days * 24 * 3600; + foreach ($this->splFile as $line) + { + $lineTime = strtotime($this->getDateString($line)); + if ($lineTime !== false and $lineTime > $newTime) + { + $tempArray[] = $line; + } + } + $this->splFile->ftruncate(0); + foreach ($tempArray as $row) + { + $this->splFile->fwrite($row); + } + } + + // Prevent users to clone the instance + public function __clone() + { + throw new Exception('error in '. __METHOD__.': clone is not allowed'); + } + +} \ No newline at end of file -- cgit v1.2.3