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
3 changed files with 6 additions and 8 deletions

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;
/** @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{