1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Log |
5
|
|
|
* |
6
|
|
|
* @author Alexey Krupskiy <[email protected]> |
7
|
|
|
* @link http://inji.ru/ |
8
|
|
|
* @copyright 2015 Alexey Krupskiy |
9
|
|
|
* @license https://github.com/injitools/cms-Inji/blob/master/LICENSE |
10
|
|
|
*/ |
11
|
|
|
class Log { |
12
|
|
|
|
13
|
|
|
public $log = []; |
14
|
|
|
public $lastLog = 0; |
15
|
|
|
public $run = true; |
16
|
|
|
public $startTime = 0; |
17
|
|
|
public $template_parsed = false; |
18
|
|
|
public $forceView = false; |
19
|
|
|
|
20
|
|
|
public function __construct() { |
21
|
|
|
if (!empty($_SERVER['REQUEST_TIME_FLOAT'])) { |
22
|
|
|
$this->startTime = $_SERVER['REQUEST_TIME_FLOAT']; |
23
|
|
|
} elseif (!empty($_SERVER['REQUEST_TIME'])) { |
24
|
|
|
$this->startTime = $_SERVER['REQUEST_TIME']; |
25
|
|
|
} else { |
26
|
|
|
$this->startTime = time(); |
27
|
|
|
} |
28
|
|
|
$this->log[] = array('name' => 'System init', 'start' => $this->startTime, 'end' => microtime(true)); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function start($name) { |
32
|
|
|
if ($this->run) { |
33
|
|
|
$this->log[] = array('name' => $name, 'start' => microtime(true)); |
34
|
|
|
end($this->log); |
35
|
|
|
return $this->lastLog = key($this->log); |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function end($key = false) { |
40
|
|
|
if ($this->run) { |
41
|
|
|
if ($key === false) { |
42
|
|
|
$this->log[$this->lastLog]['end'] = microtime(true); |
43
|
|
|
} else { |
44
|
|
|
$this->log[$key]['end'] = microtime(true); |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function event($name, $status = 'info') { |
50
|
|
|
if ($this->run) { |
51
|
|
|
$this->log[] = array('name' => $name, 'status' => $status, 'time' => microtime(true)); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function clean() { |
56
|
|
|
$this->log = []; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function stop() { |
60
|
|
|
$this->run = false; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function run() { |
64
|
|
|
$this->run = true; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function view() { |
68
|
|
|
echo '<div onclick="var image = document.getElementById(\'Inji_debug_window\'); |
69
|
|
|
image.style.display = (image.style.display == \'none\') ? \'block\' : \'none\';" style = "background:#fff;position:fixed;bottom:0;right:0;opacity:0.3;z-index:1000001;cursor:pointer;">debug</div>'; |
70
|
|
|
echo '<div id = "Inji_debug_window" style = "background:#fff;position:fixed;top:0;left:0;display:none;z-index:1000000;width: 100%;height: 100%;overflow: auto;"><table border="1" class="table table-striped table-bordered"><tr><th>Name</th><th>Time</th></tr>'; |
71
|
|
|
foreach ($this->log as $log) { |
72
|
|
|
if (!empty($log['status'])) { |
73
|
|
|
echo "<tr class = '{$log['status']}'><td>{$log['name']}</td><td>{$log['status']}</td></tr>"; |
74
|
|
|
} else { |
75
|
|
|
if (empty($log['end'])) { |
76
|
|
|
$log['end'] = microtime(true); |
77
|
|
|
} |
78
|
|
|
if (empty($log['start'])) { |
79
|
|
|
$log['start'] = microtime(true); |
80
|
|
|
} |
81
|
|
|
echo "<tr><td>{$log['name']}</td><td" . (round(($log['end'] - $log['start']), 5) > 0.1 ? ' class ="danger"' : '') . ">" . round(($log['end'] - $log['start']), 5) . "</td></tr>"; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
echo '<tr><th>Summary</th><th>' . round((microtime(true) - $this->startTime), 5) . '</th></tr>'; |
85
|
|
|
echo '<tr><th>Memory</th><th>' . $this->convertSize(memory_get_peak_usage()) . ' of ' . ini_get('memory_limit') . '</th></tr></table></div>'; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function forceView($bool) { |
89
|
|
|
App::$cur->log->forceView = $bool; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function convertSize($size) { |
93
|
|
|
|
94
|
|
|
if ($size < 1024) { |
95
|
|
|
return $size . "B"; |
96
|
|
|
} elseif ($size < 1048576) { |
97
|
|
|
return round($size / 1024, 2) . "KB"; |
98
|
|
|
} else { |
99
|
|
|
return round($size / 1048576, 2) . "MB"; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function __destruct() { |
104
|
|
|
if ($this->forceView || ($this->run && $_SERVER['REMOTE_ADDR'] == '127.0.0.1' && $this->template_parsed)) { |
105
|
|
|
$this->view(); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
} |
110
|
|
|
|