ShowPageTraceBehavior.class.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006-2014 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. namespace Behavior;
  12. use Think\Log;
  13. /**
  14. * 系统行为扩展:页面Trace显示输出
  15. */
  16. class ShowPageTraceBehavior {
  17. protected $tracePageTabs = array('BASE'=>'基本','FILE'=>'文件','INFO'=>'流程','ERR|NOTIC'=>'错误','SQL'=>'SQL','DEBUG'=>'调试');
  18. // 行为扩展的执行入口必须是run
  19. public function run(&$params){
  20. if(!IS_AJAX && !IS_CLI && C('SHOW_PAGE_TRACE')) {
  21. echo $this->showTrace();
  22. }
  23. }
  24. /**
  25. * 显示页面Trace信息
  26. * @access private
  27. */
  28. private function showTrace() {
  29. // 系统默认显示信息
  30. $files = get_included_files();
  31. $info = array();
  32. foreach ($files as $key=>$file){
  33. $info[] = $file.' ( '.number_format(filesize($file)/1024,2).' KB )';
  34. }
  35. $trace = array();
  36. $base = array(
  37. '请求信息' => date('Y-m-d H:i:s',$_SERVER['REQUEST_TIME']).' '.$_SERVER['SERVER_PROTOCOL'].' '.$_SERVER['REQUEST_METHOD'].' : '.__SELF__,
  38. '运行时间' => $this->showTime(),
  39. '吞吐率' => number_format(1/G('beginTime','viewEndTime'),2).'req/s',
  40. '内存开销' => MEMORY_LIMIT_ON?number_format((memory_get_usage() - $GLOBALS['_startUseMems'])/1024,2).' kb':'不支持',
  41. '查询信息' => N('db_query').' queries '.N('db_write').' writes ',
  42. '文件加载' => count(get_included_files()),
  43. '缓存信息' => N('cache_read').' gets '.N('cache_write').' writes ',
  44. '配置加载' => count(C()),
  45. '会话信息' => 'SESSION_ID='.session_id(),
  46. );
  47. // 读取应用定义的Trace文件
  48. $traceFile = COMMON_PATH.'Conf/trace.php';
  49. if(is_file($traceFile)) {
  50. $base = array_merge($base,include $traceFile);
  51. }
  52. $debug = trace();
  53. $tabs = C('TRACE_PAGE_TABS',null,$this->tracePageTabs);
  54. foreach ($tabs as $name=>$title){
  55. switch(strtoupper($name)) {
  56. case 'BASE':// 基本信息
  57. $trace[$title] = $base;
  58. break;
  59. case 'FILE': // 文件信息
  60. $trace[$title] = $info;
  61. break;
  62. default:// 调试信息
  63. $name = strtoupper($name);
  64. if(strpos($name,'|')) {// 多组信息
  65. $names = explode('|',$name);
  66. $result = array();
  67. foreach($names as $name){
  68. $result += isset($debug[$name])?$debug[$name]:array();
  69. }
  70. $trace[$title] = $result;
  71. }else{
  72. $trace[$title] = isset($debug[$name])?$debug[$name]:'';
  73. }
  74. }
  75. }
  76. if($save = C('PAGE_TRACE_SAVE')) { // 保存页面Trace日志
  77. if(is_array($save)) {// 选择选项卡保存
  78. $tabs = C('TRACE_PAGE_TABS',null,$this->tracePageTabs);
  79. $array = array();
  80. foreach ($save as $tab){
  81. $array[] = $tabs[$tab];
  82. }
  83. }
  84. $content = date('[ c ]').' '.get_client_ip().' '.$_SERVER['REQUEST_URI']."\r\n";
  85. foreach ($trace as $key=>$val){
  86. if(!isset($array) || in_array_case($key,$array)) {
  87. $content .= '[ '.$key." ]\r\n";
  88. if(is_array($val)) {
  89. foreach ($val as $k=>$v){
  90. $content .= (!is_numeric($k)?$k.':':'').print_r($v,true)."\r\n";
  91. }
  92. }else{
  93. $content .= print_r($val,true)."\r\n";
  94. }
  95. $content .= "\r\n";
  96. }
  97. }
  98. error_log(str_replace('<br/>',"\r\n",$content), 3,C('LOG_PATH').date('y_m_d').'_trace.log');
  99. }
  100. unset($files,$info,$base);
  101. // 调用Trace页面模板
  102. ob_start();
  103. include C('TMPL_TRACE_FILE')?C('TMPL_TRACE_FILE'):THINK_PATH.'Tpl/page_trace.tpl';
  104. return ob_get_clean();
  105. }
  106. /**
  107. * 获取运行时间
  108. */
  109. private function showTime() {
  110. // 显示运行时间
  111. G('beginTime',$GLOBALS['_beginTime']);
  112. G('viewEndTime');
  113. // 显示详细运行时间
  114. 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 )';
  115. }
  116. }