php 数据调试 by debug_backtrace

/**
 * 返回函数操作链表
 *
 * @param int $level
 *        	返回多少级
 * @param int $start
 *        	跳过原来多少级
 * @param boolean $gettree
 *        	是否按树的形式返回
 * @return array
 */
function backtracelist($level = 3, $start = 0, $gettree = false) {
	$backtrace = debug_backtrace ();
	$btlist = array ();
	if ($backtrace) {
		$level = $level + $start;
		foreach ( $backtrace as $i => $bt ) {
			if ($i < $start) {
				continue;
			}
			if ($i >= $level) {
				break;
			}
			$args = '';
			foreach ( $bt ['args'] as $a ) {
				if (! empty ( $args )) {
					$args .= ', ';
				}
				switch (gettype ( $a )) {
					case 'integer' :
					case 'double' :
						$args .= $a;
						break;
					case 'string' :
						$a = htmlspecialchars ( substr ( $a, 0, 64 ) ) . ((strlen ( $a ) > 64) ? '...' : '');
						$args .= ""$a"";
						break;
					case 'array' :
						$args .= 'Array(' . count ( $a ) . ')';
						break;
					case 'object' :
						$args .= 'Object(' . get_class ( $a ) . ')';
						break;
					case 'resource' :
						$args .= 'Resource(' . strstr ( $a, '#' ) . ')';
						break;
					case 'boolean' :
						$args .= $a ? 'True' : 'False';
						break;
					case 'NULL' :
						$args .= 'Null';
						break;
					default :
						$args .= 'Unknown';
				}
			}
			$params = array (
					"line" => $bt ['line'],
					"file" => $bt ['file'],
					"class" => $bt ['class'],
					"type" => $bt ['type'],
					"function" => $bt ['function'],
					"args" => $args,
					"next" => $params 
			);
			$btitem = array (
					'file' => "{$bt['line']} - {$bt['file']}",
					'call' => "{$bt['class']}{$bt['type']}{$bt['function']}($args)" 
			);
			if ($bt ['args']) {
				$btitem ['args'] = $bt ['args'];
			}
			$btlist [] = $btitem;
		}
	}
	if ($gettree) {
		return $params;
	}
	return $btlist;
}

 

 

/**
 * 通过在 $_GET[GDDEBUG_LEVEL]=1
 * 可以动态调整 gddebug 时 backtracelist 打印出来的层数,方便数据调试
 */
defined ( 'GDDEBUG_LEVEL' ) or define ( 'GDDEBUG_LEVEL', 'GDDEBUG_LEVEL' );

 

/**
 *
 * @author caihaibin
 *         数据打印工具,用来打印各种数据进行调试
 *         字符串为 string(1) "a"
 *         数字为 int(1)
 *         NULL 为 NULL
 *         数组为 Array(
 *         'a'=>'test'
 *         )
 *         对像为 Object xxxx
 *        
 *         另补充数据 debug_backtrace 层级调试,可以方便跟踪数据所在位置 ,具体看 backtracelist()
 *         可以通过 $_GET [GDDEBUG_LEVEL] 指定对应显示层级
 *         再将返回结果插入数据库,可以通过数据库进行数据调试
 */
function gddebug() {
	echo "rn<pre>";
	if (isset ( $_GET [GDDEBUG_LEVEL] )) {
		$level = $_GET [GDDEBUG_LEVEL];
	} else {
		$level = 0;
	}
	$btlist = backtracelist ( $level, 1 );
	if (! empty ( $btlist )) {
		echo "backtracelist:rn";
		foreach ( $btlist as &$btitem ) {
			if (isset ( $btitem ['args'] ))
				$btitem ['args'] = preg_replace ( '/n/ms', "n            ", print_r ( $btitem ['args'], true ) );
		}
		echo htmlspecialchars ( print_r ( $btlist, true ) ) . "rn";
	}
	$arrays = func_get_args ();
	echo "args:rn";
	foreach ( $arrays as $value ) {
		if (is_array ( $value ) || is_object ( $value )) {
			echo htmlspecialchars ( print_r ( $value, true ) ) . "rn";
		} elseif (is_string ( $value )) {
			echo "string(" . strlen ( $value ) . ") "" . htmlspecialchars ( $value ) . ""rn";
		} else {
			var_dump ( $value );
		}
	}
	echo "</pre>";
}

 

发表评论

电子邮件地址不会被公开。 必填项已用*标注