b2科目四模拟试题多少题驾考考爆了怎么补救
b2科目四模拟试题多少题 驾考考爆了怎么补救

log4cpp_log4qt_log4cpp全局对象(4)

电脑杂谈  发布时间:2016-12-26 04:00:47  来源:网络整理

基本使用步骤是:

读取解析配置文件;

实例化category对象;

正常使用这些category对象进行日志处理;

下面是个简单的使用代码,使用起来是非常方便的:

// FileName: test_log4cpp2.cpp 
// Test log4cpp by config file. 
// Announce: use as your own risk. 
// Compile : g++ -llog4cpp test_log4cpp2.cpp 
// Run     : ./a.out 
// Tested  : RedHat 7.2 log4cpp0.3.4b 
// Author  : liqun (liqun@nsfocus.com) 
// Data    : 2003-6-27 
#include "log4cpp/Category.hh" 
#include "log4cpp/PropertyConfigurator.hh" 
int main(int argc, char* argv[]) 
{  
	// 1 读取解析配置文件  
	// 读取出错, 完全可以忽略,可以定义一个缺省策略或者使用系统缺省策略  
	// BasicLayout输出所有优先级日志到ConsoleAppender     
	try 
	{
		log4cpp::PropertyConfigurator::configure("./log4cpp.conf");  
	} 
	catch(log4cpp::ConfigureFailure& f) 
	{
	   std::cout << "Configure Problem " << f.what() << std::endl;     
	   return -1;     
	}    
	// 2 实例化category对象  
	// 这些对象即使配置文件没有定义也可以使用,不过其属性继承其父category  
	// 通常使用引用可能不太方便,可以使用指针,以后做指针使用  
	// log4cpp::Category* root = &log4cpp::Category::getRoot();     
	log4cpp::Category& root = log4cpp::Category::getRoot();       
	log4cpp::Category& sub1 = log4cpp::Category::getInstance(std::string("sub1"));     
	log4cpp::Category& sub3 = log4cpp::Category::getInstance(std::string("sub1.sub2"));  
	// 3 正常使用这些category对象进行日志处理。  
	// sub1 has appender A1 and rootappender.  
	sub1.info("This is some info");  
	sub1.alert("A warning");    
	// sub3 only have A2 appender.  
	sub3.debug("This debug message will fail to write");  
	sub3.alert("All hands abandon ship");  
	sub3.critStream() << "This will show up << as " << 1 << " critical message" << log4cpp::CategoryStream::ENDLINE;  
	sub3 << log4cpp::Priority::ERROR << "And this will be an error" << log4cpp::CategoryStream::ENDLINE;  
	sub3.log(log4cpp::Priority::WARN, "This will be a logged warning");    
	return 0; 
} 


本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-23406-4.html

相关阅读
    发表评论  请自觉遵守互联网相关的政策法规,严禁发布、暴力、反动的言论

    热点图片
    拼命载入中...