This helper class traces executed statements. Calls it like so:
CStatementTracer::getInstance()->enableTrace( 'START' );
Source Code
<?php
declare(ticks=1);
class CStatementTracer {
static private
$_instance;
static $lastMessage;
static $skipFunctionNames;
protected function __construct( ) {
self::$skipFunctionNames = array(
);
} static public function getInstance() {
if ( self::$_instance === null ) {
self::$_instance = new CStatementTracer();
}
return self::$_instance;
}
public function addToSkipFunctionNames( $aName ) {
self::$skipFunctionNames[] = $aName;
}
public function enableTrace() {
$this->writeMessage( '*************** START ********************' );
register_tick_function( array( $this, 'traceCallback' ) );
}
public function disableTrace() {
$this->writeMessage( 'STOP - ' );
unregister_tick_function( array( $this, 'traceCallback' ) );
}
public function traceCallback( ) {
$trace = debug_backtrace();
$latestCall = $trace[ 1 ];
$shortenedArgs = array();
foreach( $latestCall['args'] as $noUseKey => $someArg ) {
if ( is_object( $someArg ) ) {
$someArg = get_class( $someArg ) . '{}';
} elseif ( is_array( $someArg ) ) {
$someArg = print_r( $someArg, TRUE );
} elseif ( is_string( $someArg )) {
$someArg = '"' . $someArg . '"';
}
$someArg = str_replace( "\n", ' ', $someArg );
$someArg = str_replace( "\r", ' ', $someArg );
$someArg = str_replace( " ", ' ', $someArg );
$someArg = str_replace( " ", ' ', $someArg );
$someArg = str_replace( " ", ' ', $someArg );
if ( 80 < strlen( $someArg ) ) {
$someArg = substr( $someArg, 0, 15 ) . '...' . substr( $someArg, -15 );
}
$shortenedArgs[] = $someArg;
}
if ( isset( $latestCall['file'] )) {
$file = explode( '\\', $latestCall[ 'file' ] );
$file = $file[ count( $file ) - 1 ];
if ( '' == $file ) {
$file = '{}';
}
$file .= '/' . $latestCall[ 'line' ];
} else {
$file = '[NpFile]';
}
if ( ( True === in_array( $latestCall['function'], self::$skipFunctionNames ) )
) {
return;
}
$args = implode( ', ', $shortenedArgs );
$newInfo = $file . ': ' . $latestCall['function'] . '( ' . $args . ' )';
$newInfo = str_pad( '', 2 * count( $trace ) ) . $newInfo;
$this->writeMessage( $newInfo );
}
public function writeMessage( $someData ) {
if ( self::$lastMessage == $someData ) {
return;
}
self::$lastMessage = $someData;
$logfilePath = dirname( __FILE__ ) . '/../statementTracer.log';
$fp = fopen( $logfilePath, 'a+' );
if ( is_array( $someData ) ) {
fwrite( $fp, print_r( $someData, TRUE ) . "\n" );
} else {
fwrite( $fp, $someData . "\n" );
}
fclose( $fp );
}
}