1 <?PHP
2
3 // $Author: Ricardo Cescon $
4 // $Date: 2017-12-22 11:00 +0100 $
5 // $Id: timetrace.lib.php 1894:1f4c988270e1 2017-12-22 11:00 +0100 projects $
6 // $Revision: 1894:1f4c988270e1 $
7 // $Lastlog: stabile version 2.9 $
8
9
10 /*
11
12 The contents of this file are subject to the Mozilla Public License
13 Version 1.1 (the "License"); you may not use this file except in compliance
14 with the License. You may obtain a copy of the License at
15 http://www.mozilla.org/MPL/MPL-1.1.html or see MPL-1.1.txt in directory "license"
16
17 Software distributed under the License is distributed on an "AS IS" basis,
18 WITHOUT WARRANTY OF ANY KIND, either expressed or implied. See the License for
19 the specific language governing rights and limitations under the License.
20
21 The Initial Developers of the Original Code are:
22 Copyright (c) 2003-2017, CR-Solutions (http://www.cr-solutions.net), Ricardo Cescon
23 All Rights Reserved.
24
25 Contributor(s): PanterMedia GmbH (http://www.panthermedia.net), Peter Ammel
26
27 crVCL PHP Framework Version 2.9
28 */
29
30
31
32
33
34 ############################################################
35 if(!defined("TIMETRACE_LIB")){
36 define ("TIMETRACE_LIB", 1);
37 ############################################################
38
39
40 class TimeTrace{
41 // private
42 var $m_logger = null;
43 var $m_stick = array();
44 var $m_enabeld = true;
45
46 // public
47 /**
48 * path for trace file
49 *
50 * @var string
51 */
52 var $m_trace_path = "";
53 /**
54 * filename of tracefile
55 *
56 * @var string
57 */
58 var $m_trace_file = "";
59 /**
60 * size before trace file will set to zero
61 *
62 * @var mixed
63 */
64 var $m_trace_delAfter = "1024K";
65 /**
66 * append info of sriptname and url
67 *
68 * @var bool
69 */
70 var $m_append_script_info = false;
71 /**
72 * append timestamp
73 *
74 * @var bool
75 */
76 var $m_append_timestamp = true;
77 /**
78 * enable critical section for write trace files
79 *
80 * @var string
81 */
82 var $m_trace_critical_section_id = null;
83 /**
84 * path to share the critical section for write trace files
85 *
86 * @var string
87 */
88 var $m_trace_critical_section_path = "./";
89 /**
90 * max lock time for zombie processes
91 *
92 * @var int
93 */
94 var $m_trace_critical_section_maxlock_ms = 1000;
95 /**
96 * sleep for retry enter critical section
97 *
98 * @var int
99 */
100 var $m_trace_critical_section_sleep_ms = 10;
101 /**
102 * write the trace to a SQLite file instead of a text file
103 *
104 * @var bool
105 */
106 var $m_trace_sqlite = false;
107 //-------------------------------------------------------------------------------------------------------------------------------------
108 // constructor
109 /**
110 * time trace
111 *
112 * @param bool $enabeld
113 * @return TimeTrace
114 */
115 function __construct($enabeld=true){
116 $this->m_logger = new Logger();
117 $this->m_enabeld = $enabeld;
118 }
119 //-------------------------------------------------------------------------------------------------------------------------------------
120 function __destruct(){
121 $this->free();
122 }
123 //-------------------------------------------------------------------------------------------------------------------------------------
124 // private
125 /**
126 * @return Logger
127 */
128 function &getLogger(){
129 return $this->m_logger;
130 }
131 //-------------------------------------------------------------------------------------------------------------------------------------
132 function writeTrace($s){
133 $path = $this->m_trace_path;
134 $file = $this->m_trace_file;
135 $del = $this->m_trace_delAfter;
136 $crit_id = $this->m_trace_critical_section_id;
137 $crit_path = $this->m_trace_critical_section_path;
138 $crit_maxlock = $this->m_trace_critical_section_maxlock_ms;
139 $crit_sleep = $this->m_trace_critical_section_sleep_ms;
140
141 if(!empty($path) || !empty($file)){
142 if(empty($file)){
143 $file = "timetrace.lib.trc";
144 }
145 $logger = &$this->getLogger();
146 $logger->setOutputFile($path, $file, $del);
147 $logger->setOutputFile_CriticalSectionId($crit_id, $crit_path,
148 $crit_maxlock, $crit_sleep);
149 $logger->enableOutputFile(true, $this->m_trace_sqlite);
150 $logger->enableHeaderOutput(true);
151 $logger->log(LOG_LEVEL_DEBUG, $s);
152 }
153 }
154 //-------------------------------------------------------------------------------------------------------------------------------------
155 function free(){
156 $this->clearAll();
157 }
158 //-------------------------------------------------------------------------------------------------------------------------------------
159 // public
160 /**
161 * clear all trace counts
162 */
163 function clearAll(){
164 $this->m_stick = array();
165 }
166 //-------------------------------------------------------------------------------------------------------------------------------------
167 /**
168 * clear a trace counts
169 */
170 function clear($id){
171 if(isset($this->m_stick[$id])){
172 unset($this->m_stick[$id]);
173 }
174 }
175 //-------------------------------------------------------------------------------------------------------------------------------------
176 /**
177 * start a trace count
178 *
179 * @param string $id
180 */
181 function start($id){
182 if(!$this->m_enabeld) return;
183 $this->m_stick[$id] = gettickcount();
184 }
185 //-------------------------------------------------------------------------------------------------------------------------------------
186 /**
187 * stop a trace count
188 *
189 * @param string $id
190 * @param string $msg
191 */
192 function stop($id, $msg=""){
193 if(!$this->m_enabeld) return;
194 if(isset($this->m_stick[$id])){
195 $stick = $this->m_stick[$id];
196 $time = gettickcount()-$stick;
197 if(!empty($msg)){
198 $msg = " - ".$msg;
199 }
200 $this->writeTrace($id.$msg." - ".$time." ms");
201 }
202 }
203 //-------------------------------------------------------------------------------------------------------------------------------------
204 /**
205 * similar stop, but reset also the trace count
206 *
207 * @param string $id
208 * @param string $msg
209 */
210 function rstop($id, $msg=""){
211 $this->stop($id, $msg);
212 $this->start($id);
213 }
214 //-------------------------------------------------------------------------------------------------------------------------------------
215 function setSeparator(){
216 if(!$this->m_enabeld) return;
217 $at_old = $this->m_append_timestamp;
218 $asi_old = $this->m_append_script_info;
219
220 $this->m_append_timestamp = false;
221 $this->m_append_script_info = false;
222 $this->writeTrace(str_repeat("#", 50)."\r\n\r\n");
223
224 $this->m_append_timestamp = $at_old;
225 $this->m_append_script_info = $asi_old;
226 }
227 //-------------------------------------------------------------------------------------------------------------------------------------
228 function setHead($msg){
229 if(!$this->m_enabeld) return;
230 $at_old = $this->m_append_timestamp;
231 $asi_old = $this->m_append_script_info;
232
233 $this->m_append_timestamp = false;
234 $this->m_append_script_info = false;
235 $this->writeTrace("- ".$msg." -");
236
237 $this->m_append_timestamp = $at_old;
238 $this->m_append_script_info = $asi_old;
239 }
240 //-------------------------------------------------------------------------------------------------------------------------------------
241 }
242 //-------------------------------------------------------------------------------------------------------------------------------------
243 ############################################################
244 }
245 ############################################################
246 ?>