Pomoc - Szukaj - U¿ytkownicy - Kalendarz
Pe³na wersja: [klasa] Debugger
Forum PHP.pl > Forum > Gotowe rozwi±zania > Algorytmy, klasy, funkcje
ActivePlayer
  1. <?php
  2. class Debugger{
  3. var $logs = array();
  4. var $start;
  5. var $last;
  6. var $queries = array();
  7. var $toDebug = array();
  8.  
  9. function &getInstance($time = null){
  10. static $instance;
  11. if(!isset($instance)){ 
  12. $instance = new Debugger($time);
  13. $instance->AddLog('Init - (created instance of Debbuger)');
  14. }
  15. return $instance;
  16. }
  17.  
  18. function Debugger($time = null){
  19. if($time == null){
  20. $time = microtime();
  21. }
  22. $this->toDebug = array(
  23. '_POST' => $_POST, 
  24. '_GET' => $_GET,
  25. '_SESSION' => $_SESSION,
  26. '_FILES' => $_FILES,
  27. '_COOKIE' => $_COOKIE,
  28. );
  29. $this->start = $time;
  30. $this->last = $time;
  31. }
  32.  
  33. function AddQuery($query){
  34. $this->queries[] = $query;
  35. }
  36.  
  37. function AddVar(&$var, $name){
  38. $this->toDebug[$name] =& $var;
  39. }
  40.  
  41. function AddLog($Value, $die = false){
  42. $time = microtime();
  43. $add->from_start = number_format(((substr($time,0,9)) + (substr($time,-10)) - (substr($this->start,0,9)) - (substr($this->start,-10))),4);
  44. $add->from_last = number_format(((substr($time,0,9)) + (substr($time,-10)) - (substr($this->last,0,9)) - (substr($this->last,-10))),4);
  45. $add->value = $Value;
  46. $this->logs[] = $add;
  47. $this->last = $time;
  48. if($die == true){
  49. die();
  50. }
  51. return $add;
  52. }
  53.  
  54. function ShowLogs(){
  55.  
  56. $this =& Debugger::getInstance();
  57. $time = microtime();
  58. ?>
  59. <script type="text/javascript">
  60. <!--
  61. function deb_SetCookie(cookieName,cookieValue,nDays) {
  62.  var today = new Date();
  63.  var expire = new Date();
  64.  if (nDays==null || nDays==0) nDays=1;
  65.  expire.setTime(today.getTime() + 3600000*24*nDays);
  66.  document.cookie = cookieName+"="+escape(cookieValue)
  67.  + ";expires="+expire.toGMTString();
  68. }
  69.  
  70.  
  71. function deb_myGetCookie(name)
  72. {
  73. if (document.cookie)
  74. {
  75. var cookies=document.cookie.split(";");
  76. for (var i=0; i<cookies.length; i++)
  77. {
  78. var varName=(cookies[i].split("=")[0]);
  79. var varValue=(cookies[i].split("=")[1]);
  80.  
  81. while (varName.charAt(0)==" ")
  82. varName=varName.substr(1,varName.length);
  83.  
  84. // the escape() function will url encode the value
  85. if (varName==name)
  86. return escape(varValue);
  87. }
  88. }
  89. return 'block';
  90. }
  91. function deb_switch(id){
  92. if(document.getElementById('deb_'+id).style.display == 'none'){
  93. document.getElementById('deb_'+id).style.display = 'block';
  94. deb_SetCookie('deb_state_'+id, 'block');
  95. }
  96. else{
  97. document.getElementById('deb_'+id).style.display = 'none';
  98. deb_SetCookie('deb_state_' + id, 'none');
  99. }
  100. }
  101. -->
  102. </script>
  103. <?
  104. echo '
  105. <div style="width: 780px; margin: auto; clear: both; border: 1px solid black; margin: 1
    0px; font-family: tahoma; font-size: 11px;">'
    ;
  106. foreach ($this->toDebug as $key=>$val){
  107.  echo '
  108.  <div style="font-weight: bold; background-color: #4C71EA; border-bottom: 1px solid #090064; padding: 2px;">
  109. <div style="float: right;"><a style="color: black; font-weight: bold; font-size: 13px;text-decoration: none;" href="#" onclick="deb_switch(''.$key.'');return false;"><div style="width: 50px; background-color: black; text-align: center; color: white; font-weight: normal;">rozwin</div></a></div>
  110. $'.$key.'
  111. </div>
  112. <div id="deb_'.addslashes($key).'"><pre>'.highlight_string(print_r($val,1),1).'</pre></div>';
  113. }
  114. <div style="font-weight: bold; background-color: #4C71EA; border-bottom: 1px solid #090064; border-top: 1px solid #090064; padding: 2px;">
  115. <div style="float: right;"><a style="color: black; font-weight: bold; font-size: 13px;text-decoration: none;" href="#" onclick="deb_switch('queries');return false;"><div style="width: 50px; background-color: black; text-align: center; color: white; font-weight: normal;">rozwin</div></a></div>
  116. Queries</div>
  117. <div id="deb_queries">
  118. <table style="width: 100%; font-family: tahoma; font-size: 11px;">
  119. <tr style="text-align: left;"><th>Query<th>Execution time<th>num_rows<th>affected_rows';
  120. foreach ((array)$this->queries as $key=>$val) {
  121. echo '<tr '.($key%2==0?'style="background-color:#efefef;"':'').'><td>'.$val->query.'<td>'.$val->time.'<td>'.$val->num_rows.'<td>'.$val->affected_rows;
  122. }
  123. echo '</table>
  124. </div>
  125. <div style="font-weight: bold; background-color: #4C71EA; border-bottom: 1px solid #090064; border-top: 1px solid #090064; padding: 2px;">
  126. <div style="float: right;"><a style="color: black; font-weight: bold; font-size: 13px;text-decoration: none;" href="#" onclick="deb_switch('db_msg');return false;"><div style="width: 50px; background-color: black; text-align: center; color: white; font-weight: normal;">rozwin</div></a></div>DEBUGER MESSAGES</div>
  127. <div id="deb_db_msg">
  128. <table style="width: 100%; font-family: tahoma; font-size: 11px;">
  129. <tr style="text-align: left;"><th>From start<th>From last<th>Message
  130. ';
  131. foreach ((array)$this->logs as $key=>$val) {
  132. echo '<tr '.($key%2==0?'style="background-color:#efefef;"':'').'><td>'.$val->from_start.'<td>'.$val->from_last.'<td>'.$val->value;
  133. }
  134. echo '
  135. </table>
  136. </div>
  137. <div style="font-weight: bold; background-color: #4C71EA; border-top: 1px solid #090064; padding: 2px;">Total execution time: '.number_format(((substr($time,0,9)) + (substr($time,-10)) - (substr($this->start,0,9)) - (substr($this->start,-10))),4).'</div>
  138. </div>
  139. ';
  140.  ?>
  141. <script type="text/javascript">
  142. <!--  
  143. document.getElementById('deb_queries').style.display = deb_myGetCookie('deb_state_queries');
  144. document.getElementById('deb_db_msg').style.display = deb_myGetCookie('deb_state_db_msg');
  145. <?
  146. foreach ($this->toDebug as $key=>$val){
  147. echo "document.getElementById('deb_{$key}').style.display = deb_myGetCookie('deb_state_{$key}');";
  148. }
  149. ?>
  150.  
  151. -->
  152. </script>
  153. <?
  154. }
  155.  
  156. function SaveLogs(){
  157. $this =& Debugger::getInstance();
  158.  
  159. if(!defined('LOG_DIR')) { // ze nie wie gdzie zapisywac
  160. return false;
  161. }
  162.  
  163. // write logs todo
  164. echo 'zapisz';
  165.  
  166. }
  167.  
  168. function Destroy(){
  169.  
  170. $this =& Debugger::getInstance();  
  171. $this->AddLog('Application ended');  
  172. if(DEBUG & DT_SHOW){
  173. Debugger::ShowLogs();
  174. }
  175. if(DEBUG & DT_LOG){
  176. Debugger::SaveLogs();
  177. }
  178.  
  179. }
  180. }
  181. ?>


kod oczywiscie nieudokumentowany zbytnio (my style sad.gif) ale mysle ze jak ktos zapatrzy o co w tym biega to moze calkiem 'podej¶æ'.. ShowLogs() do podmiany dla indywidualnych potrzeb...

obiekt $query wysylany przed AddQuery u mnie w db layerze wyglada tak:
  1. <?php
  2.  
  3. $q->query  = $this->query;
  4. $q->time  = number_format(((substr($after,0,9)) + (substr($after,-10)) - (substr($before,0,9)) - (substr($before,-10))),4);
  5. $q->num_rows  = pg_num_rows($res);
  6. $q->affected_rows  = pg_affected_rows($res);
  7.  
  8. ?>


zeby kazdy wiedzial ocobe proponuje pobawic sie guzikiem 'odswie¿' przegl±darki
Diablos
Eee, do czego to sluzy? tongue.gif
Tajny
Witam, raczej skromny debuger,

proponuje ci go poszerzyc o debugbacktrace


  1. <?php
  2.  
  3. function getFunctionName($n,$id){
  4. $backtrace=debug_backtrace();
  5. if ($id=='args') {
  6. $typ=$backtrace[$n][$id];
  7. return gettype($typ);
  8. } else {
  9. $wynik="";
  10. for ($i=0;$i<count($backtrace);$i++){
  11. //pominiecie tej funkcji 
  12. if (@$backtrace[$i][$id]!="getfunctionname"){
  13. if ($id!='line'){
  14. @$wynik.="[".$backtrace[$i][$id].":".basename($backtrace[$i]['file'],$backtrace[$i]['file']).":".$backtrace[$i]['line']."]:::";
  15. } else
  16.  @$wynik.="[".basename($backtrace[$i]['file'],$backtrace[$i]['file']).":".$backtrace[$i]['line']."]:::"; 
  17. }
  18. }
  19. return $wynik; 
  20. }
  21. }
  22.  
  23. function getFunctionFilenameName($n){
  24. $backtrace=debug_backtrace();
  25. $wynik="";
  26. for ($i=0;$i<count($backtrace);$i++){
  27. @$wynik=$wynik.$backtrace[$i]['file'].":";
  28. }
  29. return $wynik;
  30. }
  31.  
  32.  
  33. ?>


a wykorzystanie mniej wiecej tak ,
pisalem na szybko ale powinienes zrozumiec w czym rzecz, jedynie co poprawic to dane debug zapisywac do np sql lite albo gdzies do pliku a nie do tego samego sql'a ( w tym przypadku mysql'a )

pozatym dodac wiecej obslugi bledow i powinna byc bardzo uzyteczna rzecz.


  1. <?php
  2.  
  3. /** * query - wykonuje zapytanie do bazy danych */
  4. function query($query,$conn){
  5. global $session;
  6. addslashes($query);
  7. }
  8.  
  9.  
  10. $mtime = microtime(); 
  11. $mtime = explode(" ",$mtime); 
  12. $mtime = $mtime[1] + $mtime[0]; 
  13. $starttime = $mtime;  //wykonanie //domyslnie status na ok 
  14. $status_query=true;
  15. $result=mysql_query($query,$conn);
  16. if ($result==FALSE) {
  17. $status_query=false;
  18. //sprobuj ponownie
  19. }
  20. $mtime = microtime(); 
  21. $mtime = explode(" ",$mtime); 
  22. $mtime = $mtime[1] + $mtime[0]; 
  23. $endtime = $mtime; 
  24. $totaltime = ($endtime - $starttime); 
  25. //wpisywanie do bazy wynikow
  26. $nazwa_klasy=get_class($this);
  27. $caller_func=getFunctionName(2,'function');
  28. $class=getFunctionName(2,'args');
  29. $filename=basename(getFunctionFilenameName(0),getFunctionFilenameName(0));
  30. $line=getFunctionName(1,'line');
  31. if ($status_query==true) {$opis_query="OK";} else {
  32. $opis_query="ERROR";
  33. $query.="<br><b> ".mysql_error()."</b>";
  34. }
  35. if ($status_query==true) { //jesli za dlugie 
  36. if ($totaltime>MAX_QUERY_TIME) $opis_query="LONG";
  37. }
  38. $query=addslashes($query);
  39. //jezeli tylko dlugie query i ERRORY
  40.  
  41. if (($opis_query=="ERROR")||($opis_query=="LONG")){
  42. $qu="INSERT INTO ".DB_NAME.".debug_query VALUES (0,'$nazwa_klasy','$caller_func','$totaltime','$filename','$line','$query','$opis_query','$session->idu',NOW(),'".$_SERVER["REMOTE_ADDR"]."','".$this->num_active_users ."');";
  43. $a=mysql_query($qu,$conn) or die($qu); }
  44. if ($status_query==true) {
  45. return $result; }  else {
  46. die('Wystapil blad, zapytanie: .'.$query);
  47. }
  48. }
  49.  
  50. ?>


$session->idu akurat trzyma identyfikator zalogowanego uzytkownika
MAX_QUERY_TIME np 0.5 (0.5ms)
$this->num_active_users to ilosc aktywnych uzytkownikow

i przykladowy wpis z bledem to cos takiego
:

  1. <RECORD>
  2.      <IDQUERY_DEBUG>28</IDQUERY_DEBUG>
  3.      <CLASS_NAME>MySQLDB</CLASS_NAME>
  4.      <FUNC_NAME>[getFunctionName:database.php:1500]:::[query:database.php:644]:::[GetNotaSerwiso
    wa:nota_serwisowa.php:17]:::[GetNotaInfo:nota_serwisowa.php:12]:::[nota_serwis:no
    ta_serwisowa.php:8]:::</FUNC_NAME>
  5.      <EXECUTION_TIME>0.00556898117065</EXECUTION_TIME>
  6.      <FILENAME>nota_serwisowa.php:</FILENAME>
  7.      <LINE_NUM>[database.php:1503]:::[database.php:644]:::[nota_serwisowa.php:17]:::[nota_serwi
    sowa.php:12]:::[nota_serwisowa.php:8]:::</LINE_NUM>
  8.      <QUERY>SELECT * from noty_serwisowe WHERE IDNS='1' LIMIT 1<br><b> Unknown column 'IDNS' in 'where clause'</b></QUERY>
  9.      <STATUS>ERROR</STATUS>
  10.      <USER>1</USER>
  11.      <TIME>20051218T12:50:04000</TIME>
  12.      <IP>127.0.0.1</IP>
  13.      <USERS_ONLINE>1</USERS_ONLINE>
  14. </RECORD>


a z wykonaniem zbyt dlugiego zapytania


  1. <RECORD>
  2.      <IDQUERY_DEBUG>8</IDQUERY_DEBUG>
  3.      <CLASS_NAME>MySQLDB</CLASS_NAME>
  4.      <FUNC_NAME>[getFunctionName:database.php:1487]:::[query:session.php:1348]:::[sSearchZleceni
    a:lista_zlecen_grupy.php:116]:::[include:index.php:301]:::</FUNC_NAME>
  5.      <EXECUTION_TIME>0.625414133072</EXECUTION_TIME>
  6.      <FILENAME>index.php:</FILENAME>
  7.      <LINE_NUM>[database.php:1490]:::[session.php:1348]:::[lista_zlecen_grupy.php:116]:::[index
    .php:301]:::</LINE_NUM>
  8.      <QUERY>
  9. SELECT SQL_CALC_FOUND_ROWS *,IF(IDZ_PARENT=0,zlecenia.IDZ,IDZ_PARENT) as IDZ2 ,unix_timestamp(D_IDK) as D_IDK ,ID_NS
  10.            FROM      serwisy_centralne,
  11.                        podzespoly_producent,
  12.                        zlecenia,
  13.                        zlecenia_grupy,
  14.                        users,
  15.                        podzespoly,
  16.                        podzespoly_grupy,
  17.                        podzespoly_grupy2,
  18.                        kontrahenci   LEFT JOIN noty_serwisowe on zlecenia.idz=noty_serwisowe.idz_conn
  19.            WHERE
  20.            zlecenia_grupy.IDU                  =users.IDU and
  21.            zlecenia.IDZG                        =zlecenia_grupy.IDZG and
  22.            kontrahenci.IDK                        =zlecenia_grupy.IDK and
  23.            podzespoly.IDP                        =zlecenia.IDP and
  24.            podzespoly.IDPG2                  =podzespoly_grupy2.IDPG2
  25.            and podzespoly_grupy2.IDPG      =podzespoly_grupy.IDPG and
  26.            podzespoly_producent.IDF      =podzespoly.IDF and
  27.            serwisy_centralne.IDS            =zlecenia.IDS
  28.            
  29.             and lower(IDP_M) like lower("%%")  and lower(S_SN) like lower("%%")  and lower(S_IDK_F) like lower("%%")  and lower(S_IDK_M) like lower("%%")  and lower(S_IDK_DANE) like lower("%%")  and lower(serwisy_centralne.IDS_NAZWA) like lower("%%")   and zlecenia_grupy.idzg=1412  ORDER BY IDZ2 ASC,zlecenia.IDZ   ASC   LIMIT 0,1000;
  30.  
  31. </QUERY>
  32.      <STATUS>LONG</STATUS>
  33.      <USER>1</USER>
  34.      <TIME>20051217T18:16:00000</TIME>
  35.      <IP>127.0.0.1</IP>
  36.      <USERS_ONLINE>1</USERS_ONLINE>
  37. </RECORD>


pozdrawiam
To jest wersja lo-fi g³ównej zawarto¶ci. Aby zobaczyæ pe³n± wersjê z wiêksz± zawarto¶ci±, obrazkami i formatowaniem proszê kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.