From 498bffb34f08a3ea155dad44746306118f54db5d Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Sat, 29 Dec 2018 11:23:32 +0000 Subject: [PATCH] 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. --- src/pocketmine/PocketMine.php | 10 ++++++- src/pocketmine/utils/MainLogger.php | 14 ++++------ src/pocketmine/utils/Terminal.php | 43 +++++++++++++++-------------- 3 files changed, 38 insertions(+), 29 deletions(-) diff --git a/src/pocketmine/PocketMine.php b/src/pocketmine/PocketMine.php index dabc0c835..83e844f9d 100644 --- a/src/pocketmine/PocketMine.php +++ b/src/pocketmine/PocketMine.php @@ -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); diff --git a/src/pocketmine/utils/MainLogger.php b/src/pocketmine/utils/MainLogger.php index 3f5e0f3f6..78562da77 100644 --- a/src/pocketmine/utils/MainLogger.php +++ b/src/pocketmine/utils/MainLogger.php @@ -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(!Terminal::isInit()){ + Terminal::init($this->mainThreadHasFormattingCodes); //lazy-init colour codes because we don't know if they've been registered on this thread + } - 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; - } + $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; }); } diff --git a/src/pocketmine/utils/Terminal.php b/src/pocketmine/utils/Terminal.php index 5f4140e1e..f7c52a974 100644 --- a/src/pocketmine/utils/Terminal.php +++ b/src/pocketmine/utils/Terminal.php @@ -49,31 +49,29 @@ 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{ - $stdout = fopen("php://stdout", "w"); - self::$formattingCodes = (isset($opts["enable-ansi"]) or ( //user explicitly told us to enable ANSI - 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(); + throw new \InvalidStateException("Formatting codes have not been initialized"); } - return self::$formattingCodes; } + private static function detectFormattingCodesSupport() : bool{ + $stdout = fopen("php://stdout", "w"); + $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); + return $result; + } + protected static function getFallbackEscapeCodes(){ self::$FORMAT_BOLD = "\x1b[1m"; self::$FORMAT_OBFUSCATED = ""; @@ -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.