如何使用log4cpp记录多个字符串?
E.g。如果我想将所有argv记录到main:
#include <iostream>
#include <log4cpp/Category.hh>
#include <log4cpp/FileAppender.hh>
#include <log4cpp/PatternLayout.hh>
using namespace std;
int main(int argc, char* argv[]) {
log4cpp::Appender *appender = new log4cpp::FileAppender("FileAppender","mylog");
log4cpp::PatternLayout *layout = new log4cpp::PatternLayout();
layout->setConversionPattern("%d: %p - %m %n");
log4cpp::Category& category = log4cpp::Category::getInstance("Category");
appender->setLayout(layout);
category.setAppender(appender);
category.setPriority(log4cpp::Priority::INFO);
category.info("program started"); // this works fine, I see it in the logfile
for(int i=0; i<argc; ++i) {
// next line does not compile:
category.info("argv["<<i<<"] = '"<<argv[i]<<"'");
}
return 0;
}
行
category.info("argv["<<i<<"] = '"<<argv[i]<<"'");
无法编译。显然,记录器不能用作 ostream 。什么是 log4cpp方式来记录这样的东西,一次更好?
答案 0 :(得分:5)
您有两种选择:
for (int i = 0; i < argc; ++i)
{
category.info("argv[%d] = '%s'", i, argv[i]);
}
使用infoStream()
:
for (int i = 0; i < argc; ++i)
{
category.infoStream() << "argv[" << i << "] = '" << argv[i] << "'";
}
我会选择后者。