]> git.donarmstrong.com Git - roundcube.git/blob - plugins/debug_logger/runlog/runlog.php
Imported Upstream version 0.3
[roundcube.git] / plugins / debug_logger / runlog / runlog.php
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
12     private $parent_stack = array();
13
14     public $print_to_console = FALSE;
15
16     private $file_handles = array();
17
18     private $indent = 0;
19
20     public $threshold = 0;
21
22     public $tag_count = array();
23
24     public $timestamp = "d-M-Y H:i:s O";
25
26     public $max_line_size = 150;
27
28     private $run_log = array();
29
30     function runlog()
31     {
32         $this->start_time = microtime( TRUE );
33     }
34
35     public function start( $name, $tag = FALSE  )
36     {
37         $this->run_log[] = array( 'type' => 'start',
38                                   'tag' => $tag,
39                                   'index' => count($this->run_log),
40                                   'value' => $name,
41                                   'time' => microtime( TRUE ),
42                                   'parents' => $this->parent_stack,
43                                   'ended' => false,
44                                    );
45         $this->parent_stack[] = $name;
46
47         $this->print_to_console("start: ".$name, $tag, 'start');
48         $this->print_to_file("start: ".$name, $tag, 'start');
49         $this->indent++;
50     }
51
52     public function end()
53     {
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) {
57                 $lastk = $k;
58             }
59         }
60         $start = $this->run_log[$lastk]['time'];
61         $this->run_log[$lastk]['duration'] = microtime( TRUE ) - $start;
62         $this->run_log[$lastk]['ended'] = true;
63
64         $this->run_log[] = array( 'type' => 'end',
65                                   'tag' =>  $this->run_log[$lastk]['tag'],
66                                   'index' => $lastk,
67                                   'value' => $name,
68                                   'time' => microtime( TRUE ),
69                                   'duration' => microtime( TRUE ) - $start,
70                                   'parents' => $this->parent_stack,
71                                    );
72         $this->indent--;
73         if($this->run_log[$lastk]['duration'] >= $this->threshold){ 
74             $tag_report = "";
75             foreach($this->tag_count as $tag=>$count){
76                 $tag_report .= "$tag: $count, ";
77             }
78             if(!empty($tag_report)){
79 //                $tag_report = "\n$tag_report\n";
80             }
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');
84         }
85     }
86
87     public function increase_tag_count($tag){
88             if(!isset($this->tag_count[$tag])){
89                 $this->tag_count[$tag] = 0;
90             }
91             $this->tag_count[$tag]++;
92     }
93
94     public function get_text(){
95         $text = "";
96         foreach($this->run_log as $entry){
97            $text .= str_repeat("   ",count($entry['parents']));
98            if($entry['tag'] != 'text'){
99             $text .= $entry['tag'].': ';
100            }
101            $text .= $entry['value'];
102
103            if($entry['tag'] == 'end'){
104             $text .= sprintf(" - %0.4f seconds", $entry['duration'] ); 
105            }
106
107            $text .= "\n"; 
108         }
109         return $text;
110     }
111
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);
117             }
118         }
119     }
120
121     public function note( $msg, $tag = FALSE )
122     {
123         if($tag){
124             $this->increase_tag_count($tag);
125         }
126         if ( is_array( $msg )) {
127             $msg = '<pre>' . print_r( $msg, TRUE ) . '</pre>';
128         }
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,
135              );
136
137        $this->print_to_file($msg, $tag);
138        $this->print_to_console($msg, $tag);
139
140     }
141
142     public function print_to_file($msg, $tag = FALSE, $type = FALSE){
143        if(!$tag){
144         $file_handle_tag = 'master';
145        }
146        else{
147             $file_handle_tag = $tag;
148        }
149        if($file_handle_tag != 'master' && isset($this->file_handles[$file_handle_tag])){
150            $buffer = $this->get_indent();
151            $buffer .= "$msg\n";
152            if(!empty($this->timestamp)){
153                 $buffer = sprintf("[%s] %s",date($this->timestamp, mktime()), $buffer);
154            }
155            fwrite($this->file_handles[$file_handle_tag], wordwrap($buffer, $this->max_line_size, "\n     "));
156         }
157        if(isset($this->file_handles['master']) && $this->file_handles['master']){
158            $buffer = $this->get_indent();
159            if($tag){
160             $buffer .= "$tag: ";
161            }
162            $msg = str_replace("\n","",$msg);
163            $buffer .= "$msg";
164            if(!empty($this->timestamp)){
165                 $buffer = sprintf("[%s] %s",date($this->timestamp, mktime()), $buffer);
166            }
167            if(strlen($buffer) > $this->max_line_size){
168                 $buffer = substr($buffer,0,$this->max_line_size - 3)."...";
169            }
170            fwrite($this->file_handles['master'], $buffer."\n");
171        }
172     }
173
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();
179                     if($tag){
180                         echo "$tag: ";
181                     }
182                     echo "$msg\n";
183                 }
184             }
185             else{
186                 echo $this->get_indent();
187                 if($tag){
188                     echo "$tag: ";
189                 }
190                 echo "$msg\n";
191             }
192         }
193     }
194
195     public function print_totals(){
196         $totals = array();
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;
201             }
202         }
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");
206            }
207         }
208     }
209
210     private function get_indent(){
211            $buf = "";
212            for($i = 0; $i < $this->indent; $i++){
213                $buf .= "  "; 
214            }
215            return $buf;
216     }
217
218
219    function  __destruct(){
220        foreach($this->file_handles as $handle){
221             fclose($handle);
222         }
223     }
224
225 }
226
227 ?>