ENHANCEMENT Added $argCharLimit to SS_Backtrace::full_func_name(), to avoid printing really long strings its set to 10,000 by default

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@114137 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Ingo Schommer 2010-11-24 06:23:50 +00:00
parent 7be01d3d10
commit 07c821c4a7
2 changed files with 33 additions and 2 deletions

View File

@ -75,8 +75,13 @@ class SS_Backtrace {
/**
* Return the full function name. If showArgs is set to true, a string representation of the arguments will be shown
*
* @param Object $item
* @param boolean $showArg
* @param Int $argCharLimit
* @return String
*/
static function full_func_name($item, $showArgs = false) {
static function full_func_name($item, $showArgs = false, $argCharLimit = 10000) {
$funcName = '';
if(isset($item['class'])) $funcName .= $item['class'];
if(isset($item['type'])) $funcName .= $item['type'];
@ -86,7 +91,7 @@ class SS_Backtrace {
$args = array();
foreach($item['args'] as $arg) {
if(!is_object($arg) || method_exists($arg, '__toString')) {
$args[] = (string) $arg;
$args[] = (strlen((string)$arg) > $argCharLimit) ? substr((string)$arg, 0, $argCharLimit) . '...' : (string)$arg;
} else {
$args[] = get_class($arg);
}

View File

@ -0,0 +1,26 @@
<?php
/**
* @package sapphire
* @subpackage tests
*/
class BacktraceTest extends SapphireTest {
function testFullFuncNameWithArgsAndCustomCharLimit() {
$func = array(
'class' => 'MyClass',
'type' => '->',
'file' => 'MyFile.php',
'line' => 99,
'function' => 'myFunction',
'args' => array(
'number' => 1,
'mylongstring' => 'more than 20 characters 1234567890',
)
);
$this->assertEquals(
'MyClass->myFunction(1,more than 20 charact...)',
SS_Backtrace::full_func_name($func, true, 20)
);
}
}