6 * @author Ziba Scott <ziba@umich.edu>
10 private $start_time = FALSE;
12 private $parent_stack = array();
14 public $print_to_console = FALSE;
16 private $file_handles = array();
20 public $threshold = 0;
22 public $tag_count = array();
24 public $timestamp = "d-M-Y H:i:s O";
26 public $max_line_size = 150;
28 private $run_log = array();
32 $this->start_time = microtime( TRUE );
35 public function start( $name, $tag = FALSE )
37 $this->run_log[] = array( 'type' => 'start',
39 'index' => count($this->run_log),
41 'time' => microtime( TRUE ),
42 'parents' => $this->parent_stack,
45 $this->parent_stack[] = $name;
47 $this->print_to_console("start: ".$name, $tag, 'start');
48 $this->print_to_file("start: ".$name, $tag, 'start');
54 $name = array_pop( $this->parent_stack );
55 foreach ( $this->run_log as $k => $entry ) {
56 if ( $entry['value'] == $name && $entry['type'] == 'start' && $entry['ended'] == false) {
60 $start = $this->run_log[$lastk]['time'];
61 $this->run_log[$lastk]['duration'] = microtime( TRUE ) - $start;
62 $this->run_log[$lastk]['ended'] = true;
64 $this->run_log[] = array( 'type' => 'end',
65 'tag' => $this->run_log[$lastk]['tag'],
68 'time' => microtime( TRUE ),
69 'duration' => microtime( TRUE ) - $start,
70 'parents' => $this->parent_stack,
73 if($this->run_log[$lastk]['duration'] >= $this->threshold){
75 foreach($this->tag_count as $tag=>$count){
76 $tag_report .= "$tag: $count, ";
78 if(!empty($tag_report)){
79 // $tag_report = "\n$tag_report\n";
81 $end_txt = sprintf("end: $name - %0.4f seconds $tag_report", $this->run_log[$lastk]['duration'] );
82 $this->print_to_console($end_txt, $this->run_log[$lastk]['tag'] , 'end');
83 $this->print_to_file($end_txt, $this->run_log[$lastk]['tag'], 'end');
87 public function increase_tag_count($tag){
88 if(!isset($this->tag_count[$tag])){
89 $this->tag_count[$tag] = 0;
91 $this->tag_count[$tag]++;
94 public function get_text(){
96 foreach($this->run_log as $entry){
97 $text .= str_repeat(" ",count($entry['parents']));
98 if($entry['tag'] != 'text'){
99 $text .= $entry['tag'].': ';
101 $text .= $entry['value'];
103 if($entry['tag'] == 'end'){
104 $text .= sprintf(" - %0.4f seconds", $entry['duration'] );
112 public function set_file($filename, $tag = 'master'){
113 if(!isset($this->file_handle[$tag])){
114 $this->file_handles[$tag] = fopen($filename, 'a');
115 if(!$this->file_handles[$tag]){
116 trigger_error('Could not open file for writing: '.$filename);
121 public function note( $msg, $tag = FALSE )
124 $this->increase_tag_count($tag);
126 if ( is_array( $msg )) {
127 $msg = '<pre>' . print_r( $msg, TRUE ) . '</pre>';
129 $this->debug_messages[] = $msg;
130 $this->run_log[] = array( 'type' => 'note',
131 'tag' => $tag ? $tag:"text",
132 'value' => htmlentities($msg),
133 'time' => microtime( TRUE ),
134 'parents' => $this->parent_stack,
137 $this->print_to_file($msg, $tag);
138 $this->print_to_console($msg, $tag);
142 public function print_to_file($msg, $tag = FALSE, $type = FALSE){
144 $file_handle_tag = 'master';
147 $file_handle_tag = $tag;
149 if($file_handle_tag != 'master' && isset($this->file_handles[$file_handle_tag])){
150 $buffer = $this->get_indent();
152 if(!empty($this->timestamp)){
153 $buffer = sprintf("[%s] %s",date($this->timestamp, mktime()), $buffer);
155 fwrite($this->file_handles[$file_handle_tag], wordwrap($buffer, $this->max_line_size, "\n "));
157 if(isset($this->file_handles['master']) && $this->file_handles['master']){
158 $buffer = $this->get_indent();
162 $msg = str_replace("\n","",$msg);
164 if(!empty($this->timestamp)){
165 $buffer = sprintf("[%s] %s",date($this->timestamp, mktime()), $buffer);
167 if(strlen($buffer) > $this->max_line_size){
168 $buffer = substr($buffer,0,$this->max_line_size - 3)."...";
170 fwrite($this->file_handles['master'], $buffer."\n");
174 public function print_to_console($msg, $tag=FALSE){
175 if($this->print_to_console){
176 if(is_array($this->print_to_console)){
177 if(in_array($tag, $this->print_to_console)){
178 echo $this->get_indent();
186 echo $this->get_indent();
195 public function print_totals(){
197 foreach ( $this->run_log as $k => $entry ) {
198 if ( $entry['type'] == 'start' && $entry['ended'] == true) {
199 $totals[$entry['value']]['duration'] += $entry['duration'];
200 $totals[$entry['value']]['count'] += 1;
203 if($this->file_handle){
204 foreach($totals as $name=>$details){
205 fwrite($this->file_handle,$name.": ".number_format($details['duration'],4)."sec, ".$details['count']." calls \n");
210 private function get_indent(){
212 for($i = 0; $i < $this->indent; $i++){
219 function __destruct(){
220 foreach($this->file_handles as $handle){