我在Linux下使用cpp,我想使用log4cpp。
我曾尝试在带有 vs2013 的 windows 下使用它,并且效果很好。现在我在 Linux 下工作,但遇到了一个问题:
它不适用于文件。这是我的测试代码:
int main(int argc, char* argv[])
{
fstream logFile;
logFile.open("log", std::ios::app);
log4cpp::OstreamAppender* osAppender = new log4cpp::OstreamAppender("osAppender", &logFile);
// I tried cout as below and it worked, but if I tried as above with a file, it didn't work anymore.
// I mean the "log" file could be created but the message can't be written down in the file. The file is always empty.
//log4cpp::OstreamAppender* osAppender = new log4cpp::OstreamAppender("osAppender", &cout);
osAppender->setLayout(new log4cpp::BasicLayout());
log4cpp::Category& root = log4cpp::Category::getRoot();
root.addAppender(osAppender);
root.setPriority(log4cpp::Priority::DEBUG);
root.error("Hello log4cpp in aError Message!");
root.warn("Hello log4cpp in aWarning Message!");
log4cpp::Category::shutdown();
cout<<"test";
return 0;
}
我多次运行此测试代码,没有出现错误,程序成功完成,因为我可以在控制台看到“测试”。但是文件总是空的。
顺便说一句,sudo chmod +777 log
已经完成。所以不可能是权限的问题。