Improve logging and log file handling
parent
00a6d2a00a
commit
cce3c1b7f4
|
@ -18,7 +18,7 @@ class Block(BaseModel):
|
|||
class Config(BaseModel):
|
||||
blackblocks: 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()
|
||||
client_log_file: Path = Path()
|
||||
config_file: Path = Path()
|
||||
|
@ -35,6 +35,10 @@ class Config(BaseModel):
|
|||
config_dict = yaml.safe_load(f)
|
||||
config = cls(**config_dict)
|
||||
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
|
||||
|
||||
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
from datetime import datetime
|
||||
import csv
|
||||
import dbus
|
||||
import dbus.service
|
||||
import logging
|
||||
|
@ -148,19 +150,37 @@ class AntiDriftDaemon(dbus.service.Object):
|
|||
s = " ".join(map(lambda b: b.name, self.state.active_whiteblocks))
|
||||
return f"intention is {s} work" if s else "no intention"
|
||||
|
||||
def log_window(self):
|
||||
self.config.window_log_file.parent.mkdir(parents=True, exist_ok=True)
|
||||
window = XWindow()
|
||||
ts = int(time.time())
|
||||
intention = self.get_intention()
|
||||
log_line = f"{ts}, {window.name}, {window.cls}, {intention}\n"
|
||||
with self.config.window_log_file.open('a') as f:
|
||||
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.config.window_log_file.parent.mkdir(parents=True, exist_ok=True)
|
||||
window = XWindow()
|
||||
ts = int(time.time())
|
||||
intention = self.get_intention()
|
||||
log_line = f"{ts}, {window.name}, {window.cls}, {intention}\n"
|
||||
with self.config.window_log_file.open('a') as f:
|
||||
f.write(log_line)
|
||||
self.log_window()
|
||||
ONE_MINUTE_IN_MS = 60 * 1000
|
||||
GLib.timeout_add(ONE_MINUTE_IN_MS, _log)
|
||||
|
||||
|
@ -180,6 +200,7 @@ class AntiDriftDaemon(dbus.service.Object):
|
|||
monitor.connect("changed", reload_callback)
|
||||
monitors.append(monitor)
|
||||
|
||||
self.config.window_log_file.parent.mkdir(parents=True, exist_ok=True)
|
||||
_enforce()
|
||||
_log()
|
||||
mainloop = GLib.MainLoop()
|
||||
|
|
8
main.py
8
main.py
|
@ -91,10 +91,11 @@ def kill_existing_antidrift():
|
|||
pass
|
||||
|
||||
|
||||
def main_daemon(config):
|
||||
def main_daemon():
|
||||
if os.geteuid() == 0:
|
||||
newpid = os.fork()
|
||||
if newpid == 0:
|
||||
config = Config.load(os.path.expanduser("~/.config/antidrift.yaml"))
|
||||
init_logging(config.daemon_log_file)
|
||||
AntiDriftDaemon(config).run()
|
||||
else:
|
||||
|
@ -103,18 +104,19 @@ def main_daemon(config):
|
|||
cmd = ["sudo", "antidrift", "--daemon"]
|
||||
subprocess.Popen(cmd)
|
||||
else:
|
||||
config = Config.load(os.path.expanduser("~/.config/antidrift.yaml"))
|
||||
init_logging(config.daemon_log_file, dev_mode=True)
|
||||
AntiDriftDaemon(config).run(debug=True)
|
||||
|
||||
|
||||
def main() -> None:
|
||||
"""Main routine that dispatches to client or daemon"""
|
||||
config = Config.load(os.path.expanduser("~/.config/antidrift.yaml"))
|
||||
check_for_xdotool()
|
||||
args = get_args()
|
||||
if args.daemon:
|
||||
main_daemon(config)
|
||||
main_daemon()
|
||||
else:
|
||||
config = Config.load(os.path.expanduser("~/.config/antidrift.yaml"))
|
||||
antidrift.client.run(args, config)
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue