使用例子说明 http://blog.csdn.net/lx_shudong/article/details/48732999
首先注意库的字符集方式,生成lib+dll后,引用方也应该与lib生成代码的一致,下面是代码及图:
下面是调用代码:
Log.h:
#pragma once
//#ifndef LOG_API
//#define LOG_API __declspec(dllexport)
//#else
//#define LOG_API __declspec(dllimport)
//#endif
#include
#define FAT 50000
#define ERR 40000
#define WAR 30000
#define INF 20000
class tLogger;
/*LOG_API*/ bool LogInit(const std::string& config_path=std::string());
/*LOG_API*/ void WriteLog(int leven, const std::string str);
/*LOG_API*/ void LogEvent(int level, const char* format, ...);
#define FAT_LOG Logger(FAT).Input(__LINE__,__FILE__)
#define INF_LOG Logger(INF).Input(__LINE__,__FILE__)
#define ERR_LOG Logger(ERR).Input(__LINE__,__FILE__)
#define WAR_LOG Logger(WAR).Input(__LINE__,__FILE__)
Log.cpp:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "Log.h"
#include
#define BUF_LEN 8192
using namespace log4cplus;
using namespace log4cplus::helpers;
class log4cplus_Logger
{
public:
log4cplus_Logger(){};
~log4cplus_Logger(){}
bool Init(const std::string& conf_path = std::string());
static log4cplus::Logger m_log4cplus_logger_;
private:
std::string m_conf_path_;
};
log4cplus::Logger log4cplus_Logger::m_log4cplus_logger_;
bool log4cplus_Logger::Init(const std::string& conf_path){
if (conf_path.empty()){
//输出到屏幕
std::string pattern_layout = "[%d]%-5p:%m%n";
log4cplus::SharedAppenderPtr ConsoleApp(new log4cplus::ConsoleAppender());
std::auto_ptr layout(new log4cplus::PatternLayout(pattern_layout));
ConsoleApp->setLayout(layout);
log4cplus_Logger::m_log4cplus_logger_ = log4cplus::Logger::getInstance("log");
log4cplus_Logger::m_log4cplus_logger_.addAppender(ConsoleApp);
log4cplus_Logger::m_log4cplus_logger_.setLogLevel(log4cplus::ALL_LOG_LEVEL);
}
else{
m_conf_path_ = conf_path;
try{
log4cplus::PropertyConfigurator::doConfigure(conf_path.c_str());
log4cplus_Logger::m_log4cplus_logger_ = log4cplus::Logger::getInstance("log");
log4cplus_Logger::m_log4cplus_logger_.setLogLevel(log4cplus::ALL_LOG_LEVEL);
}
catch (...){
std::cout << "LogModel Init faild!" << std::endl;
return false;
}
}
return true;
}
bool LogInit(const std::string& config_path){
log4cplus_Logger log;
return log.Init(config_path);
}
void WriteLog(int leven,const std::string str){
switch (leven)
{
case INF:
LOG4CPLUS_INFO(log4cplus_Logger::m_log4cplus_logger_, str);
break;
case WAR:
LOG4CPLUS_WARN(log4cplus_Logger::m_log4cplus_logger_, str);
break;
case ERR:
LOG4CPLUS_ERROR(log4cplus_Logger::m_log4cplus_logger_, str);
break;
case FAT:
LOG4CPLUS_FATAL(log4cplus_Logger::m_log4cplus_logger_, str);
break;
default:
std::cout << "not have this log levnl" << std::endl;
break;
}
}
void LogEvent(int level, const char* format, ...){
char buf[BUF_LEN] = { 0 };
va_list args;
va_start(args, format);
_vsnprintf_s(buf, BUF_LEN, format, args);
WriteLog(level, buf); //LogEvent(INF, "starting up CCQC program.");
}
调用方:ConsoleApplication1.cpp:
#include "stdafx.h"
#include
#include
#include
#include "Log.h"
using namespace std;
#include
#include
int main()
{
/*if (!LogInit()){
std::cout
<< "log model init faild!" << std::endl;
return -1;
}*/
if (!LogInit("E:/canDel/ConsoleApplication1/ConsoleApplication1/cfg/log.conf")){
std::cout
<< "log model init faild!" << std::endl;
return -1;
}
LogEvent(INF, "-----------------------------------------------------------------------");
LogEvent(INF, "starting up CCQC program.");
LogEvent(INF,"%s--%s--%s******" ,"starting up CCQC program.", "xwefefew","txwew" );
getchar();
return 0;
}
std::thread([&](){
while (1){
LogEvent(FAT,"this is FAT");
}
}).detach();
std::thread([&](){
while (1){
LogEvent(INF, "this is INF");
}
}).detach();
std::thread([&](){
while (1){
std::this_thread::sleep_for(std::chrono::seconds(2));
LogEvent(ERR, "this is ERR");
}
}).detach();
std::thread([&](){
while (1){
std::this_thread::sleep_for(std::chrono::seconds(2));
LogEvent(WAR, "this is WAR");
}
}).detach();