对log4cpp进一步封装
//Mylog.h如下:
#pragma once
//#include <iostream>
#include <log4cpp/Category.hh>
#include <log4cpp/Appender.hh>
#include "log4cpp/FileAppender.hh"
#include <log4cpp/OstreamAppender.hh>
#include "log4cpp/NTEventLogAppender.hh"
#include <log4cpp/StringQueueAppender.hh>
#include <log4cpp/RollingFileAppender.hh>
//#include "log4cpp/SyslogAppender.hh"
#include <log4cpp/Layout.hh>
#include <log4cpp/BasicLayout.hh>
#include <log4cpp/SimpleLayout.hh>
#include <log4cpp/PatternLayout.hh>
#include <log4cpp/BasicConfigurator.hh>
#include <log4cpp/PropertyConfigurator.hh>
#include <log4cpp/Priority.hh>
#include <log4cpp/NDC.hh>
#include <string>
#include "log4cppEnum.h "
using namespace std;
using namespace log4cpp;
#pragma comment(lib,".\\Result\\log4cpp.lib")
//
//enum PriorityLevel
//{
// EMERG, //EMERG = 0,
// FATAL, //FATAL = 0,
// ALERT, //ALERT = 100,
// CRIT, //CRIT = 200,
// ERROR, //ERROR = 300,
// WARN, //WARN = 400,
// NOTICE, //NOTICE = 500,
// INFO, //INFO = 600,
// DEBUG, //DEBUG = 700,
// NOTSET, //NOTSET = 800,
// PriorityLevel_Num //PriorityLevel_Num = 10
//};
//
//enum AppenderStyle
//{
// CONSOLE_APPENDER, //无
// OSTREAM_APPENDER,
// FILE_APPENDER,
// ROLLING_FILE_APPENDER,
// STRING_QUEUE_APPENDER,
// NTEVENTLOG_APPENDER,
// SYSLOG_APPENDER, //无
// LogStyle_Num
//};
//
//enum LayoutStyle
//{
// BASIC_LAYOUT,
// SIMPLE_LAYOUT,
// PATTERN_LAYOUT,
// layoutStyle_Num
//};
class MyLog
{
private:
PriorityLevel m_prioritylevel;
AppenderStyle m_appenderstyle;
LayoutStyle m_layoutstyle;
log4cpp::Appender* m_pAppender;
log4cpp::Layout* m_pLayout;
log4cpp::Category* m_pCategory;
public:
MyLog(void);
~MyLog(void);
public:
//void NotSet();
void Debug(const char* msg);
void Info(const char* msg);
void Notice(const char* msg);
void Warn(const char* msg);
void Error(const char* msg);
void Crit(const char* msg);
void Alert(const char* msg);
void Fatal(const char* msg);
void Emerg(const char* msg);
void SetPriority(PriorityLevel level);
void SetAppender(AppenderStyle style, const char* filename = nullptr);
void SetLayout(LayoutStyle style);
void SetPatternFormat(const char* format = nullptr);
void ShowAllStringQueueMessages(const char* filename = nullptr);
void Message(PriorityLevel level, std::string format, ...);
void SetNDC(const char* ndc);
void ClearNDC();
void LoadConfigure(const char* filename = nullptr);
MyLog& operator<<(PriorityLevel level);
MyLog& operator<<(const char* msg);
MyLog& operator<<(std::string& msg);
MyLog& operator>>(AppenderStyle appender);
MyLog& operator>>(const char* szFileName);
std::string bin2str( const unsigned char* buf, int nstrlen );
};
//MyLog.cpp如下:
#include "StdAfx.h"
#include "MyLog.h"
#include <queue>
#include <fstream>
#include <cstdio> // for vsprintf
#include <cstdarg>
//#include <vector>
MyLog::MyLog(void)
{
log4cpp::NDC::clear();
m_prioritylevel = NOTSET;
m_appenderstyle = OSTREAM_APPENDER;
m_layoutstyle = BASIC_LAYOUT;
m_pAppender = new log4cpp::OstreamAppender("OstreamAppender", &std::cout);
m_pLayout = new log4cpp::BasicLayout();
m_pAppender->setLayout(m_pLayout);
m_pCategory = &log4cpp::Category::getRoot();
m_pCategory->setAppender(m_pAppender);
m_pCategory->setPriority(log4cpp::Priority::DEBUG);
}
MyLog::~MyLog(void)
{
log4cpp::NDC::clear();
log4cpp::Category::shutdown();
}
//void MyLog::NotSet()
//{
//
//}
void MyLog::Debug(const char* msg)
{
m_pCategory->debug(msg);
}
void MyLog::Info(const char* msg)
{
m_pCategory->info(msg);
}
void MyLog::Notice(const char* msg)
{
m_pCategory->notice(msg);
}
void MyLog::Warn(const char* msg)
{
m_pCategory->warn(msg);
}
void MyLog::Error(const char* msg)
{
m_pCategory->error(msg);
}
void MyLog::Crit(const char* msg)
{
m_pCategory->crit(msg);
}
void MyLog::Alert(const char* msg)
{
m_pCategory->alert(msg);
}
void MyLog::Fatal(const char* msg)
{
m_pCategory->fatal(msg);
}
void MyLog::Emerg(const char* msg)
{
m_pCategory->emerg(msg);
}
void MyLog::SetPriority(PriorityLevel level)
{
m_prioritylevel = level;
switch (m_prioritylevel)
{
case EMERG:
m_pCategory->setPriority(log4cpp::Priority::EMERG);
break;
case FATAL:
m_pCategory->setPriority(log4cpp::Priority::FATAL);
break;
case ALERT:
m_pCategory->setPriority(log4cpp::Priority::ALERT);
break;
case CRIT:
m_pCategory->setPriority(log4cpp::Priority::CRIT);
break;
case ERROR:
m_pCategory->setPriority(log4cpp::Priority::ERROR);
break;
case WARN:
m_pCategory->setPriority(log4cpp::Priority::WARN);
break;
case NOTICE:
m_pCategory->setPriority(log4cpp::Priority::NOTICE);
break;
case INFO:
m_pCategory->setPriority(log4cpp::Priority::INFO);
break;
case DEBUG:
m_pCategory->setPriority(log4cpp::Priority::DEBUG);
break;
case NOTSET: //default
//break;
default:
//m_pCategory->setPriority(log4cpp::Priority::NOTSET); //根节点不能设置为NOTSET
m_pCategory->setPriority(log4cpp::Priority::DEBUG);
break;
}
}
void MyLog::SetAppender(AppenderStyle style, const char* filename)
{
m_pCategory->removeAppender(m_pAppender);
m_appenderstyle = style;
switch (m_appenderstyle)
{
case FILE_APPENDER:
if (nullptr == filename)
{
filename = "fileappender.log";
}
m_pAppender = new log4cpp::FileAppender("FileAppender", filename);
break;
case ROLLING_FILE_APPENDER:
if (nullptr == filename)
{
filename = "RollingFileAppender.log";
}
m_pAppender = new log4cpp::RollingFileAppender( "RollingFileAppender",filename, 1*1024*1024, 10);
break;
case STRING_QUEUE_APPENDER:
m_pAppender = new log4cpp::StringQueueAppender("StringQueueAppender");
break;
case NTEVENTLOG_APPENDER:
if (nullptr == filename)
{
filename = "NTEventLogAppender.log";
}
m_pAppender = new log4cpp::NTEventLogAppender("NTEventLogAppender", filename);
break;
case SYSLOG_APPENDER: //NULL
//break;
case CONSOLE_APPENDER: //NULL
//break;
case OSTREAM_APPENDER: //default
//break;
default:
m_pAppender = new log4cpp::OstreamAppender("OstreamAppender", &std::cout);
break;
}
m_pCategory->setAppender(m_pAppender);
}
void MyLog::SetLayout(LayoutStyle style)
{
m_layoutstyle = style;
switch (m_layoutstyle)
{
case SIMPLE_LAYOUT:
m_pLayout = new log4cpp::SimpleLayout();
break;
case PATTERN_LAYOUT:
m_pLayout = new log4cpp::PatternLayout();
((PatternLayout*)m_pLayout)->setConversionPattern("%t\t%p\t%x:%m\t\t%d{%Y-%m-%d %H:%M:%S.%l}%n");
break;
case BASIC_LAYOUT:
default:
m_pLayout = new log4cpp::BasicLayout();
break;
}
m_pAppender->setLayout(m_pLayout);
}
void MyLog::ShowAllStringQueueMessages(const char* filename)
{
if (m_appenderstyle == STRING_QUEUE_APPENDER)
{
queue<string>& myStrQ = ((StringQueueAppender*)m_pAppender)->getQueue();
if (filename == nullptr)
{
filename = "stringqueue.log";
}
ofstream writetofile(filename);
while(!myStrQ.empty())
{
std::string& stringqueue = myStrQ.front();
cout<< stringqueue;
//writetofile.write(stringqueue.c_str(),stringqueue.size());
writetofile<<stringqueue<<endl;
writetofile.flush();
myStrQ.pop();
}
writetofile.close();
}
}
void MyLog::SetPatternFormat(const char* format)
{
if (m_layoutstyle == PATTERN_LAYOUT)
{
if (format != nullptr)
{
((PatternLayout*)m_pLayout)->setConversionPattern(format);
}
else
{
((PatternLayout*)m_pLayout)->setConversionPattern("%t\t%p\t%x:%m\t\t%d{%Y-%m-%d %H:%M:%S.%l}%n");
}
}
}
void MyLog::Message(PriorityLevel level, std::string format, ...)
{
std::string message; //vector<char>
va_list args;
va_start(args,format);
message.reserve(_vscprintf(format.c_str(),args) + 1); //_vscprintf只能在windows上使用
vsprintf((char*)message.c_str(),format.c_str(),args);
switch (level)
{
case EMERG:
Emerg(message.c_str());
break;
case FATAL:
Fatal(message.c_str());
break;
case ALERT:
Alert(message.c_str());
break;
case CRIT:
Crit(message.c_str());
break;
case ERROR:
Error(message.c_str());
break;
case WARN: //default
Warn(message.c_str());
break;
case NOTICE:
Notice(message.c_str());
break;
case INFO:
Info(message.c_str());
break;
case DEBUG:
Debug(message.c_str());
break;
case NOTSET:
//break;
default:
Warn(message.c_str());
break;
}
}
void MyLog::SetNDC(const char* ndc)
{
log4cpp::NDC::push(ndc);
}
void MyLog::ClearNDC()
{
log4cpp::NDC::clear();
}
void MyLog::LoadConfigure(const char* filename)
{
if (filename == nullptr)
{
filename = ".\\include\\tests\\log4cpp.property";
}
log4cpp::PropertyConfigurator::configure(filename);
m_pCategory = &log4cpp::Category::getRoot();
}
MyLog& MyLog::operator<<(PriorityLevel level)
{
SetPriority(level);
return *this;
}
MyLog& MyLog::operator<<(const char* msg)
{
Message(m_prioritylevel, msg);
return *this;
}
MyLog& MyLog::operator<<(std::string& msg)
{
Message(m_prioritylevel, msg);
return *this;
}
MyLog& MyLog::operator>>(AppenderStyle appender)
{
SetAppender(appender);
return *this;
}
MyLog& MyLog::operator>>(const char* filename)
{
SetAppender(m_appenderstyle,filename);
return *this;
}
std::string MyLog::bin2str( const unsigned char* buf, int nstrlen )
{
stringstream ss;
ss.setf(ios::hex | ios::uppercase );
ss.width(2);
ss.fill('0');
for (int i = 0;i < nstrlen ; ++i)
{
ss << hex << (short)buf[i];
}
return ss.str();
}
对log4cpp进一步封装的更多相关文章
- 进一步封装highchart,打造自己的图表插件:jHighChart.js
Highcharts 是一个用纯JavaScript编写的一个图表库, 能够很简单便捷的在web网站或是web应用程序添加有交互性的图表.支持的图表类型有曲线图.区域图.柱状图.饼状图.散状点图和综合 ...
- 对redis客户端jedis2.8.0的进一步封装
jedis2.8.0的进一步封装: 1.序列化存储对象 2.结合spring,创建redis连接池 3.提供了基础的单个实体操作,有序list操作和一对多关系list的操作,对list提供了分页的封装 ...
- 对boost::shared_from_this的进一步封装
对boost::shared_from_this的进一步封装 熟悉异步编程的同学可能会对boost::shared_from_this有所了解.我们在传入回调的时候,通常会想要其带上当前类对象的上下文 ...
- DAO设计模式实现数据库的增删改查(进一步封装JDBC工具类)
DAO设计模式实现数据库的增删改查(进一步封装JDBC工具类) 一.DAO模式简介 DAO即Data Access Object,数据访问接口.数据访问:故名思义就是与数据库打交道.夹在业务逻辑与数据 ...
- 【Android】19.3 ContentProvider及安卓进一步封装后的相关类
分类:C#.Android.VS2015: 创建日期:2016-03-08 一.简介 ContentProvider:内容提供程序. Android的ContentProvider与.NET框架的EF ...
- Http请求封装(对HttpClient类的进一步封装,使之调用更方便。另外,此类管理唯一的HttpClient对象,支持线程池调用,效率更高)
package com.ad.ssp.engine.common; import java.io.IOException; import java.util.ArrayList; import jav ...
- MySQL数据库学习笔记(十一)----DAO设计模式实现数据库的增删改查(进一步封装JDBC工具类)
[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/4 ...
- [iOS基础控件 - 4.4] 进一步封装"APP列表”,初见MVC模式
A.从ViewController分离View 之前的代码中,View的数据加载逻辑放在了总的ViewController中,增加了耦合性,应该对控制器ViewController隐藏数据加载到Vie ...
- QSqlDatabase的进一步封装(多线程支持+更加简单的操作)——同时支持MySQL, SQL Server和Sqlite
开发背景: 1.直接用QSqlDatabase我觉得太麻烦了: 2.对于某些数据库,多个线程同时使用一个QSqlDatabase的时候会崩溃: 3.这段时间没什么干货放出来觉得浑身不舒服,就想写一个. ...
随机推荐
- 典型案例收集-使用OpenVPN连通多个机房内网(iptables+静态路由)
说明: 1.这个方案是我最初实现的方案,目的在于OpenVPN连通后使其能访问各自的子网,实现互通. 2.主要以iptables为主,这个是关键点,并且这种方式配置iptables十分复杂,最后加入了 ...
- iOS 热更新方案 - lance的专栏 - 博客频道 - CSDN.NET
iOS 热更新方案 - lance的专栏 - 博客频道 - CSDN.NET Weex
- 解决因为google cdn无法访问导致无法打开stackoverflow等网站的方法
许多网站使用了Google的CDN来加速Jquery之类的库来加速网站访问,但由于方校长发福利的原因,导致这些网站很容易出现无法加载"ajax.googleapis.com"而出现 ...
- Druid 配置 wallfilter
这个文档提供基于Spring的各种配置方式 使用缺省配置的WallFilter <bean id="dataSource" class="com.alibaba.d ...
- Windows Embedded Compact 7网络编程概述(上)
如今,不论是嵌入式设备.PDA还是智能手机,网络都是必不可少的模块.网络使人们更方便地共享设备上的信息和资源.而且,利用智能手机浏览互联网,也逐渐成为生活中的常见手段.物联网所倡导的物物相联,也离不开 ...
- Ant简单工程的构建
1.在Ant的官方网站http://ant.apache.org/bindownload.cgi下载Ant最新版本(我下载的是apache-ant-1.8.2-bin.zip),Ant无需安装,直接解 ...
- 虚拟机安装android
通过 虚拟机VirtualBox安装Android x86 4.0系统. Android x86是一个致力于让android运行在x86架构机器上的民间组织搞的项目,目前在世界上有很多人加入了它,虽然 ...
- linux automake 交叉编译
. ├── aclocal.m4 ├── autoscan.log ├── config.log ├── config.status ├── configure ├── configure.in ├─ ...
- 原生对话框【Dialog】AlertDialog和.Builder
使用大全 public class MainActivity extends ListActivity ); pd.setProgress(i);//改变当前进 ...
- 自动签到升级版(JS实现的每日定时任务)
公司规定每日签到两次:日子太安逸了,有时候中午居然会忘记签到…… 于是,笔者寻思写一个自动签到的脚本:每天指定两个签到时段,每次打开页面,先检测当前是否为签到时段,如果在签到时段,则检查cookie中 ...