Bitrix-D7
23.9
Загрузка...
Поиск...
Не найдено
sqltracker.php
1
<?php
2
3
namespace
Bitrix\Main\Diag
;
4
5
use
Bitrix\Main\Application
;
6
7
class
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
{
110
return
self::$depthBackTrace
;
111
}
112
120
public
function
setDepthBackTrace
(
$depthBackTrace
)
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
}
Bitrix\Main\Application
Definition
application.php:28
Bitrix\Main\Application\getInstance
static getInstance()
Definition
application.php:95
Bitrix\Main\Diag\Helper\getBackTrace
static getBackTrace($limit=0, $options=null, $skip=1)
Definition
helper.php:26
Bitrix\Main\Diag\SqlTracker
Definition
sqltracker.php:8
Bitrix\Main\Diag\SqlTracker\setDepthBackTrace
setDepthBackTrace($depthBackTrace)
Definition
sqltracker.php:120
Bitrix\Main\Diag\SqlTracker\__construct
__construct()
Definition
sqltracker.php:20
Bitrix\Main\Diag\SqlTracker\formatSql
formatSql($sql)
Definition
sqltracker.php:200
Bitrix\Main\Diag\SqlTracker\reset
reset()
Definition
sqltracker.php:41
Bitrix\Main\Diag\SqlTracker\$queries
$queries
Definition
sqltracker.php:10
Bitrix\Main\Diag\SqlTracker\getTime
getTime()
Definition
sqltracker.php:88
Bitrix\Main\Diag\SqlTracker\writeFileLog
writeFileLog($sql, $executionTime=0.0, $additional="", $traceSkip=2)
Definition
sqltracker.php:151
Bitrix\Main\Diag\SqlTracker\key
key()
Definition
sqltracker.php:298
Bitrix\Main\Diag\SqlTracker\getCounter
getCounter()
Definition
sqltracker.php:78
Bitrix\Main\Diag\SqlTracker\$time
$time
Definition
sqltracker.php:12
Bitrix\Main\Diag\SqlTracker\stopFileLog
stopFileLog()
Definition
sqltracker.php:187
Bitrix\Main\Diag\SqlTracker\getQueries
getQueries()
Definition
sqltracker.php:98
Bitrix\Main\Diag\SqlTracker\valid
valid()
Definition
sqltracker.php:318
Bitrix\Main\Diag\SqlTracker\getDepthBackTrace
getDepthBackTrace()
Definition
sqltracker.php:108
Bitrix\Main\Diag\SqlTracker\addTime
addTime($time)
Definition
sqltracker.php:68
Bitrix\Main\Diag\SqlTracker\next
next()
Definition
sqltracker.php:308
Bitrix\Main\Diag\SqlTracker\$logFilePath
$logFilePath
Definition
sqltracker.php:18
Bitrix\Main\Diag\SqlTracker\$counter
$counter
Definition
sqltracker.php:16
Bitrix\Main\Diag\SqlTracker\rewind
rewind()
Definition
sqltracker.php:278
Bitrix\Main\Diag\SqlTracker\formatTrace
formatTrace(array $trace=null)
Definition
sqltracker.php:239
Bitrix\Main\Diag\SqlTracker\current
current()
Definition
sqltracker.php:288
Bitrix\Main\Diag\SqlTracker\$depthBackTrace
static $depthBackTrace
Definition
sqltracker.php:14
Bitrix\Main\Diag\SqlTracker\startFileLog
startFileLog($filePath)
Definition
sqltracker.php:134
Bitrix\Main\Diag\SqlTracker\getNewTrackerQuery
getNewTrackerQuery()
Definition
sqltracker.php:53
Bitrix\Main\Diag\SqlTrackerQuery
Definition
sqltrackerquery.php:5
Bitrix\Main\Diag
Definition
cachetracker.php:2
Bitrix\Main\IO
Definition
directory.php:2
Bitrix
modules
main
lib
diag
sqltracker.php
Создано системой
1.10.0