DebugLog.class.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. <?php
  2. /**
  3. * Debug logging class
  4. *
  5. * @author Christopher Han <xiphux@gmail.com>
  6. * @copyright Copyright (c) 2010 Christopher Han
  7. * @package GitPHP
  8. */
  9. class GitPHP_DebugLog implements GitPHP_Observer_Interface
  10. {
  11. /**
  12. * Stores whether logging is enabled
  13. *
  14. * @var boolean
  15. */
  16. protected $enabled = false;
  17. /**
  18. * Stores whether benchmarking is enabled
  19. *
  20. * @var boolean
  21. */
  22. protected $benchmark = false;
  23. /**
  24. * Stores the starting instant
  25. *
  26. * @var float
  27. */
  28. protected $startTime;
  29. /**
  30. * Stores the starting memory
  31. *
  32. * @var int
  33. */
  34. protected $startMem;
  35. /**
  36. * Stores the log entries
  37. *
  38. * @var string[]
  39. */
  40. protected $entries = array();
  41. /**
  42. * Stores the timers
  43. *
  44. * @var float[]
  45. */
  46. protected $timers = array();
  47. /**
  48. * @return GitPHP_DebugLog
  49. */
  50. public static function GetInstance()
  51. {
  52. static $instance;
  53. if (!$instance) $instance = new self();
  54. return $instance;
  55. }
  56. /**
  57. * You must use GetInstance()
  58. */
  59. private function __construct()
  60. {
  61. }
  62. /**
  63. * Constructor
  64. *
  65. * @param boolean $enabled whether log should be enabled
  66. * @param boolean $benchmark whether benchmarking should be enabled
  67. */
  68. public function init($enabled = false, $benchmark = false)
  69. {
  70. $this->startTime = microtime(true);
  71. $this->startMem = memory_get_usage();
  72. $this->enabled = $enabled;
  73. $this->benchmark = $benchmark;
  74. }
  75. /**
  76. * Sets start time
  77. *
  78. * @param float $start starting microtime
  79. */
  80. public function SetStartTime($start)
  81. {
  82. $this->startTime = $start;
  83. }
  84. /**
  85. * Sets start memory
  86. *
  87. * @param integer $start starting memory
  88. */
  89. public function SetStartMemory($start)
  90. {
  91. $this->startMem = $start;
  92. }
  93. /**
  94. * Shortcut to start timer
  95. */
  96. public function TimerStart()
  97. {
  98. if (!$this->benchmark) return;
  99. $this->Log('', '', 'start');
  100. }
  101. /**
  102. * Shortcut to stop timer
  103. *
  104. * @param $msg
  105. * @param $msg_data
  106. */
  107. public function TimerStop($msg, $msg_data = '')
  108. {
  109. if (!$this->benchmark) return;
  110. $this->Log($msg, $msg_data, 'stop');
  111. }
  112. /**
  113. * Log an entry
  114. *
  115. * @param string $msg message to log
  116. */
  117. public function Log($msg, $msg_data = '', $type = 'ts')
  118. {
  119. if (!$this->enabled)
  120. return;
  121. $entry = array();
  122. if ($type == 'start') {
  123. array_push($this->timers, microtime(true));
  124. return;
  125. } else if ($type == 'stop') {
  126. $timer = array_pop($this->timers);
  127. $entry['time'] = $duration = microtime(true) - $timer;
  128. foreach ($this->timers as &$item) $item += $duration;
  129. } else {
  130. if ($this->benchmark) {
  131. $entry['time'] = (microtime(true) - $this->startTime);
  132. $entry['reltime'] = true;
  133. $entry['mem'] = memory_get_usage();
  134. }
  135. }
  136. $entry['name'] = $msg;
  137. $entry['value'] = $msg_data;
  138. $bt = explode("\n", new Exception());
  139. array_shift($bt);
  140. array_shift($bt);
  141. $entry['bt'] = implode("\n", $bt);
  142. $this->entries[] = $entry;
  143. }
  144. /**
  145. * Gets whether logging is enabled
  146. *
  147. * @return boolean true if logging is enabled
  148. */
  149. public function GetEnabled()
  150. {
  151. return $this->enabled;
  152. }
  153. /**
  154. * Sets whether logging is enabled
  155. *
  156. * @param boolean $enable true if logging is enabled
  157. */
  158. public function SetEnabled($enable)
  159. {
  160. $this->enabled = $enable;
  161. }
  162. /**
  163. * Gets whether benchmarking is enabled
  164. *
  165. * @return boolean true if benchmarking is enabled
  166. */
  167. public function GetBenchmark()
  168. {
  169. return $this->benchmark;
  170. }
  171. /**
  172. * Sets whether benchmarking is enabled
  173. *
  174. * @param boolean $bench true if benchmarking is enabled
  175. */
  176. public function SetBenchmark($bench)
  177. {
  178. $this->benchmark = $bench;
  179. }
  180. /**
  181. * Clears the log
  182. */
  183. public function Clear()
  184. {
  185. $this->entries = array();
  186. }
  187. /**
  188. * Gets the log entries
  189. *
  190. * @return array entry data
  191. */
  192. public function GetEntries()
  193. {
  194. return $this->entries;
  195. }
  196. /**
  197. * Notify that observable object changed
  198. *
  199. * @param GitPHP_Observable_Interface $object object
  200. * @param int $changeType type of change
  201. * @param array $args argument array
  202. */
  203. public function ObjectChanged($object, $changeType, $args = array())
  204. {
  205. if ($changeType !== GitPHP_Observer_Interface::LoggableChange)
  206. return;
  207. if (!$this->enabled)
  208. return;
  209. if (!isset($args[0]) || empty($args[0]))
  210. return;
  211. $msg = $args[0];
  212. $msg_data = !empty($args[1]) ? $args[1] : '';
  213. $type = !empty($args[2]) ? $args[2] : 'ts';
  214. $this->Log($msg, $msg_data, $type);
  215. }
  216. }