summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMichael Gapczynski <mtgap@owncloud.com>2012-07-06 15:51:01 -0400
committerMichael Gapczynski <mtgap@owncloud.com>2012-07-06 15:51:01 -0400
commit466d7c0d99eab914960295f27496a9380cd8d125 (patch)
tree299f86a47103684ccbbbfd0f076c28124795e14f
parentb96753079542d76ef16dcba835de0c4b75158c47 (diff)
downloadnextcloud-server-466d7c0d99eab914960295f27496a9380cd8d125.tar.gz
nextcloud-server-466d7c0d99eab914960295f27496a9380cd8d125.zip
Improvements and bug fix for log reading, fixes bug oc-982
-rw-r--r--lib/log/owncloud.php41
1 files changed, 27 insertions, 14 deletions
diff --git a/lib/log/owncloud.php b/lib/log/owncloud.php
index 92914af8fca..42ae7867ff0 100644
--- a/lib/log/owncloud.php
+++ b/lib/log/owncloud.php
@@ -63,25 +63,38 @@ class OC_Log_Owncloud {
self::init();
$minLevel=OC_Config::getValue( "loglevel", OC_Log::WARN );
$entries = array();
- $handle = @fopen(self::$logFile, 'r');
+ $handle = @fopen(self::$logFile, 'rb');
if ($handle) {
- // Just a guess to set the file pointer to the right spot
- $maxLineLength = 150;
- fseek($handle, -($limit * $maxLineLength + $offset * $maxLineLength), SEEK_END);
- // Skip first line, because it is most likely a partial line
- fgets($handle);
- while (!feof($handle)) {
- $line = fgets($handle);
- if (!empty($line)) {
- $entry = json_decode($line);
- if ($entry->level >= $minLevel) {
- $entries[] = $entry;
+ fseek($handle, 0, SEEK_END);
+ $pos = ftell($handle);
+ $line = '';
+ $entriesCount = 0;
+ $lines = 0;
+ // Loop through each character of the file looking for new lines
+ while ($pos >= 0 && $entriesCount < $limit) {
+ fseek($handle, $pos);
+ $ch = fgetc($handle);
+ if ($ch == "\n" || $pos == 0) {
+ 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);
- // Extract the needed entries and reverse the order
- $entries = array_reverse(array_slice($entries, -($limit + $offset), $limit));
}
return $entries;
}