diff --git a/src/pocketmine/PocketMine.php b/src/pocketmine/PocketMine.php index 6c18045eb..34c5c6ec5 100644 --- a/src/pocketmine/PocketMine.php +++ b/src/pocketmine/PocketMine.php @@ -365,6 +365,10 @@ namespace pocketmine { } function kill($pid){ + global $logger; + if($logger instanceof MainLogger){ + $logger->syncFlushBuffer(); + } switch(Utils::getOS()){ case "win": exec("taskkill.exe /F /PID " . ((int) $pid) . " > NUL"); diff --git a/src/pocketmine/utils/MainLogger.php b/src/pocketmine/utils/MainLogger.php index 0f9426a27..4839d615e 100644 --- a/src/pocketmine/utils/MainLogger.php +++ b/src/pocketmine/utils/MainLogger.php @@ -39,6 +39,8 @@ class MainLogger extends \AttachableThreadedLogger{ protected $logDebug; /** @var MainLogger */ public static $logger = null; + /** @var bool */ + private $syncFlush = false; /** * @param string $logFile @@ -162,6 +164,8 @@ class MainLogger extends \AttachableThreadedLogger{ foreach(\pocketmine\getTrace(0, $trace) as $i => $line){ $this->debug($line, true); } + + $this->syncFlushBuffer(); } public function log($level, $message){ @@ -228,6 +232,17 @@ class MainLogger extends \AttachableThreadedLogger{ $this->logStream[] = date("Y-m-d", $now) . " " . $cleanMessage . PHP_EOL; } + public function syncFlushBuffer(){ + $this->syncFlush = true; + $this->synchronized(function(){ + $this->notify(); //write immediately + + while($this->syncFlush){ + $this->wait(); //block until it's all been written to disk + } + }); + } + /** * @param resource $logResource */ @@ -236,6 +251,11 @@ class MainLogger extends \AttachableThreadedLogger{ $chunk = $this->logStream->shift(); fwrite($logResource, $chunk); } + + if($this->syncFlush){ + $this->syncFlush = false; + $this->notify(); //if this was due to a sync flush, tell the caller to stop waiting + } } public function run(){