Permalink
Browse files

Added logging.

  • Loading branch information...
1 parent ef3f04d commit 86b7a1f445186068324e64824ee168a185b22361 @frontierpsycho committed Oct 5, 2012
Showing with 65 additions and 4 deletions.
  1. +9 −3 levelui.py
  2. +32 −0 logs/logging.conf
  3. +24 −1 main.py
View
@@ -9,12 +9,16 @@
import threading
import operator
-
import math
import re
+import logging
+import logging.config
import graphics_helpers
+logging.config.fileConfig('logs/logging.conf')
+logger = logging.getLogger('ui')
+
class LevelUI(threading.Thread):
def __init__(self, level_number, in_queue, out_queue, starting_score=0):
threading.Thread.__init__(self)
@@ -60,8 +64,10 @@ def run(self):
self.stopped = True # submarine starts stopped
self.completed = False # level not completed
- self.check_input()
+ logger.debug("UI built, entering loops")
+ self.check_input()
+
self.root.mainloop()
def check_input(self):
@@ -187,7 +193,7 @@ def steer(self, direction, speed=2):
dx, dy = (0, speed)
else:
# panic! Just kidding.
- print "Received", direction,"and don't know how to handle it!"
+ logger.error("Received %s and don't know how to handle it!" % direction)
self.canvas.move(self.submarine, dx, dy)
if not self.check_underwater(self.submarine):
View
@@ -0,0 +1,32 @@
+[loggers]
+keys=root
+
+[logger_root]
+handlers=screen,file
+level=DEBUG
+
+[formatters]
+keys=simple,complex
+
+[formatter_simple]
+format=%(asctime)s - %(name)s - %(levelname)s - %(message)s
+
+[formatter_complex]
+format=%(asctime)s - %(name)s - %(levelname)s - %(module)s : %(lineno)d - %(message)s
+
+[handlers]
+keys=file,screen
+
+[handler_file]
+class=handlers.TimedRotatingFileHandler
+interval=midnight
+backupCount=5
+formatter=complex
+level=DEBUG
+args=('logs/torpedoalley.log',)
+
+[handler_screen]
+class=StreamHandler
+formatter=simple
+level=INFO
+args=(sys.stdout,)
View
25 main.py
@@ -9,8 +9,13 @@
import random
import time
import math
+import logging
+import logging.config
from threading import Timer
+logging.config.fileConfig('logs/logging.conf')
+logger = logging.getLogger('main')
+
class TorpedoAlley:
def __init__(self):
self.in_queue = Queue()
@@ -43,6 +48,8 @@ def __init__(self):
self.total_score = 0
+ logger.debug("TorpedoAlley created")
+
def change_state(self, transition):
self._current_state.cleanup()
self._current_state_name = self.current_transitions()[transition]
@@ -63,12 +70,14 @@ def run(self):
while not quit:
try:
event = self.in_queue.get_nowait()
+ logger.debug("Received event: %s" % str(event))
if event in self.current_transitions():
self.out_queue.put("close")
self.total_score += self._current_state.score
self.change_state(event)
self.build_ui()
+ logger.debug("Transitioned to state %s" % self._current_state_name)
else:
self._current_state.handle(event)
@@ -77,6 +86,8 @@ def run(self):
except Empty:
pass
+ logger.debug("TorpedoAlley terminated.")
+
# state logic
class Menu:
pass
@@ -91,7 +102,10 @@ def __init__(self, level_number):
self.score = 0
self.score_per_ship = 50*self.level_number
+ logger.debug("Level %d created." % level_number)
+
def cleanup(self):
+ logger.debug("Cleaning up level %d" % self.level_number)
self.ship_timer.cancel()
def display(self, in_queue, out_queue):
@@ -102,25 +116,32 @@ def display(self, in_queue, out_queue):
# schedule random ship appearances
self.ship_timer.start()
+ logger.debug("UI built for level %d" % self.level_number)
+
return ui
def handle(self, event):
if re.match(r'launch (\d+),(\d+)', event):
+ logger.debug("Torpedo launched")
+
# torpedo launched, we could throttle number here or somesuch
# for now, we simply notify the UI to launch it
self.out_queue.put(event)
elif re.match(r'hit (\d+)', event):
self.increase_score()
+ logger.debug("Hit! Score is %d" % self.score)
+
self.out_queue.put(event)
else:
- print event
+ logger.debug("Received unhandled event: %s" % str(event))
def increase_score(self):
self.score += self.score_per_ship
self.out_queue.put("score %d" % self.score)
def ship_appearance(self):
+ logger.debug("A new ship appears.")
self.out_queue.put("ship")
self.number_of_ships += 1
@@ -132,9 +153,11 @@ def ship_appearance(self):
self.ship_timer = Timer(nextTime, self.ship_appearance, ())
self.ship_timer.start()
else:
+ logger.debug("All ships arrived")
self.level_complete()
def level_complete(self):
+ logger.debug("Level complete")
self.out_queue.put("complete")
class Exit:

0 comments on commit 86b7a1f

Please sign in to comment.