diff --git a/src/pocketmine/PocketMine.php b/src/pocketmine/PocketMine.php index fa7eee849..04c07673c 100644 --- a/src/pocketmine/PocketMine.php +++ b/src/pocketmine/PocketMine.php @@ -177,7 +177,7 @@ namespace pocketmine { define('pocketmine\RESOURCE_PATH', \pocketmine\PATH . 'src' . DIRECTORY_SEPARATOR . 'pocketmine' . DIRECTORY_SEPARATOR . '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); @@ -197,6 +197,14 @@ namespace pocketmine { //Logger has a dependency on timezone $tzError = 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(); diff --git a/src/pocketmine/utils/MainLogger.php b/src/pocketmine/utils/MainLogger.php index f3922d00f..83d0f379b 100644 --- a/src/pocketmine/utils/MainLogger.php +++ b/src/pocketmine/utils/MainLogger.php @@ -302,20 +302,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 5d1d171b4..5061f76cf 100644 --- a/src/pocketmine/utils/Terminal.php +++ b/src/pocketmine/utils/Terminal.php @@ -57,31 +57,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 = ""; @@ -148,8 +146,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; } @@ -169,6 +168,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.