thrift是一个跨平台的RPC框架,用了很久,但一直不知道如何输出它的内部日志,很长时间了,因为用不上,拿倒也相安无事。
今天遇到thrift 底层socket通讯的问题,一直找不到原因,就把TSocket.cpp代码撸了一遍,才搞明白thrift库输出日志的方式。
thrift有一个类型为apache::thrift::TOutput
的全局变量GlobalOutput
(定义在thrift/TOutput.h
),通过调用其 setOutputFunction
函数设置一个实现输出日志的回调函数,就可以让Thrift根据你的要求输出日志。实现示例如下:
#include <thrift/TOutput.h>
#include <iostream>
#if __ANDROID__
#include <android/log.h>
#endif
/** thrift 日志输出回调函数 */
static void thriftOutput(const char* message) {
if (debugFlag) {
#if __ANDROID__
__android_log_print(ANDROID_LOG_INFO,"THRIFT","%s", message);
#else
std::cout << "THRIFT:" << message;
#endif
}
}
void init(){
// ....
/** 设置thrift库日志输出函数(全局) */
apache::thrift::GlobalOutput.setOutputFunction(thriftOutput);
// ....
}