From f013079ff66d3c8610d5ced0f5a5be34fbf56c63 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Wed, 3 Apr 2024 15:31:37 +0100 Subject: [PATCH] Fixed MainLogger BC break --- src/PocketMine.php | 2 +- src/utils/MainLogger.php | 2 +- src/utils/MainLoggerThread.php | 23 ++++++++++++----------- tests/phpunit/scheduler/AsyncPoolTest.php | 3 +-- 4 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/PocketMine.php b/src/PocketMine.php index f0fba9dcb..84054a2de 100644 --- a/src/PocketMine.php +++ b/src/PocketMine.php @@ -327,7 +327,7 @@ JIT_WARNING } $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){ $logger->notice("Logging to file disabled. Ensure logs are collected by other means (e.g. Docker logs)."); } diff --git a/src/utils/MainLogger.php b/src/utils/MainLogger.php index da2ba73da..2eaee7883 100644 --- a/src/utils/MainLogger.php +++ b/src/utils/MainLogger.php @@ -44,7 +44,7 @@ class MainLogger extends AttachableThreadSafeLogger implements \BufferedLogger{ /** * @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(); $this->logDebug = $logDebug; diff --git a/src/utils/MainLoggerThread.php b/src/utils/MainLoggerThread.php index e7acf9737..990644f65 100644 --- a/src/utils/MainLoggerThread.php +++ b/src/utils/MainLoggerThread.php @@ -52,12 +52,12 @@ final class MainLoggerThread extends Thread{ public function __construct( private string $logFile, - private string $archiveDir, + private ?string $archiveDir, private readonly int $maxFileSize = 32 * 1024 * 1024 //32 MB ){ $this->buffer = new ThreadSafeArray(); 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: " . ( 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 * @return resource */ - private function archiveLogFile($logResource, int &$size){ + private function archiveLogFile($logResource, int &$size, string $archiveDir){ fclose($logResource); clearstatcache(); @@ -125,7 +125,7 @@ final class MainLoggerThread extends Thread{ }while(file_exists($out)); //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); $logResource = $this->openLogFile($this->logFile, $size); @@ -141,12 +141,12 @@ final class MainLoggerThread extends Thread{ /** * @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){ fwrite($logResource, $chunk); $size += strlen($chunk); - if($this->logFileReadyToArchive($size)){ - $logResource = $this->archiveLogFile($logResource, $size); + if($archiveDir !== null && $this->logFileReadyToArchive($size)){ + $logResource = $this->archiveLogFile($logResource, $size, $archiveDir); } } @@ -161,12 +161,13 @@ final class MainLoggerThread extends Thread{ public function run() : void{ $size = 0; $logResource = $this->openLogFile($this->logFile, $size); - if($this->logFileReadyToArchive($size)){ - $logResource = $this->archiveLogFile($logResource, $size); + $archiveDir = $this->archiveDir; + if($archiveDir !== null && $this->logFileReadyToArchive($size)){ + $logResource = $this->archiveLogFile($logResource, $size, $archiveDir); } while(!$this->shutdown){ - $this->writeLogStream($logResource, $size); + $this->writeLogStream($logResource, $size, $archiveDir); $this->synchronized(function() : void{ if(!$this->shutdown && !$this->syncFlush){ $this->wait(); @@ -174,7 +175,7 @@ final class MainLoggerThread extends Thread{ }); } - $this->writeLogStream($logResource, $size); + $this->writeLogStream($logResource, $size, $archiveDir); fclose($logResource); } diff --git a/tests/phpunit/scheduler/AsyncPoolTest.php b/tests/phpunit/scheduler/AsyncPoolTest.php index d7bacd391..479cfee44 100644 --- a/tests/phpunit/scheduler/AsyncPoolTest.php +++ b/tests/phpunit/scheduler/AsyncPoolTest.php @@ -44,13 +44,12 @@ class AsyncPoolTest extends TestCase{ public function setUp() : void{ @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()); } public function tearDown() : void{ $this->pool->shutdown(); - $this->mainLogger->shutdownLogWriterThread(); } public function testTaskLeak() : void{