From b5790d19bf5761e273ed4f5f6b927488bad01071 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Fri, 14 Jul 2017 11:39:04 +0100 Subject: [PATCH] Cleaned up MainLogger registration and declared a typehint Remove null hint from MainLogger::getLogger() so static analysis doesn't complain everywhere just make sure you register the logger before trying to use MainLogger::getLogger() on a different thread! --- src/pocketmine/scheduler/AsyncWorker.php | 4 +--- src/pocketmine/utils/MainLogger.php | 16 ++++++++-------- tests/plugins/PocketMine-TesterPlugin | 2 +- 3 files changed, 10 insertions(+), 12 deletions(-) diff --git a/src/pocketmine/scheduler/AsyncWorker.php b/src/pocketmine/scheduler/AsyncWorker.php index 5339c84e5..5f8a2333c 100644 --- a/src/pocketmine/scheduler/AsyncWorker.php +++ b/src/pocketmine/scheduler/AsyncWorker.php @@ -38,9 +38,7 @@ class AsyncWorker extends Worker{ public function run(){ $this->registerClassLoader(); - if(MainLogger::getLogger() === null){ - $this->logger->registerStatic(); - } + $this->logger->registerStatic(); gc_enable(); ini_set("memory_limit", '-1'); diff --git a/src/pocketmine/utils/MainLogger.php b/src/pocketmine/utils/MainLogger.php index 3cad79782..818e7976a 100644 --- a/src/pocketmine/utils/MainLogger.php +++ b/src/pocketmine/utils/MainLogger.php @@ -54,22 +54,22 @@ class MainLogger extends \AttachableThreadedLogger{ } /** - * @return MainLogger|null + * @return MainLogger */ - public static function getLogger(){ + public static function getLogger() : MainLogger{ return static::$logger; } /** - * Assigns the MainLogger instance to the {@link MainLogger#logger} static property. Because static properties are - * thread-local, this must be called from the body of every Thread if you want the logger to be accessible via - * {@link MainLogger#getLogger}. + * Assigns the MainLogger instance to the {@link MainLogger#logger} static property. + * + * WARNING: Because static properties are thread-local, this MUST be called from the body of every Thread if you + * want the logger to be accessible via {@link MainLogger#getLogger}. */ public function registerStatic(){ - if(static::$logger instanceof MainLogger){ - throw new \RuntimeException("MainLogger has been already registered"); + if(static::$logger === null){ + static::$logger = $this; } - static::$logger = $this; } public function emergency($message){ diff --git a/tests/plugins/PocketMine-TesterPlugin b/tests/plugins/PocketMine-TesterPlugin index ddac19dbb..6daeb27e5 160000 --- a/tests/plugins/PocketMine-TesterPlugin +++ b/tests/plugins/PocketMine-TesterPlugin @@ -1 +1 @@ -Subproject commit ddac19dbbdd9e8b71611ab7fb91069619fcba6f9 +Subproject commit 6daeb27e507323053e81d48b7f5742968829cfbc