13 private $showOnDisplay = 0;
14 private $exclusiveUserId =
null;
15 private $enableLog =
false;
18 private static $instance;
19 private $stackSql = array();
20 private $stackMemory = array();
24 private function __construct()
35 if(!isset(self::$instance))
37 self::$instance =
new self;
40 return self::$instance;
51 $this->showOnDisplay = $showOnDisplay;
63 $this->exclusiveUserId = $exclusiveUserId;
74 $this->enableLog =
true;
84 $this->enableLog =
false;
94 if ($this->enableLog !==
true || ($this->exclusiveUserId !==
null && $this->getUser()->getId() != $this->exclusiveUserId))
100 if(empty($this->stackSql))
102 $this->connection->startTracker(
true);
103 array_push($this->stackSql, array($uniqueId, 0, array()));
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)));
111 $this->connection->startTracker(
true);
112 array_push($this->stackSql, array($uniqueId, 0, array()));
114 array_push($this->stackMemory, array($uniqueId, memory_get_usage(
true)));
117 private function getDebugInfoSql()
119 if ($tracker = $this->connection->getTracker())
121 $sqlTrackerQueries = $tracker->getQueries();
122 return array(count($sqlTrackerQueries), $sqlTrackerQueries);
124 return array(0, array());
134 public function logDebugInfo($uniqueId, $label =
null)
141 if ($this->enableLog !==
true || ($this->exclusiveUserId !==
null && $this->getUser()->getId() != $this->exclusiveUserId))
150 "Time: {$timeLabels[$uniqueId]['time']}"
153 list($prevLabel, $prevLabelCount, $prevSqlTrackerQueries) = array_pop($this->stackSql);
154 list($countQueries, $sqlTrackerQueries) = $this->getDebugInfoSql();
155 if($countQueries ===
null)
157 $sqlTrackerQueries = array();
158 $debugData[] =
'Sql tracker has not been found.';
162 if($prevLabel === $uniqueId)
164 $countQueries += $prevLabelCount;
165 $sqlTrackerQueries = array_merge($prevSqlTrackerQueries, $sqlTrackerQueries);
167 $debugData[] =
'Count sql: ' . $countQueries;
171 foreach($sqlTrackerQueries as $query)
173 $debugData[] = array(
176 $this->reformatBackTrace($query->getTrace())
181 list($prevLabel, $prevMemoryStart) = array_pop($this->stackMemory);
182 if($prevLabel === $uniqueId)
184 $debugData[] =
'Memory start: ' . \CFile::FormatSize($prevMemoryStart);
185 $debugData[] =
'Memory diff: ' . \CFile::FormatSize(memory_get_usage(
true) - $prevMemoryStart);
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);
198 public function log($data)
200 $this->showOnDisplay && var_dump($data);
201 AddMessage2Log(var_export($data,
true),
'vote', 0);
204 private function reformatBackTrace(array $backtrace)
206 $functionStack = $filesStack =
'';
207 foreach($backtrace as $b)
209 if($functionStack <>
'')
211 $functionStack .=
" < ";
214 if(isset($b[
"class"]))
216 $functionStack .= $b[
"class"] .
"::";
219 $functionStack .= $b[
"function"];
221 if(isset($b[
"file"]))
223 $filesStack .=
"\t" . $b[
"file"] .
":" . $b[
"line"] .
"\n";
227 return $functionStack .
"\n" . $filesStack;
233 private function getUser()
static startTimeLabel($name)
static endTimeLabel($name)