Add Support::getCallStack
returns full call stack for call (excluding self)
This commit is contained in:
@@ -513,7 +513,7 @@ final class CoreLibsDebugSupportTest extends TestCase
|
|||||||
public function testGetCallerMethodList(array $expected): void
|
public function testGetCallerMethodList(array $expected): void
|
||||||
{
|
{
|
||||||
$compare = Support::getCallerMethodList();
|
$compare = Support::getCallerMethodList();
|
||||||
// 10: legact
|
// 10: legacy
|
||||||
// 11: direct
|
// 11: direct
|
||||||
// 12: full call
|
// 12: full call
|
||||||
switch (count($compare)) {
|
switch (count($compare)) {
|
||||||
@@ -571,6 +571,31 @@ final class CoreLibsDebugSupportTest extends TestCase
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Undocumented function
|
||||||
|
*
|
||||||
|
* @cover ::getCallStack
|
||||||
|
* @testdox getCallStack check if it returns data [$_dataName]
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function testGetCallStack(): void
|
||||||
|
{
|
||||||
|
$call_stack = Support::getCallStack();
|
||||||
|
// print "Get CALL: " . print_r(Support::getCallStack(), true) . "\n";
|
||||||
|
if ($call_stack < 8) {
|
||||||
|
$this->assertFalse(true, 'getCallStack too low: 8');
|
||||||
|
} else {
|
||||||
|
$this->assertTrue(true, 'getCallSteck ok');
|
||||||
|
}
|
||||||
|
// just test top entry
|
||||||
|
$first = array_shift($call_stack);
|
||||||
|
$this->assertStringEndsWith(
|
||||||
|
':tests\CoreLibsDebugSupportTest->testGetCallStack',
|
||||||
|
$first,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* test the lowest one (one above base)
|
* test the lowest one (one above base)
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -470,7 +470,7 @@ function intervalStringFormatDeprecated(
|
|||||||
// on first hit turn off (full off)
|
// on first hit turn off (full off)
|
||||||
if ($value) {
|
if ($value) {
|
||||||
$skip_last_zero = null;
|
$skip_last_zero = null;
|
||||||
} elseif (!$value && $skip_last_zero === false) {
|
} elseif ($skip_last_zero === false) {
|
||||||
$zero_last_list[] = $part;
|
$zero_last_list[] = $part;
|
||||||
}
|
}
|
||||||
// build format
|
// build format
|
||||||
|
|||||||
@@ -170,6 +170,8 @@ class TestL
|
|||||||
{
|
{
|
||||||
print "* GETCALLERCLASS(INSIDE CLASS): " . \CoreLibs\Debug\Support::getCallerClass() . "<br>";
|
print "* GETCALLERCLASS(INSIDE CLASS): " . \CoreLibs\Debug\Support::getCallerClass() . "<br>";
|
||||||
print "* GETCALLERTOPCLASS(INSIDE CLASS): " . \CoreLibs\Debug\Support::getCallerTopLevelClass() . "<br>";
|
print "* GETCALLERTOPCLASS(INSIDE CLASS): " . \CoreLibs\Debug\Support::getCallerTopLevelClass() . "<br>";
|
||||||
|
print "* GETCALLSTACK(INSIDE CLASS): <pre>"
|
||||||
|
. DebugSupport::prAr(\CoreLibs\Debug\Support::getCallStack()) . "</pre><br>";
|
||||||
$this->log->debug('TESTL', 'Logging in class testL' . ($ts !== null ? ': ' . $ts : ''));
|
$this->log->debug('TESTL', 'Logging in class testL' . ($ts !== null ? ': ' . $ts : ''));
|
||||||
$this->log->debug('TESTL', 'Some other message');
|
$this->log->debug('TESTL', 'Some other message');
|
||||||
return true;
|
return true;
|
||||||
@@ -193,6 +195,8 @@ class TestR extends TestL
|
|||||||
{
|
{
|
||||||
print "** GETCALLERCLASS(INSIDE EXTND CLASS): " . \CoreLibs\Debug\Support::getCallerClass() . "<br>";
|
print "** GETCALLERCLASS(INSIDE EXTND CLASS): " . \CoreLibs\Debug\Support::getCallerClass() . "<br>";
|
||||||
print "** GETCALLERTOPCLASS(INSIDE EXTND CLASS): " . \CoreLibs\Debug\Support::getCallerTopLevelClass() . "<br>";
|
print "** GETCALLERTOPCLASS(INSIDE EXTND CLASS): " . \CoreLibs\Debug\Support::getCallerTopLevelClass() . "<br>";
|
||||||
|
print "** GETCALLSTACK(INSIDE EXTND CLASS): <pre>"
|
||||||
|
. DebugSupport::prAr(\CoreLibs\Debug\Support::getCallStack()) . "</pre><br>";
|
||||||
$this->log->debug('TESTR', 'Logging in class testR (extends testL)');
|
$this->log->debug('TESTR', 'Logging in class testR (extends testL)');
|
||||||
$this->test('TESTR INSIDE');
|
$this->test('TESTR INSIDE');
|
||||||
$this->log->debug('TESTR', 'Array: '
|
$this->log->debug('TESTR', 'Array: '
|
||||||
|
|||||||
@@ -295,8 +295,7 @@ class Support
|
|||||||
* Will start with start_level to skip unwanted from stack
|
* Will start with start_level to skip unwanted from stack
|
||||||
* Defaults to skip level 0 wich is this methid
|
* Defaults to skip level 0 wich is this methid
|
||||||
*
|
*
|
||||||
* @param integer $start_level From what level on, as defaul starts with 1
|
* @param integer $start_level [=1] From what level on, starts with 1 to exclude self
|
||||||
* to exclude self
|
|
||||||
* @return array<mixed> All method names in list where max is last called
|
* @return array<mixed> All method names in list where max is last called
|
||||||
*/
|
*/
|
||||||
public static function getCallerMethodList(int $start_level = 1): array
|
public static function getCallerMethodList(int $start_level = 1): array
|
||||||
@@ -304,15 +303,46 @@ class Support
|
|||||||
$traces = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
|
$traces = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
|
||||||
$methods = [];
|
$methods = [];
|
||||||
foreach ($traces as $level => $data) {
|
foreach ($traces as $level => $data) {
|
||||||
if ($level >= $start_level) {
|
if ($level < $start_level) {
|
||||||
if (!empty($data['function'])) {
|
continue;
|
||||||
array_unshift($methods, $data['function']);
|
}
|
||||||
}
|
if (!empty($data['function'])) {
|
||||||
|
array_unshift($methods, $data['function']);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return $methods;
|
return $methods;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the full call stack from a certain starting level
|
||||||
|
* The return string is
|
||||||
|
* file:line:class->method
|
||||||
|
*
|
||||||
|
* Note that '::' is used for static calls
|
||||||
|
*
|
||||||
|
* @param int $start_level [=1] starts with 1 to exclude itself
|
||||||
|
* @return array<string> string with file, line, class and method
|
||||||
|
*/
|
||||||
|
public static function getCallStack(int $start_level = 1): array
|
||||||
|
{
|
||||||
|
$call_stack = [];
|
||||||
|
$backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
|
||||||
|
foreach ($backtrace as $level => $call_trace) {
|
||||||
|
if ($level < $start_level) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
$call_stack[] =
|
||||||
|
($call_trace['file'] ?? 'n/f') . ':'
|
||||||
|
. ($call_trace['line'] ?? '-') . ':'
|
||||||
|
. (!empty($call_trace['class']) ?
|
||||||
|
$call_trace['class'] . ($call_trace['type'] ?? '') :
|
||||||
|
''
|
||||||
|
)
|
||||||
|
. $call_trace['function'];
|
||||||
|
}
|
||||||
|
return $call_stack;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the current class where this function is called
|
* Get the current class where this function is called
|
||||||
* Is mostly used in debug log statements to get the class where the debug
|
* Is mostly used in debug log statements to get the class where the debug
|
||||||
|
|||||||
Reference in New Issue
Block a user