PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/dmCorePlugin/lib/debug/dmDebug.php

https://github.com/xdade/diem
PHP | 361 lines | 269 code | 42 blank | 50 comment | 28 complexity | f65acc77645e0d45ba252abe14238c77 MD5 | raw file
  1. <?php
  2. class dmDebug
  3. {
  4. const MAX_DEBUG_LENGTH = 1000000;
  5. public function __construct()
  6. {
  7. self::kill(func_get_args());
  8. }
  9. /**
  10. * Gets a sfTimer instance.
  11. *
  12. * It returns the timer named $name or create a new one if it does not exist.
  13. *
  14. * @param string $name The name of the timer
  15. *
  16. * @return sfTimer The timer instance
  17. */
  18. public static function timer($name)
  19. {
  20. return sfTimerManager::getTimer('[Diem] '.$name);
  21. }
  22. /**
  23. * @return sfTimer if logging is enabled or null if logging is disabled
  24. */
  25. public static function timerOrNull($name)
  26. {
  27. if (sfConfig::get('sf_debug') && sfConfig::get('sf_logging_enabled'))
  28. {
  29. return self::timer($name);
  30. }
  31. return null;
  32. }
  33. /**
  34. * How many time elapsed since request ?
  35. */
  36. public static function getTimeFromStart()
  37. {
  38. return sprintf('%.0f', (microtime(true) - dm::getStartTime()) * 1000);
  39. }
  40. /**
  41. * Builds an string
  42. *
  43. * @param $something Any PHP type
  44. *
  45. * @return string An formatted string
  46. */
  47. protected static function formatAsString($something)
  48. {
  49. if (is_array($something))
  50. {
  51. foreach($something as $key => $val)
  52. {
  53. if (!is_string($val))
  54. {
  55. $something[$key] = print_r($val, true);
  56. }
  57. }
  58. $string = implode(" - ", $something);
  59. }
  60. else
  61. {
  62. $string = (string) $string;
  63. }
  64. $return = substr($string, 0, self::MAX_DEBUG_LENGTH);
  65. if (strlen($string) > self::MAX_DEBUG_LENGTH)
  66. {
  67. $return .= "\n---TRUNCATED---\n";
  68. }
  69. return htmlspecialchars($return);
  70. }
  71. /**
  72. * Logs all parameters with symfony logger
  73. */
  74. public static function log()
  75. {
  76. dmContext::getInstance()->getLogger()->err(self::formatAsString(func_get_args()));
  77. }
  78. /**
  79. * Shows all parameter in the page with a <div>
  80. */
  81. public static function show()
  82. {
  83. return self::debugger(func_get_args(), 2, array('tag' => 'div'));
  84. }
  85. /**
  86. * Shows all parameter in the page with a <pre>
  87. */
  88. public static function showPre()
  89. {
  90. return self::debugger(func_get_args(), 2, array("tag" => "pre"));
  91. }
  92. /**
  93. * Shows all parameter in the page with a <div>
  94. */
  95. public static function traceShow()
  96. {
  97. return self::debugger(func_get_args(), 2, array("tag" => "div"));
  98. }
  99. /**
  100. * Shows all parameter in the page with a <pre>, even if debugging is disabled
  101. */
  102. public static function traceForce()
  103. {
  104. return self::debugger(func_get_args(), 2, array("force" => true));
  105. }
  106. public static function traceString()
  107. {
  108. return self::debugger(func_get_args(), 2, array("to_string" => true));
  109. }
  110. public static function kill()
  111. {
  112. return self::debugger(func_get_args(), 3);
  113. }
  114. public static function killString()
  115. {
  116. return self::debugger(func_get_args(), 3, array("to_string" => true));
  117. }
  118. public static function killForce()
  119. {
  120. return self::debugger(func_get_args(), 3, array("force" => true));
  121. }
  122. public static function showForce()
  123. {
  124. return self::debugger(func_get_args(), 2, array("force" => true, 'tag' => 'pre'));
  125. }
  126. public static function simpleStack($msg = "")
  127. {
  128. $result = "$msg\n";
  129. $trace = debug_backtrace();
  130. foreach ($trace as $element)
  131. {
  132. if ($first)
  133. {
  134. $first = false;
  135. }
  136. else
  137. {
  138. $result .= "File: " . $lastFile . " function: " . (isset($element['function']) ? $element['function'] : '') . " line: " .$lastLine . "\n<br />";
  139. }
  140. $lastFile = isset($element['file']) ? $element['file'] : '';
  141. $lastLine = isset($element['line']) ? $element['line'] : '';
  142. }
  143. echo $result;
  144. }
  145. /**
  146. * Displays a debug backtrace improved with javascript
  147. */
  148. public static function stack($msg = "")
  149. {
  150. if (!dmContext::hasInstance())
  151. {
  152. return self::simpleStack($msg);
  153. }
  154. dmContext::getInstance()->getConfiguration()->loadHelpers(array('Javascript', 'Tag'));
  155. $result = "";
  156. $trace = debug_backtrace();
  157. $traceId = "pkSimpleBacktrace" . dmString::random();
  158. $traceIdShow = $traceId . "Show";
  159. $traceIdHide = $traceId . "Hide";
  160. $result .= "<div class='pkSimpleBacktrace'>stack $msg" .
  161. link_to_function("&gt;&gt;&gt;",
  162. "document.getElementById('$traceId').style.display = 'block'; " .
  163. "document.getElementById('$traceId').style.display = 'block'; " .
  164. "document.getElementById('$traceIdShow').style.display = 'none'; " .
  165. "document.getElementById('$traceIdHide').style.display = 'inline'",
  166. array("id" => $traceIdShow)) .
  167. link_to_function("&lt;&lt;&lt;",
  168. "document.getElementById('$traceId').style.display = 'none'; " .
  169. "document.getElementById('$traceIdHide').style.display = 'none'; " .
  170. "document.getElementById('$traceIdShow').style.display = 'inline'",
  171. array("id" => $traceIdHide, "style" => 'display: none'));
  172. $result .= "</div>";
  173. $result .= "<pre id='$traceId' style='display: none'>\n";
  174. $first = true;
  175. foreach ($trace as $element)
  176. {
  177. if ($first)
  178. {
  179. $first = false;
  180. }
  181. else
  182. {
  183. $result .= "File: " . $lastFile . " function: " . $element['function'] . " line: " .$lastLine . "\n";
  184. }
  185. $lastFile = dmArray::get($element, 'file');
  186. $lastLine = dmArray::get($element, 'line');
  187. }
  188. $result .= "</pre>\n";
  189. echo $result;
  190. }
  191. protected static function debugger($var, $level = 1, $opt = array())
  192. {
  193. $CR = "\n";
  194. $die = ($level > 2);
  195. $opt = dmString::toArray($opt);
  196. if (!sfConfig::get('sf_debug') && !dmArray::get($opt, "force"))
  197. {
  198. return;
  199. }
  200. $tag = dmArray::get($opt, "tag", "pre");
  201. if (dmArray::get($opt, "to_string", false) && is_array($var))
  202. {
  203. array_walk_recursive($var, create_function(
  204. '&$val',
  205. 'if(is_object($val)) {
  206. if (method_exists($val, "toString")) {
  207. $val = get_class($val)." : ".$val->toString();
  208. }
  209. elseif (method_exists($val, "__toString")) {
  210. $val = get_class($val)." : ".$val->__toString();
  211. }
  212. }'
  213. ));
  214. }
  215. elseif(is_array($var))
  216. {
  217. array_walk_recursive($var, create_function(
  218. '&$val',
  219. 'if(is_object($val)) {
  220. if (method_exists($val, "toDebug")) {
  221. $val = get_class($val)." : ".print_r($val->toDebug(), true);
  222. }
  223. elseif (method_exists($val, "toArray")) {
  224. $val = get_class($val)." : ".print_r($val->toArray(), true);
  225. }
  226. }'
  227. ));
  228. }
  229. if(dmConfig::isCli())
  230. {
  231. $debugString = print_r($var, true);
  232. $debugString = substr($debugString, 0, self::MAX_DEBUG_LENGTH);
  233. echo $debugString;
  234. if (strlen($debugString) > self::MAX_DEBUG_LENGTH)
  235. {
  236. echo "\n---TRUNCATED---\n";
  237. }
  238. if($die) { die; }
  239. }
  240. else
  241. {
  242. array_walk_recursive($var, create_function(
  243. '&$val',
  244. 'if(is_string($val)) { $val = htmlspecialchars($val); }'
  245. ));
  246. if (count($var) == 1)
  247. {
  248. $var = dmArray::first($var);
  249. }
  250. if (dmContext::hasInstance() && $request = dm::getRequest())
  251. {
  252. if ($request->isXmlHttpRequest())
  253. {
  254. echo "\n<$tag>";
  255. $debugString = print_r($var, true);
  256. echo substr($debugString, 0, self::MAX_DEBUG_LENGTH);
  257. if (strlen($debugString) > self::MAX_DEBUG_LENGTH)
  258. {
  259. echo "\n---TRUNCATED---\n";
  260. }
  261. echo "</$tag>\n";
  262. if ($die)
  263. die();
  264. return;
  265. }
  266. }
  267. ob_start();
  268. if ($level > 1)
  269. {
  270. print('<br /><'.$tag.' style="text-align: left; border: 1px solid #aaa; border-left-width: 10px; background-color: #f4F4F4; color: #000; margin: 3px; padding: 3px; font-size: 11px;">');
  271. $debugString = print_r($var, true);
  272. echo substr($debugString, 0, self::MAX_DEBUG_LENGTH);
  273. if (strlen($debugString) > self::MAX_DEBUG_LENGTH)
  274. {
  275. echo "\n---TRUNCATED---\n";
  276. }
  277. print("</$tag>");
  278. }
  279. $buffer = ob_get_clean();
  280. if ($level == 4)
  281. {
  282. ob_start();
  283. echo'<pre>';
  284. debug_print_backtrace();
  285. echo '</pre>';
  286. $dieMsg = ob_get_clean();
  287. }
  288. else
  289. {
  290. $backtrace = debug_backtrace();
  291. $dieMsg =
  292. str_replace(sfConfig::get("sf_root_dir"), "", dmArray::get($backtrace[1], 'file')).
  293. " l.".dmArray::get($backtrace[1], 'line');
  294. // $dieMsg = '<pre>';
  295. // $dieMsg .= isset($backtrace[0]['file']) ? '> file : <b>'.
  296. // $backtrace[1]['file'] .'</b>'. $CR : '';
  297. // $dieMsg .= isset($backtrace[0]['line']) ? '> line : <b>'.
  298. // $backtrace[1]['line'] .'</b>'. $CR : '';
  299. // $dieMsg .= isset($backtrace[1]['class']) ? '> class : <b>'.
  300. // dmArray::get(dmArray::get($backtrace, 2, array()), 'class') .'</b>'. $CR : '';
  301. // $dieMsg .= isset($backtrace[1]['function']) ? '> function : <b>'.
  302. // dmArray::get(dmArray::get($backtrace, 2, array()), 'function') .'</b>'. $CR : '';
  303. // $dieMsg .= '</pre>';
  304. }
  305. if ($level > 1)
  306. {
  307. print($buffer);
  308. if ($die)
  309. die($dieMsg);
  310. else
  311. print($dieMsg);
  312. }
  313. else
  314. {
  315. sfWebDebug::getInstance()->logShortMessage($buffer.$dieMsg);
  316. }
  317. }
  318. }
  319. }