PageRenderTime 26ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Ecart/FirePhp.php

https://code.google.com/p/ecartcommerce/
PHP | 254 lines | 128 code | 27 blank | 99 comment | 20 complexity | 653291fb439a736c19531d99732b97c5 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.1
  1. <?php
  2. /**
  3. * Ecart
  4. *
  5. * This file is part of Ecart.
  6. *
  7. * Ecart is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * Ecart is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with Ecart. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. * @category Ecart
  21. * @package Ecart_Debug
  22. * @copyright Copyright 2008-2009 E-Cart LLC
  23. * @license GNU Public License V3.0
  24. */
  25. /**
  26. *
  27. * @category Ecart
  28. * @package Ecart_Debug
  29. * @author Ecart Core Team <core@ecartcommerce.com>
  30. */
  31. class Ecart_FirePhp
  32. {
  33. /**
  34. * @static
  35. * @var Zend_Log
  36. */
  37. private static $_logger;
  38. protected static $_time;
  39. protected static $_memory = 0;
  40. protected static $_memoryPeak = 0;
  41. protected static $_timer = null;
  42. //protected static $vars = null;
  43. /**
  44. * FirePhp logger
  45. *
  46. * @static
  47. * @return Zend_Log
  48. */
  49. public static function getLogger()
  50. {
  51. if (null === self::$_logger) {
  52. $writer = new Zend_Log_Writer_Firebug();
  53. self::$_logger = new Zend_Log($writer);
  54. }
  55. return self::$_logger;
  56. }
  57. /**
  58. *
  59. * @param mixed
  60. * @static
  61. * @return void
  62. */
  63. public static function dump()
  64. {
  65. $headers = apache_request_headers();
  66. $args = func_get_args();
  67. self::log($args[0]);
  68. print_r($headers['X-Requested-With']);
  69. if ((isset($headers['X_REQUESTED_WITH']) && $headers['X_REQUESTED_WITH'] == 'XMLHttpRequest') ||
  70. isset($headers['X-REQUESTED-WITH']) && $headers['X-REQUESTED-WITH'] == 'XMLHttpRequest' ||
  71. isset($headers['X_Requested_With']) && $headers['X_Requested_With'] == 'XMLHttpRequest' ||
  72. isset($headers['X-Requested-With']) && $headers['X-Requested-With'] == 'XMLHttpRequest' ) {
  73. print_r($args[0]);
  74. return;
  75. }
  76. Zend_Debug::dump($args[0], $args[1]);
  77. }
  78. /**
  79. * Write content to FirePhp log
  80. *
  81. * @static
  82. * @param mixed $content
  83. */
  84. public static function log($content, $extras = null)
  85. {
  86. /*if(empty($content)) {
  87. $content = 'NULL';
  88. }*/
  89. self::getLogger()->log($content, Zend_Log::INFO, $extras);
  90. }
  91. /**
  92. * @static
  93. */
  94. // public static function dump($content)
  95. // {
  96. // self::log($content);
  97. // }
  98. /**
  99. * Return call stack
  100. *
  101. * @static
  102. * @return array
  103. */
  104. public static function callstack()
  105. {
  106. $trace = debug_backtrace();
  107. foreach ($trace as &$ent) {
  108. unset($ent['object']);
  109. unset($ent['args']);
  110. }
  111. unset($trace[0]);
  112. $trace = array_reverse($trace);
  113. self::getLogger()->log($trace, Zend_Log::INFO);
  114. return $trace;
  115. }
  116. /**
  117. * Dump vars
  118. *
  119. * @static
  120. * @param mixed dumped vars
  121. * @param string label
  122. */
  123. public static function debug($dump, $label = null)
  124. {
  125. if (null !== $label) {
  126. $dump = array($label, $dump);
  127. }
  128. self::getLogger()->debug($dump);
  129. }
  130. //@todo
  131. /**
  132. * @static
  133. * @return string
  134. */
  135. protected static function _getPosition()
  136. {
  137. $back = debug_backtrace();
  138. return basename(@$back[0]["file"]) . ':' . @$back[0]["line"];
  139. }
  140. /**
  141. * @copyright http://anton.shevchuk.name/php/debug-zend-framework-application-with-firephp/
  142. * @static
  143. */
  144. public static function timeStamp($comment = "")
  145. {
  146. if (!self::$_timer instanceof Zend_Wildfire_Plugin_FirePhp_TableMessage) {
  147. self::$_timer = new Zend_Wildfire_Plugin_FirePhp_TableMessage('Timer');
  148. self::$_timer->setBuffered(true);
  149. self::$_timer->setHeader(array('Time (sec)', 'Total (sec)', 'Memory (Kb)', 'Total (Kb)', 'Comment', 'File'));
  150. self::$_timer->setOption('includeLineNumbers', false);
  151. }
  152. $back = debug_backtrace();
  153. list ($msec, $sec) = explode(chr(32), microtime());
  154. list ($mTotal, $mSec) = self::getMemoryUsage();
  155. if (! isset(self::$_time)) {
  156. self::$_time["start"] = $sec + $msec;
  157. self::$_time["section"] = $sec + $msec;
  158. self::$_timer->addRow(array(
  159. sprintf("%01.4f", 0),
  160. sprintf("%01.4f", 0),
  161. $mSec,$mTotal,
  162. $comment,
  163. basename(@$back[0]["file"]) . ':' . @$back[0]["line"],
  164. ));
  165. } else {
  166. $start = self::$_time["section"];
  167. self::$_time["section"] = $sec + $msec;
  168. self::$_timer->addRow(array(
  169. sprintf("%01.4f", round(self::$_time["section"] - $start, 4)),
  170. sprintf("%01.4f", round(self::$_time["section"] - self::$_time["start"], 4)),
  171. $mSec,$mTotal,
  172. $comment,
  173. basename(@$back[0]["file"]) . ':' . @$back[0]["line"],
  174. ));
  175. }
  176. self::updateMessageLabel();
  177. Zend_Wildfire_Plugin_FirePhp::getInstance()->send(self::$_timer);
  178. }
  179. /**
  180. * Update the label of the message holding the profile info.
  181. *
  182. * @static
  183. * @return void
  184. */
  185. protected static function updateMessageLabel()
  186. {
  187. self::$_timer->setLabel(sprintf('Timer (%s sec @ %s Kb)', round(self::$_time["section"] - self::$_time["start"], 4), number_format(self::$_memory / 1024, 2, '.', ' ') ));
  188. }
  189. /**
  190. * @static
  191. * @return array
  192. */
  193. protected static function getMemoryUsage()
  194. {
  195. if (! function_exists('memory_get_usage')) {
  196. if (substr(PHP_OS, 0, 3) == 'WIN') {
  197. $output = array();
  198. exec('tasklist /FI "PID eq ' . getmypid() . '" /FO LIST', $output);
  199. $memory = preg_replace('/[\D]/', '', $output[5]) * 1024;
  200. } else {
  201. $pid = getmypid();
  202. exec("ps -eo%mem,rss,pid | grep $pid", $output);
  203. $output = explode(" ", $output[0]);
  204. $memory = @$output[1] * 1024;
  205. }
  206. } else {
  207. $memory = memory_get_usage();
  208. }
  209. $memorySection = $memory - self::$_memory;
  210. $memoryTotal = sprintf("%08s", $memory);
  211. $memorySection = sprintf("%08s", $memorySection);
  212. self::$_memory = $memory;
  213. return array($memoryTotal, $memorySection);
  214. }
  215. /**
  216. *
  217. * @static
  218. * @return <type>
  219. */
  220. protected static function getMemoryPeak()
  221. {
  222. if (function_exists('memory_get_peak_usage')) {
  223. self::$_memoryPeak = sprintf("%07s", memory_get_peak_usage());
  224. }
  225. return self::$_memoryPeak;
  226. }
  227. }