Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
sqltracker.php
1<?php
2
3namespace Bitrix\Main\Diag;
4
6
7class SqlTracker implements \Iterator
8{
10 protected $queries = array();
12 protected $time = 0.0;
14 protected static $depthBackTrace = 0;
16 protected $counter = 0;
18 protected $logFilePath = "";
19
20 public function __construct()
21 {
22 if (self::$depthBackTrace == 0)
23 {
24 $eh = \Bitrix\Main\Config\Configuration::getValue('exception_handling');
25 if (!empty($eh['depth_back_trace']))
26 {
27 self::$depthBackTrace = (int) $eh['depth_back_trace'];
28 }
29 else
30 {
31 self::$depthBackTrace = 8;
32 }
33 }
34 }
35
41 public function reset()
42 {
43 $this->queries = array();
44 $this->time = 0.0;
45 $this->counter = 0;
46 }
47
53 public function getNewTrackerQuery()
54 {
55 $query = new SqlTrackerQuery($this);
56 $this->queries[] = $query;
57 $this->counter++;
58 return $query;
59 }
60
68 public function addTime($time)
69 {
70 $this->time += $time;
71 }
72
78 public function getCounter()
79 {
80 return $this->counter;
81 }
82
88 public function getTime()
89 {
90 return $this->time;
91 }
92
98 public function getQueries()
99 {
100 return $this->queries;
101 }
102
108 public function getDepthBackTrace()
109 {
111 }
112
121 {
122 self::$depthBackTrace = (int) $depthBackTrace;
123 }
124
134 public function startFileLog($filePath)
135 {
136 $this->logFilePath = (string)$filePath;
137 }
138
151 public function writeFileLog($sql, $executionTime = 0.0, $additional = "", $traceSkip = 2)
152 {
153 if ($this->logFilePath)
154 {
155 $application = Application::getInstance();
156 if ($application->isInitialized() && $application->getKernelSession()->isStarted())
157 {
158 $sessionId = $application->getKernelSession()->getId();
159 }
160 else
161 {
162 $sessionId = '-';
163 }
164
165 $header = "TIME: ".round($executionTime, 6)." SESSION: ".$sessionId." ".$additional."\n";
166 $headerLength = mb_strlen($header);
167 $body = $this->formatSql($sql);
168 $trace = $this->formatTrace(Helper::getBackTrace(self::$depthBackTrace, null, $traceSkip));
169 $footer = str_repeat("-", $headerLength);
170 $message =
171 "\n".$header.
172 "\n".$body.
173 "\n\n".$trace.
174 "\n".$footer.
175 "\n";
176 \Bitrix\Main\IO\File::putFileContents($this->logFilePath, $message, \Bitrix\Main\IO\File::APPEND);
177 }
178 }
179
187 public function stopFileLog()
188 {
189 $this->logFilePath = "";
190 }
191
200 protected function formatSql($sql)
201 {
202 $sqlLines = explode("\n", $sql);
203 $skip = true;
204 $tabs = 0;
205 foreach ($sqlLines as $i => $line)
206 {
207 if ($skip)
208 {
209 if (trim($line, "\n\r\t ") == "")
210 {
211 unset($sqlLines[$i]);
212 }
213 else
214 {
215 $skip = false;
216 $tabs = mb_strlen($line) - mb_strlen(ltrim($line, "\t"));
217 }
218 }
219 if ($tabs)
220 {
221 $line = preg_replace("/^[\\t]{1,$tabs}/", "", $line);
222 if ($line !== "")
223 $sqlLines[$i] = $line;
224 else
225 unset($sqlLines[$i]);
226 }
227 }
228 return implode("\n", $sqlLines);
229 }
230
239 protected function formatTrace(array $trace = null)
240 {
241 if ($trace)
242 {
243 $traceLines = array();
244 foreach ($trace as $traceInfo)
245 {
246 $traceLine = '';
247
248 if (array_key_exists('class', $traceInfo))
249 $traceLine .= $traceInfo['class'].$traceInfo['type'];
250
251 if (array_key_exists('function', $traceInfo))
252 $traceLine .= $traceInfo['function'].'()';
253
254 if (array_key_exists('file', $traceInfo))
255 {
256 $traceLine .= ' '.$traceInfo['file'];
257 if (array_key_exists('line', $traceInfo))
258 $traceLine .= ':'.$traceInfo['line'];
259 }
260
261 if ($traceLine)
262 $traceLines[] = ' from '.$traceLine;
263 }
264
265 return implode("\n", $traceLines);
266 }
267 else
268 {
269 return "";
270 }
271 }
272
278 public function rewind()
279 {
280 reset($this->queries);
281 }
282
288 public function current()
289 {
290 return current($this->queries);
291 }
292
298 public function key()
299 {
300 return key($this->queries);
301 }
302
308 public function next()
309 {
310 next($this->queries);
311 }
312
318 public function valid()
319 {
320 return key($this->queries) !== null;
321 }
322}
static getBackTrace($limit=0, $options=null, $skip=1)
Definition helper.php:26
setDepthBackTrace($depthBackTrace)
writeFileLog($sql, $executionTime=0.0, $additional="", $traceSkip=2)
formatTrace(array $trace=null)