Skip to content

Commit

Permalink
Merge branch 'sandermangel-patch-2' into 1.5.1-dev #67
Browse files Browse the repository at this point in the history
  • Loading branch information
madalinoprea committed Mar 3, 2016
2 parents 25fc65f + 2112a50 commit ac74be9
Showing 1 changed file with 25 additions and 8 deletions.
33 changes: 25 additions & 8 deletions code/Debug/Model/Logging.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,21 +210,38 @@ public function getTotalLineCount()
*/
public function getContent($filePath, $startPosition, $endPosition)
{
if (!file_exists($filePath)) {
// End position exists but is less then start position
if ($endPosition <= $startPosition) {
return '';
}

// End position not saved yet
if (!$endPosition) {
return trim(file_get_contents($filePath, null, null, $startPosition));
}
$contentLength = $endPosition - $startPosition; // calculate how many characters we want to buffer

// End position exists but is less then start position
if ($endPosition <= $startPosition) {
if (!file_exists($filePath) || !($h = fopen($filePath, "r"))) { // file not found or issue reading
return '';
}

return trim(file_get_contents($filePath, null, null, $startPosition, $endPosition = $startPosition));
fseek($h, $startPosition); // jump to the start position

$lengthCounter = 0;
$outputBuffer = "";
while (($line = fgets($h)) !== false) { // read line by line
$lengthCounter += strlen($line); // add length of read content to total counter

if ($endPosition!=false && $lengthCounter > $contentLength) { // check if current line is longer than allowed length
$line = substr($line, 0, $lengthCounter - $contentLength); // trim the lenght of line
}

$outputBuffer = $line . PHP_EOL;

if ($endPosition!=false && $lengthCounter > $contentLength) { // read enough, stahp
break;
}
}

fclose($h);

return trim($outputBuffer);
}

}

0 comments on commit ac74be9

Please sign in to comment.