From 89a589af7299111b765f498f5d2362dbf48f8894 Mon Sep 17 00:00:00 2001 From: XxKingsxX-Pinu <58925163+rai68@users.noreply.github.com> Date: Tue, 9 Jul 2024 04:22:18 +1000 Subject: [PATCH] changes to logging and threading --- .gitignore | 2 ++ pwnagotchi/agent.py | 9 +++++--- pwnagotchi/ai/train.py | 3 ++- pwnagotchi/defaults.toml | 1 + pwnagotchi/log.py | 42 ++++++++++++++++++++++++++-------- pwnagotchi/mesh/utils.py | 6 +++-- pwnagotchi/plugins/__init__.py | 5 +++- pwnagotchi/ui/display.py | 3 ++- pwnagotchi/ui/web/server.py | 7 ++++-- 9 files changed, 58 insertions(+), 20 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..fd20fddf --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ + +*.pyc diff --git a/pwnagotchi/agent.py b/pwnagotchi/agent.py index b13fd888..07f3665e 100644 --- a/pwnagotchi/agent.py +++ b/pwnagotchi/agent.py @@ -4,7 +4,8 @@ import os import re import logging import asyncio -import _thread +#import _thread +import threading import pwnagotchi import pwnagotchi.utils as utils @@ -304,7 +305,8 @@ class Agent(Client, Automata, AsyncAdvertiser, AsyncTrainer): raise def start_session_fetcher(self): - _thread.start_new_thread(self._fetch_stats, ()) + #_thread.start_new_thread(self._fetch_stats, ()) + threading.Thread(target=self._fetch_stats, args=(), name="Session Fetcher").start() def _fetch_stats(self): while True: @@ -387,7 +389,8 @@ class Agent(Client, Automata, AsyncAdvertiser, AsyncTrainer): def start_event_polling(self): # start a thread and pass in the mainloop - _thread.start_new_thread(self._event_poller, (asyncio.get_event_loop(),)) + #_thread.start_new_thread(self._event_poller, (asyncio.get_event_loop(),)) + threading.Thread(target=self._event_poller, args=(asyncio.get_event_loop(),), name="Event Polling") def is_module_running(self, module): s = self.session() diff --git a/pwnagotchi/ai/train.py b/pwnagotchi/ai/train.py index 58758d6f..035ccfd0 100644 --- a/pwnagotchi/ai/train.py +++ b/pwnagotchi/ai/train.py @@ -111,7 +111,8 @@ class AsyncTrainer(object): return self._training_epochs def start_ai(self): - _thread.start_new_thread(self._ai_worker, ()) + #_thread.start_new_thread(self._ai_worker, ()) + threading.Thread(target=self._ai_worker, args=(), name="AI Worker").start() def _save_ai(self): logging.info("[AI] saving model to %s ..." % self._nn_path) diff --git a/pwnagotchi/defaults.toml b/pwnagotchi/defaults.toml index 59a9ba47..3fa07753 100644 --- a/pwnagotchi/defaults.toml +++ b/pwnagotchi/defaults.toml @@ -112,6 +112,7 @@ main.mon_max_blind_epochs = 50 main.no_restart = false main.log.path = "/etc/pwnagotchi/log/pwnagotchi.log" +main.log.path-debug = "/etc/pwnagotchi/log/pwnagotchi.log" main.log.rotation.enabled = true main.log.rotation.size = "10M" diff --git a/pwnagotchi/log.py b/pwnagotchi/log.py index 5cf120e9..7b56a2c4 100644 --- a/pwnagotchi/log.py +++ b/pwnagotchi/log.py @@ -217,25 +217,45 @@ class LastSession(object): def setup_logging(args, config): cfg = config['main']['log'] filename = cfg['path'] + filenameDebug = cfg['path'] - formatter = logging.Formatter("[%(asctime)s] [%(levelname)s] %(message)s") - root = logging.getLogger() - - root.setLevel(logging.DEBUG if args.debug else logging.INFO) + #global formatter + formatter = logging.Formatter("[%(asctime)s] [%(levelname)s] [%(threadName)s] : %(message)s") + logger = logging.getLogger() + + for handler in logger.handlers: + handler.setLevel(logging.DEBUG if args.debug else logging.INFO) + handler.setFormatter(formatter) + + + logger.setLevel(logging.DEBUG if args.debug else logging.INFO) if filename: # since python default log rotation might break session data in different files, # we need to do log rotation ourselves log_rotation(filename, cfg) + log_rotation(filenameDebug, cfg) - file_handler = logging.FileHandler(filename) - file_handler.setFormatter(formatter) - root.addHandler(file_handler) + + + # File handler for logging all normal messages + file_handler = logging.FileHandler(filename) #creates new + file_handler.setLevel(logging.INFO) + file_handler.setFormatter(formatter) + logger.addHandler(file_handler) - console_handler = logging.StreamHandler() - console_handler.setFormatter(formatter) - root.addHandler(console_handler) + # File handler for logging all debug messages + file_handler = logging.FileHandler(filenameDebug) #creates new + file_handler.setLevel(logging.DEBUG) + file_handler.setFormatter(formatter) + logger.addHandler(file_handler) + # Console handler for logging debug messages if args.debug is true else just log normal + #console_handler = logging.StreamHandler() #creates new + #console_handler.setLevel(logging.DEBUG if args.debug else logging.INFO) + #console_handler.setFormatter(formatter) + #logger.addHandler(console_handler) + if not args.debug: # disable scapy and tensorflow logging logging.getLogger("scapy").disabled = True @@ -250,6 +270,8 @@ def setup_logging(args, config): requests_log.prpagate = False + + def log_rotation(filename, cfg): rotation = cfg['rotation'] if not rotation['enabled']: diff --git a/pwnagotchi/mesh/utils.py b/pwnagotchi/mesh/utils.py index c46bae5d..27d7dee4 100644 --- a/pwnagotchi/mesh/utils.py +++ b/pwnagotchi/mesh/utils.py @@ -1,4 +1,5 @@ -import _thread +#import _thread +import threading import logging import time @@ -41,7 +42,8 @@ class AsyncAdvertiser(object): def start_advertising(self): if self._config['personality']['advertise']: - _thread.start_new_thread(self._adv_poller, ()) + #_thread.start_new_thread(self._adv_poller, ()) + threading.Thread(target=self._adv_poller,args=(), name="Grid").start() grid.set_advertisement_data(self._advertisement) grid.advertise(True) diff --git a/pwnagotchi/plugins/__init__.py b/pwnagotchi/plugins/__init__.py index 0d913b49..d0212db7 100644 --- a/pwnagotchi/plugins/__init__.py +++ b/pwnagotchi/plugins/__init__.py @@ -1,4 +1,5 @@ import _thread +import threading import glob import importlib import importlib.util @@ -95,8 +96,10 @@ def one(plugin_name, event_name, *args, **kwargs): if callback is not None and callable(callback): try: lock_name = "%s::%s" % (plugin_name, cb_name) + loggingFormat = "%s.%s" % (plugin_name, cb_name) locked_cb_args = (lock_name, callback, *args, *kwargs) - _thread.start_new_thread(locked_cb, locked_cb_args) + #_thread.start_new_thread(locked_cb, locked_cb_args) + threading.Thread(target=locked_cb, args=locked_cb_args, name=loggingFormat).start() except Exception as e: logging.error("error while running %s.%s : %s" % (plugin_name, cb_name, e)) logging.error(e, exc_info=True) diff --git a/pwnagotchi/ui/display.py b/pwnagotchi/ui/display.py index c64adcb9..cc749a46 100644 --- a/pwnagotchi/ui/display.py +++ b/pwnagotchi/ui/display.py @@ -21,7 +21,8 @@ class Display(View): self._canvas_next = None self._render_thread_instance = threading.Thread( target=self._render_thread, - daemon=True + daemon=True, + name="Renderer" ) self._render_thread_instance.start() diff --git a/pwnagotchi/ui/web/server.py b/pwnagotchi/ui/web/server.py index a2e96613..8f4c9a2e 100644 --- a/pwnagotchi/ui/web/server.py +++ b/pwnagotchi/ui/web/server.py @@ -1,4 +1,5 @@ -import _thread +#import _thread +import threading import secrets import logging import os @@ -25,7 +26,9 @@ class Server: self._origin = self._config['origin'] if self._enabled: - _thread.start_new_thread(self._http_serve, ()) + #_thread.start_new_thread(self._http_serve, ()) + logging.info("Starting WebServer thread") + self._thread = threading.Thread(target=self._http_serve, name="WebServer").start() def _http_serve(self): if self._address is not None: