From 55a223aef45b2f0a7f7fe255a930e19126da96c6 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Wed, 16 Dec 2015 09:51:46 +0800 Subject: [PATCH] =?UTF-8?q?debug=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/debug.php | 27 +++++++ library/think/debug/local.php | 133 ++++++++++++++++++++++++++++++++++ 2 files changed, 160 insertions(+) create mode 100644 library/think/debug/local.php diff --git a/library/think/debug.php b/library/think/debug.php index 4b27f25d..acd0e148 100644 --- a/library/think/debug.php +++ b/library/think/debug.php @@ -17,6 +17,27 @@ class Debug protected static $info = []; protected static $mem = []; + /** + * 操作句柄 + * @var object + * @access protected + */ + protected static $handler = null; + + /** + * 初始化调试驱动 + * @access public + * @param array $options 配置数组 + * @return object + */ + public static function init($options = []) + { + $type = !empty($options['type']) ? $options['type'] : 'Local'; + $class = 'think\\debug\\driver\\' . ucwords($type); + self::$handler = new $class($options); + return self::$handler; + } + /** * 记录时间(微秒)和内存使用情况 * @param string $name 标记位置 @@ -121,4 +142,10 @@ class Debug return $output; } } + + // 静态调用 + public static function __callStatic($method, $params) + { + return call_user_func_array([self::$handler, $method], $params); + } } diff --git a/library/think/debug/local.php b/library/think/debug/local.php new file mode 100644 index 00000000..5f624751 --- /dev/null +++ b/library/think/debug/local.php @@ -0,0 +1,133 @@ + +// +---------------------------------------------------------------------- +namespace think\debug\driver; + +/** + * 本地化调试输出 + */ +class Local +{ + protected $tracePageTabs = array('BASE' => '基本', 'FILE' => '文件', 'INFO' => '流程', 'ERR|NOTIC' => '错误', 'SQL' => 'SQL', 'DEBUG' => '调试'); + + // 获取trace信息 + public static function trace(){ + + } + + // 输出调试信息 + public static function show(){ + + } + + // 记录调试信息 + public static function record(){ + + } + + /** + * 显示页面Trace信息 + * @access private + */ + private function show() + { + // 系统默认显示信息 + $files = get_included_files(); + $info = array(); + foreach ($files as $key => $file) { + $info[] = $file . ' ( ' . number_format(filesize($file) / 1024, 2) . ' KB )'; + } + $trace = array(); + $base = array( + '请求信息' => date('Y-m-d H:i:s', $_SERVER['REQUEST_TIME']) . ' ' . $_SERVER['SERVER_PROTOCOL'] . ' ' . $_SERVER['REQUEST_METHOD'] . ' : ' . __SELF__, + '运行时间' => $this->showTime(), + '吞吐率' => number_format(1 / G('beginTime', 'viewEndTime'), 2) . 'req/s', + '内存开销' => MEMORY_LIMIT_ON ? number_format((memory_get_usage() - $GLOBALS['_startUseMems']) / 1024, 2) . ' kb' : '不支持', + '查询信息' => N('db_query') . ' queries ' . N('db_write') . ' writes ', + '文件加载' => count(get_included_files()), + '缓存信息' => N('cache_read') . ' gets ' . N('cache_write') . ' writes ', + '配置加载' => count(C()), + '会话信息' => 'SESSION_ID=' . session_id(), + ); + // 读取应用定义的Trace文件 + $traceFile = COMMON_PATH . 'Conf/trace.php'; + if (is_file($traceFile)) { + $base = array_merge($base, include $traceFile); + } + $debug = trace(); + $tabs = C('TRACE_PAGE_TABS', null, $this->tracePageTabs); + foreach ($tabs as $name => $title) { + switch (strtoupper($name)) { + case 'BASE': // 基本信息 + $trace[$title] = $base; + break; + case 'FILE': // 文件信息 + $trace[$title] = $info; + break; + default: // 调试信息 + $name = strtoupper($name); + if (strpos($name, '|')) { + // 多组信息 + $names = explode('|', $name); + $result = array(); + foreach ($names as $name) { + $result += isset($debug[$name]) ? $debug[$name] : array(); + } + $trace[$title] = $result; + } else { + $trace[$title] = isset($debug[$name]) ? $debug[$name] : ''; + } + } + } + if ($save = C('PAGE_TRACE_SAVE')) { + // 保存页面Trace日志 + if (is_array($save)) { + // 选择选项卡保存 + $tabs = C('TRACE_PAGE_TABS', null, $this->tracePageTabs); + $array = array(); + foreach ($save as $tab) { + $array[] = $tabs[$tab]; + } + } + $content = date('[ c ]') . ' ' . get_client_ip() . ' ' . $_SERVER['REQUEST_URI'] . "\r\n"; + foreach ($trace as $key => $val) { + if (!isset($array) || in_array_case($key, $array)) { + $content .= '[ ' . $key . " ]\r\n"; + if (is_array($val)) { + foreach ($val as $k => $v) { + $content .= (!is_numeric($k) ? $k . ':' : '') . print_r($v, true) . "\r\n"; + } + } else { + $content .= print_r($val, true) . "\r\n"; + } + $content .= "\r\n"; + } + } + error_log(str_replace('
', "\r\n", $content), 3, C('LOG_PATH') . date('y_m_d') . '_trace.log'); + } + unset($files, $info, $base); + // 调用Trace页面模板 + ob_start(); + include C('TMPL_TRACE_FILE') ? C('TMPL_TRACE_FILE') : THINK_PATH . 'Tpl/page_trace.tpl'; + return ob_get_clean(); + } + + /** + * 获取运行时间 + */ + private function showTime() + { + // 显示运行时间 + G('beginTime', $GLOBALS['_beginTime']); + G('viewEndTime'); + // 显示详细运行时间 + return G('beginTime', 'viewEndTime') . 's ( Load:' . G('beginTime', 'loadTime') . 's Init:' . G('loadTime', 'initTime') . 's Exec:' . G('initTime', 'viewStartTime') . 's Template:' . G('viewStartTime', 'viewEndTime') . 's )'; + } +}