mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-07-05 17:41:46 +00:00
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:
parent
bdbfa70558
commit
06e7338ff9
@ -24,15 +24,10 @@ declare(strict_types=1);
|
|||||||
namespace pocketmine\utils;
|
namespace pocketmine\utils;
|
||||||
|
|
||||||
use LogLevel;
|
use LogLevel;
|
||||||
use pocketmine\errorhandler\ErrorTypeToStringMap;
|
|
||||||
use pocketmine\thread\Thread;
|
use pocketmine\thread\Thread;
|
||||||
use pocketmine\thread\Worker;
|
use pocketmine\thread\Worker;
|
||||||
use function get_class;
|
|
||||||
use function implode;
|
use function implode;
|
||||||
use function is_int;
|
|
||||||
use function preg_replace;
|
|
||||||
use function sprintf;
|
use function sprintf;
|
||||||
use function trim;
|
|
||||||
use const PHP_EOL;
|
use const PHP_EOL;
|
||||||
use const PTHREADS_INHERIT_NONE;
|
use const PTHREADS_INHERIT_NONE;
|
||||||
|
|
||||||
@ -137,46 +132,11 @@ class MainLogger extends \AttachableThreadedLogger implements \BufferedLogger{
|
|||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function logException(\Throwable $e, $trace = null){
|
public function logException(\Throwable $e, $trace = null){
|
||||||
if($trace === null){
|
$this->critical(implode("\n", Utils::printableExceptionInfo($e, $trace)));
|
||||||
$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->syncFlushBuffer();
|
$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){
|
public function log($level, $message){
|
||||||
switch($level){
|
switch($level){
|
||||||
case LogLevel::EMERGENCY:
|
case LogLevel::EMERGENCY:
|
||||||
|
@ -28,6 +28,7 @@ declare(strict_types=1);
|
|||||||
namespace pocketmine\utils;
|
namespace pocketmine\utils;
|
||||||
|
|
||||||
use DaveRandom\CallbackValidator\CallbackType;
|
use DaveRandom\CallbackValidator\CallbackType;
|
||||||
|
use pocketmine\errorhandler\ErrorTypeToStringMap;
|
||||||
use Ramsey\Uuid\Uuid;
|
use Ramsey\Uuid\Uuid;
|
||||||
use Ramsey\Uuid\UuidInterface;
|
use Ramsey\Uuid\UuidInterface;
|
||||||
use function array_combine;
|
use function array_combine;
|
||||||
@ -46,6 +47,7 @@ use function file;
|
|||||||
use function file_exists;
|
use function file_exists;
|
||||||
use function file_get_contents;
|
use function file_get_contents;
|
||||||
use function function_exists;
|
use function function_exists;
|
||||||
|
use function get_class;
|
||||||
use function get_current_user;
|
use function get_current_user;
|
||||||
use function get_loaded_extensions;
|
use function get_loaded_extensions;
|
||||||
use function getenv;
|
use function getenv;
|
||||||
@ -53,6 +55,7 @@ use function gettype;
|
|||||||
use function implode;
|
use function implode;
|
||||||
use function is_array;
|
use function is_array;
|
||||||
use function is_bool;
|
use function is_bool;
|
||||||
|
use function is_int;
|
||||||
use function is_object;
|
use function is_object;
|
||||||
use function is_string;
|
use function is_string;
|
||||||
use function mb_check_encoding;
|
use function mb_check_encoding;
|
||||||
@ -384,6 +387,49 @@ final class Utils{
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static function printableExceptionMessage(\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";
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param mixed[] $trace
|
||||||
|
* @return string[]
|
||||||
|
*/
|
||||||
|
public static function printableExceptionInfo(\Throwable $e, $trace = null) : array{
|
||||||
|
if($trace === null){
|
||||||
|
$trace = $e->getTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
$lines = [self::printableExceptionMessage($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::printableExceptionMessage($prev);
|
||||||
|
foreach(Utils::printableTrace($prev->getTrace()) as $line){
|
||||||
|
$lines[] = " " . $line;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$lines[] = "--- End of exception information ---";
|
||||||
|
return $lines;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param mixed[][] $trace
|
* @param mixed[][] $trace
|
||||||
* @phpstan-param list<array<string, mixed>> $trace
|
* @phpstan-param list<array<string, mixed>> $trace
|
||||||
|
@ -180,21 +180,26 @@ parameters:
|
|||||||
count: 1
|
count: 1
|
||||||
path: ../../../src/utils/Internet.php
|
path: ../../../src/utils/Internet.php
|
||||||
|
|
||||||
-
|
|
||||||
message: "#^Part \\$errno \\(mixed\\) of encapsed string cannot be cast to string\\.$#"
|
|
||||||
count: 1
|
|
||||||
path: ../../../src/utils/MainLogger.php
|
|
||||||
|
|
||||||
-
|
-
|
||||||
message: "#^Parameter \\#1 \\$path of static method pocketmine\\\\utils\\\\Filesystem\\:\\:cleanPath\\(\\) expects string, mixed given\\.$#"
|
message: "#^Parameter \\#1 \\$path of static method pocketmine\\\\utils\\\\Filesystem\\:\\:cleanPath\\(\\) expects string, mixed given\\.$#"
|
||||||
count: 1
|
count: 1
|
||||||
path: ../../../src/utils/Utils.php
|
path: ../../../src/utils/Utils.php
|
||||||
|
|
||||||
|
-
|
||||||
|
message: "#^Parameter \\#1 \\$trace of static method pocketmine\\\\utils\\\\Utils\\:\\:printableTrace\\(\\) expects array\\<int, array\\<string, mixed\\>\\>, array given\\.$#"
|
||||||
|
count: 1
|
||||||
|
path: ../../../src/utils/Utils.php
|
||||||
|
|
||||||
-
|
-
|
||||||
message: "#^Parameter \\#2 \\$array of function array_map expects array, mixed given\\.$#"
|
message: "#^Parameter \\#2 \\$array of function array_map expects array, mixed given\\.$#"
|
||||||
count: 1
|
count: 1
|
||||||
path: ../../../src/utils/Utils.php
|
path: ../../../src/utils/Utils.php
|
||||||
|
|
||||||
|
-
|
||||||
|
message: "#^Part \\$errno \\(mixed\\) of encapsed string cannot be cast to string\\.$#"
|
||||||
|
count: 1
|
||||||
|
path: ../../../src/utils/Utils.php
|
||||||
|
|
||||||
-
|
-
|
||||||
message: "#^Parameter \\#1 \\$keys of function array_fill_keys expects array, mixed given\\.$#"
|
message: "#^Parameter \\#1 \\$keys of function array_fill_keys expects array, mixed given\\.$#"
|
||||||
count: 1
|
count: 1
|
||||||
|
Loading…
x
Reference in New Issue
Block a user