PocketMine-MP 5.15.1 git-fb9a74e8799c71ed8292cfa53abe7a4c9204629d
MainLoggerThread.php
1<?php
2
3/*
4 *
5 * ____ _ _ __ __ _ __ __ ____
6 * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7 * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8 * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9 * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10 *
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * @author PocketMine Team
17 * @link http://www.pocketmine.net/
18 *
19 *
20 */
21
22declare(strict_types=1);
23
24namespace pocketmine\utils;
25
26use pmmp\thread\Thread;
27use pmmp\thread\ThreadSafeArray;
28use function clearstatcache;
29use function date;
30use function fclose;
31use function file_exists;
32use function fopen;
33use function fstat;
34use function fwrite;
35use function is_dir;
36use function is_file;
37use function is_resource;
38use function mkdir;
39use function pathinfo;
40use function rename;
41use function strlen;
42use function touch;
43use const PATHINFO_EXTENSION;
44use const PATHINFO_FILENAME;
45
46final class MainLoggerThread extends Thread{
47
49 private ThreadSafeArray $buffer;
50 private bool $syncFlush = false;
51 private bool $shutdown = false;
52
53 public function __construct(
54 private string $logFile,
55 private ?string $archiveDir,
56 private readonly int $maxFileSize = 32 * 1024 * 1024 //32 MB
57 ){
58 $this->buffer = new ThreadSafeArray();
59 touch($this->logFile);
60 if($this->archiveDir !== null && !@mkdir($this->archiveDir) && !is_dir($this->archiveDir)){
61 throw new \RuntimeException("Unable to create archive directory: " . (
62 is_file($this->archiveDir) ? "it already exists and is not a directory" : "permission denied"));
63 }
64 }
65
66 public function write(string $line) : void{
67 $this->synchronized(function() use ($line) : void{
68 $this->buffer[] = $line;
69 $this->notify();
70 });
71 }
72
73 public function syncFlushBuffer() : void{
74 $this->synchronized(function() : void{
75 $this->syncFlush = true;
76 $this->notify(); //write immediately
77 });
78 $this->synchronized(function() : void{
79 while($this->syncFlush){
80 $this->wait(); //block until it's all been written to disk
81 }
82 });
83 }
84
85 public function shutdown() : void{
86 $this->synchronized(function() : void{
87 $this->shutdown = true;
88 $this->notify();
89 });
90 $this->join();
91 }
92
94 private function openLogFile(string $file, int &$size){
95 $logResource = fopen($file, "ab");
96 if(!is_resource($logResource)){
97 throw new \RuntimeException("Couldn't open log file");
98 }
99 $stat = fstat($logResource);
100 if($stat === false){
101 throw new AssumptionFailedError("fstat() should not fail here");
102 }
103 $size = $stat['size'];
104 return $logResource;
105 }
106
111 private function archiveLogFile($logResource, int &$size, string $archiveDir){
112 fclose($logResource);
113
114 clearstatcache();
115
116 $i = 0;
117 $date = date("Y-m-d\TH.i.s");
118 $baseName = pathinfo($this->logFile, PATHINFO_FILENAME);
119 $extension = pathinfo($this->logFile, PATHINFO_EXTENSION);
120 do{
121 //this shouldn't be necessary, but in case the user messes with the system time for some reason ...
122 $fileName = "$baseName.$date.$i.$extension";
123 $out = $this->archiveDir . "/" . $fileName;
124 $i++;
125 }while(file_exists($out));
126
127 //the user may have externally deleted the whole directory - make sure it exists before we do anything
128 @mkdir($archiveDir);
129 rename($this->logFile, $out);
130
131 $logResource = $this->openLogFile($this->logFile, $size);
132 fwrite($logResource, "--- Starting new log file - old log file archived as $fileName ---\n");
133
134 return $logResource;
135 }
136
137 private function logFileReadyToArchive(int $size) : bool{
138 return $size >= $this->maxFileSize;
139 }
140
144 private function writeLogStream(&$logResource, int &$size, ?string $archiveDir) : void{
145 while(($chunk = $this->buffer->shift()) !== null){
146 fwrite($logResource, $chunk);
147 $size += strlen($chunk);
148 if($archiveDir !== null && $this->logFileReadyToArchive($size)){
149 $logResource = $this->archiveLogFile($logResource, $size, $archiveDir);
150 }
151 }
152
153 $this->synchronized(function() : void{
154 if($this->syncFlush){
155 $this->syncFlush = false;
156 $this->notify(); //if this was due to a sync flush, tell the caller to stop waiting
157 }
158 });
159 }
160
161 public function run() : void{
162 $size = 0;
163 $logResource = $this->openLogFile($this->logFile, $size);
164 $archiveDir = $this->archiveDir;
165 if($archiveDir !== null && $this->logFileReadyToArchive($size)){
166 $logResource = $this->archiveLogFile($logResource, $size, $archiveDir);
167 }
168
169 while(!$this->shutdown){
170 $this->writeLogStream($logResource, $size, $archiveDir);
171 $this->synchronized(function() : void{
172 if(!$this->shutdown && !$this->syncFlush){
173 $this->wait();
174 }
175 });
176 }
177
178 $this->writeLogStream($logResource, $size, $archiveDir);
179
180 fclose($logResource);
181 }
182}