class-debug-log-reader.php
3.28 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<?php
/**
* Class MC4WP_Debug_Log_Reader
*/
class MC4WP_Debug_Log_Reader
{
/**
* @var resource|null
*/
private $handle;
/**
* @var string
*/
private static $regex = '/^(\[[\d \-\:]+\]) (\w+\:) (.*)$/S';
/**
* @var string
*/
private static $html_template = '<span class="time">$1</span> <span class="level">$2</span> <span class="message">$3</span>';
/**
* @var string The log file location.
*/
private $file;
/**
* MC4WP_Debug_Log_Reader constructor.
*
* @param $file
*/
public function __construct($file)
{
$this->file = $file;
}
/**
* @return string
*/
public function all()
{
return file_get_contents($this->file);
}
/**
* Sets file pointer to $n of lines from the end of file.
*
* @param int $n
*/
private function seek_line_from_end($n)
{
$line_count = 0;
// get line count
while (! feof($this->handle)) {
fgets($this->handle);
++$line_count;
}
// rewind to beginning
rewind($this->handle);
// calculate target
$target = $line_count - $n;
$target = $target > 1 ? $target : 1; // always skip first line because oh PHP header
$current = 0;
// keep reading until we're at target
while ($current < $target) {
fgets($this->handle);
++$current;
}
}
/**
* @return string|null
*/
public function read()
{
// open file if not yet opened
if (! is_resource($this->handle)) {
// doesn't exist?
if (! file_exists($this->file)) {
return null;
}
$this->handle = @fopen($this->file, 'r');
// unable to read?
if (! is_resource($this->handle)) {
return null;
}
// set pointer to 1000 files from EOF
$this->seek_line_from_end(1000);
}
// stop reading once we're at the end
if (feof($this->handle)) {
fclose($this->handle);
$this->handle = null;
return null;
}
// read line, up to 8kb
$text = fgets($this->handle);
// strip tags & trim
$text = strip_tags($text);
$text = trim($text);
return $text;
}
/**
* @return string
*/
public function read_as_html()
{
$line = $this->read();
// null means end of file
if (is_null($line)) {
return null;
}
// empty string means empty line, but not yet eof
if (empty($line)) {
return '';
}
$line = preg_replace(self::$regex, self::$html_template, $line);
return $line;
}
/**
* Reads X number of lines.
*
* If $start is negative, reads from end of log file.
*
* @param int $start
* @param int $number
* @return string
*/
public function lines($start, $number)
{
$handle = fopen($start, 'r');
$lines = '';
$current_line = 0;
while ($current_line < $number) {
$lines .= fgets($handle);
}
fclose($handle);
return $lines;
}
}