Improve logging and log file handling
This commit is contained in:
@@ -18,7 +18,7 @@ class Block(BaseModel):
|
|||||||
class Config(BaseModel):
|
class Config(BaseModel):
|
||||||
blackblocks: List[Block]
|
blackblocks: List[Block]
|
||||||
whiteblocks: List[Block]
|
whiteblocks: List[Block]
|
||||||
window_log_file: Path = Path("/home/felixm/tmp/antidrift/history.log")
|
window_log_file: Path = Path("~/tmp/antidrift/history.log")
|
||||||
daemon_log_file: Path = Path()
|
daemon_log_file: Path = Path()
|
||||||
client_log_file: Path = Path()
|
client_log_file: Path = Path()
|
||||||
config_file: Path = Path()
|
config_file: Path = Path()
|
||||||
@@ -35,6 +35,10 @@ class Config(BaseModel):
|
|||||||
config_dict = yaml.safe_load(f)
|
config_dict = yaml.safe_load(f)
|
||||||
config = cls(**config_dict)
|
config = cls(**config_dict)
|
||||||
config.config_file = Path(config_file)
|
config.config_file = Path(config_file)
|
||||||
|
# Expand the paths for the log files
|
||||||
|
config.window_log_file = Path(os.path.expanduser(config.window_log_file))
|
||||||
|
config.daemon_log_file = Path(os.path.expanduser(config.daemon_log_file))
|
||||||
|
config.client_log_file = Path(os.path.expanduser(config.client_log_file))
|
||||||
return config
|
return config
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
from datetime import datetime
|
||||||
|
import csv
|
||||||
import dbus
|
import dbus
|
||||||
import dbus.service
|
import dbus.service
|
||||||
import logging
|
import logging
|
||||||
@@ -148,12 +150,7 @@ class AntiDriftDaemon(dbus.service.Object):
|
|||||||
s = " ".join(map(lambda b: b.name, self.state.active_whiteblocks))
|
s = " ".join(map(lambda b: b.name, self.state.active_whiteblocks))
|
||||||
return f"intention is {s} work" if s else "no intention"
|
return f"intention is {s} work" if s else "no intention"
|
||||||
|
|
||||||
def run(self, debug: bool = False):
|
def log_window(self):
|
||||||
def _enforce():
|
|
||||||
self.enforce()
|
|
||||||
GLib.timeout_add(self.config.polling_cycle_ms, _enforce)
|
|
||||||
|
|
||||||
def _log():
|
|
||||||
self.config.window_log_file.parent.mkdir(parents=True, exist_ok=True)
|
self.config.window_log_file.parent.mkdir(parents=True, exist_ok=True)
|
||||||
window = XWindow()
|
window = XWindow()
|
||||||
ts = int(time.time())
|
ts = int(time.time())
|
||||||
@@ -161,6 +158,29 @@ class AntiDriftDaemon(dbus.service.Object):
|
|||||||
log_line = f"{ts}, {window.name}, {window.cls}, {intention}\n"
|
log_line = f"{ts}, {window.name}, {window.cls}, {intention}\n"
|
||||||
with self.config.window_log_file.open('a') as f:
|
with self.config.window_log_file.open('a') as f:
|
||||||
f.write(log_line)
|
f.write(log_line)
|
||||||
|
|
||||||
|
def log_window(self):
|
||||||
|
window = XWindow()
|
||||||
|
utc_timestamp = datetime.now().strftime('%Y-%m-%dT%H:%M:%S')
|
||||||
|
|
||||||
|
# Remove time strings and numbers with decimal from window.name
|
||||||
|
window_name = re.sub(r'\b\d\d:\d\d\b', '', window.name)
|
||||||
|
window_name = re.sub(r'\b\d+.\d\d\b', '', window_name)
|
||||||
|
window_name = re.sub(r'[+-]\d+.\d+%', '', window_name)
|
||||||
|
|
||||||
|
intention = self.get_intention()
|
||||||
|
log_line = [utc_timestamp, window_name, window.cls, intention]
|
||||||
|
with self.config.window_log_file.open('a', newline='') as f:
|
||||||
|
writer = csv.writer(f)
|
||||||
|
writer.writerow(log_line)
|
||||||
|
|
||||||
|
def run(self, debug: bool = False):
|
||||||
|
def _enforce():
|
||||||
|
self.enforce()
|
||||||
|
GLib.timeout_add(self.config.polling_cycle_ms, _enforce)
|
||||||
|
|
||||||
|
def _log():
|
||||||
|
self.log_window()
|
||||||
ONE_MINUTE_IN_MS = 60 * 1000
|
ONE_MINUTE_IN_MS = 60 * 1000
|
||||||
GLib.timeout_add(ONE_MINUTE_IN_MS, _log)
|
GLib.timeout_add(ONE_MINUTE_IN_MS, _log)
|
||||||
|
|
||||||
@@ -180,6 +200,7 @@ class AntiDriftDaemon(dbus.service.Object):
|
|||||||
monitor.connect("changed", reload_callback)
|
monitor.connect("changed", reload_callback)
|
||||||
monitors.append(monitor)
|
monitors.append(monitor)
|
||||||
|
|
||||||
|
self.config.window_log_file.parent.mkdir(parents=True, exist_ok=True)
|
||||||
_enforce()
|
_enforce()
|
||||||
_log()
|
_log()
|
||||||
mainloop = GLib.MainLoop()
|
mainloop = GLib.MainLoop()
|
||||||
|
|||||||
8
main.py
8
main.py
@@ -91,10 +91,11 @@ def kill_existing_antidrift():
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
def main_daemon(config):
|
def main_daemon():
|
||||||
if os.geteuid() == 0:
|
if os.geteuid() == 0:
|
||||||
newpid = os.fork()
|
newpid = os.fork()
|
||||||
if newpid == 0:
|
if newpid == 0:
|
||||||
|
config = Config.load(os.path.expanduser("~/.config/antidrift.yaml"))
|
||||||
init_logging(config.daemon_log_file)
|
init_logging(config.daemon_log_file)
|
||||||
AntiDriftDaemon(config).run()
|
AntiDriftDaemon(config).run()
|
||||||
else:
|
else:
|
||||||
@@ -103,18 +104,19 @@ def main_daemon(config):
|
|||||||
cmd = ["sudo", "antidrift", "--daemon"]
|
cmd = ["sudo", "antidrift", "--daemon"]
|
||||||
subprocess.Popen(cmd)
|
subprocess.Popen(cmd)
|
||||||
else:
|
else:
|
||||||
|
config = Config.load(os.path.expanduser("~/.config/antidrift.yaml"))
|
||||||
init_logging(config.daemon_log_file, dev_mode=True)
|
init_logging(config.daemon_log_file, dev_mode=True)
|
||||||
AntiDriftDaemon(config).run(debug=True)
|
AntiDriftDaemon(config).run(debug=True)
|
||||||
|
|
||||||
|
|
||||||
def main() -> None:
|
def main() -> None:
|
||||||
"""Main routine that dispatches to client or daemon"""
|
"""Main routine that dispatches to client or daemon"""
|
||||||
config = Config.load(os.path.expanduser("~/.config/antidrift.yaml"))
|
|
||||||
check_for_xdotool()
|
check_for_xdotool()
|
||||||
args = get_args()
|
args = get_args()
|
||||||
if args.daemon:
|
if args.daemon:
|
||||||
main_daemon(config)
|
main_daemon()
|
||||||
else:
|
else:
|
||||||
|
config = Config.load(os.path.expanduser("~/.config/antidrift.yaml"))
|
||||||
antidrift.client.run(args, config)
|
antidrift.client.run(args, config)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user