2

I was profiling some of my code with xhprof, and I noticed that debug_backtrace() and _drupal_error_handler_real() have a very high memory usage.

screenshot

I set "Error message display" to "None" at admin/config/development/logging, and I also have database logging module disabled. Still, these functions seem to collect error information. Do you have any idea why it happens?

My code does generate some PHP warnings. If I fix them, would these error calls disappear?

  • Ok, after I fixed the php warnings, these massive memory usages for debug and error_handler calls disappeared... – giorgio79 Nov 11 '12 at 13:38
2

_drupal_error_handler_real() is the function called from the error handler installed from Drupal (_drupal_error_handler()). A PHP error handler is called every time there is a not-handled runtime error (not an exception), including a warning.

The code execute from _drupal_error_handler_real() is the following one. debug_backtrace() is always called, if the error level is one set in PHP.

  if ($error_level & error_reporting()) {
    $types = drupal_error_levels();
    list($severity_msg, $severity_level) = $types[$error_level];
    $caller = _drupal_get_last_caller(debug_backtrace());

    if (!function_exists('filter_xss_admin')) {
      require_once DRUPAL_ROOT . '/includes/common.inc';
    }

    // We treat recoverable errors as fatal.
    _drupal_log_error(array(
      '%type' => isset($types[$error_level]) ? $severity_msg : 'Unknown error',
      // The standard PHP error handler considers that the error messages
      // are HTML. We mimick this behavior here. 
      '!message' => filter_xss_admin($message), 
      '%function' => $caller['function'], 
      '%file' => $caller['file'], 
      '%line' => $caller['line'], 
      'severity_level' => $severity_level,
    ), $error_level == E_RECOVERABLE_ERROR);
  }

Also _drupal_log_error() is called when debug_backtrace() is called, but without any module recording the error, the error is not effectively recorded. In fact, the error is recorded if there are modules implementing hook_watchdog(), such as the Database logging module, and the Syslog module. The task of watchdog(), the function called by _drupal_log_error() is pass the information about the error to the implementations of hook_watchdog() which are then free to record the error.

// Prepare the fields to be logged
$log_entry = array(
  'type' => $type, 
  'message' => $message, 
  'variables' => $variables, 
  'severity' => $severity, 
  'link' => $link, 
  'user' => $user, 
  'uid' => $user_uid, 
  'request_uri' => $base_root . request_uri(), 
  'referer' => isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '', 
  'ip' => ip_address(),
  // Request time isn't accurate for long processes, use time() instead. 
  'timestamp' => time(),
);

// Call the logging hooks to log/process the message
foreach (module_implements('watchdog') as $module) {
  module_invoke($module, 'watchdog', $log_entry);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.