为什么在调用C函数写入日志时出现错误的文件描述符错误?

时间:2016-07-18 10:20:53

标签: python-3.x logging ctypes

此函数调用C函数从Python写入日志。它确实打印日志文本但在它之后抛出错误。我做错了什么?

def py_log_callback(msg, level):
    if not string_at(msg):
        return
    msg = "Log entry: {}".format(string_at(msg).decode())
    print(msg)

log_callback = CFUNCTYPE(None, c_char_p, c_int)(py_log_callback)
lib = CDLL(libpath)
lib.Foo(log_callback, DEBUG_LEVEL)

输出:

Log entry: <log text here>

Traceback (most recent call last):
  File "_ctypes/callbacks.c", line 234, in 'calling callback function'
  File "test.py", line 36, in py_log_callback
    print(msg)
OSError: [Errno 9] Bad file descriptor

1 个答案:

答案 0 :(得分:0)

我刚刚将输出保存到文本文件中,问题已修复。

sys.stdout = open('stdout.txt', 'w')