diff --git a/events/sendPrivateMessageEvent.py b/events/sendPrivateMessageEvent.py index 7af1967..13b5cfc 100644 --- a/events/sendPrivateMessageEvent.py +++ b/events/sendPrivateMessageEvent.py @@ -6,7 +6,7 @@ from objects import glob from objects import fokabot from constants import exceptions from constants import messageTemplates -from time import gmtime, strftime +from helpers import generalFunctions def handle(userToken, packetData): """ @@ -51,7 +51,7 @@ def handle(userToken, packetData): # Log to file with open(".data/chatlog_private.txt", "a") as f: - f.write("[{date}] {fro} -> {to}: {message}\n".format(date=strftime("%Y-%m-%d %H:%M:%S", gmtime()), fro=username, to=packetData["to"], message=str(packetData["message"].encode("utf-8"))[2:-1])) + f.write("[{date}] {fro} -> {to}: {message}\n".format(date=generalFunctions.getTimestamp(), fro=username, to=packetData["to"], message=str(packetData["message"].encode("utf-8"))[2:-1])) except exceptions.tokenNotFoundException: # Token not found, user disconnected consoleHelper.printColored("[!] {} tried to send a message to {}, but their token couldn't be found".format(username, packetData["to"]), bcolors.RED) diff --git a/helpers/generalFunctions.py b/helpers/generalFunctions.py index 6f04eab..fec2f28 100644 --- a/helpers/generalFunctions.py +++ b/helpers/generalFunctions.py @@ -1,5 +1,6 @@ """Some functions that don't fit in any other file""" from constants import mods +from time import gmtime, strftime def stringToBool(s): """ @@ -55,3 +56,10 @@ def readableMods(__mods): def strContains(s, w): return (' ' + w + ' ') in (' ' + s + ' ') + +def getTimestamp(): + """ + Return current time in YYYY-MM-DD HH:MM:SS format. + Used in logs. + """ + return strftime("%Y-%m-%d %H:%M:%S", gmtime()) diff --git a/helpers/logHelper.py b/helpers/logHelper.py new file mode 100644 index 0000000..4c75d23 --- /dev/null +++ b/helpers/logHelper.py @@ -0,0 +1,41 @@ +from constants import bcolors +from helpers import discordBotHelper +from helpers import generalFunctions + +def logMessage(message, alertType, messageColor, discord = False, alertDev = False, of = None): + if alertType == "INFO": + typeColor = bcolors.GREEN + elif alertType == "WARNING": + typeColor = bcolors.YELLOW + elif typeColor == "ERROR": + typeColor = bcolors.RED + else: + typeColor = bcolors.ENDC + + finalMessage = "[{time}] {type} - {message}".format(time=generalFunctions.getTimestamp(), type=alertType, message=message) + finalMessageConsole = "{typeColor}[{time}] {type}{endc} - {messageColor}{message}{endc}".format( + time=generalFunctions.getTimestamp(), + type=alertType, + message=message, + + typeColor=typeColor, + messageColor=messageColor, + endc=bcolors.ENDC) + + # Always log to console + print(finalMessageConsole) + + # Log to discord if needed + if discord == True: + discordBotHelper.sendConfidential(message, alertDev) + + # Log to file if needed + if of != None: + with open(".data/{}".format(of), "a") as f: + f.write(finalMessage) + +def warning(message, discord = False, alertDev = False): + logMessage(message, "WARNING", bcolors.YELLOW, discord, alertDev, "warnings.txt") + +def error(message, discord = False, alertDev = True): + logMessage(message, "ERROR", bcolors.RED, discord, alertDev, "errors.txt") diff --git a/pep.py b/pep.py index a2122f4..da9bed5 100644 --- a/pep.py +++ b/pep.py @@ -190,6 +190,8 @@ if __name__ == "__main__": # Server start message and console output discordBotHelper.sendConfidential("Server started!") + from helpers import logHelper + logHelper.warning("Test warning") consoleHelper.printColored("> Tornado listening for clients on 127.0.0.1:{}...".format(serverPort), bcolors.GREEN) # Start tornado