Fixed MainLogger BC break

This commit is contained in:
Dylan K. Taylor 2024-04-03 15:31:37 +01:00
parent 11fbc8db6f
commit f013079ff6
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D
4 changed files with 15 additions and 15 deletions

View File

@ -327,7 +327,7 @@ JIT_WARNING
} }
$logFile = isset($opts[BootstrapOptions::NO_LOG_FILE]) ? null : Path::join($dataPath, "server.log"); $logFile = isset($opts[BootstrapOptions::NO_LOG_FILE]) ? null : Path::join($dataPath, "server.log");
$logger = new MainLogger($logFile, Path::join($dataPath, "log_archive"), Terminal::hasFormattingCodes(), "Server", new \DateTimeZone(Timezone::get())); $logger = new MainLogger($logFile, Terminal::hasFormattingCodes(), "Server", new \DateTimeZone(Timezone::get()), false, Path::join($dataPath, "log_archive"));
if($logFile === null){ if($logFile === null){
$logger->notice("Logging to file disabled. Ensure logs are collected by other means (e.g. Docker logs)."); $logger->notice("Logging to file disabled. Ensure logs are collected by other means (e.g. Docker logs).");
} }

View File

@ -44,7 +44,7 @@ class MainLogger extends AttachableThreadSafeLogger implements \BufferedLogger{
/** /**
* @throws \RuntimeException * @throws \RuntimeException
*/ */
public function __construct(?string $logFile, string $logArchiveDir, bool $useFormattingCodes, string $mainThreadName, \DateTimeZone $timezone, bool $logDebug = false){ public function __construct(?string $logFile, bool $useFormattingCodes, string $mainThreadName, \DateTimeZone $timezone, bool $logDebug = false, ?string $logArchiveDir = null){
parent::__construct(); parent::__construct();
$this->logDebug = $logDebug; $this->logDebug = $logDebug;

View File

@ -52,12 +52,12 @@ final class MainLoggerThread extends Thread{
public function __construct( public function __construct(
private string $logFile, private string $logFile,
private string $archiveDir, private ?string $archiveDir,
private readonly int $maxFileSize = 32 * 1024 * 1024 //32 MB private readonly int $maxFileSize = 32 * 1024 * 1024 //32 MB
){ ){
$this->buffer = new ThreadSafeArray(); $this->buffer = new ThreadSafeArray();
touch($this->logFile); touch($this->logFile);
if(!@mkdir($this->archiveDir) && !is_dir($this->archiveDir)){ if($this->archiveDir !== null && !@mkdir($this->archiveDir) && !is_dir($this->archiveDir)){
throw new \RuntimeException("Unable to create archive directory: " . ( throw new \RuntimeException("Unable to create archive directory: " . (
is_file($this->archiveDir) ? "it already exists and is not a directory" : "permission denied")); is_file($this->archiveDir) ? "it already exists and is not a directory" : "permission denied"));
} }
@ -108,7 +108,7 @@ final class MainLoggerThread extends Thread{
* @param resource $logResource * @param resource $logResource
* @return resource * @return resource
*/ */
private function archiveLogFile($logResource, int &$size){ private function archiveLogFile($logResource, int &$size, string $archiveDir){
fclose($logResource); fclose($logResource);
clearstatcache(); clearstatcache();
@ -125,7 +125,7 @@ final class MainLoggerThread extends Thread{
}while(file_exists($out)); }while(file_exists($out));
//the user may have externally deleted the whole directory - make sure it exists before we do anything //the user may have externally deleted the whole directory - make sure it exists before we do anything
@mkdir($this->archiveDir); @mkdir($archiveDir);
rename($this->logFile, $out); rename($this->logFile, $out);
$logResource = $this->openLogFile($this->logFile, $size); $logResource = $this->openLogFile($this->logFile, $size);
@ -141,12 +141,12 @@ final class MainLoggerThread extends Thread{
/** /**
* @param resource $logResource * @param resource $logResource
*/ */
private function writeLogStream(&$logResource, int &$size) : void{ private function writeLogStream(&$logResource, int &$size, ?string $archiveDir) : void{
while(($chunk = $this->buffer->shift()) !== null){ while(($chunk = $this->buffer->shift()) !== null){
fwrite($logResource, $chunk); fwrite($logResource, $chunk);
$size += strlen($chunk); $size += strlen($chunk);
if($this->logFileReadyToArchive($size)){ if($archiveDir !== null && $this->logFileReadyToArchive($size)){
$logResource = $this->archiveLogFile($logResource, $size); $logResource = $this->archiveLogFile($logResource, $size, $archiveDir);
} }
} }
@ -161,12 +161,13 @@ final class MainLoggerThread extends Thread{
public function run() : void{ public function run() : void{
$size = 0; $size = 0;
$logResource = $this->openLogFile($this->logFile, $size); $logResource = $this->openLogFile($this->logFile, $size);
if($this->logFileReadyToArchive($size)){ $archiveDir = $this->archiveDir;
$logResource = $this->archiveLogFile($logResource, $size); if($archiveDir !== null && $this->logFileReadyToArchive($size)){
$logResource = $this->archiveLogFile($logResource, $size, $archiveDir);
} }
while(!$this->shutdown){ while(!$this->shutdown){
$this->writeLogStream($logResource, $size); $this->writeLogStream($logResource, $size, $archiveDir);
$this->synchronized(function() : void{ $this->synchronized(function() : void{
if(!$this->shutdown && !$this->syncFlush){ if(!$this->shutdown && !$this->syncFlush){
$this->wait(); $this->wait();
@ -174,7 +175,7 @@ final class MainLoggerThread extends Thread{
}); });
} }
$this->writeLogStream($logResource, $size); $this->writeLogStream($logResource, $size, $archiveDir);
fclose($logResource); fclose($logResource);
} }

View File

@ -44,13 +44,12 @@ class AsyncPoolTest extends TestCase{
public function setUp() : void{ public function setUp() : void{
@define('pocketmine\\COMPOSER_AUTOLOADER_PATH', dirname(__DIR__, 3) . '/vendor/autoload.php'); @define('pocketmine\\COMPOSER_AUTOLOADER_PATH', dirname(__DIR__, 3) . '/vendor/autoload.php');
$this->mainLogger = new MainLogger(null, sys_get_temp_dir(), false, "Main", new \DateTimeZone('UTC')); $this->mainLogger = new MainLogger(null, false, "Main", new \DateTimeZone('UTC'));
$this->pool = new AsyncPool(2, 1024, new ThreadSafeClassLoader(), $this->mainLogger, new SleeperHandler()); $this->pool = new AsyncPool(2, 1024, new ThreadSafeClassLoader(), $this->mainLogger, new SleeperHandler());
} }
public function tearDown() : void{ public function tearDown() : void{
$this->pool->shutdown(); $this->pool->shutdown();
$this->mainLogger->shutdownLogWriterThread();
} }
public function testTaskLeak() : void{ public function testTaskLeak() : void{