snip–><–snip

Capturing Nuke’s stdout and err to a file

by on Jan.20, 2015, under Nuke, Python

Something that has bothered me for quite some time, is not being able to capture Nuke’s STDOUT and STDERR, to a file ot ticket, so that when the Artist come accross an issue and none of the TD’s can help we can save it to a ticket or log for when someone has time.

I was reading through some of the python script in nuke an came accross what i was after at the bottom of:

C:\Program Files\Nuke<version>\plugins\menu.py

So in our site menu.py, i have added the following code that is executed  after the above script is run by nuke:

#################################################################################

# save to an env, so that nuke and any threads that share the environment can find the logger
log_path = os.environ['NUKE_LOGGING_PATH']

import logging
logger = logging.getLogger()
hdlr = logging.FileHandler(log_path)
formatter = logging.Formatter('%(levelname)s %(message)s') #%(asctime)s
hdlr.setFormatter(formatter)
logger.addHandler(hdlr)
logger.setLevel(logging.DEBUG)

# stream redirectors
# pulled from: C:\Program Files\Nuke<version>\plugins\menu.py

class SESysStdIn(SERedirector, nuke.FnPySingleton):
  def readline(self):
    return ""

class SESysStdOut(SERedirector, nuke.FnPySingleton):
  def write(self, out):
    if not len(out.strip()) == 0:
        logger.info(out.strip('\n'))
    nuke.output_redirector(out)

class SESysStdErr(SERedirector, nuke.FnPySingleton):
  def write(self, out):
    if not len(out.strip()) == 0:
        logger.error(out.strip('\n'))
    nuke.stderr_redirector(out)

if nuke.GUI:
  sys.stdin  = SESysStdIn(sys.stdin)
  sys.stdout = SESysStdOut(sys.stdout)
  sys.stderr = SESysStdErr(sys.stderr)
#################################################################################

Then, when nuke ui starts it will create this log and write to it.
The “os.environ[‘NUKE_LOGGING_PATH’]” is because we define a new log for every nuke instance. this is defined when running the init.py in your site config.

:,
No comments for this entry yet...

Leave a Reply


7 + = ten

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!