.BANCHO. Thread safe log to file

This commit is contained in:
Nyo 2016-06-06 12:39:24 +02:00
parent bd8c810f45
commit c8f9825e6b
5 changed files with 53 additions and 5 deletions

View File

@ -5,6 +5,7 @@ from helpers.systemHelper import runningUnderUnix
from objects import glob
ENDL = "\n" if runningUnderUnix() else "\r\n"
def logMessage(message, alertType = "INFO", messageColor = bcolors.ENDC, discord = False, alertDev = False, of = None, stdout = True):
"""
Logs a message to stdout/discord/file
@ -52,9 +53,12 @@ def logMessage(message, alertType = "INFO", messageColor = bcolors.ENDC, discord
# Log to file if needed
if of != None:
# TODO: Lock
try:
glob.fLocks.lockFile(of)
with open(".data/{}".format(of), "a") as f:
f.write(finalMessage+ENDL)
finally:
glob.fLocks.unlockFile(of)
def warning(message, discord = False, alertDev = False):
"""

20
objects/fileLocks.py Normal file
View File

@ -0,0 +1,20 @@
import threading
class fileLocks:
def __init__(self):
# Dictionary containing threading.Lock s
self.locks = {}
def lockFile(self, fileName):
if fileName in self.locks:
# Acquire existing lock
self.locks[fileName].acquire()
else:
# Create new lock and acquire it
self.locks[fileName] = threading.Lock()
self.locks[fileName].acquire()
def unlockFile(self, fileName):
if fileName in self.locks:
# Release lock if it exists
self.locks[fileName].release()

View File

@ -3,6 +3,7 @@
from objects import tokenList
from objects import channelList
from objects import matchList
from objects import fileLocks
VERSION = "1.2"
@ -14,6 +15,7 @@ channels = channelList.channelList()
matches = matchList.matchList()
restarting = False
pool = None
fLocks = fileLocks.fileLocks()
debug = False

24
objects/logThread.py Normal file
View File

@ -0,0 +1,24 @@
'''
import threading
class task:
def __init__(self, function, args = (), kwargs = {}):
self.function = function
self.args = args
self.kwargs = kwargs
class logThread:
def __init__(self):
self.thread = threading.Thread()
self.queue = []
def enqueue(self, function, args = (), kwargs = {}):
self.queue.append(task(function, args, kwargs))
def run(self):
for i in self.queue:
self.thread = threading.Thread(i.function, i.args, i.kwargs)
self.thread.run()
self.thread.join()
self.queue = []
'''

View File

@ -5,8 +5,6 @@ from constants import matchTeamTypes
from constants import matchModModes
from constants import slotStatuses
from objects import glob
from helpers import consoleHelper
from constants import bcolors
from constants import serverPackets
from constants import dataTypes
from constants import matchTeams