Creating a LogListener

viblo2

24-07-2006 14:57:13

I have tried to create my own LogListener class to capture logs, but I don't manage to get any runnable code.


class LogListener1(ogre.LogListener):
def __init__(self, func):
#ogre.LogListener() # Line 3
self.func = func
def write(self, name, message, lml= ogre.LML_NORMAL, maskDebug=False):

self.func(message)

#and
ll = LogListener1(self.hookOut)
self.logManager.addListener(ll)


This code generates "TypeError: in method 'LogManager_addListener', argument 2 of type 'Ogre::LogListener *'"
And if I uncomment Line 3
def __init__(self): raise AttributeError, "No constructor defined"
AttributeError: No constructor defined


What is the correct way to do this?

Istari

24-07-2006 21:39:02

I have fixed the problem, LogListener didn't have a constructor so that SWIG couldn't create an instance of it.
To fix this I created a subclass of LogListener, called PyLogListener, which you should use instead.
These changes are in svn and will soon be in the binaries on my server.

Here is the code I used for testing:from pyogre import ogre

class MyLogListener(ogre.PyLogListener):
def __init__(self, func):
ogre.PyLogListener.__init__(self)
self.func = func

def write(self, name, message, lml=ogre.LML_NORMAL, maskDebug=False):
self.func(name, message)

def printMessage(name, message):
print 'MyLogListener -> %s:' % name,
print message

try:
root = ogre.Root()
theListener = MyLogListener(printMessage)

logMgr = ogre.LogManager.getSingleton()
logMgr.addListener(theListener)

theLog = logMgr.createLog('temp.log', True, True, True)
theLog.logMessage('test message')
finally:
logMgr.removeListener(theListener)
del root

viblo2

24-07-2006 22:31:08

Thanks :)

viblo2

07-08-2006 22:00:31

I just tested this (more than just running a test). I then noticed that you need to hold on to the logListener instance yourself.. Bug or feature?

Istari

07-08-2006 23:35:15

Definitely a feature.
For all Swig classes there is a __del__ method that frees the pointer that the object represents.
The alternative would be to leak memory or handle memory allocation.

If you don't mind leaking the memory for this object, you could do this:myLogListener.thisown = False