Debug.class.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. <?php
  2. namespace KIF\Debug;
  3. use KIF\Debug\FirePHP;
  4. use KIF\Core\Config;
  5. /**
  6. *
  7. * Enter description here ...
  8. * @author gaoxiaogang
  9. * 用例:
  10. * KIF\Debug\Debug::start();// 打开
  11. * register_shutdown_function(array('KIF\Debug\Debug', 'show'));
  12. */
  13. class Debug
  14. {
  15. /**
  16. * @desc Debug开关,默认为关闭
  17. * @var bool
  18. */
  19. static $open = false ;
  20. /**
  21. * @desc Debug类实例化对象
  22. * @var bool
  23. */
  24. static $instance = false;
  25. /**
  26. * @desc 运行时间显示数组
  27. * @var array
  28. */
  29. static $time_table = array();
  30. /**
  31. * @desc 用户自定义中间变量显示数组
  32. * @var array
  33. */
  34. static $log_table = array();
  35. /**
  36. * @desc 数据库查询执行时间数组
  37. * @var array
  38. */
  39. static $db_table = array();
  40. /**
  41. * @desc 缓存查询执行时间数组
  42. * @var array
  43. */
  44. static $cache_table = array();
  45. /**
  46. *
  47. * 全文检索sphinx查询执行时间数组
  48. * @var array
  49. */
  50. static $sphinx_table = array();
  51. /**
  52. * @desc 表单方式的接口
  53. */
  54. static $form_table = array();
  55. /**
  56. * @desc 起始时间
  57. * @var int
  58. */
  59. static $begin_time;
  60. /**
  61. * @name __construct
  62. * @desc 构造函数
  63. */
  64. protected function __construct()
  65. {
  66. }
  67. /**
  68. * @name start
  69. * @desc 启动debug类
  70. * @return null
  71. */
  72. static public function start()
  73. {
  74. self::$open = true;
  75. self::$begin_time = microtime();
  76. self::$time_table = array(array('Description', 'Time', 'Caller'));
  77. self::$log_table = array(array('Label', 'Results', 'Caller'));
  78. }
  79. /**
  80. * @name getTime
  81. * @desc 获得从起始时间到目前为止所花费的时间
  82. * @return int
  83. */
  84. static public function getTime()
  85. {
  86. if(false === self::$open)
  87. {
  88. return ;
  89. }
  90. list($pusec, $psec) = explode(" ", self::$begin_time);
  91. list($usec, $sec) = explode(" ", microtime());
  92. return ((float)$usec - (float)$pusec) + ((float)$sec - (float)$psec);
  93. }
  94. /**
  95. * @name getInstance
  96. * @desc 返回debug类的实例
  97. * @return object
  98. */
  99. static public function getInstance()
  100. {
  101. if(false === self::$instance)
  102. {
  103. self::$instance = new self();
  104. }
  105. return self::$instance;
  106. }
  107. /**
  108. * @name log
  109. * @desc 记录用户自定义变量
  110. * @param string $label 自定义变量显示名称
  111. * @param mixed $results 自定义变量结果
  112. * @param string $callfile 调用记录用户自定义变量的文件名
  113. * @return null
  114. * @access public
  115. */
  116. static public function log($label, $results = '', $caller = '')
  117. {
  118. if(false === self::$open || (defined('DEBUG_SHOW_LOG') && !DEBUG_SHOW_LOG))
  119. {
  120. return ;
  121. }
  122. array_push(self::$log_table, array($label, $results, $caller));
  123. }
  124. /**
  125. * @name db
  126. * @desc 记录数据库查询操作执行时间
  127. * @param string $ip 数据库IP
  128. * @param int $port 数据库端口
  129. * @param string $sql 执行的SQL语句
  130. * @param float $times 花费时间
  131. * @param mixed $results 查询结果
  132. * @return null
  133. * @access public
  134. */
  135. static public function db($ip, $database ,$sql, $times, $results)
  136. {
  137. if(false === self::$open || (defined('DEBUG_SHOW_DB') && !DEBUG_SHOW_DB))
  138. {
  139. return ;
  140. }
  141. array_push(self::$db_table, array($ip, $database, $times, $sql, $results));
  142. }
  143. /**
  144. * @name cache
  145. * @desc 缓存查询执行时间
  146. * @param array $server 缓存服务器及端口列表
  147. * @param string $key 缓存所使用的key
  148. * @param float $times 花费时间
  149. * @param mixed $results 查询结果
  150. * @return null
  151. * @access public
  152. */
  153. static public function cache($server, $key, $times, $results, $method = null)
  154. {
  155. if(false === self::$open || (defined('DEBUG_SHOW_CACHE') && !DEBUG_SHOW_CACHE))
  156. {
  157. return ;
  158. }
  159. array_push(self::$cache_table, array($server ,$key, $times, $results, $method));
  160. }
  161. /**
  162. * @name 全文检索sphinx
  163. * @desc 全文检索sphinx查询执行时间
  164. * @param array $condition 查询条件
  165. * @param float $times 花费时间
  166. * @param mixed $results 查询结果
  167. * @return null
  168. * @access public
  169. */
  170. static public function sphinx($condition, $times, $results, $method = null)
  171. {
  172. if(false === self::$open || (defined('DEBUG_SHOW_SPHINX') && !DEBUG_SHOW_SPHINX))
  173. {
  174. return ;
  175. }
  176. array_push(self::$sphinx_table, array($condition, $times, $results, $method));
  177. }
  178. /**
  179. * @name time
  180. * @desc 记录程序执行时间
  181. * @param string $desc 描述
  182. * @param mixed $results 结果
  183. * @return null
  184. * @access public
  185. */
  186. static public function time($desc, $caller)
  187. {
  188. if(false === self::$open || (defined('DEBUG_SHOW_TIME') && !DEBUG_SHOW_TIME))
  189. {
  190. return ;
  191. }
  192. array_push(self::$time_table, array($desc, self::getTime(), $caller));
  193. }
  194. /**
  195. * 记录form表单的方式接口请求
  196. * @param label 说明标签
  197. * @param action 表单的请求地址
  198. * @param params 表单的数据项
  199. * @param caller 处理程序
  200. */
  201. static public function form($label, $action, $params = array(),$method='post', $times = 0, $results = '', $caller = __FILE__)
  202. {
  203. if (false === self::$open || (defined('DEBUG_SHOW_FORM') && !DEBUG_SHOW_FORM))
  204. {
  205. return ;
  206. }
  207. $form_html = '<html><head><meta http-equiv="content-type" content="text/html;charset=utf-8" /><title>Debug Form</title></head><body><form action="'.$action.'" method="'.$method.'">';
  208. if ($params)
  209. {
  210. foreach ($params as $k => $v)
  211. {
  212. $form_html .= $k.': <input type="text" name="'.$k.'" value="'.$v.'" /><br/>';
  213. }
  214. }
  215. $form_html .= '<input type="submit" value="submit" /></form></body></html>';
  216. array_push(self::$form_table, array($label, $form_html, $times, $results, $caller));
  217. }
  218. /**
  219. * @name fb
  220. * @desc 调用FirePHP函数
  221. * @return mixed
  222. * @access public
  223. */
  224. static public function fb()
  225. {
  226. $instance = FirePHP::getInstance(true);
  227. $args = func_get_args();
  228. return call_user_func_array(array($instance,'fb'),$args);
  229. }
  230. /**
  231. * @name show
  232. * @desc 显示调试信息
  233. * @todo 目前只实现了在FirePHP中显示结果.需要实现记录LOG日志形式
  234. * @return null
  235. * @access public
  236. */
  237. static public function show()
  238. {
  239. if (!defined('DEBUG_SHOW_LOG') || (defined('DEBUG_SHOW_LOG') && DEBUG_SHOW_LOG))
  240. {
  241. //用户记录变量
  242. self::fb(array('Custom Log Object', self::$log_table), FirePHP::TABLE );
  243. }
  244. if (!defined('DEBUG_SHOW_TIME') || (defined('DEBUG_SHOW_TIME') && DEBUG_SHOW_TIME))
  245. {
  246. //页面执行时间
  247. self::fb(array('This Page Spend Times ' . self::getTime(), self::$time_table), FirePHP::TABLE );
  248. }
  249. /*---------记录至日记文件中------------*/
  250. /*
  251. if(count(self::$log_table) > 1 || count(self::$time_table) > 1)
  252. {
  253. if(isset($_SERVER['TERM']))
  254. {
  255. $string = "PWD:" . $_SERVER['PWD'] . "\n";
  256. $string .= "SCRIPT_NAME:" . $_SERVER['SCRIPT_NAME'] . "\n";
  257. $string .= "ARGV:" . var_export($_SERVER['argv'], true) . "\n";
  258. }else
  259. {
  260. $string = "HTTP_HOST:" . $_SERVER['HTTP_HOST'] . "\n";
  261. $string .= "SCRIPT_NAME:" . $_SERVER['SCRIPT_NAME'] . "\n";
  262. $string .= "QUERY_STRING:" . $_SERVER['QUERY_STRING'] . "\n";
  263. }
  264. $string .= 'This Page Spend Times:' . self::getTime() . "\n";
  265. array_shift(self::$log_table);
  266. array_shift(self::$time_table);
  267. if(!empty(self::$time_table))
  268. {
  269. $string .= "\n";
  270. foreach (self::$time_table as $v)
  271. {
  272. $string .= "|-- ".$v[0]." ".$v[1]." ".$v[2]." --|\n";
  273. }
  274. }
  275. if(!empty(self::$log_table))
  276. {
  277. $string .= "\n";
  278. foreach (self::$log_table as $v)
  279. {
  280. $string .= "|---- ".$v[0]." ".$v[2]." ----|\n";
  281. $string .= var_export($v[1], true) . "\n";
  282. }
  283. }
  284. $filename = "debug_" . date("Ymd") . ".log";
  285. Log::customLog($filename, $string);
  286. }
  287. */
  288. //数据库执行时间
  289. if(count(self::$db_table) > 0)
  290. {
  291. $i = 0 ;
  292. $db_total_times = 0 ;
  293. foreach (self::$db_table as $v)
  294. {
  295. $db_total_times += $v[2];
  296. $i++;
  297. }
  298. array_unshift(self::$db_table, array('IP', 'Database', 'Time', 'SQL Statement','Results'));
  299. self::fb(array($i . ' SQL queries took '.$db_total_times.' seconds', self::$db_table), FirePHP::TABLE );
  300. }
  301. //Cache执行时间
  302. if(count(self::$cache_table) > 0)
  303. {
  304. $i = 0 ;
  305. $cache_total_times = 0 ;
  306. foreach (self::$cache_table as $v)
  307. {
  308. $cache_total_times += $v[2];
  309. $i++;
  310. }
  311. array_unshift(self::$cache_table, array('Server', 'Cache Key', 'Time','Results', 'Method'));
  312. self::fb(array($i.' Cache queries took '.$cache_total_times.' seconds', self::$cache_table), FirePHP::TABLE );
  313. }
  314. //sphinx执行时间
  315. if(count(self::$sphinx_table) > 0)
  316. {
  317. $i = 0 ;
  318. $sphinx_total_times = 0 ;
  319. foreach (self::$sphinx_table as $v)
  320. {
  321. $sphinx_total_times += $v[1];
  322. $i++;
  323. }
  324. array_unshift(self::$sphinx_table, array('Condition', 'Time','Results', 'Method'));
  325. self::fb(array($i.' Sphinx queries took '.$sphinx_total_times.' seconds', self::$sphinx_table), FirePHP::TABLE );
  326. }
  327. //Form执行时间
  328. if(self::$form_table)
  329. {
  330. $i = 0;
  331. $form_total_times = 0;
  332. foreach (self::$form_table as $v)
  333. {
  334. $form_total_times += $v[2];
  335. $i++;
  336. }
  337. array_unshift(self::$form_table, array('Label', 'FormHtml', 'Times', 'Results', 'Caller'));
  338. self::fb(array($i.' Form action request took '.$form_total_times.' seconds', self::$form_table), FirePHP::TABLE );
  339. }
  340. if (!defined('DEBUG_SHOW_UTILITY') || (defined('DEBUG_SHOW_UTILITY') && DEBUG_SHOW_UTILITY))
  341. {
  342. //自定义函数
  343. $functions = get_defined_functions();
  344. //定义的常量
  345. $constants = get_defined_constants(true);
  346. $sessions = isset($_SESSION) ? $_SESSION : array();
  347. self::fb(array('Utility Variables',
  348. array(
  349. array('name', 'values'),
  350. array('GET Variables', $_GET),
  351. array('POST Variables', $_POST),
  352. array('Custom Defined Functions', $functions['user']),
  353. array('Include Files', get_included_files()),
  354. array('Defined Constants', $constants['user']),
  355. array('SESSION Variables', $sessions),
  356. array('SERVER Variables', $_SERVER),
  357. array('appConfigs', Config::all())
  358. )
  359. ), FirePHP::TABLE );
  360. }
  361. }
  362. }