PageRenderTime 63ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/code/ryzom/tools/server/www/webtt/plugins/debug_kit/vendors/debug_kit_debugger.php

https://bitbucket.org/mattraykowski/ryzomcore_demoshard
PHP | 400 lines | 240 code | 11 blank | 149 comment | 39 complexity | c70b067f4742956a3a13249029d343f0 MD5 | raw file
Possible License(s): AGPL-3.0, GPL-3.0, LGPL-2.1
  1. <?php
  2. /**
  3. * DebugKit Debugger class. Extends and enhances core
  4. * debugger. Adds benchmarking and timing functionality.
  5. *
  6. * PHP versions 4 and 5
  7. *
  8. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  9. * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
  10. *
  11. * Licensed under The MIT License
  12. * Redistributions of files must retain the above copyright notice.
  13. *
  14. * @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
  15. * @link http://cakephp.org
  16. * @package debug_kit
  17. * @subpackage debug_kit.vendors
  18. * @since DebugKit 0.1
  19. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  20. **/
  21. App::import('Core', 'Debugger');
  22. App::import('Vendor', 'DebugKit.FireCake');
  23. /**
  24. * Debug Kit Temporary Debugger Class
  25. *
  26. * Provides the future features that are planned. Yet not implemented in the 1.2 code base
  27. *
  28. * This file will not be needed in future version of CakePHP.
  29. */
  30. class DebugKitDebugger extends Debugger {
  31. /**
  32. * Internal benchmarks array
  33. *
  34. * @var array
  35. **/
  36. var $__benchmarks = array();
  37. /**
  38. * Internal memory points array
  39. *
  40. * @var array
  41. **/
  42. var $__memoryPoints = array();
  43. /**
  44. * destruct method
  45. *
  46. * Allow timer info to be displayed if the code dies or is being debugged before rendering the view
  47. * Cheat and use the debug log class for formatting
  48. *
  49. * @return void
  50. * @access private
  51. */
  52. function __destruct() {
  53. $_this =& DebugKitDebugger::getInstance();
  54. if (Configure::read('debug') < 2 || !$_this->__benchmarks) {
  55. return;
  56. }
  57. $timers = array_values(DebugKitDebugger::getTimers());
  58. $end = end($timers);
  59. echo '<table class="cake-sql-log"><tbody>';
  60. echo '<caption>Debug timer info</caption>';
  61. echo '<tr><th>Message</th><th>Start Time (ms)</th><th>End Time (ms)</th><th>Duration (ms)</th></tr>';
  62. $i = 0;
  63. foreach ($timers as $timer) {
  64. $indent = 0;
  65. for ($j = 0; $j < $i; $j++) {
  66. if (($timers[$j]['end']) > ($timer['start']) && ($timers[$j]['end']) > ($timer['end'])) {
  67. $indent++;
  68. }
  69. }
  70. $indent = str_repeat(' Âť ', $indent);
  71. extract($timer);
  72. $start = round($start * 1000, 0);
  73. $end = round($end * 1000, 0);
  74. $time = round($time * 1000, 0);
  75. echo "<tr><td>{$indent}$message</td><td>$start</td><td>$end</td><td>$time</td></tr>";
  76. $i++;
  77. }
  78. echo '</tbody></table>';
  79. }
  80. /**
  81. * Start an benchmarking timer.
  82. *
  83. * @param string $name The name of the timer to start.
  84. * @param string $message A message for your timer
  85. * @return bool true
  86. * @static
  87. **/
  88. function startTimer($name = null, $message = null) {
  89. $start = getMicrotime();
  90. $_this =& DebugKitDebugger::getInstance();
  91. if (!$name) {
  92. $named = false;
  93. $calledFrom = debug_backtrace();
  94. $_name = $name = Debugger::trimpath($calledFrom[0]['file']) . ' line ' . $calledFrom[0]['line'];
  95. } else {
  96. $named = true;
  97. }
  98. if (!$message) {
  99. $message = $name;
  100. }
  101. $_name = $name;
  102. $i = 1;
  103. while (isset($_this->__benchmarks[$name])) {
  104. $i++;
  105. $name = $_name . ' #' . $i;
  106. }
  107. if ($i > 1) {
  108. $message .= ' #' . $i;
  109. }
  110. $_this->__benchmarks[$name] = array(
  111. 'start' => $start,
  112. 'message' => $message,
  113. 'named' => $named
  114. );
  115. return true;
  116. }
  117. /**
  118. * Stop a benchmarking timer.
  119. *
  120. * $name should be the same as the $name used in startTimer().
  121. *
  122. * @param string $name The name of the timer to end.
  123. * @access public
  124. * @return boolean true if timer was ended, false if timer was not started.
  125. * @static
  126. */
  127. function stopTimer($name = null) {
  128. $end = getMicrotime();
  129. $_this =& DebugKitDebugger::getInstance();
  130. if (!$name) {
  131. $names = array_reverse(array_keys($_this->__benchmarks));
  132. foreach($names as $name) {
  133. if (!empty($_this->__benchmarks[$name]['end'])) {
  134. continue;
  135. }
  136. if (empty($_this->__benchmarks[$name]['named'])) {
  137. break;
  138. }
  139. }
  140. } else {
  141. $i = 1;
  142. $_name = $name;
  143. while (isset($_this->__benchmarks[$name])) {
  144. if (empty($_this->__benchmarks[$name]['end'])) {
  145. break;
  146. }
  147. $i++;
  148. $name = $_name . ' #' . $i;
  149. }
  150. }
  151. if (!isset($_this->__benchmarks[$name])) {
  152. return false;
  153. }
  154. $_this->__benchmarks[$name]['end'] = $end;
  155. return true;
  156. }
  157. /**
  158. * Get all timers that have been started and stopped.
  159. * Calculates elapsed time for each timer. If clear is true, will delete existing timers
  160. *
  161. * @param bool $clear false
  162. * @return array
  163. * @access public
  164. **/
  165. function getTimers($clear = false) {
  166. $_this =& DebugKitDebugger::getInstance();
  167. $start = DebugKitDebugger::requestStartTime();
  168. $now = getMicrotime();
  169. $times = array();
  170. if (!empty($_this->__benchmarks)) {
  171. $firstTimer = current($_this->__benchmarks);
  172. $_end = $firstTimer['start'];
  173. } else {
  174. $_end = $now;
  175. }
  176. $times['Core Processing (Derived)'] = array(
  177. 'message' => __d('debug_kit', 'Core Processing (Derived)', true),
  178. 'start' => 0,
  179. 'end' => $_end - $start,
  180. 'time' => round($_end - $start, 6),
  181. 'named' => null
  182. );
  183. foreach ($_this->__benchmarks as $name => $timer) {
  184. if (!isset($timer['end'])) {
  185. $timer['end'] = $now;
  186. }
  187. $times[$name] = array_merge($timer, array(
  188. 'start' => $timer['start'] - $start,
  189. 'end' => $timer['end'] - $start,
  190. 'time' => DebugKitDebugger::elapsedTime($name)
  191. ));
  192. }
  193. if ($clear) {
  194. $_this->__benchmarks = array();
  195. }
  196. return $times;
  197. }
  198. /**
  199. * Clear all existing timers
  200. *
  201. * @return bool true
  202. **/
  203. function clearTimers() {
  204. $_this =& DebugKitDebugger::getInstance();
  205. $_this->__benchmarks = array();
  206. return true;
  207. }
  208. /**
  209. * Get the difference in time between the timer start and timer end.
  210. *
  211. * @param $name string the name of the timer you want elapsed time for.
  212. * @param $precision int the number of decimal places to return, defaults to 5.
  213. * @return float number of seconds elapsed for timer name, 0 on missing key
  214. * @static
  215. **/
  216. function elapsedTime($name = 'default', $precision = 5) {
  217. $_this =& DebugKitDebugger::getInstance();
  218. if (!isset($_this->__benchmarks[$name]['start']) || !isset($_this->__benchmarks[$name]['end'])) {
  219. return 0;
  220. }
  221. return round($_this->__benchmarks[$name]['end'] - $_this->__benchmarks[$name]['start'], $precision);
  222. }
  223. /**
  224. * Get the total execution time until this point
  225. *
  226. * @access public
  227. * @return float elapsed time in seconds since script start.
  228. * @static
  229. */
  230. function requestTime() {
  231. $start = DebugKitDebugger::requestStartTime();
  232. $now = getMicroTime();
  233. return ($now - $start);
  234. }
  235. /**
  236. * get the time the current request started.
  237. *
  238. * @access public
  239. * @return float time of request start
  240. * @static
  241. */
  242. function requestStartTime() {
  243. if (defined('TIME_START')) {
  244. $startTime = TIME_START;
  245. } else if (isset($GLOBALS['TIME_START'])) {
  246. $startTime = $GLOBALS['TIME_START'];
  247. } else {
  248. $startTime = env('REQUEST_TIME');
  249. }
  250. return $startTime;
  251. }
  252. /**
  253. * get current memory usage
  254. *
  255. * @return integer number of bytes ram currently in use. 0 if memory_get_usage() is not available.
  256. * @static
  257. **/
  258. function getMemoryUse() {
  259. if (!function_exists('memory_get_usage')) {
  260. return 0;
  261. }
  262. return memory_get_usage();
  263. }
  264. /**
  265. * Get peak memory use
  266. *
  267. * @return integer peak memory use (in bytes). Returns 0 if memory_get_peak_usage() is not available
  268. * @static
  269. **/
  270. function getPeakMemoryUse() {
  271. if (!function_exists('memory_get_peak_usage')) {
  272. return 0;
  273. }
  274. return memory_get_peak_usage();
  275. }
  276. /**
  277. * Stores a memory point in the internal tracker.
  278. * Takes a optional message name which can be used to identify the memory point.
  279. * If no message is supplied a debug_backtrace will be done to identifty the memory point.
  280. * If you don't have memory_get_xx methods this will not work.
  281. *
  282. * @param string $message Message to identify this memory point.
  283. * @return boolean
  284. **/
  285. function setMemoryPoint($message = null) {
  286. $memoryUse = DebugKitDebugger::getMemoryUse();
  287. if (!$message) {
  288. $named = false;
  289. $trace = debug_backtrace();
  290. $message = Debugger::trimpath($trace[0]['file']) . ' line ' . $trace[0]['line'];
  291. }
  292. $self =& DebugKitDebugger::getInstance();
  293. if (isset($self->__memoryPoints[$message])) {
  294. $originalMessage = $message;
  295. $i = 1;
  296. while (isset($self->__memoryPoints[$message])) {
  297. $i++;
  298. $message = $originalMessage . ' #' . $i;
  299. }
  300. }
  301. $self->__memoryPoints[$message] = $memoryUse;
  302. return true;
  303. }
  304. /**
  305. * Get all the stored memory points
  306. *
  307. * @param boolean $clear Whether you want to clear the memory points as well. Defaults to false.
  308. * @return array Array of memory marks stored so far.
  309. **/
  310. function getMemoryPoints($clear = false) {
  311. $self =& DebugKitDebugger::getInstance();
  312. $marks = $self->__memoryPoints;
  313. if ($clear) {
  314. $self->__memoryPoints = array();
  315. }
  316. return $marks;
  317. }
  318. /**
  319. * Clear out any existing memory points
  320. *
  321. * @return void
  322. **/
  323. function clearMemoryPoints() {
  324. $self =& DebugKitDebugger::getInstance();
  325. $self->__memoryPoints = array();
  326. }
  327. /**
  328. * Handles object conversion to debug string.
  329. *
  330. * @param string $var Object to convert
  331. * @access protected
  332. */
  333. function _output($data = array()) {
  334. extract($data);
  335. if (is_array($level)) {
  336. $error = $level['error'];
  337. $code = $level['code'];
  338. if (isset($level['helpID'])) {
  339. $helpID = $level['helpID'];
  340. } else {
  341. $helpID = '';
  342. }
  343. $description = $level['description'];
  344. $file = $level['file'];
  345. $line = $level['line'];
  346. $context = $level['context'];
  347. $level = $level['level'];
  348. }
  349. $files = $this->trace(array('start' => 2, 'format' => 'points'));
  350. $listing = $this->excerpt($files[0]['file'], $files[0]['line'] - 1, 1);
  351. $trace = $this->trace(array('start' => 2, 'depth' => '20'));
  352. if ($this->_outputFormat == 'fb') {
  353. $kontext = array();
  354. foreach ((array)$context as $var => $value) {
  355. $kontext[] = "\${$var}\t=\t" . $this->exportVar($value, 1);
  356. }
  357. $this->_fireError($error, $code, $description, $file, $line, $trace, $kontext);
  358. } else {
  359. $data = compact(
  360. 'level', 'error', 'code', 'helpID', 'description', 'file', 'path', 'line', 'context'
  361. );
  362. echo parent::_output($data);
  363. }
  364. }
  365. /**
  366. * Create a FirePHP error message
  367. *
  368. * @param string $error Name of error
  369. * @param string $code Code of error
  370. * @param string $description Description of error
  371. * @param string $file File error occured in
  372. * @param string $line Line error occured on
  373. * @param string $trace Stack trace at time of error
  374. * @param string $context context of error
  375. * @return void
  376. * @access protected
  377. */
  378. function _fireError($error, $code, $description, $file, $line, $trace, $context) {
  379. $name = $error . ' - ' . $description;
  380. $message = "$error $code $description on line: $line in file: $file";
  381. FireCake::group($name);
  382. FireCake::error($message, $name);
  383. FireCake::log($context, 'Context');
  384. FireCake::log($trace, 'Trace');
  385. FireCake::groupEnd();
  386. }
  387. }
  388. Debugger::invoke(DebugKitDebugger::getInstance('DebugKitDebugger'));
  389. Debugger::getInstance('DebugKitDebugger');