Skip to content

Commit ac0053d

Browse files
author
euromark
committed
Added a shorthand stackTrace() method
1 parent 3a70d9c commit ac0053d

File tree

2 files changed

+79
-29
lines changed

2 files changed

+79
-29
lines changed

lib/Cake/Test/Case/BasicsTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
App::uses('Folder', 'Utility');
2222
App::uses('CakeResponse', 'Network');
23+
App::uses('Debugger', 'Utility');
2324

2425
/**
2526
* BasicsTest class
@@ -1140,6 +1141,24 @@ public function testStripslashesDeepSybase() {
11401141
$this->assertEquals($expected, stripslashes_deep($nested));
11411142
}
11421143

1144+
/**
1145+
* Tests that the stackTrace() method is a shortcut for Debugger::trace()
1146+
*
1147+
* @return void
1148+
*/
1149+
public function testStackTrace() {
1150+
ob_start();
1151+
list(, $expected) = array(stackTrace(), Debugger::trace());
1152+
$result = ob_get_clean();
1153+
$this->assertEquals($expected, $result);
1154+
1155+
$opts = array('args' => true);
1156+
ob_start();
1157+
list(, $expected) = array(stackTrace($opts), Debugger::trace($opts));
1158+
$result = ob_get_clean();
1159+
$this->assertEquals($expected, $result);
1160+
}
1161+
11431162
/**
11441163
* test pluginSplit
11451164
*

lib/Cake/basics.php

Lines changed: 60 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -71,51 +71,82 @@ function config() {
7171
* @link http://book.cakephp.org/2.0/en/core-libraries/global-constants-and-functions.html#debug
7272
*/
7373
function debug($var, $showHtml = null, $showFrom = true) {
74-
if (Configure::read('debug') > 0) {
75-
App::uses('Debugger', 'Utility');
76-
$file = '';
77-
$line = '';
78-
$lineInfo = '';
79-
if ($showFrom) {
80-
$trace = Debugger::trace(array('start' => 1, 'depth' => 2, 'format' => 'array'));
81-
$file = str_replace(array(CAKE_CORE_INCLUDE_PATH, ROOT), '', $trace[0]['file']);
82-
$line = $trace[0]['line'];
83-
}
84-
$html = <<<HTML
74+
if (!Configure::read('debug')) {
75+
return;
76+
}
77+
App::uses('Debugger', 'Utility');
78+
79+
$file = '';
80+
$line = '';
81+
$lineInfo = '';
82+
if ($showFrom) {
83+
$trace = Debugger::trace(array('start' => 1, 'depth' => 2, 'format' => 'array'));
84+
$file = str_replace(array(CAKE_CORE_INCLUDE_PATH, ROOT), '', $trace[0]['file']);
85+
$line = $trace[0]['line'];
86+
}
87+
$html = <<<HTML
8588
<div class="cake-debug-output">
8689
%s
8790
<pre class="cake-debug">
8891
%s
8992
</pre>
9093
</div>
9194
HTML;
92-
$text = <<<TEXT
95+
$text = <<<TEXT
9396
%s
9497
########## DEBUG ##########
9598
%s
9699
###########################
97100
98101
TEXT;
99-
$template = $html;
100-
if (php_sapi_name() === 'cli' || $showHtml === false) {
101-
$template = $text;
102-
if ($showFrom) {
103-
$lineInfo = sprintf('%s (line %s)', $file, $line);
104-
}
105-
}
106-
if ($showHtml === null && $template !== $text) {
107-
$showHtml = true;
102+
$template = $html;
103+
if (php_sapi_name() === 'cli' || $showHtml === false) {
104+
$template = $text;
105+
if ($showFrom) {
106+
$lineInfo = sprintf('%s (line %s)', $file, $line);
108107
}
109-
$var = Debugger::exportVar($var, 25);
110-
if ($showHtml) {
111-
$template = $html;
112-
$var = h($var);
113-
if ($showFrom) {
114-
$lineInfo = sprintf('<span><strong>%s</strong> (line <strong>%s</strong>)</span>', $file, $line);
115-
}
108+
}
109+
if ($showHtml === null && $template !== $text) {
110+
$showHtml = true;
111+
}
112+
$var = Debugger::exportVar($var, 25);
113+
if ($showHtml) {
114+
$template = $html;
115+
$var = h($var);
116+
if ($showFrom) {
117+
$lineInfo = sprintf('<span><strong>%s</strong> (line <strong>%s</strong>)</span>', $file, $line);
116118
}
117-
printf($template, $lineInfo, $var);
118119
}
120+
printf($template, $lineInfo, $var);
121+
}
122+
123+
}
124+
125+
if (!function_exists('stackTrace')) {
126+
127+
/**
128+
* Outputs a stack trace based on the supplied options.
129+
*
130+
* ### Options
131+
*
132+
* - `depth` - The number of stack frames to return. Defaults to 999
133+
* - `args` - Should arguments for functions be shown? If true, the arguments for each method call
134+
* will be displayed.
135+
* - `start` - The stack frame to start generating a trace from. Defaults to 1
136+
*
137+
* @param array $options Format for outputting stack trace
138+
* @return mixed Formatted stack trace
139+
* @see Debugger::trace()
140+
*/
141+
function stackTrace(array $options = array()) {
142+
if (!Configure::read('debug')) {
143+
return;
144+
}
145+
App::uses('Debugger', 'Utility');
146+
147+
$options += array('start' => 0);
148+
$options['start']++;
149+
echo Debugger::trace($options);
119150
}
120151

121152
}

0 commit comments

Comments
 (0)