Skip to content

Commit

Permalink
Fix debug backtrace error on older PW
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianbj committed Feb 27, 2024
1 parent e6b799e commit bf9d0b2
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 27 deletions.
52 changes: 27 additions & 25 deletions TracyDebugger.module.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public static function getModuleInfo() {
'summary' => __('Tracy debugger from Nette with many PW specific custom tools.', __FILE__),
'author' => 'Adrian Jones',
'href' => 'https://processwire.com/talk/forum/58-tracy-debugger/',
'version' => '4.25.19',
'version' => '4.25.20',
'autoload' => 100000, // in PW 3.0.114+ higher numbers are loaded first - we want Tracy first
'singular' => true,
'requires' => 'ProcessWire>=2.7.2, PHP>=5.4.4',
Expand Down Expand Up @@ -564,31 +564,33 @@ public function init() {
$this->wire()->addHookAfter('PageFinder::getQuery', null, function($event) {
$event->setArgument(2, Debug::timer($this->timerkey));

// add backtrace to allow tracking of caller
$trace_depth = 3;
$trace = Debug::backtrace();

// filter out expected admin sources...
$filtered_trace = array_filter($trace, function($v) {
return
(strpos($v['file'], '/wire/') !== 0) &&
(strpos($v['file'], '/site/assets/cache/') !== 0)
;
});
if(method_exists('\ProcessWire\Debug', 'backtrace')) {
// add backtrace to allow tracking of caller
$trace_depth = 3;
$trace = Debug::backtrace();

// filter out expected admin sources...
$filtered_trace = array_filter($trace, function($v) {
return
(strpos($v['file'], '/wire/') !== 0) &&
(strpos($v['file'], '/site/assets/cache/') !== 0)
;
});

// if there are any paths left in the trace, prepare a shorthand listing...
if (!empty($filtered_trace)) {
$first_idx = array_keys($filtered_trace)[0];
$trace = array_slice($trace, $first_idx, $trace_depth);
$filtered_trace = array_reduce($filtered_trace, function($carry, $v) use (&$first_idx) {
$carry .= "\n[$first_idx] " . $v['file'] . ' ' . $v['call'];
$first_idx++;
return $carry;
}, '');
$event->backtrace = $filtered_trace;
}
else {
$event->backtrace = '';
// if there are any paths left in the trace, prepare a shorthand listing...
if (!empty($filtered_trace)) {
$first_idx = array_keys($filtered_trace)[0];
$trace = array_slice($trace, $first_idx, $trace_depth);
$filtered_trace = array_reduce($filtered_trace, function($carry, $v) use (&$first_idx) {
$carry .= "\n[$first_idx] " . $v['file'] . ' ' . $v['call'];
$first_idx++;
return $carry;
}, '');
$event->backtrace = $filtered_trace;
}
else {
$event->backtrace = '';
}
}

static::$pageFinderQueries[] = $event;
Expand Down
9 changes: 7 additions & 2 deletions panels/DebugModePanel.php
Original file line number Diff line number Diff line change
Expand Up @@ -252,8 +252,13 @@ public function getPanel() {
$selectorQueries = $this->sectionHeader(array('Order', 'Selector', 'Caller', 'SQL Query', 'Settings', 'Time (ms)'));
foreach(\TracyDebugger::$pageFinderQueries as $n => $query) {

$backtrace = $query->backtrace ?? '';
$backtrace = nl2br($this->wire('sanitizer')->entities1($backtrace));
if(method_exists('\ProcessWire\Debug', 'backtrace')) {
$backtrace = $query->backtrace ?? '';
$backtrace = nl2br($this->wire('sanitizer')->entities1($backtrace));
}
else {
$backtrace = '';
}
$caller = $query->arguments[1]['caller'] ?? '';

$selectorQueries_oc++;
Expand Down

0 comments on commit bf9d0b2

Please sign in to comment.