From 33cbf9670d6abde5544f8be0a837cd19ccb79973 Mon Sep 17 00:00:00 2001 From: Jonathan Weth Date: Mon, 27 Jul 2020 12:13:26 +0200 Subject: Add logging Close #5 --- service.py | 40 ++++++++++++++++++++++++++++++---------- 1 file changed, 30 insertions(+), 10 deletions(-) (limited to 'service.py') diff --git a/service.py b/service.py index 9d5394f..2033713 100755 --- a/service.py +++ b/service.py @@ -1,6 +1,7 @@ #!/usr/bin/env python3 import argparse import json +import logging import signal import time from threading import Thread @@ -12,9 +13,12 @@ import dbus.service from session import Session from trigger import TriggerServerThread +logging.basicConfig(format="%(asctime)s %(levelname)s: %(message)s", level=logging.INFO) + ONE_TIME_SERVICE = False ALLOW_ONLY_ONE_SESSION = True + class RWAService(dbus.service.Object): def __init__(self, loop, mockup_mode: bool = False): self.loop = loop @@ -30,10 +34,16 @@ class RWAService(dbus.service.Object): self.sessions = {} super().__init__(name, "/RWA") + logging.info("DBus service has been started.") + @dbus.service.method("org.ArcticaProject.RWA", out_signature="s") def start(self): """Start a new remote session.""" if ALLOW_ONLY_ONE_SESSION and len(self.sessions.values()) > 0: + logging.warning( + "There is already one session running and the service is configured to allow only one " + "session, so this session won't be started." + ) return json.dumps({"status": "error"}) # Start session @@ -48,6 +58,8 @@ class RWAService(dbus.service.Object): return_json = session.client_meta return_json["status"] = "success" + logging.info(f"New session #{session.pid} was started with meta {return_json}.") + return json.dumps(return_json) @dbus.service.method("org.ArcticaProject.RWA", in_signature="i", out_signature="s") @@ -86,29 +98,31 @@ class RWAService(dbus.service.Object): def _update_session(self, pid: int): """Update the status of a session.""" + logging.info(f"Update status for session #{pid} …") + try: session = self.sessions[pid] except KeyError: - print(f"Session #{pid}") - print("Session is dead.") + logging.warning(" Session is dead.") return - print(f"Session #{session.pid}") - # Check if VNC process is still running running = session.vnc_process_running if running: - print("Session is running") + logging.info(" Session is running.") elif session.status_text == "stopped" and session.pid in self.sessions: + logging.warning(" Session is dead.") + del self.sessions[session.pid] else: - print("Session is dead.") + logging.warning(" VNC was stopped, so session is dead.") session.stop() del self.sessions[session.pid] def _update_sessions(self): """Go through all running sessions and update their status using ``_update_session``.""" + logging.info("Started update service for sessions.") while len(self.sessions.values()) > 0: for session in list(self.sessions.values()): self._update_session(session.pid) @@ -116,23 +130,26 @@ class RWAService(dbus.service.Object): time.sleep(2) self.update_service_running = False + logging.info("Stopped update service for sessions.") if ONE_TIME_SERVICE: self._stop_all() def _trigger(self, token: str) -> bool: """Trigger a specific session via trigger token.""" - print("Triggered with token", token) + logging.info(f"Triggered with token {token}") for session in self.sessions.values(): if token == session.trigger_token: - print("Trigger session", session) + logging.info(f"Session #{session.pid} matches the token.") session.trigger() return True + logging.warning(" No matching session found for this token.") return False def _stop_all(self): """Stop all sessions and this daemon.""" + logging.info("Stop all sessions and exit service.") for session in list(self.sessions.values()): session.stop() del self.sessions[session.pid] @@ -172,7 +189,10 @@ if __name__ == "__main__": mockup_mode = args.mockup_mode if mockup_mode: - print("All API responses are faked and should NOT BE USED IN " + "PRODUCTION!") + logging.warning( + "All API responses are faked and should NOT BE USED IN PRODUCTION!" + ) + import dbus.mainloop.glib from gi.repository import GLib @@ -182,7 +202,7 @@ if __name__ == "__main__": object = RWAService(loop, mockup_mode) def signal_handler(sig, frame): - print("You pressed Ctrl+C!") + logging.info("Service was terminated.") object._stop_all() signal.signal(signal.SIGINT, signal_handler) -- cgit v1.2.3