【发布时间】:2016-05-14 14:15:01
【问题描述】:
我有一个 cplusplus 共享库,带有一个 c 接口,可以在标准输出中写入日志条目。我在使用ctypes 库的python 应用程序中使用它。 python 应用程序使用logging 库来写入日志条目。
我需要做的是捕获共享库的标准输出条目,以使用logging 模块写入日志条目。换句话说,我想将 c 库的 stdout 条目重定向到 logging 模块,因此我可以使用 logging 写入文件并使用其处理程序进行控制台。
我发现可以捕获标准输出 (see this SO question),但我只能在 c 模块调用结束时访问它,因此它对日志记录毫无用处。我想要一种无阻塞的方式来访问标准输出条目。
一个最小的例子如下。
module.cpp(用g++ -fPIC -shared module.cpp -o module.so编译)
#include <unistd.h>
#include <iostream>
using namespace std;
extern "C" int callme()
{
cout<<"Hello world\n";
sleep(2);
cout<<"Some words\n";
sleep(2);
cout<<"Goodby world\n";
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 模块调用结束时访问它" - 你确定吗?你不能在另一个线程中运行模块调用,或者在另一个线程中运行管道检查吗?
-
@Claudiu 感谢您的关注。这句话的意思是另一个问题的答案不会“按原样”工作。我没有尝试您提出的基于线程的解决方案,我会尝试一下。但与此同时,如果你能用这个想法写一个答案,那将不胜感激:)
标签: python c++ c logging ctypes