Commit 1d1086b4 authored by Ralf's avatar Ralf

more consistent logging

parent 3c0b6901
......@@ -3,6 +3,8 @@ import selectors
import socket
import labyrinth
import random
import sys
from util import log_file
from treasure import Treasure
class Game:
......@@ -56,9 +58,17 @@ class Game:
callback = key.data
callback(key.fileobj, mask)
def log(self, *args):
def logDebug(self, *args):
if self.logFile is not None:
print(*args, file=self.logFile, flush=True)
log_file(self.logFile, *args)
def logInfo(self, *args):
log_file(sys.stdout, *args)
if self.logFile is not None:
log_file(self.logFile, *args)
def logError(self, *args):
log_file(sys.stderr, *args)
if self.logFile is not None:
log_file(self.logFile, *args)
def showAdmins(self):
if not self.admins: return
......
......@@ -37,16 +37,17 @@ class Player(Thing):
self.uid = playerUID
self.obstinacy = 0
playerUID += 1
log_stdout("New player: {0}".format(self.uid))
self.game.logInfo("New player: {0}".format(self.uid))
# low-level functions
def send(self, data, end='\n'):
assert type(data) == str
self.game.logDebug("To [{0}]: {1}".format(self.uid, data))
send_async(self.conn, (data+end).encode('utf-8'))
def close(self): # only to be called by Game - it has to remove us from its list
self.conn.close()
log_stdout("Player left: {0}".format(self.uid))
self.game.logInfo("Player left: {0}".format(self.uid))
return self.conn
def read(self, conn, mask):
......@@ -98,7 +99,7 @@ class Player(Thing):
# high(er)-level functions
def readCmd(self, cmd):
self.game.log("Someone [{1}] wrote '{0}'".format(cmd, self.uid))
self.game.logDebug("From [{1}]: '{0}'".format(cmd, self.uid))
words = cmd.lower().split()
if not words:
self.send("What did you mean?")
......@@ -172,6 +173,7 @@ class Player(Thing):
self.send("There's nothing here, sorry!")
return
if isinstance(objs[0], treasure.Treasure):
self.game.logInfo("Player {0} found the treasure".format(self.uid))
self.send("Finally - after a long journey - you are close enough to the treasure to reach for it. What a moment! But what's that? The treasure fades to transparent. You try to grab it, but it's too late. Quantum effects seem to be really weird in this place. All you are left with is a note, saying \"I found the treasure in the Haxogreen maze, and didn't even get a lousy T-Shirt\".")
for player in self.game.players:
if player == self: continue
......
......@@ -6,20 +6,12 @@ def fire_and_forget(f):
try:
f()
except Exception:
log_stderr("fire_and_forget: Got exception out of callback:\n%s" % traceback.format_exc())
log_file(sys.stderr, "fire_and_forget: Got exception out of callback:\n%s" % traceback.format_exc())
t = threading.Thread(target=_fire_and_forget)
t.start()
def send_async(conn, data):
fire_and_forget(lambda: conn.send(data))
def log_stdout(*args):
print(*args, file=sys.stdout, flush=True)
def log_stderr(*args):
print(*args, file=sys.stdout, flush=True)
def compose(*args):
def do_both():
for f in reversed(args):
f()
return do_both
def log_file(file, *args):
print(*args, file=file, flush=True)
\ No newline at end of file
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