Move exception printing utilities from MainLogger to Utils

where they can be useful to other stuff apart from just the logger
This commit is contained in:
Dylan K. Taylor
2021-10-14 15:54:20 +01:00
parent bdbfa70558
commit 06e7338ff9
3 changed files with 57 additions and 46 deletions

View File

@@ -24,15 +24,10 @@ declare(strict_types=1);
namespace pocketmine\utils;
use LogLevel;
use pocketmine\errorhandler\ErrorTypeToStringMap;
use pocketmine\thread\Thread;
use pocketmine\thread\Worker;
use function get_class;
use function implode;
use function is_int;
use function preg_replace;
use function sprintf;
use function trim;
use const PHP_EOL;
use const PTHREADS_INHERIT_NONE;
@@ -137,46 +132,11 @@ class MainLogger extends \AttachableThreadedLogger implements \BufferedLogger{
* @return void
*/
public function logException(\Throwable $e, $trace = null){
if($trace === null){
$trace = $e->getTrace();
}
$lines = [self::printExceptionMessage($e)];
$lines[] = "--- Stack trace ---";
foreach(Utils::printableTrace($trace) as $line){
$lines[] = " " . $line;
}
for($prev = $e->getPrevious(); $prev !== null; $prev = $prev->getPrevious()){
$lines[] = "--- Previous ---";
$lines[] = self::printExceptionMessage($prev);
foreach(Utils::printableTrace($prev->getTrace()) as $line){
$lines[] = " " . $line;
}
}
$lines[] = "--- End of exception information ---";
$this->critical(implode("\n", $lines));
$this->critical(implode("\n", Utils::printableExceptionInfo($e, $trace)));
$this->syncFlushBuffer();
}
private static function printExceptionMessage(\Throwable $e) : string{
$errstr = preg_replace('/\s+/', ' ', trim($e->getMessage()));
$errno = $e->getCode();
if(is_int($errno)){
try{
$errno = ErrorTypeToStringMap::get($errno);
}catch(\InvalidArgumentException $ex){
//pass
}
}
$errfile = Filesystem::cleanPath($e->getFile());
$errline = $e->getLine();
return get_class($e) . ": \"$errstr\" ($errno) in \"$errfile\" at line $errline";
}
public function log($level, $message){
switch($level){
case LogLevel::EMERGENCY: