Register MainLogger on AsyncWorkers to make MainLogger::getLogger() usable in AsyncTasks

Threaded static properties aren't thread-local anymore in pthreads 3.1.7dev
This commit is contained in:
Dylan K. Taylor 2017-07-04 15:47:05 +01:00
parent 97f6a32557
commit 9c9095060f
4 changed files with 21 additions and 3 deletions

View File

@ -174,6 +174,7 @@ namespace pocketmine {
date_default_timezone_set("UTC");
$logger = new MainLogger(\pocketmine\DATA . "server.log");
$logger->registerStatic();
if(!ini_get("date.timezone")){
if(($timezone = detect_system_timezone()) and date_default_timezone_set($timezone)){

View File

@ -25,6 +25,7 @@ namespace pocketmine\scheduler;
use pocketmine\Collectable;
use pocketmine\Server;
use pocketmine\utils\MainLogger;
/**
* Class used to run async tasks in other threads.

View File

@ -23,6 +23,7 @@ declare(strict_types=1);
namespace pocketmine\scheduler;
use pocketmine\utils\MainLogger;
use pocketmine\Worker;
class AsyncWorker extends Worker{
@ -30,13 +31,17 @@ class AsyncWorker extends Worker{
private $logger;
private $id;
public function __construct(\ThreadedLogger $logger, $id){
public function __construct(MainLogger $logger, $id){
$this->logger = $logger;
$this->id = $id;
}
public function run(){
$this->registerClassLoader();
if(MainLogger::getLogger() === null){
$this->logger->registerStatic();
}
gc_enable();
ini_set("memory_limit", '-1');

View File

@ -46,7 +46,6 @@ class MainLogger extends \AttachableThreadedLogger{
if(static::$logger instanceof MainLogger){
throw new \RuntimeException("MainLogger has been already created");
}
static::$logger = $this;
touch($logFile);
$this->logFile = $logFile;
$this->logDebug = (bool) $logDebug;
@ -55,12 +54,24 @@ class MainLogger extends \AttachableThreadedLogger{
}
/**
* @return MainLogger
* @return MainLogger|null
*/
public static function getLogger(){
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}.
*/
public function registerStatic(){
if(static::$logger instanceof MainLogger){
throw new \RuntimeException("MainLogger has been already registered");
}
static::$logger = $this;
}
public function emergency($message){
$this->send($message, \LogLevel::EMERGENCY, "EMERGENCY", TextFormat::RED);
}