使用 ctypes 捕获 c++ 共享库日志条目

Capture c++ shared library log entries with ctypes

本文关键字:日志 共享 ctypes 捕获 c++ 使用      更新时间:2023-10-16

我有一个cplusplus共享库,带有c接口,可以在stdout中写入日志条目。我正在使用ctypes库在 python 应用程序中使用它。python 应用程序使用logging库来写入日志条目。

我需要做的是捕获共享库的 stdout 条目,以使用 logging 模块写入日志条目。换句话说,我想将 c 库的 stdout 条目重定向到 logging 模块,这样我就可以使用 logging 通过其处理程序写入文件和控制台。

我发现可以捕获标准输出(请参阅此 SO 问题(,但我只能在 c 模块调用结束时访问它,因此它对日志记录毫无用处。我想要一种非阻塞的方式来访问标准输出条目。

一个最小示例如下。

模块.cpp(使用 g++ -fPIC -shared module.cpp -o module.so 编译(

#include <unistd.h>
#include <iostream>
using namespace std;
extern "C" int callme()
{
  cout<<"Hello worldn";
  sleep(2);
  cout<<"Some wordsn";
  sleep(2);
  cout<<"Goodby worldn";
  return 0;
}

调用它的 python 应用程序:

import ctypes as ct
import logging
format='%(asctime)s - %(levelname)s - %(message)s', level=logging.DEBUG
logging.basicConfig(format=format)
logging.debug('This logging modules works like a charm!')
mymodule = ct.CDLL('./module.so')
mymodule.callme()
logging.info('I want to capture the shared library log entries')
logging.warning('Can I?')

这会产生:

2016-02-04 16:16:35,976 - DEBUG - This logging modules works like a charm!
Hello world
Some words
Goodby world
2016-02-04 16:16:39,979 - INFO - I want to capture the shared library log entries
2016-02-04 16:16:39,979 - WARNING - Can I?

我可以访问 c++ 库,因此也欢迎需要在库中修改的解决方案。

您应该能够在 C 模块调用运行时通过线程中的管道读取来修改链接答案中的代码。以下内容应该有效,尽管我还没有使用长时间运行的模块调用对其进行测试:

def redirected_printed_output(module_call):
    # the pipe would fail for some reason if I didn't write to stdout at some point
    # so I write a space, then backspace (will show as empty in a normal terminal)
    sys.stdout.write(' b')
    pipe_out, pipe_in = os.pipe()
    # save a copy of stdout
    stdout = os.dup(1)
    # replace stdout with our write pipe
    os.dup2(pipe_in, 1)
    # check if we have more to read from the pipe
    def more_data():
        r, _, _ = select.select([pipe_out], [], [], 0)
        return bool(r)
    # read the pipe, writing to (former) stdout
    def write_pipe_to_stdout():
        while more_data():
            os.write(stdout, os.read(pipe_out, 1024))
    done = False
    def read_loop():
        # rewrite the pipe out to stdout in a loop while the call is running
        while not done:
            write_pipe_to_stdout()
        # Finish the remnants
        write_pipe_to_stdout()
    t = threading.Thread(target=read_loop)
    t.start()
    module_call()
    done = True
    t.join() # wait for the thread to finish
    # put stdout back in place 
    os.dup2(stdout, 1)

我按如下方式对其进行了测试(OSX(:

import ctypes
libc = ctypes.CDLL('libc.dylib')
def zomg():
    for i in xrange(5):
        libc.printf('libc stdout: %dn', i)
        time.sleep(1)
redirected_printed_output(zomg)