1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc. |
4
|
|
|
* |
5
|
|
|
* @author Andreas Fischer <[email protected]> |
6
|
|
|
* @author Bart Visscher <[email protected]> |
7
|
|
|
* @author duritong <[email protected]> |
8
|
|
|
* @author Georg Ehrke <[email protected]> |
9
|
|
|
* @author Joas Schilling <[email protected]> |
10
|
|
|
* @author Juan Pablo Villafáñez <[email protected]> |
11
|
|
|
* @author Lukas Reschke <[email protected]> |
12
|
|
|
* @author Michael Gapczynski <[email protected]> |
13
|
|
|
* @author Morris Jobke <[email protected]> |
14
|
|
|
* @author Phiber2000 <[email protected]> |
15
|
|
|
* @author Robin Appelman <[email protected]> |
16
|
|
|
* @author Roeland Jago Douma <[email protected]> |
17
|
|
|
* @author Roger Szabo <[email protected]> |
18
|
|
|
* @author Thomas Müller <[email protected]> |
19
|
|
|
* @author Thomas Pulzer <[email protected]> |
20
|
|
|
* @author Vincent Petry <[email protected]> |
21
|
|
|
* |
22
|
|
|
* @license AGPL-3.0 |
23
|
|
|
* |
24
|
|
|
* This code is free software: you can redistribute it and/or modify |
25
|
|
|
* it under the terms of the GNU Affero General Public License, version 3, |
26
|
|
|
* as published by the Free Software Foundation. |
27
|
|
|
* |
28
|
|
|
* This program is distributed in the hope that it will be useful, |
29
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
30
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
31
|
|
|
* GNU Affero General Public License for more details. |
32
|
|
|
* |
33
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3, |
34
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/> |
35
|
|
|
* |
36
|
|
|
*/ |
37
|
|
|
|
38
|
|
|
namespace OC\Log; |
39
|
|
|
use OC\SystemConfig; |
40
|
|
|
use OCP\Log\IFileBased; |
41
|
|
|
use OCP\Log\IWriter; |
42
|
|
|
use OCP\ILogger; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* logging utilities |
46
|
|
|
* |
47
|
|
|
* Log is saved at data/nextcloud.log (on default) |
48
|
|
|
*/ |
49
|
|
|
|
50
|
|
|
class File extends LogDetails implements IWriter, IFileBased { |
51
|
|
|
/** @var string */ |
52
|
|
|
protected $logFile; |
53
|
|
|
/** @var int */ |
54
|
|
|
protected $logFileMode; |
55
|
|
|
/** @var SystemConfig */ |
56
|
|
|
private $config; |
57
|
|
|
|
58
|
|
|
public function __construct(string $path, string $fallbackPath = '', SystemConfig $config) { |
59
|
|
|
parent::__construct($config); |
60
|
|
|
$this->logFile = $path; |
61
|
|
|
if (!file_exists($this->logFile)) { |
62
|
|
|
if( |
63
|
|
|
( |
64
|
|
|
!is_writable(dirname($this->logFile)) |
65
|
|
|
|| !touch($this->logFile) |
66
|
|
|
) |
67
|
|
|
&& $fallbackPath !== '' |
68
|
|
|
) { |
69
|
|
|
$this->logFile = $fallbackPath; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
$this->config = $config; |
73
|
|
|
$this->logFileMode = $config->getValue('logfilemode', 0640); |
|
|
|
|
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* write a message in the log |
78
|
|
|
* @param string $app |
79
|
|
|
* @param string|array $message |
80
|
|
|
* @param int $level |
81
|
|
|
*/ |
82
|
|
|
public function write(string $app, $message, int $level) { |
83
|
|
|
$entry = $this->logDetailsAsJSON($app, $message, $level); |
84
|
|
|
$handle = @fopen($this->logFile, 'a'); |
85
|
|
|
if ($this->logFileMode > 0 && (fileperms($this->logFile) & 0777) != $this->logFileMode) { |
86
|
|
|
@chmod($this->logFile, $this->logFileMode); |
|
|
|
|
87
|
|
|
} |
88
|
|
|
if ($handle) { |
|
|
|
|
89
|
|
|
fwrite($handle, $entry."\n"); |
90
|
|
|
fclose($handle); |
91
|
|
|
} else { |
92
|
|
|
// Fall back to error_log |
93
|
|
|
error_log($entry); |
94
|
|
|
} |
95
|
|
|
if (php_sapi_name() === 'cli-server') { |
96
|
|
|
if (!\is_string($message)) { |
97
|
|
|
$message = json_encode($message); |
98
|
|
|
} |
99
|
|
|
error_log($message, 4); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* get entries from the log in reverse chronological order |
105
|
|
|
* @param int $limit |
106
|
|
|
* @param int $offset |
107
|
|
|
* @return array |
108
|
|
|
*/ |
109
|
|
|
public function getEntries(int $limit=50, int $offset=0):array { |
110
|
|
|
$minLevel = $this->config->getValue("loglevel", ILogger::WARN); |
111
|
|
|
$entries = array(); |
112
|
|
|
$handle = @fopen($this->logFile, 'rb'); |
113
|
|
|
if ($handle) { |
|
|
|
|
114
|
|
|
fseek($handle, 0, SEEK_END); |
115
|
|
|
$pos = ftell($handle); |
116
|
|
|
$line = ''; |
117
|
|
|
$entriesCount = 0; |
118
|
|
|
$lines = 0; |
119
|
|
|
// Loop through each character of the file looking for new lines |
120
|
|
|
while ($pos >= 0 && ($limit === null ||$entriesCount < $limit)) { |
121
|
|
|
fseek($handle, $pos); |
122
|
|
|
$ch = fgetc($handle); |
123
|
|
|
if ($ch == "\n" || $pos == 0) { |
124
|
|
|
if ($line != '') { |
125
|
|
|
// Add the first character if at the start of the file, |
126
|
|
|
// because it doesn't hit the else in the loop |
127
|
|
|
if ($pos == 0) { |
128
|
|
|
$line = $ch.$line; |
129
|
|
|
} |
130
|
|
|
$entry = json_decode($line); |
131
|
|
|
// Add the line as an entry if it is passed the offset and is equal or above the log level |
132
|
|
|
if ($entry->level >= $minLevel) { |
133
|
|
|
$lines++; |
134
|
|
|
if ($lines > $offset) { |
135
|
|
|
$entries[] = $entry; |
136
|
|
|
$entriesCount++; |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
$line = ''; |
140
|
|
|
} |
141
|
|
|
} else { |
142
|
|
|
$line = $ch.$line; |
143
|
|
|
} |
144
|
|
|
$pos--; |
145
|
|
|
} |
146
|
|
|
fclose($handle); |
147
|
|
|
} |
148
|
|
|
return $entries; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @return string |
153
|
|
|
*/ |
154
|
|
|
public function getLogFilePath():string { |
155
|
|
|
return $this->logFile; |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.