You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

runlog.php 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <?php
  2. /**
  3. * runlog
  4. *
  5. * @author Ziba Scott <ziba@umich.edu>
  6. */
  7. class runlog {
  8. private $start_time = FALSE;
  9. private $parent_stack = array();
  10. private $file_handles = array();
  11. private $indent = 0;
  12. private $run_log = array();
  13. public $print_to_console = FALSE;
  14. public $threshold = 0;
  15. public $tag_count = array();
  16. public $timestamp = "d-M-Y H:i:s O";
  17. public $max_line_size = 150;
  18. function runlog()
  19. {
  20. $this->start_time = microtime(true);
  21. }
  22. public function start($name, $tag = false)
  23. {
  24. $this->run_log[] = array(
  25. 'type' => 'start',
  26. 'tag' => $tag,
  27. 'index' => count($this->run_log),
  28. 'value' => $name,
  29. 'time' => microtime(true),
  30. 'parents' => $this->parent_stack,
  31. 'ended' => false,
  32. );
  33. $this->parent_stack[] = $name;
  34. $this->print_to_console("start: ".$name, $tag, 'start');
  35. $this->print_to_file("start: ".$name, $tag, 'start');
  36. $this->indent++;
  37. }
  38. public function end()
  39. {
  40. $name = array_pop($this->parent_stack);
  41. foreach ($this->run_log as $k => $entry) {
  42. if ($entry['value'] == $name && $entry['type'] == 'start' && !$entry['ended']) {
  43. $lastk = $k;
  44. }
  45. }
  46. $start = $this->run_log[$lastk]['time'];
  47. $this->run_log[$lastk]['duration'] = microtime(true) - $start;
  48. $this->run_log[$lastk]['ended'] = true;
  49. $this->run_log[] = array(
  50. 'type' => 'end',
  51. 'tag' => $this->run_log[$lastk]['tag'],
  52. 'index' => $lastk,
  53. 'value' => $name,
  54. 'time' => microtime(true),
  55. 'duration' => microtime(true) - $start,
  56. 'parents' => $this->parent_stack,
  57. );
  58. $this->indent--;
  59. if ($this->run_log[$lastk]['duration'] >= $this->threshold) {
  60. $tag_report = "";
  61. foreach($this->tag_count as $tag => $count){
  62. $tag_report .= "$tag: $count, ";
  63. }
  64. if (!empty($tag_report)) {
  65. // $tag_report = "\n$tag_report\n";
  66. }
  67. $end_txt = sprintf("end: $name - %0.4f seconds $tag_report", $this->run_log[$lastk]['duration']);
  68. $this->print_to_console($end_txt, $this->run_log[$lastk]['tag'], 'end');
  69. $this->print_to_file($end_txt, $this->run_log[$lastk]['tag'], 'end');
  70. }
  71. }
  72. public function increase_tag_count($tag)
  73. {
  74. if (!isset($this->tag_count[$tag])) {
  75. $this->tag_count[$tag] = 0;
  76. }
  77. $this->tag_count[$tag]++;
  78. }
  79. public function get_text()
  80. {
  81. $text = "";
  82. foreach ($this->run_log as $entry){
  83. $text .= str_repeat(" ",count($entry['parents']));
  84. if ($entry['tag'] != 'text'){
  85. $text .= $entry['tag'].': ';
  86. }
  87. $text .= $entry['value'];
  88. if ($entry['tag'] == 'end') {
  89. $text .= sprintf(" - %0.4f seconds", $entry['duration']);
  90. }
  91. $text .= "\n";
  92. }
  93. return $text;
  94. }
  95. public function set_file($filename, $tag = 'master')
  96. {
  97. if (!isset($this->file_handle[$tag])) {
  98. $this->file_handles[$tag] = fopen($filename, 'a');
  99. if (!$this->file_handles[$tag]) {
  100. trigger_error('Could not open file for writing: '.$filename);
  101. }
  102. }
  103. }
  104. public function note($msg, $tag = false)
  105. {
  106. if ($tag) {
  107. $this->increase_tag_count($tag);
  108. }
  109. if (is_array($msg)) {
  110. $msg = '<pre>' . print_r($msg, true) . '</pre>';
  111. }
  112. $this->debug_messages[] = $msg;
  113. $this->run_log[] = array(
  114. 'type' => 'note',
  115. 'tag' => $tag ?: 'text',
  116. 'value' => htmlentities($msg),
  117. 'time' => microtime(true),
  118. 'parents' => $this->parent_stack,
  119. );
  120. $this->print_to_file($msg, $tag);
  121. $this->print_to_console($msg, $tag);
  122. }
  123. public function print_to_file($msg, $tag = false, $type = false)
  124. {
  125. if (!$tag) {
  126. $file_handle_tag = 'master';
  127. }
  128. else{
  129. $file_handle_tag = $tag;
  130. }
  131. if ($file_handle_tag != 'master' && isset($this->file_handles[$file_handle_tag])) {
  132. $buffer = $this->get_indent();
  133. $buffer .= "$msg\n";
  134. if (!empty($this->timestamp)) {
  135. $buffer = sprintf("[%s] %s",date($this->timestamp, time()), $buffer);
  136. }
  137. fwrite($this->file_handles[$file_handle_tag], wordwrap($buffer, $this->max_line_size, "\n "));
  138. }
  139. if (isset($this->file_handles['master']) && $this->file_handles['master']) {
  140. $buffer = $this->get_indent();
  141. if ($tag) {
  142. $buffer .= "$tag: ";
  143. }
  144. $msg = str_replace("\n","",$msg);
  145. $buffer .= "$msg";
  146. if (!empty($this->timestamp)) {
  147. $buffer = sprintf("[%s] %s",date($this->timestamp, time()), $buffer);
  148. }
  149. if(strlen($buffer) > $this->max_line_size){
  150. $buffer = substr($buffer,0,$this->max_line_size - 3) . "...";
  151. }
  152. fwrite($this->file_handles['master'], $buffer."\n");
  153. }
  154. }
  155. public function print_to_console($msg, $tag = false)
  156. {
  157. if ($this->print_to_console) {
  158. if (is_array($this->print_to_console)) {
  159. if (in_array($tag, $this->print_to_console)) {
  160. echo $this->get_indent();
  161. if ($tag) {
  162. echo "$tag: ";
  163. }
  164. echo "$msg\n";
  165. }
  166. }
  167. else {
  168. echo $this->get_indent();
  169. if ($tag) {
  170. echo "$tag: ";
  171. }
  172. echo "$msg\n";
  173. }
  174. }
  175. }
  176. public function print_totals()
  177. {
  178. $totals = array();
  179. foreach ($this->run_log as $entry) {
  180. if ($entry['type'] == 'start' && $entry['ended']) {
  181. $totals[$entry['value']]['duration'] += $entry['duration'];
  182. $totals[$entry['value']]['count'] += 1;
  183. }
  184. }
  185. if ($this->file_handle) {
  186. foreach ($totals as $name => $details) {
  187. fwrite($this->file_handle,$name.": ".number_format($details['duration'],4)."sec, ".$details['count']." calls \n");
  188. }
  189. }
  190. }
  191. private function get_indent()
  192. {
  193. $buf = "";
  194. for ($i = 0; $i < $this->indent; $i++) {
  195. $buf .= " ";
  196. }
  197. return $buf;
  198. }
  199. function __destruct()
  200. {
  201. foreach ($this->file_handles as $handle) {
  202. fclose($handle);
  203. }
  204. }
  205. }