From f527a4c8fe3787c7e4b187f0ed6d174c6dd5e5a7 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Fri, 15 Mar 2024 17:53:50 +0000 Subject: [PATCH] Added --no-log-file command line option while this would be more user-friendly as a config option, configs are a pain because they aren't initialized until after the server log has already been set up. In any case, I foresee that people will likely want to bake this into Dockerfiles directly anyway. --- src/BootstrapOptions.php | 2 ++ src/PocketMine.php | 8 +++++-- src/utils/MainLogger.php | 28 ++++++++++++++--------- tests/phpunit/scheduler/AsyncPoolTest.php | 2 +- 4 files changed, 26 insertions(+), 14 deletions(-) diff --git a/src/BootstrapOptions.php b/src/BootstrapOptions.php index c34dda94b..6a1469625 100644 --- a/src/BootstrapOptions.php +++ b/src/BootstrapOptions.php @@ -47,4 +47,6 @@ final class BootstrapOptions{ public const DATA = "data"; /** Shows basic server version information and exits */ public const VERSION = "version"; + /** Disables writing logs to server.log */ + public const NO_LOG_FILE = "no-log-file"; } diff --git a/src/PocketMine.php b/src/PocketMine.php index d6e0ae506..f0fba9dcb 100644 --- a/src/PocketMine.php +++ b/src/PocketMine.php @@ -317,7 +317,7 @@ JIT_WARNING //Logger has a dependency on timezone Timezone::init(); - $opts = getopt("", [BootstrapOptions::NO_WIZARD, BootstrapOptions::ENABLE_ANSI, BootstrapOptions::DISABLE_ANSI]); + $opts = getopt("", [BootstrapOptions::NO_WIZARD, BootstrapOptions::ENABLE_ANSI, BootstrapOptions::DISABLE_ANSI, BootstrapOptions::NO_LOG_FILE]); if(isset($opts[BootstrapOptions::ENABLE_ANSI])){ Terminal::init(true); }elseif(isset($opts[BootstrapOptions::DISABLE_ANSI])){ @@ -325,8 +325,12 @@ JIT_WARNING }else{ Terminal::init(); } + $logFile = isset($opts[BootstrapOptions::NO_LOG_FILE]) ? null : Path::join($dataPath, "server.log"); - $logger = new MainLogger(Path::join($dataPath, "server.log"), Path::join($dataPath, "log_archive"), Terminal::hasFormattingCodes(), "Server", new \DateTimeZone(Timezone::get())); + $logger = new MainLogger($logFile, Path::join($dataPath, "log_archive"), Terminal::hasFormattingCodes(), "Server", new \DateTimeZone(Timezone::get())); + if($logFile === null){ + $logger->notice("Logging to file disabled. Ensure logs are collected by other means (e.g. Docker logs)."); + } \GlobalLogger::set($logger); diff --git a/src/utils/MainLogger.php b/src/utils/MainLogger.php index 20f9fb6e7..da2ba73da 100644 --- a/src/utils/MainLogger.php +++ b/src/utils/MainLogger.php @@ -39,12 +39,12 @@ class MainLogger extends AttachableThreadSafeLogger implements \BufferedLogger{ private bool $useFormattingCodes = false; private string $mainThreadName; private string $timezone; - private MainLoggerThread $logWriterThread; + private ?MainLoggerThread $logWriterThread = null; /** * @throws \RuntimeException */ - public function __construct(string $logFile, string $logArchiveDir, bool $useFormattingCodes, string $mainThreadName, \DateTimeZone $timezone, bool $logDebug = false){ + public function __construct(?string $logFile, string $logArchiveDir, bool $useFormattingCodes, string $mainThreadName, \DateTimeZone $timezone, bool $logDebug = false){ parent::__construct(); $this->logDebug = $logDebug; @@ -52,8 +52,10 @@ class MainLogger extends AttachableThreadSafeLogger implements \BufferedLogger{ $this->mainThreadName = $mainThreadName; $this->timezone = $timezone->getName(); - $this->logWriterThread = new MainLoggerThread($logFile, $logArchiveDir); - $this->logWriterThread->start(NativeThread::INHERIT_NONE); + if($logFile !== null){ + $this->logWriterThread = new MainLoggerThread($logFile, $logArchiveDir); + $this->logWriterThread->start(NativeThread::INHERIT_NONE); + } } /** @@ -166,10 +168,12 @@ class MainLogger extends AttachableThreadSafeLogger implements \BufferedLogger{ } public function shutdownLogWriterThread() : void{ - if(NativeThread::getCurrentThreadId() === $this->logWriterThread->getCreatorId()){ - $this->logWriterThread->shutdown(); - }else{ - throw new \LogicException("Only the creator thread can shutdown the logger thread"); + if($this->logWriterThread !== null){ + if(NativeThread::getCurrentThreadId() === $this->logWriterThread->getCreatorId()){ + $this->logWriterThread->shutdown(); + }else{ + throw new \LogicException("Only the creator thread can shutdown the logger thread"); + } } } @@ -193,7 +197,9 @@ class MainLogger extends AttachableThreadSafeLogger implements \BufferedLogger{ $this->synchronized(function() use ($message, $level, $time) : void{ Terminal::writeLine($message); - $this->logWriterThread->write($time->format("Y-m-d") . " " . TextFormat::clean($message) . PHP_EOL); + if($this->logWriterThread !== null){ + $this->logWriterThread->write($time->format("Y-m-d") . " " . TextFormat::clean($message) . PHP_EOL); + } /** * @var ThreadSafeLoggerAttachment $attachment @@ -205,11 +211,11 @@ class MainLogger extends AttachableThreadSafeLogger implements \BufferedLogger{ } public function syncFlushBuffer() : void{ - $this->logWriterThread->syncFlushBuffer(); + $this->logWriterThread?->syncFlushBuffer(); } public function __destruct(){ - if(!$this->logWriterThread->isJoined() && NativeThread::getCurrentThreadId() === $this->logWriterThread->getCreatorId()){ + if($this->logWriterThread !== null && !$this->logWriterThread->isJoined() && NativeThread::getCurrentThreadId() === $this->logWriterThread->getCreatorId()){ $this->shutdownLogWriterThread(); } } diff --git a/tests/phpunit/scheduler/AsyncPoolTest.php b/tests/phpunit/scheduler/AsyncPoolTest.php index 51622dd22..294092ead 100644 --- a/tests/phpunit/scheduler/AsyncPoolTest.php +++ b/tests/phpunit/scheduler/AsyncPoolTest.php @@ -45,7 +45,7 @@ class AsyncPoolTest extends TestCase{ public function setUp() : void{ @define('pocketmine\\COMPOSER_AUTOLOADER_PATH', dirname(__DIR__, 3) . '/vendor/autoload.php'); - $this->mainLogger = new MainLogger(tempnam(sys_get_temp_dir(), "pmlog"), sys_get_temp_dir(), false, "Main", new \DateTimeZone('UTC')); + $this->mainLogger = new MainLogger(null, sys_get_temp_dir(), false, "Main", new \DateTimeZone('UTC')); $this->pool = new AsyncPool(2, 1024, new ThreadSafeClassLoader(), $this->mainLogger, new SleeperHandler()); }