This function calls a C function to write to log from Python. It does print the log text but throws an error after it. What am I doing wrong?
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)
Output:
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
I just saved the output to a text file and the problem was fixed.
sys.stdout = open('stdout.txt', 'w')
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments