superedit-DEBUG_S.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. //funciton by Arkadiusz Binder @2010-11-11 ; purpose to debug applications inc code easly
  3. //example DEBUG_S(0,"Brak oczekiwanego pliku do zalaczenia",$DEBUG_ARR/STRING);
  4. //version 2010-12-04
  5. //changelog:
  6. // * 2010-12-05 by plabudda: js(file in head not needed); style
  7. // * 2010-12-06 by plabudda: $FILE,$FUNCTION,$LINE from debug_backtrace(); style
  8. // * 2010-05-25 by plabudda: debug_backtrace() -> Exception->getTrace()
  9. function DEBUG_S($DEBUG_LEVEL,$DESCRIPTION,$DEBUG_STRING = null,$FILE=null,$FUNCTION=null,$LINE=null) {
  10. static $debug_rand_color;
  11. if(isset($_SESSION['DEBUG']))
  12. $SESSION['DEBUG']=$_SESSION['DEBUG']; // narzucamy zmienna session dla celowych wyzwolen DEBUG_S , bez ich koniecznego deklarowania jej jako SESSION , aby tylko raz uzyc
  13. else $SESSION['DEBUG']=null;
  14. if($DEBUG_LEVEL<0) $SESSION['DEBUG']=abs($DEBUG_LEVEL); //jezeli podamy w parametr $DEBUG_LEVEL=-3 , to funkcja dziala tak, jakby zmienna sesji byla na $_SESSION['DEBUG']=3
  15. if (empty($SESSION['DEBUG'])) return;
  16. if ($FILE) $FILE = end(explode('/', $FILE));
  17. if (!is_array($debug_rand_color)) {
  18. $debug_rand_color = array();
  19. for($i=1;$i<2;$i++) {
  20. $debug_rand_color []= "#".dechex(rand(16, 64)).'00'.'00';
  21. $debug_rand_color []= "#".dechex(rand(64, 128)).'00'.'00';
  22. $debug_rand_color []= "#00".dechex(rand(16, 64)).dechex(rand(16, 128));
  23. $debug_rand_color []= "#".dechex(rand(64, 128)).dechex(rand(16, 64)).dechex(rand(16, 128));
  24. $debug_rand_color []= "#".dechex(rand(16, 64)).dechex(rand(64, 128)).dechex(rand(16, 128));
  25. $debug_rand_color []= "#".dechex(rand(64, 128)).dechex(rand(16, 128)).dechex(rand(16, 128));
  26. $debug_rand_color []= "#".dechex(rand(16, 64)).'00'.dechex(rand(64, 128));
  27. $debug_rand_color []= "#".dechex(rand(64, 128)).'00'.dechex(rand(16, 128));
  28. $debug_rand_color []= "#".dechex(rand(16, 64)).dechex(rand(64, 128)).dechex(rand(16, 128));
  29. }
  30. }
  31. if (next($debug_rand_color)); else reset($debug_rand_color);
  32. $color = current($debug_rand_color);
  33. $dbg_trace = '';
  34. /* if ($_SESSION['DEBUG'] > 0) {
  35. try {
  36. throw new Exception('DEBUG_S ...');
  37. } catch (Exception $e) {
  38. $first = true;
  39. $dbg_trace_arr = $e->getTrace();
  40. foreach ($dbg_trace_arr as $v) {
  41. $tmp_dbg_file = end(explode('/',$v['file']));
  42. $dbg_trace .= $tmp_dbg_file.'('.$v['line'].')'.':';
  43. if ($first) {
  44. $dbg_trace .= $DESCRIPTION;
  45. if (!$FILE) $FILE = $tmp_dbg_file;
  46. if (!$LINE) $LINE = $v['line'];
  47. $first = false;
  48. } else {
  49. $dbg_trace .= $v['function'];
  50. }
  51. $dbg_trace .= "\n";
  52. }//end foreach
  53. }
  54. }
  55. */
  56. if ($SESSION['DEBUG'] <= 1) {
  57. echo"\n".'<!-- '."DBG:\n".$dbg_trace.' -->'."\n";
  58. } else {
  59. if ($SESSION['DEBUG'] >= $DEBUG_LEVEL) {
  60. $_SESSION['DEBUG_BOX_ID'] = (isset($_SESSION['DEBUG_BOX_ID']))? $_SESSION['DEBUG_BOX_ID'] + 1 : 1;
  61. $js = "var a=this.nextSibling;if(a.style.display=='none'){a.style.display='';}else{a.style.display='none';}return false;";
  62. echo'<div style="background:#fff;border:1px solid '.$color.';"><a href="#" onclick="'.$js.'" style="display:block;color:'.$color.';font-size:xx-small;">+DEBUG';
  63. echo':';
  64. echo "$FILE($LINE):$FUNCTION: <i>$DESCRIPTION</i>";
  65. echo'</a><div style="display:none;color:'.$color.';font-size:x-small;font-family:monospace;">';
  66. echo str_replace("\n", '<br />', $dbg_trace);
  67. if ($DEBUG_STRING) {
  68. echo'<hr />';
  69. if (is_string($DEBUG_STRING)) {
  70. if($SESSION['DEBUG']==3) echo''.$DEBUG_STRING; //jezeli typ 3 to robimy normalny html
  71. else
  72. echo''.htmlspecialchars($DEBUG_STRING);
  73. } else {
  74. echo'<pre>';
  75. htmlspecialchars(print_r($DEBUG_STRING));
  76. echo'</pre>';
  77. }
  78. }
  79. if($SESSION['DEBUG']>6) {
  80. echo'<hr />';
  81. echo"enviroment:\n".'<br />';
  82. if(isset($_SERVER['REQUEST_URI'])) echo "<b>REQUEST_URI</b>:".$_SERVER['REQUEST_URI']."<br>\n";
  83. if(isset($_SERVER['SCRIPT_NAME'])) echo "<b>SCRIPT_NAME</b>:".$_SERVER['SCRIPT_NAME']."<br>\n";
  84. if(isset($_SERVER['SCRIPT_FILENAME'])) echo "<b>SCRIPT_FILENAME</b>:".$_SERVER['SCRIPT_FILENAME']."<br>\n";
  85. if(isset($_SERVER['REQUEST_METHOD'])) echo "<b>REQUEST_METHOD</b>:".$_SERVER['REQUEST_METHOD']."<br>\n";
  86. if(isset($_SERVER['QUERY_STRING'])) echo "<b>QUERY_STRING</b>:".$_SERVER['QUERY_STRING']."<br>\n";
  87. if(isset($_SERVER['HTTP_HOST'])) echo "<b>HTTP_HOST</b>:".$_SERVER['HTTP_HOST']." ";
  88. if(isset($_SERVER['SERVER_ADDR'])) echo "<b>SERVER_ADDR</b>:".$_SERVER['SERVER_ADDR']." ";
  89. if(isset($_SERVER['SERVER_PORT'])) echo "<b>SERVER_PORT</b>:".$_SERVER['SERVER_PORT']."<br>\n";
  90. }
  91. echo'</div></div>'."\n";
  92. }// if($_SESSION['DEBUG']>=$DEBUG_LEVEL)
  93. }
  94. }