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.
This commit is contained in:
Dylan K. Taylor 2021-02-04 19:16:22 +00:00
parent 6d64fb9af8
commit 709b4154d7
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D
3 changed files with 6 additions and 8 deletions

View File

@ -237,7 +237,7 @@ namespace pocketmine {
Terminal::init(); Terminal::init();
} }
$logger = new MainLogger($dataPath . "server.log"); $logger = new MainLogger($dataPath . "server.log", Terminal::hasFormattingCodes());
\GlobalLogger::set($logger); \GlobalLogger::set($logger);
emit_performance_warnings($logger); emit_performance_warnings($logger);

View File

@ -42,7 +42,7 @@ class MainLogger extends \AttachableThreadedLogger implements \BufferedLogger{
private $format = TextFormat::AQUA . "[%s] " . TextFormat::RESET . "%s[%s/%s]: %s" . TextFormat::RESET; private $format = TextFormat::AQUA . "[%s] " . TextFormat::RESET . "%s[%s/%s]: %s" . TextFormat::RESET;
/** @var bool */ /** @var bool */
private $mainThreadHasFormattingCodes = false; private $useFormattingCodes = false;
/** @var string */ /** @var string */
private $timezone; private $timezone;
@ -53,12 +53,11 @@ class MainLogger extends \AttachableThreadedLogger implements \BufferedLogger{
/** /**
* @throws \RuntimeException * @throws \RuntimeException
*/ */
public function __construct(string $logFile, bool $logDebug = false){ public function __construct(string $logFile, bool $useFormattingCodes, bool $logDebug = false){
parent::__construct(); parent::__construct();
$this->logDebug = $logDebug; $this->logDebug = $logDebug;
//Child threads may not inherit command line arguments, so if there's an override it needs to be recorded here $this->useFormattingCodes = $useFormattingCodes;
$this->mainThreadHasFormattingCodes = Terminal::hasFormattingCodes();
$this->timezone = Timezone::get(); $this->timezone = Timezone::get();
$this->logWriterThread = new MainLoggerThread($logFile); $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)); $message = sprintf($this->format, $time->format("H:i:s.v"), $color, $threadName, $prefix, TextFormat::clean($message, false));
if(!Terminal::isInit()){ 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{ $this->synchronized(function() use ($message, $level, $time) : void{

View File

@ -42,9 +42,8 @@ class AsyncPoolTest extends TestCase{
private $mainLogger; private $mainLogger;
public function setUp() : void{ public function setUp() : void{
Terminal::init();
@define('pocketmine\\COMPOSER_AUTOLOADER_PATH', dirname(__DIR__, 3) . '/vendor/autoload.php'); @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()); $this->pool = new AsyncPool(2, 1024, new \BaseClassLoader(), $this->mainLogger, new SleeperHandler());
} }