Refactor getTraceString and add call type determination.
This commit is contained in:
parent
b6417cba97
commit
e927c7a8ec
2 changed files with 72 additions and 21 deletions
|
@ -157,36 +157,34 @@
|
||||||
* If $ansi is true, the output will be colored using ANSI escape codes.
|
* If $ansi is true, the output will be colored using ANSI escape codes.
|
||||||
* If the event has no backtrace, the constant CallType::LAMBDA_CALL will be returned.
|
* If the event has no backtrace, the constant CallType::LAMBDA_CALL will be returned.
|
||||||
*/
|
*/
|
||||||
public static function getTraceString(Event $event, bool $ansi=true): ?string
|
public static function getTraceString(Event $event, bool $ansi = true): ?string
|
||||||
{
|
{
|
||||||
if($event->getBacktrace() === null || count($event->getBacktrace()) === 0)
|
if ($event->getBacktrace() === null || count($event->getBacktrace()) === 0)
|
||||||
{
|
{
|
||||||
return CallType::LAMBDA_CALL->value;
|
return CallType::LAMBDA_CALL->value;
|
||||||
}
|
}
|
||||||
|
|
||||||
$backtrace = $event->getBacktrace()[count($event->getBacktrace()) - 1];
|
$backtrace = $event->getBacktrace()[count($event->getBacktrace()) - 1];
|
||||||
|
|
||||||
// Ignore \LogLib namespace
|
// Ignore \LogLib namespace
|
||||||
if(isset($backtrace['class']) && str_starts_with($backtrace['class'], 'LogLib'))
|
if (isset($backtrace['class']) && str_starts_with($backtrace['class'], 'LogLib'))
|
||||||
{
|
{
|
||||||
if(isset($backtrace['file']))
|
if (isset($backtrace['file']))
|
||||||
{
|
{
|
||||||
if($ansi)
|
if ($ansi)
|
||||||
{
|
{
|
||||||
return "\033[1;37m" . basename($backtrace['file']) . "\033[0m";
|
return "\033[1;37m" . basename($backtrace['file']) . "\033[0m";
|
||||||
}
|
}
|
||||||
|
|
||||||
return basename($backtrace['file']);
|
return basename($backtrace['file']);
|
||||||
}
|
}
|
||||||
|
|
||||||
return basename($backtrace['file']);
|
return self::determineCallType($event->getBacktrace())->value; // Return a placeholder value
|
||||||
}
|
}
|
||||||
|
|
||||||
if($backtrace['function'] === '{closure}')
|
if ($backtrace['function'] === '{closure}')
|
||||||
{
|
{
|
||||||
if(isset($backtrace['file']))
|
if (isset($backtrace['file']))
|
||||||
{
|
{
|
||||||
if($ansi)
|
if ($ansi)
|
||||||
{
|
{
|
||||||
return "\033[1;37m" . basename($backtrace['file']) . "\033[0m" . CallType::STATIC_CALL->value . CallType::LAMBDA_CALL->value;
|
return "\033[1;37m" . basename($backtrace['file']) . "\033[0m" . CallType::STATIC_CALL->value . CallType::LAMBDA_CALL->value;
|
||||||
}
|
}
|
||||||
|
@ -194,25 +192,24 @@
|
||||||
return basename($backtrace['file']) . CallType::STATIC_CALL->value . CallType::LAMBDA_CALL->value;
|
return basename($backtrace['file']) . CallType::STATIC_CALL->value . CallType::LAMBDA_CALL->value;
|
||||||
}
|
}
|
||||||
|
|
||||||
return basename($backtrace['file']) . CallType::STATIC_CALL->value . CallType::LAMBDA_CALL->value;
|
return self::determineCallType($event->getBacktrace())->value . CallType::STATIC_CALL->value . CallType::LAMBDA_CALL->value; // Adjusted to handle missing 'file'
|
||||||
}
|
}
|
||||||
|
|
||||||
if($backtrace['function'] === 'eval')
|
if ($backtrace['function'] === 'eval')
|
||||||
{
|
{
|
||||||
if(isset($backtrace['file']))
|
if (isset($backtrace['file']))
|
||||||
{
|
{
|
||||||
if($ansi)
|
if ($ansi)
|
||||||
{
|
{
|
||||||
return "\033[1;37m" . basename($backtrace['file']) . "\033[0m" . CallType::STATIC_CALL->value . CallType::EVAL_CALL->value;
|
return "\033[1;37m" . basename($backtrace['file']) . "\033[0m" . CallType::STATIC_CALL->value . CallType::EVAL_CALL->value;
|
||||||
}
|
}
|
||||||
|
|
||||||
return basename($backtrace['file']) . CallType::STATIC_CALL->value . CallType::EVAL_CALL->value;
|
return basename($backtrace['file']) . CallType::STATIC_CALL->value . CallType::EVAL_CALL->value;
|
||||||
}
|
}
|
||||||
|
return self::determineCallType($event->getBacktrace())->value . CallType::STATIC_CALL->value . CallType::EVAL_CALL->value; // Adjusted to handle missing 'file'
|
||||||
return basename($backtrace['file']) . CallType::STATIC_CALL->value . CallType::EVAL_CALL->value;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if($ansi)
|
if ($ansi)
|
||||||
{
|
{
|
||||||
$function = sprintf("\033[1;37m%s\033[0m", $backtrace['function']);
|
$function = sprintf("\033[1;37m%s\033[0m", $backtrace['function']);
|
||||||
}
|
}
|
||||||
|
@ -223,9 +220,9 @@
|
||||||
|
|
||||||
$class = null;
|
$class = null;
|
||||||
|
|
||||||
if(isset($backtrace["class"]))
|
if (isset($backtrace["class"]))
|
||||||
{
|
{
|
||||||
if($ansi)
|
if ($ansi)
|
||||||
{
|
{
|
||||||
$class = sprintf("\033[1;37m%s\033[0m", $backtrace['class']);
|
$class = sprintf("\033[1;37m%s\033[0m", $backtrace['class']);
|
||||||
}
|
}
|
||||||
|
@ -235,7 +232,7 @@
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if($class === null)
|
if ($class === null)
|
||||||
{
|
{
|
||||||
return $function . CallType::FUNCTION_CALL->value;
|
return $function . CallType::FUNCTION_CALL->value;
|
||||||
}
|
}
|
||||||
|
@ -244,6 +241,32 @@
|
||||||
return "{$class}{$type->value}{$function}" . CallType::FUNCTION_CALL->value;
|
return "{$class}{$type->value}{$function}" . CallType::FUNCTION_CALL->value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Determines the type of call based on the provided backtrace.
|
||||||
|
*
|
||||||
|
* @param array $backtrace The backtrace information of the calling code.
|
||||||
|
* @return CallType The type of call detected.
|
||||||
|
*/
|
||||||
|
private static function determineCallType(array $backtrace): CallType
|
||||||
|
{
|
||||||
|
if(BacktraceParser::fromErrorHandler($backtrace))
|
||||||
|
{
|
||||||
|
return CallType::ERROR_HANDLER;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(BacktraceParser::fromExceptionHandler($backtrace))
|
||||||
|
{
|
||||||
|
return CallType::EXCEPTION_HANDLER;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(BacktraceParser::fromShutdownHandler($backtrace))
|
||||||
|
{
|
||||||
|
return CallType::SHUTDOWN_HANDLER;
|
||||||
|
}
|
||||||
|
|
||||||
|
return CallType::UNKNOWN_FILE;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Converts an exception object to an array representation.
|
* Converts an exception object to an array representation.
|
||||||
*
|
*
|
||||||
|
|
|
@ -38,4 +38,32 @@
|
||||||
* @var string EVAL_CALL
|
* @var string EVAL_CALL
|
||||||
*/
|
*/
|
||||||
case EVAL_CALL = 'eval()';
|
case EVAL_CALL = 'eval()';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents an unknown file.
|
||||||
|
*
|
||||||
|
* @var string UNKNOWN_FILE
|
||||||
|
*/
|
||||||
|
case UNKNOWN_FILE = '?';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents a runtime error handler.
|
||||||
|
*
|
||||||
|
* @var string ERROR_HANDLER
|
||||||
|
*/
|
||||||
|
case ERROR_HANDLER = 'RUNTIME_ERROR';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents a shutdown handler event.
|
||||||
|
*
|
||||||
|
* @var string SHUTDOWN_HANDLER
|
||||||
|
*/
|
||||||
|
case SHUTDOWN_HANDLER = 'SHUTDOWN_ERROR';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents an exception handler for runtime exceptions.
|
||||||
|
*
|
||||||
|
* @var string EXCEPTION_HANDLER
|
||||||
|
*/
|
||||||
|
case EXCEPTION_HANDLER = 'RUNTIME_EXCEPTION';
|
||||||
}
|
}
|
Loading…
Add table
Reference in a new issue