You are here

public function Log::findCaller in Drupal 8

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/Database/Log.php \Drupal\Core\Database\Log::findCaller()

Determine the routine that called this query.

We define "the routine that called this query" as the first entry in the call stack that is not inside the includes/Drupal/Database directory, does not begin with db_ and does have a file (which excludes call_user_func_array(), anonymous functions and similar). That makes the climbing logic very simple, and handles the variable stack depth caused by the query builders.

See the debug_backtrace() function.

Return value

This method returns a stack trace entry similar to that generated by debug_backtrace(). However, it flattens the trace entry and the trace entry before it so that we get the function and args of the function that called into the database system, not the function and args of the database call itself.

1 call to Log::findCaller()
Log::log in core/lib/Drupal/Core/Database/Log.php
Log a query to all active logging keys.

File

core/lib/Drupal/Core/Database/Log.php, line 145

Class

Log
Database query logger.

Namespace

Drupal\Core\Database

Code

public function findCaller() {
  $stack = debug_backtrace();
  for ($i = 0, $stack_count = count($stack); $i < $stack_count; ++$i) {

    // If the call was made from a function, 'class' will be empty. It's
    // just easier to give it a default value than to try and integrate
    // that into the if statement below.
    if (empty($stack[$i + 1]['class'])) {
      $stack[$i + 1]['class'] = '';
    }
    if (strpos($stack[$i + 1]['class'], __NAMESPACE__) === FALSE && strpos($stack[$i + 1]['function'], 'db_') === FALSE && !empty($stack[$i]['file'])) {
      $stack[$i] += [
        'file' => '?',
        'line' => '?',
        'args' => [],
      ];
      return [
        'file' => $stack[$i]['file'],
        'line' => $stack[$i]['line'],
        'function' => $stack[$i + 1]['function'],
        'class' => $stack[$i + 1]['class'],
        'type' => isset($stack[$i + 1]['type']) ? $stack[$i + 1]['type'] : NULL,
        'args' => $stack[$i + 1]['args'],
      ];
    }
  }
}