Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
diag.php
1<?php
2
3namespace Bitrix\Vote\Base;
4
10
11final class Diag
12{
13 private $showOnDisplay = 0;
14 private $exclusiveUserId = null;
15 private $enableLog = false;
16
18 private static $instance;
19 private $stackSql = array();
20 private $stackMemory = array();
22 private $connection;
23
24 private function __construct()
25 {
26 $this->connection = Application::getInstance()->getConnection();
27 }
28
33 public static function getInstance()
34 {
35 if(!isset(self::$instance))
36 {
37 self::$instance = new self;
38 }
39
40 return self::$instance;
41 }
42
43
49 public function setShowOnDisplay($showOnDisplay)
50 {
51 $this->showOnDisplay = $showOnDisplay;
52
53 return $this;
54 }
55
61 public function setExclusiveUserId($exclusiveUserId)
62 {
63 $this->exclusiveUserId = $exclusiveUserId;
64
65 return $this;
66 }
67
72 public function activate()
73 {
74 $this->enableLog = true;
75 return $this;
76 }
77
82 public function deactivate()
83 {
84 $this->enableLog = false;
85 return $this;
86 }
92 public function collectDebugInfo($uniqueId)
93 {
94 if ($this->enableLog !== true || ($this->exclusiveUserId !== null && $this->getUser()->getId() != $this->exclusiveUserId))
95 {
96 return;
97 }
98 Debug::startTimeLabel($uniqueId);
99
100 if(empty($this->stackSql))
101 {
102 $this->connection->startTracker(true);
103 array_push($this->stackSql, array($uniqueId, 0, array()));
104 }
105 else
106 {
107 list($prevLabel, $prevLabelCount, $prevSqlTrackerQueries) = array_pop($this->stackSql);
108 list($countQueries, $sqlTrackerQueries) = $this->getDebugInfoSql();
109 array_push($this->stackSql, array($prevLabel, $countQueries + $prevLabelCount, array_merge($prevSqlTrackerQueries, $sqlTrackerQueries)));
110
111 $this->connection->startTracker(true);
112 array_push($this->stackSql, array($uniqueId, 0, array()));
113 }
114 array_push($this->stackMemory, array($uniqueId, memory_get_usage(true)));
115 }
116
117 private function getDebugInfoSql()
118 {
119 if ($tracker = $this->connection->getTracker())
120 {
121 $sqlTrackerQueries = $tracker->getQueries();
122 return array(count($sqlTrackerQueries), $sqlTrackerQueries);
123 }
124 return array(0, array());
125 }
126
134 public function logDebugInfo($uniqueId, $label = null)
135 {
136 if($label === null)
137 {
138 $label = $uniqueId;
139 }
140
141 if ($this->enableLog !== true || ($this->exclusiveUserId !== null && $this->getUser()->getId() != $this->exclusiveUserId))
142 {
143 return;
144 }
145
146 Debug::endTimeLabel($uniqueId);
147 $timeLabels = Debug::getTimeLabels();
148
149 $debugData = array(
150 "Time: {$timeLabels[$uniqueId]['time']}"
151 );
152
153 list($prevLabel, $prevLabelCount, $prevSqlTrackerQueries) = array_pop($this->stackSql);
154 list($countQueries, $sqlTrackerQueries) = $this->getDebugInfoSql();
155 if($countQueries === null)
156 {
157 $sqlTrackerQueries = array();
158 $debugData[] = 'Sql tracker has not been found.';
159 }
160 else
161 {
162 if($prevLabel === $uniqueId)
163 {
164 $countQueries += $prevLabelCount;
165 $sqlTrackerQueries = array_merge($prevSqlTrackerQueries, $sqlTrackerQueries);
166 }
167 $debugData[] = 'Count sql: ' . $countQueries;
168
169 }
171 foreach($sqlTrackerQueries as $query)
172 {
173 $debugData[] = array(
174 $query->getTime(),
175 $query->getSql(),
176 $this->reformatBackTrace($query->getTrace())
177 );
178 unset($query);
179 }
180
181 list($prevLabel, $prevMemoryStart) = array_pop($this->stackMemory);
182 if($prevLabel === $uniqueId)
183 {
184 $debugData[] = 'Memory start: ' . \CFile::FormatSize($prevMemoryStart);
185 $debugData[] = 'Memory diff: ' . \CFile::FormatSize(memory_get_usage(true) - $prevMemoryStart);
186 }
187 $debugData[] = 'Memory amount: ' . \CFile::FormatSize(memory_get_usage(true));
188 $debugData[] = 'Memory peak usage: ' . \CFile::FormatSize(memory_get_peak_usage(true));
189 array_unshift($debugData, "Label: {$label}");
190 $this->log($debugData);
191 }
192
198 public function log($data)
199 {
200 $this->showOnDisplay && var_dump($data);
201 AddMessage2Log(var_export($data, true), 'vote', 0);
202 }
203
204 private function reformatBackTrace(array $backtrace)
205 {
206 $functionStack = $filesStack = '';
207 foreach($backtrace as $b)
208 {
209 if($functionStack <> '')
210 {
211 $functionStack .= " < ";
212 }
213
214 if(isset($b["class"]))
215 {
216 $functionStack .= $b["class"] . "::";
217 }
218
219 $functionStack .= $b["function"];
220
221 if(isset($b["file"]))
222 {
223 $filesStack .= "\t" . $b["file"] . ":" . $b["line"] . "\n";
224 }
225 }
226
227 return $functionStack . "\n" . $filesStack;
228 }
229
233 private function getUser()
234 {
235 global $USER;
236 return $USER;
237 }
238}
static startTimeLabel($name)
Definition debug.php:9
static getTimeLabels()
Definition debug.php:27
static endTimeLabel($name)
Definition debug.php:18
collectDebugInfo($uniqueId)
Definition diag.php:92
setExclusiveUserId($exclusiveUserId)
Definition diag.php:61
setShowOnDisplay($showOnDisplay)
Definition diag.php:49
static getInstance()
Definition diag.php:33