Fixed --enable-ansi and --disable-ansi not being respected on threads

this causes some breakage to the behaviour of Terminal, and for that reason this is going on 4.0.

Terminal::hasFormattingCodes() will no longer auto-detect whether colour codes are supported.
This commit is contained in:
Dylan K. Taylor 2018-12-29 11:23:32 +00:00
parent 10ac322b8f
commit 498bffb34f
3 changed files with 38 additions and 29 deletions

View File

@ -183,7 +183,7 @@ namespace pocketmine {
define('pocketmine\RESOURCE_PATH', \pocketmine\PATH . 'resources' . DIRECTORY_SEPARATOR);
$opts = getopt("", ["data:", "plugins:", "no-wizard"]);
$opts = getopt("", ["data:", "plugins:", "no-wizard", "enable-ansi", "disable-ansi"]);
define('pocketmine\DATA', isset($opts["data"]) ? $opts["data"] . DIRECTORY_SEPARATOR : realpath(getcwd()) . DIRECTORY_SEPARATOR);
define('pocketmine\PLUGIN_PATH', isset($opts["plugins"]) ? $opts["plugins"] . DIRECTORY_SEPARATOR : realpath(getcwd()) . DIRECTORY_SEPARATOR . "plugins" . DIRECTORY_SEPARATOR);
@ -195,6 +195,14 @@ namespace pocketmine {
//Logger has a dependency on timezone
Timezone::init();
if(isset($opts["enable-ansi"])){
Terminal::init(true);
}elseif(isset($opts["disable-ansi"])){
Terminal::init(false);
}else{
Terminal::init();
}
$logger = new MainLogger(\pocketmine\DATA . "server.log");
$logger->registerStatic();
\GlobalLogger::set($logger);

View File

@ -275,20 +275,18 @@ class MainLogger extends \AttachableThreadedLogger{
$message = sprintf($this->format, $time->format("H:i:s"), $color, $threadName, $prefix, $message);
$this->synchronized(function() use ($message, $level, $time) : void{
$cleanMessage = TextFormat::clean($message);
if($this->mainThreadHasFormattingCodes and Terminal::hasFormattingCodes()){ //hasFormattingCodes() lazy-inits colour codes because we don't know if they've been registered on this thread
echo Terminal::toANSI($message) . PHP_EOL;
}else{
echo $cleanMessage . PHP_EOL;
if(!Terminal::isInit()){
Terminal::init($this->mainThreadHasFormattingCodes); //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{
echo Terminal::toANSI($message) . PHP_EOL;
foreach($this->attachments as $attachment){
$attachment->call($level, $message);
}
$this->logStream[] = $time->format("Y-m-d") . " " . $cleanMessage . PHP_EOL;
$this->logStream[] = $time->format("Y-m-d") . " " . TextFormat::clean($message) . PHP_EOL;
});
}

View File

@ -49,29 +49,27 @@ abstract class Terminal{
public static $COLOR_YELLOW = "";
public static $COLOR_WHITE = "";
/** @var bool|null */
private static $formattingCodes = null;
public static function hasFormattingCodes(){
public static function hasFormattingCodes() : bool{
if(self::$formattingCodes === null){
$opts = getopt("", ["enable-ansi", "disable-ansi"]);
if(isset($opts["disable-ansi"])){
self::$formattingCodes = false;
}else{
throw new \InvalidStateException("Formatting codes have not been initialized");
}
return self::$formattingCodes;
}
private static function detectFormattingCodesSupport() : bool{
$stdout = fopen("php://stdout", "w");
self::$formattingCodes = (isset($opts["enable-ansi"]) or ( //user explicitly told us to enable ANSI
$result = (
stream_isatty($stdout) and //STDOUT isn't being piped
(
getenv('TERM') !== false or //Console says it supports colours
(function_exists('sapi_windows_vt100_support') and sapi_windows_vt100_support($stdout)) //we're on windows and have vt100 support
)
));
);
fclose($stdout);
}
self::init();
}
return self::$formattingCodes;
return $result;
}
protected static function getFallbackEscapeCodes(){
@ -140,8 +138,9 @@ abstract class Terminal{
}
}
public static function init(){
if(!self::hasFormattingCodes()){
public static function init(?bool $enableFormatting = null) : void{
self::$formattingCodes = $enableFormatting ?? self::detectFormattingCodesSupport();
if(!self::$formattingCodes){
return;
}
@ -161,6 +160,10 @@ abstract class Terminal{
//TODO: iOS
}
public static function isInit() : bool{
return self::$formattingCodes !== null;
}
/**
* Returns a string with colorized ANSI Escape codes for the current terminal
* Note that this is platform-dependent and might produce different results depending on the terminal type and/or OS.