changeset 58:3da4de9decd0

Use logging module
author Thibaut Girka <thib@sitedethib.com>
date Tue, 23 Aug 2011 21:01:50 +0200
parents 694f25881d0f
children 4fe37a620b22
files eclviewer.py pytouhou/formats/ecl.py pytouhou/formats/pbg3.py pytouhou/formats/std.py pytouhou/game/eclrunner.py pytouhou/game/sprite.py pytouhou/utils/helpers.py stageviewer.py
diffstat 8 files changed, 46 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/eclviewer.py
+++ b/eclviewer.py
@@ -125,7 +125,6 @@ def main(path, stage_num):
             # This is so that objects on the (O, x, y) plane use pixel coordinates
             gluLookAt(192., 224., - 835.979370 * dz,
                       192. + dx, 224. - dy, 0., 0., -1., 0.)
-            #print(glGetFloat(GL_MODELVIEW_MATRIX))
             glTranslatef(-x, -y, -z)
 
             glEnable(GL_DEPTH_TEST)
--- a/pytouhou/formats/ecl.py
+++ b/pytouhou/formats/ecl.py
@@ -16,8 +16,9 @@
 from struct import pack, unpack
 from pytouhou.utils.helpers import read_string
 
-from collections import namedtuple
+from pytouhou.utils.helpers import get_logger
 
+logger = get_logger(__name__)
 
 class ECL(object):
     _instructions = {0: ('', 'noop?'),
@@ -167,7 +168,7 @@ class ECL(object):
                     args = unpack('<%s' % cls._instructions[opcode][0], data)
                 else:
                     args = (data, )
-                    print('Warning: unknown opcode %d' % opcode) #TODO
+                    logger.warn('unknown opcode %d', opcode)
 
                 ecl.subs[-1].append((time, opcode, rank_mask, param_mask, args))
 
@@ -195,7 +196,7 @@ class ECL(object):
             if instr_type in (0, 2, 4, 6): # Enemy spawn
                 args = unpack('<ffIhHHH', data)
             else:
-                print('ECL: Warning: unknown opcode %d (%r)' % (instr_type, data)) #TODO
+                logger.warn('unknown main opcode %d (data: %r)', instr_type, data)
                 args = (data,)
             ecl.main.append((time, sub, instr_type, args))
 
--- a/pytouhou/formats/pbg3.py
+++ b/pytouhou/formats/pbg3.py
@@ -1,7 +1,3 @@
-from pytouhou.utils.bitstream import BitStream
-import pytouhou.utils.lzss as lzss
-
-
 # -*- encoding: utf-8 -*-
 ##
 ## Copyright (C) 2011 Thibaut Girka <thib@sitedethib.com>
@@ -16,6 +12,14 @@ import pytouhou.utils.lzss as lzss
 ## GNU General Public License for more details.
 ##
 
+from pytouhou.utils.bitstream import BitStream
+import pytouhou.utils.lzss as lzss
+
+from pytouhou.utils.helpers import get_logger
+
+logger = get_logger(__name__)
+
+
 class PBG3BitStream(BitStream):
     def read_int(self):
         size = self.read(2)
@@ -80,6 +84,6 @@ class PBG3(object):
                 value += ord(c)
                 value &= 0xFFFFFFFF
             if value != checksum:
-                print('Warning: corrupted data') #TODO
+                logger.warn('corrupted data!')
         return data
 
--- a/pytouhou/formats/std.py
+++ b/pytouhou/formats/std.py
@@ -14,8 +14,9 @@
 
 
 from struct import pack, unpack
-from pytouhou.utils.helpers import read_string
+from pytouhou.utils.helpers import read_string, get_logger
 
+logger = get_logger(__name__)
 
 
 class Object(object):
@@ -108,7 +109,7 @@ class Stage(object):
                 args = tuple(unpack('<III', data)[:1])
             else:
                 args = (data,)
-                print('Warning: unknown opcode %d' % message_type) #TODO
+                logger.warn('unknown opcode %d (data: %r)', message_type, data)
             stage.script.append((frame, message_type, args))
 
         return stage
--- a/pytouhou/game/eclrunner.py
+++ b/pytouhou/game/eclrunner.py
@@ -15,6 +15,11 @@
 
 from math import atan2, cos, sin
 
+from pytouhou.utils.helpers import get_logger
+
+logger = get_logger(__name__)
+
+
 
 class MetaRegistry(type):
     def __new__(mcs, name, bases, classdict):
@@ -96,7 +101,7 @@ class ECLRunner(object):
                 try:
                     callback = self._handlers[instr_type]
                 except KeyError:
-                    print('Warning: unhandled opcode %d!' % instr_type) #TODO
+                    logger.warn('unhandled opcode %d (args: %r)', instr_type, args)
                 else:
                     callback(self, *args)
 
--- a/pytouhou/game/sprite.py
+++ b/pytouhou/game/sprite.py
@@ -17,6 +17,9 @@ from random import randrange
 from struct import unpack
 
 from pytouhou.utils.matrix import Matrix
+from pytouhou.utils.helpers import get_logger
+
+logger = get_logger(__name__)
 
 
 class AnmWrapper(object):
@@ -154,7 +157,7 @@ class Sprite(object):
                         self.keep_still = True
                         break
                     else:
-                        print('unhandled opcode: %d, %r' % (instr_type, args)) #TODO
+                        logger.warn('unhandled instruction %d (args: %r)', instr_type, args)
         self.frame += 1
 
         ax, ay, az = self.rotations_3d
--- a/pytouhou/utils/helpers.py
+++ b/pytouhou/utils/helpers.py
@@ -12,6 +12,20 @@
 ## GNU General Public License for more details.
 ##
 
+
+from logging import StreamHandler, Formatter, getLogger
+
+
+def get_logger(name):
+    handler = StreamHandler()
+    formatter = Formatter(fmt='[%(name)s] [%(levelname)s]: %(message)s')
+    handler.setFormatter(formatter)
+    logger = getLogger(name)
+    logger.addHandler(handler)
+    logger.propagate = False
+    return logger
+
+
 def read_string(file, size, encoding=None):
     data = file.read(size)
 
@@ -24,3 +38,4 @@ def read_string(file, size, encoding=Non
         return data.decode(encoding)
     else:
         return data
+
--- a/stageviewer.py
+++ b/stageviewer.py
@@ -36,6 +36,11 @@ from OpenGL.GL import *
 from OpenGL.GLU import *
 
 
+from reflexions.helpers import get_logger
+
+logger = get_logger(__name__)
+
+
 def main(path, stage_num):
     # Initialize pygame
     pygame.init()
@@ -106,7 +111,6 @@ def main(path, stage_num):
             # This is so that objects on the (O, x, y) plane use pixel coordinates
             gluLookAt(192., 224., - 835.979370 * dz,
                       192. + dx, 224. - dy, 0., 0., -1., 0.)
-            #print(glGetFloat(GL_MODELVIEW_MATRIX))
             glTranslatef(-x, -y, -z)
 
             for texture_key, (nb_vertices, vertices, uvs, colors) in background.objects_by_texture.items():