Improvements and bug fix for log reading, fixes bug oc-982

This commit is contained in:
Michael Gapczynski 2012-07-06 15:51:01 -04:00
parent b967530795
commit 466d7c0d99
1 changed files with 27 additions and 14 deletions

View File

@ -63,25 +63,38 @@ class OC_Log_Owncloud {
self::init(); self::init();
$minLevel=OC_Config::getValue( "loglevel", OC_Log::WARN ); $minLevel=OC_Config::getValue( "loglevel", OC_Log::WARN );
$entries = array(); $entries = array();
$handle = @fopen(self::$logFile, 'r'); $handle = @fopen(self::$logFile, 'rb');
if ($handle) { if ($handle) {
// Just a guess to set the file pointer to the right spot fseek($handle, 0, SEEK_END);
$maxLineLength = 150; $pos = ftell($handle);
fseek($handle, -($limit * $maxLineLength + $offset * $maxLineLength), SEEK_END); $line = '';
// Skip first line, because it is most likely a partial line $entriesCount = 0;
fgets($handle); $lines = 0;
while (!feof($handle)) { // Loop through each character of the file looking for new lines
$line = fgets($handle); while ($pos >= 0 && $entriesCount < $limit) {
if (!empty($line)) { fseek($handle, $pos);
$entry = json_decode($line); $ch = fgetc($handle);
if ($entry->level >= $minLevel) { if ($ch == "\n" || $pos == 0) {
$entries[] = $entry; if ($line != '') {
// Add the first character if at the start of the file, because it doesn't hit the else in the loop
if ($pos == 0) {
$line = $ch.$line;
}
$lines++;
$entry = json_decode($line);
// Add the line as an entry if it is passed the offset and is equal or above the log level
if ($lines > $offset && $entry->level >= $minLevel) {
$entries[] = $entry;
$entriesCount++;
}
$line = '';
} }
} else {
$line = $ch.$line;
} }
$pos--;
} }
fclose($handle); fclose($handle);
// Extract the needed entries and reverse the order
$entries = array_reverse(array_slice($entries, -($limit + $offset), $limit));
} }
return $entries; return $entries;
} }