Dynamic log_filename with dates in setup.py

Hi, I am trying to distribute my Panda3D app and I want to have the panda log output to a logfile filename based on the time the user uses the app.
So something like

$USER_APPDATA/Panda3D/log/’‘YEAR’’/’‘MONTH’’/DD-HH-MM.log

So the log would log crashes and would not be overwritten.

setup(
    name='my app',
    version='1.0.0',
    options={
        'build_apps': {
            'console_apps': {'My App': 'main.py'},
            'log_filename': '$USER_APPDATA/Panda3D/log/output1.9.log',
            'log_append': False,
        }
    }
)

Thanks for the help!

I think instead of adding it in the seup.py, in the main.py you can create the file.

For example:

log_file = open("log_file.log", "wt")
log_file.write(program_time)
log_file.close()

It seems this isn’t supported by pandas setup tools at the moment. Though you can rather easily redirect pandas output to a log file. Here’s a sample that should match your desired style and location. Just put that somewhere before you initialize the engine.

    from direct.showbase.ShowBase import ShowBase
    from panda3d.core import MultiplexStream, Notify, Filename
    import sys, os, string
    from datetime import datetime

    # create log path
    logPath = os.path.join(
        "$USER_APPDATA",
        str(datetime.now().year),
        str(datetime.now().month))

    # part taken from the setup tools code
    t = string.Template(logPath)
    if sys.platform.startswith('win'):
        logPath = t.substitute(HOME='~', USER_APPDATA='~/AppData/Local')
    elif sys.platform.startswith('macosx'):
        logPath = t.substitute(HOME='~', USER_APPDATA='~/Documents')
    else:
        logPath = t.substitute(HOME='~', USER_APPDATA='~/.local/share')

    # make sure ~ and other special path elements are converted to a full path
    logPath = os.path.expanduser(logPath)

    # create logfile full name
    logfile = os.path.join(
        logPath,
        datetime.now().strftime("%d-%H-%M.log"))

    # chec if the path exists
    if not os.path.exists(logPath):
        os.makedirs(logPath)

    # Redirect pandas logging (notify) to our own stream
    nout = MultiplexStream()
    Notify.ptr().setOstreamPtr(nout, 0)

    # make sure our log file doesn't exist yet
    if os.path.exists(logfile):
        os.remove(logfile)

    # set our otuput stream to write to our log file
    nout.addFile(Filename(logfile))


    # Run your app
    app = ShowBase()
    app.run()
1 Like

This is not a bad idea. We could without much effort support strftime format in the filename.

Please file a feature request on GitHub, thanks!

This is implemented in Panda3D 1.10.9, see:

1 Like