Merge pull request #286 from pi-hole/new/taillog

Add non-blocking tailing of pihole.log
This commit is contained in:
Adam Warner
2016-12-23 23:27:29 +00:00
committed by GitHub
5 changed files with 95 additions and 0 deletions
+4
View File
@@ -80,6 +80,10 @@
$data = array_merge($data, getGravity());
}
if (isset($_GET['tailLog'])) {
$data = array_merge($data, tailPiholeLog($_GET['tailLog']));
}
function filterArray(&$inArray) {
$outArray = array();
foreach ($inArray as $key=>$value) {
+27
View File
@@ -293,6 +293,33 @@
return $allQueries;
}
function tailPiholeLog($param) {
// Not using SplFileObject here, since direct
// usage of f-streams will be much faster for
// files as large as the pihole.log
global $logListName;
$file = fopen($logListName,"r");
$offset = intval($param);
if($offset > 0)
{
// Seeks on the file pointer where we want to continue reading is known
fseek($file, $offset);
$lines = [];
while (!feof($file)) {
array_push($lines,fgets($file));
}
return ["offset" => ftell($file), "lines" => $lines];
}
else
{
// Locate the current position of the file read/write pointer
fseek($file, -1, SEEK_END);
// Add one to skip the very last "\n" in the log file
return ["offset" => ftell($file)+1];
}
fclose($file);
}
/******** Private Members ********/
function gravityCount() {
global $gravityListName,$blackListFile;
+6
View File
@@ -353,6 +353,12 @@
<i class="fa fa-search"></i> <span>Query adlists</span>
</a>
</li>
<!-- Tail pihole.log -->
<li>
<a href="taillog.php">
<i class="fa fa-list-ul"></i> <span>Tail pihole.log</span>
</a>
</li>
<!-- Toggle -->
<?php
if ($pistatus == "1") {
+40
View File
@@ -0,0 +1,40 @@
var offset, timer, pre, scrolling = true;
// Check every 200msec for fresh data
var interval = 200;
// Function that asks the API for new data
function reloadData(){
clearTimeout(timer);
$.getJSON("api.php?tailLog="+offset, function (data)
{
offset = data["offset"];
pre.append(data["lines"]);
});
if(scrolling)
{
window.scrollTo(0,document.body.scrollHeight);
}
timer = setTimeout(reloadData, interval);
}
$(function(){
// Get offset at first loading of page
$.getJSON("api.php?tailLog", function (data)
{
offset = data["offset"];
});
pre = $("#output");
// Trigger function that looks for new data
reloadData();
});
$("#chk1").click(function() {
$("#chk2").prop("checked",this.checked);
scrolling = this.checked;
});
$("#chk2").click(function() {
$("#chk1").prop("checked",this.checked);
scrolling = this.checked;
});
+18
View File
@@ -0,0 +1,18 @@
<?php
require "header.php";
?>
<!-- Title -->
<div class="page-header">
<h1>Output the last lines of the pihole.log file (live)</h1>
</div>
<div class="checkbox"><label><input type="checkbox" name="active" checked id="chk1"> Automatic scrolling on update</label></div>
<pre id="output" style="width: 100%; height: 100%;"></pre>
<div class="checkbox"><label><input type="checkbox" name="active" checked id="chk2"> Automatic scrolling on update</label></div>
<?php
require "footer.php";
?>
<script src="js/pihole/taillog.js"></script>