Dumping debug info to a file

When using ppython.exe, my program works fine…but if I try ppythonw.exe (the windows version without console), it will crash after executing for a while.

So basically what I need now is to write out all the debug info from the console to a text file. I refered to the following link:

http://www.panda3d.org/manual/index.php/Log_Messages

But seems that it only dumps Panda message, and ignore all my “print” command dumping.

self.nout = MultiplexStream() 
Notify.ptr().setOstreamPtr(self.nout, 0) 
self.nout.addFile(Filename("out.txt")) 

I should put these code after my print command, or where it should be set up?

Right, that only dumps the Panda output. You can also get your print output to a file–maybe even the same file, if the filename is the same–with something like this:

file = open('filename.log', 'a')
sys.stdout = file
sys.stderr = file

You should do both of these at the beginning of your program, before you generate any output.

David

David,

I’m afraid I’m having some difficulty with this example. The following code causes the Panda engine to crash consistently:

from pandac.PandaModules import MultiplexStream,Notify,Filename
import sys

loggingInitialized=0

def initLogging(filename):
    if loggingInitialized:
        return
    try:

        import direct.directbase.DirectStart
    
        nout = MultiplexStream()
        Notify.ptr().setOstreamPtr(nout, 0)
        nout.addFile(Filename(filename))

        myfile = open(filename, 'a')
        sys.stdout = myfile
        sys.stderr = myfile


        

        print "Logging enabled to '"+str(filename)+"'"

        loggingInitalized=1

    except Exception, args:
        print "Unable to multiplex Panda error stream."
        print str(args)

The exception isn’t triggered; instead, the whole environment crashes. Any idea what I’ve done wrong?

Thanks,
Mark

Frustration upon frustration!

For reasons I don’t understand, the system is fine if I replace a call to initLogging with the contents of the initLogging function. But if I call the function, it crashes.

I ran the following code and compared results:

import direct.directbase.DirectStart
from direct.showbase import DirectObject
from pandac.PandaModules import *
from direct.task import Task
from direct.actor import Actor
from direct.interval.IntervalGlobal import*
from direct.gui.DirectGui import *

loggingInitialized=0

def initLogging():
    try:
        nout = MultiplexStream()
        print "Ptr is "+str(Notify.ptr())
        Notify.ptr().setOstreamPtr(nout, 0)
        nout.addFile(Filename("out.txt"))
        loggingInitialized=1
    except Exception, args:
        print "Unable to multiplex Panda error stream."
        print str(args)
    

if __name__=="__main__":
    print "Ptr is "+str(Notify.ptr())
    initLogging()
    run()

If I replace the call to initLogging() with the contents of the initLogging function, the program starts and runs fine. If, however, I call initLogging (as I do above), the program crashes shortly after Notify.ptr().setOstreamPtr.

Any suggestions would be greatly appreciated!
-Mark

You need to protect nout from being destructed once you have set it up like this. Try something like this:

nout = None
loggingInitialized=0

def initLogging(filename):
    if loggingInitialized:
        return
    try:
        global nout
        nout = MultiplexStream()
  (etc.)

David