1.20.x log.inc public DatabaseLog::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 includes/database 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.

@link http://www.php.net/debug_backtrace

Return value

array: 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.

File

includes/database/log.inc, line 143
Logging classes for the database layer.

Class

DatabaseLog
Database query logger.

Code

public function findCaller() {
  $stack = debug_backtrace();
  $stack_count = count($stack);
  $includes_db_dir = 'includes' . DIRECTORY_SEPARATOR . 'database';
  $drivers_dir = 'drivers' . DIRECTORY_SEPARATOR;
  for ($i = 0; $i < $stack_count; ++$i) {
    if (!empty($stack[$i]['file']) && 
      strpos($stack[$i]['file'], $includes_db_dir) === FALSE && 
      strpos($stack[$i]['file'], $drivers_dir) === FALSE) {
      $stack[$i] += array('args' => array());
      break;
    }
  }
  return array(
    'file' => $stack[$i]['file'],
    'line' => $stack[$i]['line'],
    'function' => $stack[$i + 1]['function'],
    'class' => isset($stack[$i + 1]['class']) ? $stack[$i + 1]['class'] : NULL,
    'type' => isset($stack[$i + 1]['type']) ? $stack[$i + 1]['type'] : NULL,
    'args' => $stack[$i + 1]['args'],
  );
}