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