Skip to content

Instantly share code, notes, and snippets.

@dwaghmare
Last active August 29, 2015 14:18
Show Gist options
  • Save dwaghmare/694745b4c375d0356054 to your computer and use it in GitHub Desktop.
Save dwaghmare/694745b4c375d0356054 to your computer and use it in GitHub Desktop.
<?php
/**
* Acquia Debug function. This logs a backtrace and an optional message into a logfile
* which will be helpful for debugging.
* INSTALLATION:
* Place this at the end of your settings.php file.
* You can alter the ACQUIA_DEBUG_LOG constant to your preferred path.
* USAGE:
* You can call acquia_debug() with an optional message.
* Example:
* acquia_debug("Debug message 1");
* acquia_debug("Contents of $this:" . var_export($this, true));
* The output is as follows:
* [{process_id} - {internal_counter} - {timestamp}] {URL-if-http-request} : [backtrace...]
* {optional message}
* SAMPLE OUTPUT:
* mysite@ded-1234:/var/www/html/mysite.dev/docroot$ drush ev 'acquia_debug("Hello World!");'
* mysite@ded-1234:/var/www/html/mysite.dev/docroot$ tail -3 /mnt//tmp/[sitename]/acquia_debug.log
* [22872 - 1 - 1414074029.736] : [drush.php 16] drush_main() --> [drush.php 61] _drush_bootstrap_and_dispatch() --> [drush.php 92] drush_dispatch() --> [command.inc 182] call_user_func_array() --> drush_command() --> [command.inc 214] _drush_invoke_hooks() --> [command.inc 362] call_user_func_array() --> drush_core_php_eval() --> [core.drush.inc 1100] eval()
* Hello World!
*/
define('ACQUIA_DEBUG_LOG', '/mnt/tmp/[sitename]/acquia_debug.log');
function acquia_debug($message = '') {
static $count = 0;
$count++;
$time = round(microtime(TRUE), 3);
$request_string = '[' . getmypid() . " - $count - $time] ";
$request_string .= php_sapi_name() != 'cli' ? "{$_SERVER['REQUEST_METHOD']} {$_SERVER['REQUEST_URI']}" : '';
$things = func_get_args();
$backtrace = debug_backtrace();
$first = array_shift($backtrace);
$file_info = isset($first['file']) ? '[' . basename($first['file']) . ' ' . $first['line'] . '] ' : '';
$temp_backtrace[] = $file_info . (isset($first['class']) ? $first['class'] . $first['type'] : '') . $first['function'] . '()';
foreach ($backtrace as $trace) {
$file_info = isset($trace['file']) ? '[' . basename($trace['file']) . ' ' . $trace['line'] . '] ' : '';
$temp_backtrace[] = $file_info . (isset($trace['class']) ? $trace['class'] . $trace['type'] : '') . $trace['function'] . '()';
}
array_shift($temp_backtrace);
$message = implode(" --> ", array_reverse($temp_backtrace)) . "\n";
foreach ($things as $thing) {
switch (gettype($thing)) {
case 'object':
case 'array':
$message .= print_r($thing, TRUE);
break;
case 'resource':
break;
default:
$message .= $thing . "\n";
}
}
file_put_contents(ACQUIA_DEBUG_LOG, "$request_string: $message\n", FILE_APPEND);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment