forked from pipprit/XIVPads-LodestoneAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.php
124 lines (100 loc) · 3.17 KB
/
logger.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php
// Namespace
namespace Viion\Lodestone;
/* class 'logger'
* - Logs things!
*/
class Logger
{
// public
public $start;
public $finish;
public $duration;
// private
private $previous = false;
private $messages = [];
// init
function __construct() { }
// Get the time now
public function now()
{
$time = round(microtime(true) * 1000);
return $time;
}
// calculate: will set start, finish and duration based on log
public function calculate()
{
// Sort it
ksort($this->messages);
// Get start
reset($this->messages);
$this->start = str_ireplace('-', null, key($this->messages));
// Get finish
end($this->messages);
$this->finish = str_ireplace('-', null, key($this->messages));
// Get duration
$this->duration = $this->finish - $this->start;
// Format it to be more readable
if ($this->duration > 0)
{
$this->duration = round(($this->duration / 1000), 5);
}
}
// show results
public function show()
{
// If no messages, return right away
if (empty($this->messages))
{
return false;
}
// Calculate times
$this->calculate();
// ksort
ksort($this->messages);
show($this->messages);
}
// get results
public function get()
{
// If no messages, return right away
if (empty($this->messages))
{
return false;
}
// Calculate times
$this->calculate();
// ksort
ksort($this->messages);
return $this->messages;
}
public function log($text)
{
// Get the delay from the previous entry
$delay = '-';
if ($this->previous !== false)
{
$delay = ($this->now() - $this->previous);
}
// Set the previous time to now
$this->previous = $this->now();
// Style delay
$delay = $this->styleDelay($delay);
// Log it
$timeformat = substr_replace($this->now(), '-', -4, 0);
$this->messages[$timeformat][] = '('. $delay .' ms) '. $text;
}
private function styleDelay($delay)
{
$html = '{{delay}}';
if ($delay > 2000) { $html = '<span style="background-color:#f00;color:#fff;font-weight:bold;padding:0 8px;border-radius:3px;">{{delay}}</span>'; }
else if ($delay > 500) { $html = '<span style="background-color:#f00;color:#fff;font-weight:bold;border-radius:3px;">{{delay}}</span>'; }
else if ($delay > 250) { $html = '<span style="background-color:#AC7206;color:#fff;font-weight:bold;border-radius:3px;">{{delay}}</span>'; }
else if ($delay > 100) { $html = '<span style="background-color:#8EC018;color:#fff;font-weight:bold;border-radius:3px;">{{delay}}</span>'; }
else if ($delay < 10) { $html = '<span style="color:#5CA21E;font-weight:bold;">{{delay}}</span>'; }
$html = str_ireplace('{{delay}}', $delay, $html);
return $html;
}
}
// Alias for LodestoneAPI()
class_alias('Viion\Lodestone\Logger', 'API\Logger');