此函数调用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
答案 0 :(得分:0)
我刚刚将输出保存到文本文件中,问题已修复。
sys.stdout = open('stdout.txt', 'w')