Host-customized fork of https://github.com/tecnovert/basicswap/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
39 lines
1.1 KiB
39 lines
1.1 KiB
2 years ago
|
import threading
|
||
|
|
||
|
|
||
|
class ThreadWithLoggedException(threading.Thread):
|
||
|
"""
|
||
|
Similar to Thread but will log exceptions to passed logger.
|
||
|
|
||
|
Args:
|
||
|
logger: Logger instance used to log any exception in child thread
|
||
|
|
||
|
Exception is also reachable via <thread>.exception from the main thread.
|
||
|
"""
|
||
|
|
||
|
DIVIDER = "*"*80
|
||
|
|
||
|
def __init__(self, *args, **kwargs):
|
||
|
try:
|
||
|
self.logger = kwargs.pop("logger")
|
||
|
except KeyError:
|
||
|
raise Exception("Missing 'logger' in kwargs")
|
||
|
super().__init__(*args, **kwargs)
|
||
|
self.exception = None
|
||
|
|
||
|
def run(self):
|
||
|
try:
|
||
|
if self._target is not None:
|
||
|
self._target(*self._args, **self._kwargs)
|
||
|
except Exception as exception:
|
||
|
thread = threading.current_thread()
|
||
|
self.exception = exception
|
||
|
self.logger.exception(f"{self.DIVIDER}\nException in child thread {thread}: {exception}\n{self.DIVIDER}")
|
||
|
finally:
|
||
|
del self._target, self._args, self._kwargs
|
||
|
|
||
|
|
||
|
class WebsocketServerThread(ThreadWithLoggedException):
|
||
|
"""Dummy wrapper to make debug messages a bit more readable"""
|
||
|
pass
|