Skip to content

Instantly share code, notes, and snippets.

@GDmac
Last active December 29, 2015 15:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GDmac/7689850 to your computer and use it in GitHub Desktop.
Save GDmac/7689850 to your computer and use it in GitHub Desktop.
Codeigniter profiler, where are the queries coming from ?
<?php
// from http://php.net/manual/en/function.debug-backtrace.php#111255
// adapted for codeigniter
// database/DB_driver.php method query()
//...
// Save the query for debugging
if ($this->save_queries == TRUE)
{
$this->queries[] = $sql . "\n# ".get_caller_info();
}
// get_caller_info funtion somewhere in global scope
function get_caller_info() {
$c = '';
$file = '';
$line = '';
$func = '';
$class = '';
$trace = debug_backtrace();
// if (isset($trace[2])) {
// $file = $trace[1]['file'];
// $line = $trace[1]['line'];
// $func = $trace[2]['function'];
// if ((substr($func, 0, 7) == 'include') || (substr($func, 0, 7) == 'require')) {
// $func = '';
// }
// } else if (isset($trace[1])) {
// $file = $trace[1]['file'];
// $line = $trace[1]['line'];
// $func = '';
// }
if (isset($trace[3]['class'])) {
$class = $trace[3]['class'];
$func = $trace[3]['function'];
$file = $trace[2]['file'];
$line = $trace[2]['line'];
} else if (isset($trace[2]['class'])) {
$class = $trace[2]['class'];
$func = $trace[2]['function'];
$file = $trace[1]['file'];
$line = $trace[1]['line'];
}
if ($file != ''){
if (strpos($file,BASEPATH)===0) {
$file = 'CI ' . str_replace(BASEPATH,'',$file);
}elseif (strpos($file,APPPATH)===0) {
$file = 'APP ' . str_replace(APPPATH,'',$file);
}else {
$file = 'UNKNOWN ' . basename($file);
}
}
$c = $file . ": ";
$c .= ($class != '') ? ":" . $class . "->" : "";
$c .= ($func != '') ? $func . "(): " : "";
$c .= ' Line: '.$line;
return($c);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment