Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [klasa] Debugger
ActivePlayer
post 11.12.2005, 03:00:41
Post #1





Grupa: Przyjaciele php.pl
Postów: 1 224
Pomógł: 40
Dołączył: 6.07.2004
Skąd: Wuppertal

Ostrzeżenie: (0%)
-----


  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
Go to the top of the page
+Quote Post

Posty w temacie
- ActivePlayer   [klasa] Debugger   11.12.2005, 03:00:41
- - Diablos   Eee, do czego to sluzy?   19.12.2005, 22:52:13
- - Tajny   RE: [klasa] Debugger   20.12.2005, 00:24:42


Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 18.07.2025 - 01:37