add unhandled exception logging

This commit is contained in:
Lukas 2021-09-25 15:28:26 +02:00
parent dddd444e4d
commit 1df3e08bee
2 changed files with 38 additions and 7 deletions

View File

@ -201,7 +201,7 @@ class TSbot:
def notifyAdmin(self): def notifyAdmin(self):
""" """
Ping every available admin Ping every available admin.
""" """
clients = self.bot.clientlist() clients = self.bot.clientlist()
@ -294,7 +294,7 @@ class TSbot:
def list(self, what, invkr_id): def list(self, what, invkr_id):
""" """
Message the invoker of the function either a list of channels or a list of clients Message the invoker of the function either a list of channels or a list of clients.
""" """
if what == "channel": if what == "channel":
@ -317,7 +317,7 @@ class TSbot:
def isqueryclient(self, cluid): def isqueryclient(self, cluid):
""" """
Check if the given client-uid is a query client Check if the given client-uid is a query client.
""" """
# client = self.bot.clientlist(uid=uid) # client = self.bot.clientlist(uid=uid)
@ -332,7 +332,7 @@ class TSbot:
def isadmin(self, cldbid): def isadmin(self, cldbid):
""" """
Check if the given client-databaseid is an admin Check if the given client-databaseid is an admin.
""" """
groups = self.bot.servergroupsbyclientid(cldbid=cldbid) groups = self.bot.servergroupsbyclientid(cldbid=cldbid)
@ -351,6 +351,21 @@ class TSbot:
def lookupcommand(self, msg, invkr_id): def lookupcommand(self, msg, invkr_id):
""" """
Every message starting with '.' gets passed and evaluated in this function.
Command Parameter 1 Parameter 2
---------------------------------------------------------
.annoy target message
.kickall message
.test
.btc / .eth
.dot / .ada
.list channel/clients
.follow
.rename nickname
.stop / .quit / .q
.pingall message
.admin
""" """
@ -424,13 +439,16 @@ class TSbot:
self.bot.sendtextmessage(targetmode=1, target=invkr_id, msg=err) self.bot.sendtextmessage(targetmode=1, target=invkr_id, msg=err)
pass pass
elif command == ".admin":
self.notifyAdmin()
else: else:
err = f"Unknown Command: [{command}]" err = f"Unknown Command: [{command}]"
self.bot.sendtextmessage(targetmode=1, target=invkr_id, msg=err) self.bot.sendtextmessage(targetmode=1, target=invkr_id, msg=err)
def printable_clientinfo(self, clid): def printable_clientinfo(self, clid):
""" """
Generate printable clientinfo from clid.
""" """
usrd = {} usrd = {}
@ -451,11 +469,11 @@ class TSbot:
def checkgrp(self): def checkgrp(self):
""" """
Print all assigned groups for every connected client.
""" """
clients = self.bot.clientlist() clients = self.bot.clientlist()
clients_cldbid = [client["client_database_id"] for client in clients if client["client_type"] != "1"] # clients_cldbid = [client["client_database_id"] for client in clients if client["client_type"] != "1"]
clients = self.bot.clientlist(groups=True) clients = self.bot.clientlist(groups=True)
clients_groups = [client["client_servergroups"] for client in clients if client["client_type"] != "1"] clients_groups = [client["client_servergroups"] for client in clients if client["client_type"] != "1"]
@ -468,6 +486,9 @@ def main():
# Start logger # Start logger
log = util.setupLogger() log = util.setupLogger()
# Log unhandled exception
sys.excepthook = util.unhandledException
# Load Dotenv # Load Dotenv
dotenv_file = dotenv.find_dotenv() dotenv_file = dotenv.find_dotenv()
if not dotenv_file: if not dotenv_file:

10
util.py
View File

@ -10,14 +10,24 @@ __status__ = "Development"
# Default # Default
import sys
import logging import logging
from datetime import date from datetime import date
def unhandledException(exc_type, exc_value, exc_traceback):
if issubclass(exc_type, KeyboardInterrupt):
sys.__excepthook__(exc_type, exc_value, exc_traceback)
return
log.critical("Uncaught exception", exc_info=(exc_type, exc_value, exc_traceback))
def setupLogger(): def setupLogger():
""" """
""" """
global log
log = logging.getLogger() log = logging.getLogger()
now = date.today().strftime("%Y-%m-%d") now = date.today().strftime("%Y-%m-%d")
handler = logging.FileHandler(f"myTS3_{now}.log", encoding="utf-8") handler = logging.FileHandler(f"myTS3_{now}.log", encoding="utf-8")