-
-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Tail admin logs to always display data
Follow-up #4876 Thanks to Torleif Berger for it's code for Tail tackling large files in PHP http://www.geekality.net/?p=1654
- Loading branch information
Showing
6 changed files
with
123 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<?php | ||
/** | ||
* File tools for Lizmap. | ||
* | ||
* @author 3liz | ||
* @copyright 2024 3liz | ||
* | ||
* @see https://3liz.com | ||
* | ||
* @license Mozilla Public License : http://www.mozilla.org/MPL/ | ||
*/ | ||
|
||
namespace Lizmap\App; | ||
|
||
class FileTools | ||
{ | ||
/** | ||
* Tail in PHP, capable of eating big files. | ||
* | ||
* @author Torleif Berger | ||
* | ||
* @see http://www.geekality.net/?p=1654 | ||
* | ||
* @return string | ||
*/ | ||
public static function tail(string $filepath, int $lines = 10, int $buffer = 4096) | ||
{ | ||
// Check if the file path exists and is a file | ||
if (!file_exists($filepath) || !is_file($filepath)) { | ||
return ''; | ||
} | ||
|
||
// Open the file | ||
$f = fopen($filepath, 'rb'); | ||
// Jump to last character | ||
fseek($f, -1, SEEK_END); | ||
|
||
// Prepare to collect output | ||
$output = ''; | ||
$chunk = ''; | ||
|
||
// Start reading it and adjust line number if necessary | ||
// (Otherwise the result would be wrong if file doesn't end with a blank line) | ||
$TAIL_NL = "\n"; | ||
if (fread($f, 1) != $TAIL_NL) { | ||
--$lines; | ||
} | ||
|
||
// While we would like more | ||
while (ftell($f) > 0 && $lines >= 0) { | ||
// Figure out how far back we should jump | ||
$seek = min(ftell($f), $buffer); | ||
|
||
// Do the jump (backwards, relative to where we are) | ||
fseek($f, -$seek, SEEK_CUR); | ||
|
||
// Read a chunk and prepend it to our output | ||
$output = ($chunk = fread($f, $seek)).$output; | ||
|
||
// Jump back to where we started reading | ||
fseek($f, -mb_strlen($chunk, '8bit'), SEEK_CUR); | ||
|
||
// Decrease our line counter | ||
$lines -= substr_count($chunk, $TAIL_NL); | ||
} | ||
|
||
// While we have too many lines | ||
// (Because of buffer size we might have read too many) | ||
while ($lines++ < 0) { | ||
// Find first newline and remove all text before that | ||
$output = substr($output, strpos($output, $TAIL_NL) + 1); | ||
} | ||
|
||
// Close file and return | ||
fclose($f); | ||
|
||
return $output; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Lizmap\App\FileTools; | ||
|
||
class FileToolsTest extends TestCase | ||
{ | ||
function testTail() { | ||
$TAIL_NL = "\n"; | ||
|
||
$oneLinePath = __DIR__.'/Ressources/one-line.txt'; | ||
$this->assertEquals('1 line', trim(FileTools::tail($oneLinePath, 1))); | ||
$this->assertEquals('1 line', trim(FileTools::tail($oneLinePath, 5))); | ||
|
||
$tenLinePath = __DIR__.'/Ressources/ten-lines.txt'; | ||
$this->assertEquals('10 lines', trim(FileTools::tail($tenLinePath, 1))); | ||
|
||
$lines = explode($TAIL_NL, trim(FileTools::tail($tenLinePath, 5))); | ||
$this->assertCount(5, $lines); | ||
$this->assertEquals('10 lines', $lines[4]); | ||
$this->assertEquals('6 lines', $lines[0]); | ||
|
||
$dirPath = __DIR__.'/Ressources'; | ||
$this->assertEquals('', trim(FileTools::tail($dirPath, 1))); | ||
$this->assertEquals('', trim(FileTools::tail($dirPath, 5))); | ||
|
||
$unknownPath = __DIR__.'/Ressources/unknown.txt'; | ||
$this->assertEquals('', trim(FileTools::tail($unknownPath, 1))); | ||
$this->assertEquals('', trim(FileTools::tail($unknownPath, 5))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
1 line |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
1 line | ||
2 lines | ||
3 lines | ||
4 lines | ||
5 lines | ||
6 lines | ||
7 lines | ||
8 lines | ||
9 lines | ||
10 lines |