我有我的应用程序的不同部分调用记录器功能来记录详细信息。C++连接不同类型为函数的字符串
Logger类
std::string filename = "blahblah"; // variable to store the location of the properties file
log4cpp::PropertyConfigurator::configure(filename);
void Logger::logging(const std::string& msg)
{
Log4cpp::Category& myLogger = log4cpp::Category::getRoot();
myLogger.log(log4cpp::Priority::INFO, msg);//only takes in string as input
}
调用类
Logger logMe;
int a = 5;
double b = 6;
logMe.logging("log this msg" + a + "," + b);
我意识到,上面给我的错误,因为a
和b
是不同类型的。解决这个问题的方法之一是使用std::to_string
logMe.logging("log this msg" + std::to_string(a) + "," + std::to_string(b));
不过,我有上百个电话到日志功能,这将是耗时每个调用编辑到std::to_string
。是否有更简单的方法可以做到这一点?
呵呵并且为了澄清,代码之前的工作方式是通过定义#define函数。
#Define logging(FLAG, X)\
do {\
...
clog << x; \
}while(0)
logging(LogFlag::Warning, "log this msg" << a << "," << b << endl);
但我现在正在重写部分代码以符合静态测试。
在此先感谢。
应该以标准库为例 - 实现'operator <<'。 – LogicStuff