author | Urja (ARMLFS builder)
<urja+armlfs@urja.dev> 2024-07-12 16:09:26 UTC |
committer | Urja (ARMLFS builder)
<urja+armlfs@urja.dev> 2024-07-12 16:09:26 UTC |
parent | 9a2b0aacc2cc8440db8e63314df0682862504c68 |
logwatcher.py | +46 | -0 |
diff --git a/logwatcher.py b/logwatcher.py new file mode 100755 index 0000000..102e13b --- /dev/null +++ b/logwatcher.py @@ -0,0 +1,46 @@ +#!/usr/bin/env python3 + +import os +import sys +from time import strftime +import subprocess +from subprocess import DEVNULL, PIPE, STDOUT + +import pyinotify + +dlist = [] +with os.scandir() as it: + for e in it: + if e.name.startswith("logs-") and e.is_dir(): + dlist.append((e.stat().st_mtime, e.name)) + + +dlist.sort(reverse=True, key=lambda e: e[0]) +#print(dlist) + +os.chdir(dlist[0][1]) + +class EventHandler(pyinotify.ProcessEvent): + def process_IN_CREATE(self, event): + global tf + print(event.pathname) + tf.kill() + tf = subprocess.Popen(['tail','-f',event.pathname], stdin=DEVNULL) + +wm = pyinotify.WatchManager() +mask = pyinotify.IN_CREATE +handler = EventHandler() +notifier = pyinotify.Notifier(wm, handler) +wdd = wm.add_watch('.', mask) + +flist = [] +with os.scandir() as it: + for e in it: + if e.name.endswith(".log") and e.is_file(): + flist.append((e.stat().st_mtime, e.name)) + +flist.sort(reverse=True, key=lambda e: e[0]) +print(flist[0]) + +tf = subprocess.Popen(['tail','-f',flist[0][1]], stdin=DEVNULL) +notifier.loop()