diff --git a/api.php b/api.php index 150dc9e2..7d2ee8e9 100644 --- a/api.php +++ b/api.php @@ -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) { diff --git a/data.php b/data.php index 5cc0fdfd..2e7ec397 100644 --- a/data.php +++ b/data.php @@ -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; diff --git a/js/pihole/taillog.js b/js/pihole/taillog.js index 0ae63f41..e6682761 100644 --- a/js/pihole/taillog.js +++ b/js/pihole/taillog.js @@ -1,30 +1,29 @@ -var source; -function eventsource() { - var ta = $("#output"); - source = new EventSource("php/taillog.php"); +var offset, timer, pre; - ta.html(""); - ta.show(); +// Check every 200msec for fresh data +var interval = 200; - source.addEventListener("message", function(e) { - if(e.data.indexOf("Ctrl-C") === -1) - { - // Hide the "Press Ctrl-C to exit" message - ta.append(e.data); - } - - }, false); - - // Will be called when script has finished - source.addEventListener("error", function(e) { - source.close(); - }, false); +// 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"]); + console.log(offset); + }); + timer = setTimeout(reloadData, interval); } $(function(){ - eventsource(); + // Get offset at first loading of page + $.getJSON("api.php?tailLog", function (data) + { + offset = data["offset"]; + console.log(offset); + }); + pre = $("#output"); + // Trigger function that looks for new data + reloadData(); }); -$(window).unload(function(){ - source.close(); -}); diff --git a/php/taillog.php b/php/taillog.php deleted file mode 100644 index 210397d4..00000000 --- a/php/taillog.php +++ /dev/null @@ -1,23 +0,0 @@ - diff --git a/taillog.php b/taillog.php index fd328837..10800fc1 100644 --- a/taillog.php +++ b/taillog.php @@ -6,7 +6,7 @@