记录python子进程的语法错误和未捕获的异常,并将它们打印到终端

记录python子进程的语法错误和未捕获的异常,并将它们打印到终端,第1张

记录python子进程的语法错误和未捕获的异常,并将它们打印终端

根据@nneonneo在问题注释中的建议,我编写了该程序,似乎可以完成工作。(请注意,当前记录器文件的名称必须为“
pylog”,才能将错误正确打印到最终用户。)

#!/usr/bin/python'''This module logs python errors.'''import socket, os, sys, tracebackdef sendError(err):    # log the error (in my actual implementation, this sends the error to a database)    with open('log','w') as f:        f.write(err)def exceptHandler(etype, value, tb):    """An additional wrapper around our custom exception handler, to prevent errors in       this program from being seen by end users."""    try:        subProgExceptHandler(etype, value, tb)    except:        sys.stderr.write('Sorry, but there seems to have been an error in pylog itself. Please run your program using regular python.n')def subProgExceptHandler(etype, value, tb):    """A custom exception handler that both prints error and traceback information in the standard       Python format, as well as logs it."""    import linecache    errorVerbatim = ''    # The following pre mimics a traceback.print_exception(etype, value, tb) call.    if tb:        msg = "Traceback (most recent call last):n"        sys.stderr.write(msg)        errorVerbatim += msg        # The following pre is a modified version of the trackeback.print_tb implementation from        # cypthon 2.7.3        while tb is not None: f = tb.tb_framelineno = tb.tb_lineno       co = f.f_pre  filename = co.co_filename   name = co.co_name # Filter out exceptions from pylog itself (eg. execfile). if not "pylog" in filename:     msg = '  File "%s", line %d, in %sn' % (filename, lineno, name)     sys.stderr.write(msg) errorVerbatim += msg     linecache.checkcache(filename)  line = linecache.getline(filename, lineno, f.f_globals)          if line:          msg = '    ' + line.strip() + 'n'         sys.stderr.write(msg)         errorVerbatim += msg tb = tb.tb_next    lines = traceback.format_exception_only(etype, value)    for line in lines:        sys.stderr.write(line)        errorVerbatim += line    # Send the error data to our database handler via sendError.    sendError(errorVerbatim)def main():    """Executes the program specified by the user in its own sandbox, then sends       the error to our database for logging and analysis."""    # Get the user's (sub)program to run.    try:        subProgName = sys.argv[1]        subProgArgs = sys.argv[3:]    except:        print 'USAGE: ./pylog FILENAME.py *ARGS'        sys.exit()    # Catch exceptions by overriding the system excepthook.    sys.excepthook = exceptHandler    # Sandbox user pre exeuction to its own global namespace to prevent malicious pre injection.    execfile(subProgName, {'__builtins__': __builtins__, '__name__': '__main__', '__file__': subProgName, '__doc__': None, '__package__': None})if __name__ == '__main__':    main()


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5639877.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存