lp__calltrace()

The lp__calltrace function is used to generate a call trace, which is a list of the functions and methods that were called leading up to the current point in the code. This can be useful for debugging purposes to understand the flow of execution and to track the sequence of function calls that led to a particular point in the code.


Plugin


Namespace

Launchpad


Return

array

An array representing the call trace.


Source

File: \includes\lp-utility-functions.php

function lp__calltrace() {
 
    $e = new Exception();
    $trace = explode("\n", $e->getTraceAsString());
    // reverse array to make steps line up chronologically
    $trace = array_reverse($trace);
     
    array_shift($trace); // remove {main}
    array_pop($trace); // remove calls to this method
 
    $length = count($trace);
    $result = array();
     
    for ($i = 0; $i < $length; $i++)
    {
        $result[] = ($i + 1)  . ')' . substr($trace[$i], strpos($trace[$i], ' ')); // replace '#someNum' with '$i)', set the right ordering
    }
 
    return $result;
     
}

Example

// Simulated function call
function foo() {
    bar();
}

// Another simulated function call
function bar() {
    baz();
}

// Yet another simulated function call
function baz() {
    // Generate and display the call trace
    $call_trace = lp__calltrace();
    echo "Call Trace:\n";
    echo implode("\n", $call_trace);
}

// Initial function call
foo();

In this example, the foo function calls the bar function, which in turn calls the baz function. Inside the baz function, the lp__calltrace() function is called to generate the call trace, and then it’s displayed using echo.

Result

Call Trace:
1) foo()
2) bar()
3) baz()
Please note that the example is for illustrative purposes and might vary based on the actual context, structure of your code and hosting environment. Most generated logs contains the files and code line numbers where the call were traced.

Related

Use to log error messages with control to include call traces and log on page.

Used to dump variable contents into the error log for debugging.