Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
/**
* Format a backtrace in a way that makes sense to me visually.
* Command-click on filenames in terminal output to jump to them.
*
* @param array $backtrace
* @return string
*/
function backtrace_to_log( array $backtrace ) : string {
$table = [ [ ' file', 'function' ], [ '---------', '--------' ] ];
foreach ( $backtrace as $trace_entry ) {
$table[] = [
$trace_entry['file'] && $trace_entry['line']
? sprintf(
' %s#%d',
str_replace( '/usr/src/app/', '', $trace_entry['file'] ),
$trace_entry['line']
)
: '',
$trace_entry['args']
? sprintf(
"%s%s( %s )",
( $trace_entry['class'] ?? '' ) ? ( $trace_entry['class'] . '->' ) : '',
$trace_entry['function'] ?? '',
implode(
', ',
array_map(
function( $arg ) {
$type = gettype( $arg );
switch ( $type ) {
case 'string':
return sprintf( '"%s"', strlen( $arg ) > 20 ? substr( $arg, 0, 20 ) . '...' : $arg );
case 'integer':
return $type;
case 'NULL':
return 'null';
case 'boolean':
return $arg ? 'true' : 'false';
case 'double':
return round( $arg, 2 );
case 'array':
$count = count( $arg );
if ( $count === 0 ) {
return '[]';
}
return sprintf(
'[%s%s]',
implode( ', ', array_map( 'gettype', array_slice( $arg, 0, 4 ) ) ),
count( $arg ) > 4 ? ', ...' : ''
);
case 'object':
return get_class( $arg );
default:
return gettype( $arg );
}
},
$trace_entry['args']
)
)
)
: '',
];
}
$max_col_1_len = 0;
foreach ( $table as $row ) {
$file_length = strlen( $row[0] );
if ( $file_length > $max_col_1_len ) {
$max_col_1_len = $file_length;
}
}
foreach ( $table as $idx => $row ) {
$file = $row[0];
while ( strlen( $file ) <= $max_col_1_len ) {
$file = $file . ' ';
}
$table[$idx] = $file . $row[1];
}
return "\n\n" . implode( "\n", $table );
}
file function
--------- --------
vendor/humanmade/cavalcade/inc/class-job.php#370 wp_cache_set( "jobs::96a210e8463fc5...", [object], "cavalcade-jobs" )
vendor/humanmade/cavalcade/inc/connector/namespace.php#320 HM\Cavalcade\Plugin\Job->get_jobs_by_query( [NULL, string, array, integer, ...] )
wordpress/wp-includes/class-wp-hook.php#303 HM\Cavalcade\Plugin\Connector\pre_get_scheduled_event( null, "altis.analytics.long...", [], null )
wordpress/wp-includes/plugin.php#189 WP_Hook->apply_filters( null, [NULL, string, array, NULL] )
wordpress/wp-includes/cron.php#748 apply_filters( "pre_get_scheduled_ev...", null, "altis.analytics.long...", [], null )
wordpress/wp-includes/cron.php#809 wp_get_scheduled_event( "altis.analytics.long...", [] )
vendor/altis/aws-analytics/inc/namespace.php#75 wp_next_scheduled( "altis.analytics.long..." )
wordpress/wp-includes/class-wp-hook.php#303 Altis\Analytics\schedule_events( "" )
wordpress/wp-includes/class-wp-hook.php#327 WP_Hook->apply_filters( null, [string] )
wordpress/wp-includes/plugin.php#470 WP_Hook->do_action( [string] )
wordpress/wp-settings.php#578 do_action( "init" )
wp-config.php#152 require_once( "/usr/src/app/wordpre..." )
wordpress/wp-load.php#55 require_once( "/usr/src/app/wp-conf..." )
wordpress/wp-blog-header.php#13 require_once( "/usr/src/app/wordpre..." )
index.php#21 require( "/usr/src/app/wordpre..." )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment