Created
February 7, 2014 20:38
-
-
Save rotexdegba/8871324 to your computer and use it in GitHub Desktop.
This php code snippets generates a backtrace log string of method call stacks along a an inheritance hierarchy. Work in progress.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//////////////////////////////// | |
/// Generate Call Trace Info /// | |
//////////////////////////////// | |
$backtrace = debug_backtrace(~DEBUG_BACKTRACE_PROVIDE_OBJECT); | |
$backtrace_start_index = 1; //index to start reading from in the | |
//array returned by debug_backtrace | |
$class = "[\n"; | |
foreach ( $backtrace as $key => $bactrace_data ) { | |
if ( | |
array_key_exists('class', $bactrace_data) | |
&& $bactrace_data['class'] != __CLASS__ | |
) { | |
$backtrace_start_index = $key; | |
break; | |
} | |
} | |
$num_backtrace_records = count($backtrace); | |
while ($backtrace_start_index < $num_backtrace_records) { | |
if( | |
array_key_exists( 'class', $backtrace[$backtrace_start_index + 1] ) | |
&& is_subclass_of( $backtrace[$backtrace_start_index + 1]['class'], __CLASS__ ) | |
) { | |
$backtrace_start_index++; | |
} else { | |
break; | |
} | |
} | |
$initial_backtrace_start_index = $backtrace_start_index; | |
while ( $backtrace_start_index > 0 ) { | |
$prev_info_index = $backtrace_start_index -1; | |
$curr_info_index = $backtrace_start_index; | |
$line_being_called_from = | |
array_key_exists('line', $backtrace[$prev_info_index])? | |
$backtrace[$prev_info_index]['line'] : 'unknown_line'; | |
$file_being_called_from = | |
array_key_exists('file', $backtrace[$prev_info_index])? | |
$backtrace[$prev_info_index]['file'] : 'unknown_file'; | |
if ( $initial_backtrace_start_index === $backtrace_start_index ) { | |
$calling_function = "On line ({$line_being_called_from}) in " | |
. "\"{$file_being_called_from}\", " | |
. $backtrace[$curr_info_index]['class'] | |
. '::'.$backtrace[$curr_info_index]['function']."(...)"; | |
} else { | |
$calling_function = $backtrace[$curr_info_index]['class'] | |
. '::'.$backtrace[$curr_info_index]['function']."(...)" | |
. " which on line ({$line_being_called_from}) in " | |
. "\"{$file_being_called_from}\" "; | |
} | |
$class .= $calling_function."\t\n called "; | |
$backtrace_start_index--; | |
} | |
$class .= __METHOD__."(...)\n]\n"; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment