Implement colorful logging and autoreload for more fun.
This commit is contained in:
32
antidrift.py
32
antidrift.py
@@ -4,13 +4,17 @@
|
||||
import logging
|
||||
import shutil
|
||||
import sys
|
||||
import signal
|
||||
from pathlib import Path
|
||||
import antidrift.client as client
|
||||
from antidrift.config import Config
|
||||
from antidrift.daemon import AntiDriftDaemon
|
||||
from dbus.mainloop.glib import DBusGMainLoop
|
||||
DBusGMainLoop(set_as_default=True)
|
||||
signal.signal(signal.SIGINT, signal.SIG_DFL)
|
||||
|
||||
|
||||
def init_logging(log_file: Path):
|
||||
def init_logging(log_file: Path, mode: str):
|
||||
class DuplicateFilter(logging.Filter):
|
||||
def filter(self, record) -> bool:
|
||||
current_log = (record.module, record.levelno, record.msg)
|
||||
@@ -18,8 +22,9 @@ def init_logging(log_file: Path):
|
||||
self.last_log = current_log
|
||||
return True
|
||||
return False
|
||||
format_str = f'%(asctime)s | %(levelname)-8s | {mode} | %(message)s'
|
||||
logging.basicConfig(filename=log_file,
|
||||
format='%(asctime)s | %(levelname)-8s | %(message)s',
|
||||
format=format_str,
|
||||
datefmt='%H:%M:%S',
|
||||
encoding='utf-8',
|
||||
level=logging.DEBUG)
|
||||
@@ -35,14 +40,33 @@ def check_for_xdotool():
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
def tailf(config):
|
||||
import time
|
||||
from rich import print
|
||||
with open(config.log_file, 'r') as f:
|
||||
f.seek(0, 2)
|
||||
while True:
|
||||
line = f.readline()
|
||||
if not line:
|
||||
time.sleep(0.1)
|
||||
else:
|
||||
print(line.strip())
|
||||
|
||||
|
||||
def main() -> None:
|
||||
""" Main routine that dispatches to client or daemon """
|
||||
check_for_xdotool()
|
||||
config = Config.load("config.yaml")
|
||||
init_logging(config.log_file)
|
||||
|
||||
if len(sys.argv) > 1 and sys.argv[1] == "tailf":
|
||||
tailf(config)
|
||||
return
|
||||
|
||||
check_for_xdotool()
|
||||
if client.antidrift_is_running():
|
||||
init_logging(config.log_file, "[blue]client[/blue]")
|
||||
client.client_mode(config)
|
||||
else:
|
||||
init_logging(config.log_file, "[red]server[/red]")
|
||||
add = AntiDriftDaemon(config)
|
||||
add.run()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user