Commit 44bbf786 authored by Ralf's avatar Ralf

add some logging

parent e74dbd75
......@@ -5,7 +5,7 @@ import labyrinth
import random
class Game:
def __init__(self, labyrinthFileName, port=1234):
def __init__(self, labyrinthFileName, logFileName=None, port=1234):
self.players = []
with open(labyrinthFileName,'r') as f:
self.labyrinth = labyrinth.Labyrinth(stream=f)
......@@ -14,6 +14,7 @@ class Game:
self.sock.listen(100)
self.sel = selectors.DefaultSelector()
self.sel.register(self.sock, selectors.EVENT_READ, self.accept)
self.logFile = None if logFileName is None else open(logFileName, 'a')
# low-level functions
def accept(self, sock, mask):
......@@ -36,6 +37,8 @@ class Game:
player.close()
self.players.clear()
self.sock.close()
if self.logFile is not None:
self.logFile.close()
def run(self):
while True:
......@@ -44,5 +47,9 @@ class Game:
callback = key.data
callback(key.fileobj, mask)
def log(self, *args):
if self.logFile is not None:
print(*args, file=self.logFile, flush=True)
# high(er)-level functions
......@@ -7,10 +7,12 @@ parser = argparse.ArgumentParser(description='Run a door')
parser.add_argument("-p", "--port", default=1234, type=int, dest="port",
help="Port to listen on (default: 1234)")
parser.add_argument("-l", "--labyrinth-file", required=True, dest="labyrinthFileName",
help="Fallback mode for unfunctional hardware: Depend on less sensor input")
help="Name of labyrinth file")
parser.add_argument("--log-file", dest="logFileName",
help="Name of log file")
args = parser.parse_args()
game = Game(labyrinthFileName=args.labyrinthFileName, port=args.port)
game = Game(labyrinthFileName=args.labyrinthFileName, port=args.port, logFileName=args.logFileName)
try:
game.run()
......
......@@ -45,6 +45,7 @@ class Player(Thing):
# high(er)-level functions
def readCmd(self, cmd):
self.game.log("Someone wrote '{0}'".format(cmd))
words = cmd.lower().split()
if not words:
self.send("What did you mean?")
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment