From 709b4154d75742cc15a84c5b9b18ca77d9b5953c Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Thu, 4 Feb 2021 19:16:22 +0000 Subject: [PATCH] MainLogger: Require useFormattingCodes as a constructor parameter this avoids needing to call Terminal::init() before starting a MainLogger. Since it inits the formatting codes anyway when log messages are first recorded, it shouldn't be necessary to pre-initialize it. --- src/PocketMine.php | 2 +- src/utils/MainLogger.php | 9 ++++----- tests/phpunit/scheduler/AsyncPoolTest.php | 3 +-- 3 files changed, 6 insertions(+), 8 deletions(-) diff --git a/src/PocketMine.php b/src/PocketMine.php index 22357648e..daab079ee 100644 --- a/src/PocketMine.php +++ b/src/PocketMine.php @@ -237,7 +237,7 @@ namespace pocketmine { Terminal::init(); } - $logger = new MainLogger($dataPath . "server.log"); + $logger = new MainLogger($dataPath . "server.log", Terminal::hasFormattingCodes()); \GlobalLogger::set($logger); emit_performance_warnings($logger); diff --git a/src/utils/MainLogger.php b/src/utils/MainLogger.php index 010cffcdc..ee30da4b4 100644 --- a/src/utils/MainLogger.php +++ b/src/utils/MainLogger.php @@ -42,7 +42,7 @@ class MainLogger extends \AttachableThreadedLogger implements \BufferedLogger{ private $format = TextFormat::AQUA . "[%s] " . TextFormat::RESET . "%s[%s/%s]: %s" . TextFormat::RESET; /** @var bool */ - private $mainThreadHasFormattingCodes = false; + private $useFormattingCodes = false; /** @var string */ private $timezone; @@ -53,12 +53,11 @@ class MainLogger extends \AttachableThreadedLogger implements \BufferedLogger{ /** * @throws \RuntimeException */ - public function __construct(string $logFile, bool $logDebug = false){ + public function __construct(string $logFile, bool $useFormattingCodes, bool $logDebug = false){ parent::__construct(); $this->logDebug = $logDebug; - //Child threads may not inherit command line arguments, so if there's an override it needs to be recorded here - $this->mainThreadHasFormattingCodes = Terminal::hasFormattingCodes(); + $this->useFormattingCodes = $useFormattingCodes; $this->timezone = Timezone::get(); $this->logWriterThread = new MainLoggerThread($logFile); @@ -234,7 +233,7 @@ class MainLogger extends \AttachableThreadedLogger implements \BufferedLogger{ $message = sprintf($this->format, $time->format("H:i:s.v"), $color, $threadName, $prefix, TextFormat::clean($message, false)); if(!Terminal::isInit()){ - Terminal::init($this->mainThreadHasFormattingCodes); //lazy-init colour codes because we don't know if they've been registered on this thread + Terminal::init($this->useFormattingCodes); //lazy-init colour codes because we don't know if they've been registered on this thread } $this->synchronized(function() use ($message, $level, $time) : void{ diff --git a/tests/phpunit/scheduler/AsyncPoolTest.php b/tests/phpunit/scheduler/AsyncPoolTest.php index 810e475e9..98cd180b7 100644 --- a/tests/phpunit/scheduler/AsyncPoolTest.php +++ b/tests/phpunit/scheduler/AsyncPoolTest.php @@ -42,9 +42,8 @@ class AsyncPoolTest extends TestCase{ private $mainLogger; public function setUp() : void{ - Terminal::init(); @define('pocketmine\\COMPOSER_AUTOLOADER_PATH', dirname(__DIR__, 3) . '/vendor/autoload.php'); - $this->mainLogger = new MainLogger(tempnam(sys_get_temp_dir(), "pmlog")); + $this->mainLogger = new MainLogger(tempnam(sys_get_temp_dir(), "pmlog"), false); $this->pool = new AsyncPool(2, 1024, new \BaseClassLoader(), $this->mainLogger, new SleeperHandler()); }