From 874ec8c9db9b13fb9b0c2dd879dc634df40792ce Mon Sep 17 00:00:00 2001 From: DL6ER Date: Thu, 23 Feb 2017 15:03:53 +0100 Subject: [PATCH] Implemented tailLog into FTL API --- api_FTL.php | 25 +++++++++++++++++++++++++ scripts/pi-hole/js/taillog.js | 4 ++-- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/api_FTL.php b/api_FTL.php index 05baba7d..2597564b 100644 --- a/api_FTL.php +++ b/api_FTL.php @@ -249,6 +249,31 @@ elseif (isset($_GET['disable'], $_GET['token']) && $auth) { $data = array_merge($data, array("status" => "disabled")); } +if (isset($_GET['tailLog']) && $auth) { + // Not using SplFileObject here, since direct + // usage of f-streams will be much faster for + // files as large as the pihole.log + $file = fopen("/var/log/pihole.log","r"); + $offset = intval($_GET['tailLog']); + 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)); + $data = array_merge($data, array("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 + $data = array_merge($data, array("offset" => ftell($file)+1)); + } + fclose($file); +} + if(isset($_GET["recentBlocked"])) { sendRequestFTL("recentBlocked"); diff --git a/scripts/pi-hole/js/taillog.js b/scripts/pi-hole/js/taillog.js index 32272de9..ffe370fc 100644 --- a/scripts/pi-hole/js/taillog.js +++ b/scripts/pi-hole/js/taillog.js @@ -12,7 +12,7 @@ var interval = 200; // Function that asks the API for new data function reloadData(){ clearTimeout(timer); - $.getJSON("api_PHP.php?tailLog="+offset, function (data) + $.getJSON("api.php?tailLog="+offset, function (data) { offset = data["offset"]; pre.append(data["lines"]); @@ -27,7 +27,7 @@ function reloadData(){ $(function(){ // Get offset at first loading of page - $.getJSON("api_PHP.php?tailLog", function (data) + $.getJSON("api.php?tailLog", function (data) { offset = data["offset"]; });