2017-12-30 15:20:25 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
import os
|
|
|
|
import datetime
|
2018-01-05 09:42:31 +00:00
|
|
|
import traceback
|
|
|
|
import sys
|
|
|
|
import sendmail
|
|
|
|
|
2017-12-30 15:20:25 +00:00
|
|
|
|
2017-12-30 15:33:34 +00:00
|
|
|
class Logger(object):
|
2018-01-05 09:42:31 +00:00
|
|
|
"""
|
|
|
|
builds log files, writes the log messages.
|
|
|
|
If a critical error occurs, handles the bugtracking and error
|
|
|
|
messages.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, config):
|
|
|
|
"""
|
|
|
|
logs everything & sends bugtracking messages.
|
2017-12-30 15:20:25 +00:00
|
|
|
|
2018-01-05 09:42:31 +00:00
|
|
|
:param config: config file
|
|
|
|
"""
|
|
|
|
self.config = config
|
2017-12-30 15:20:25 +00:00
|
|
|
|
|
|
|
# initialize logging
|
2018-01-05 09:42:31 +00:00
|
|
|
if config["logging"]["logpath"]:
|
|
|
|
self.logpath = os.path.join(self.config["logging"]["logpath"], str(datetime.datetime.now()))
|
2017-12-30 15:20:25 +00:00
|
|
|
else:
|
|
|
|
self.logpath = os.path.join("logs", str(datetime.datetime.now()))
|
|
|
|
print("Path of logfile: " + self.logpath)
|
|
|
|
|
2018-01-05 09:42:31 +00:00
|
|
|
# intialize shutdown contact
|
|
|
|
try:
|
|
|
|
self.no_shutdown_contact = False
|
|
|
|
self.contact = self.config['mail']['contact']
|
|
|
|
except KeyError:
|
|
|
|
self.no_shutdown_contact = True
|
|
|
|
|
2017-12-30 15:20:25 +00:00
|
|
|
def log(self, message):
|
|
|
|
"""
|
|
|
|
Writing an error message & sometimes a traceback to a logfile in logs/
|
|
|
|
and prints it.
|
|
|
|
|
2017-12-30 15:33:34 +00:00
|
|
|
:param message: (string) Logger message to be displayed
|
2017-12-30 15:20:25 +00:00
|
|
|
"""
|
|
|
|
time = str(datetime.datetime.now())
|
|
|
|
line = "[" + time + "] " + message + "\n"
|
|
|
|
with open(self.logpath, 'a') as f:
|
|
|
|
try:
|
|
|
|
f.write(line)
|
|
|
|
except UnicodeEncodeError:
|
2018-01-05 09:52:15 +00:00
|
|
|
message = "Failed to save log message due to UTF-8 error. "
|
|
|
|
message = message + self.generate_tb(sys.exc_info())
|
|
|
|
self.log(message)
|
2017-12-30 15:20:25 +00:00
|
|
|
print(line, end="")
|
|
|
|
|
2018-01-05 09:42:31 +00:00
|
|
|
def generate_tb(self, exc):
|
|
|
|
tb = traceback.extract_tb(exc[2]) # returns StackSummary object
|
|
|
|
tb = "\n".join(tb.format()) # string of the actual traceback
|
|
|
|
message = ("Traceback (most recent call last):\n",
|
|
|
|
tb,
|
|
|
|
exc[0].__name__) # the type of the Exception
|
|
|
|
message = "".join(message) # concatenate to full traceback message
|
|
|
|
return message
|
|
|
|
|
|
|
|
def shutdown(self, tb):
|
|
|
|
""" If something breaks, it shuts down the bot and messages the owner.
|
|
|
|
|
|
|
|
:param tb: (string) traceback
|
|
|
|
"""
|
|
|
|
logmessage = "Shit went wrong, closing down.\n" + tb + "\n\n"
|
|
|
|
if self.no_shutdown_contact:
|
|
|
|
self.log(logmessage)
|
|
|
|
return
|
|
|
|
logmessage = logmessage + "Sending message to " + self.contact
|
|
|
|
self.log(logmessage)
|
|
|
|
try:
|
|
|
|
mailer = sendmail.Mailer(self.config)
|
2018-01-05 10:20:07 +00:00
|
|
|
mailer.send(tb, self.contact, "Ticketfrei Crash Report", attachment=self.logpath)
|
2018-01-05 09:42:31 +00:00
|
|
|
except:
|
2018-01-05 09:52:15 +00:00
|
|
|
self.log("Error while shutdown: " + self.generate_tb(sys.exc_info()))
|
2018-01-05 09:42:31 +00:00
|
|
|
print()
|