Base for a static organization website

DebugKitDebugger.php 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * Redistributions of files must retain the above copyright notice.
  8. *
  9. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  10. * @link http://cakephp.org CakePHP(tm) Project
  11. * @since DebugKit 0.1
  12. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  13. */
  14. App::uses('Debugger', 'Utility');
  15. App::uses('FireCake', 'DebugKit.Lib');
  16. App::uses('DebugTimer', 'DebugKit.Lib');
  17. App::uses('DebugMemory', 'DebugKit.Lib');
  18. /**
  19. * DebugKit Temporary Debugger Class
  20. *
  21. * Provides the future features that are planned. Yet not implemented in the 1.2 code base
  22. *
  23. * This file will not be needed in future version of CakePHP.
  24. *
  25. * @since DebugKit 0.1
  26. */
  27. class DebugKitDebugger extends Debugger {
  28. /**
  29. * destruct method
  30. *
  31. * Allow timer info to be displayed if the code dies or is being debugged before rendering the view
  32. * Cheat and use the debug log class for formatting
  33. *
  34. * @return void
  35. */
  36. public function __destruct() {
  37. $timers = DebugTimer::getAll();
  38. if (Configure::read('debug') < 2 || count($timers) > 0) {
  39. return;
  40. }
  41. $timers = array_values($timers);
  42. $end = end($timers);
  43. echo '<table class="cake-sql-log"><tbody>';
  44. echo '<caption>Debug timer info</caption>';
  45. echo '<tr><th>Message</th><th>Start Time (ms)</th><th>End Time (ms)</th><th>Duration (ms)</th></tr>';
  46. $i = 0;
  47. foreach ($timers as $timer) {
  48. $indent = 0;
  49. for ($j = 0; $j < $i; $j++) {
  50. if (($timers[$j]['end']) > ($timer['start']) && ($timers[$j]['end']) > ($timer['end'])) {
  51. $indent++;
  52. }
  53. }
  54. $indent = str_repeat(' &raquo; ', $indent);
  55. extract($timer);
  56. $start = round($start * 1000, 0);
  57. $end = round($end * 1000, 0);
  58. $time = round($time * 1000, 0);
  59. echo "<tr><td>{$indent}$message</td><td>$start</td><td>$end</td><td>$time</td></tr>";
  60. $i++;
  61. }
  62. echo '</tbody></table>';
  63. }
  64. /**
  65. * Start an benchmarking timer.
  66. *
  67. * @param string $name The name of the timer to start.
  68. * @param string $message A message for your timer
  69. * @return boolean true
  70. * @deprecated use DebugTimer::start()
  71. */
  72. public static function startTimer($name = null, $message = null) {
  73. return DebugTimer::start($name, $message);
  74. }
  75. /**
  76. * Stop a benchmarking timer.
  77. *
  78. * $name should be the same as the $name used in startTimer().
  79. *
  80. * @param string $name The name of the timer to end.
  81. * @return boolean true if timer was ended, false if timer was not started.
  82. * @deprecated use DebugTimer::stop()
  83. */
  84. public static function stopTimer($name = null) {
  85. return DebugTimer::stop($name);
  86. }
  87. /**
  88. * Get all timers that have been started and stopped.
  89. * Calculates elapsed time for each timer. If clear is true, will delete existing timers
  90. *
  91. * @param boolean $clear false
  92. * @return array
  93. * @deprecated use DebugTimer::getAll()
  94. */
  95. public static function getTimers($clear = false) {
  96. return DebugTimer::getAll($clear);
  97. }
  98. /**
  99. * Clear all existing timers
  100. *
  101. * @return boolean true
  102. * @deprecated use DebugTimer::clear()
  103. */
  104. public static function clearTimers() {
  105. return DebugTimer::clear();
  106. }
  107. /**
  108. * Get the difference in time between the timer start and timer end.
  109. *
  110. * @param $name string the name of the timer you want elapsed time for.
  111. * @param $precision int the number of decimal places to return, defaults to 5.
  112. * @return float number of seconds elapsed for timer name, 0 on missing key
  113. * @deprecated use DebugTimer::elapsedTime()
  114. */
  115. public static function elapsedTime($name = 'default', $precision = 5) {
  116. return DebugTimer::elapsedTime($name, $precision);
  117. }
  118. /**
  119. * Get the total execution time until this point
  120. *
  121. * @return float elapsed time in seconds since script start.
  122. * @deprecated use DebugTimer::requestTime()
  123. */
  124. public static function requestTime() {
  125. return DebugTimer::requestTime();
  126. }
  127. /**
  128. * get the time the current request started.
  129. *
  130. * @return float time of request start
  131. * @deprecated use DebugTimer::requestStartTime()
  132. */
  133. public static function requestStartTime() {
  134. return DebugTimer::requestStartTime();
  135. }
  136. /**
  137. * get current memory usage
  138. *
  139. * @return integer number of bytes ram currently in use. 0 if memory_get_usage() is not available.
  140. * @deprecated Use DebugMemory::getCurrent() instead.
  141. */
  142. public static function getMemoryUse() {
  143. return DebugMemory::getCurrent();
  144. }
  145. /**
  146. * Get peak memory use
  147. *
  148. * @return integer peak memory use (in bytes). Returns 0 if memory_get_peak_usage() is not available
  149. * @deprecated Use DebugMemory::getPeak() instead.
  150. */
  151. public static function getPeakMemoryUse() {
  152. return DebugMemory::getPeak();
  153. }
  154. /**
  155. * Stores a memory point in the internal tracker.
  156. * Takes a optional message name which can be used to identify the memory point.
  157. * If no message is supplied a debug_backtrace will be done to identifty the memory point.
  158. * If you don't have memory_get_xx methods this will not work.
  159. *
  160. * @param string $message Message to identify this memory point.
  161. * @return boolean
  162. * @deprecated Use DebugMemory::getAll() instead.
  163. */
  164. public static function setMemoryPoint($message = null) {
  165. return DebugMemory::record($message);
  166. }
  167. /**
  168. * Get all the stored memory points
  169. *
  170. * @param boolean $clear Whether you want to clear the memory points as well. Defaults to false.
  171. * @return array Array of memory marks stored so far.
  172. * @deprecated Use DebugMemory::getAll() instead.
  173. */
  174. public static function getMemoryPoints($clear = false) {
  175. return DebugMemory::getAll($clear);
  176. }
  177. /**
  178. * Clear out any existing memory points
  179. *
  180. * @return void
  181. * @deprecated Use DebugMemory::clear() instead.
  182. */
  183. public static function clearMemoryPoints() {
  184. DebugMemory::clear();
  185. }
  186. /**
  187. * Create a FirePHP error message
  188. *
  189. * @param array $data Data of the error
  190. * @param array $links Links for the error
  191. * @return void
  192. */
  193. public static function fireError($data, $links) {
  194. $name = $data['error'] . ' - ' . $data['description'];
  195. $message = "{$data['error']} {$data['code']} {$data['description']} on line: {$data['line']} in file: {$data['file']}";
  196. FireCake::group($name);
  197. FireCake::error($message, $name);
  198. if (isset($data['context'])) {
  199. FireCake::log($data['context'], 'Context');
  200. }
  201. if (isset($data['trace'])) {
  202. FireCake::log(preg_split('/[\r\n]+/', $data['trace']), 'Trace');
  203. }
  204. FireCake::groupEnd();
  205. }
  206. }
  207. DebugKitDebugger::getInstance('DebugKitDebugger');
  208. Debugger::addFormat('fb', array('callback' => 'DebugKitDebugger::fireError'));