// log4cplus.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"

#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/PatternLayout.hh>
#include <log4cpp/BasicConfigurator.hh>
#include <log4cpp/PropertyConfigurator.hh>
#include <log4cpp/Priority.hh>
#include <log4cpp/NDC.hh>

#include <string>
#include <queue>

using namespace std;
using namespace log4cpp;

#pragma comment(lib,".\\Result\\log4cpp.lib")

void TestFileAppender();
void TestOstreamAppender();
void TestSyslogAppender();
void Ndc();
void TestNTEventAppender();
void TestPatternLayout();
void TestPropConfig();
void TestStringQueue();
void TestRollingFile();

int _tmain(int argc, _TCHAR* argv[])
{
    TestFileAppender();
    TestOstreamAppender();
    //TestSyslogAppender();            //#include <syslog.h> 不存在
    Ndc();
    TestNTEventAppender();
    TestPatternLayout();
    TestPropConfig();
    TestStringQueue();
    TestRollingFile();

system("puase");
    return 0;
}

void testLogva(log4cpp::Category& category,log4cpp::Priority::Value priority, const char* stringFormat,...)
{
    va_list va;
    va_start(va, stringFormat);
    category.logva(priority, stringFormat, va);
    va_end(va);
}

void TestFileAppender()
{
    log4cpp::Appender* appender = new log4cpp::FileAppender("default", "fileappender.log");
    log4cpp::Layout* layout = new log4cpp::BasicLayout();
    appender->setLayout(layout);
    log4cpp::Category& root = log4cpp::Category::getRoot();
    root.addAppender(appender);
    root.setPriority(log4cpp::Priority::INFO);
    root.log(log4cpp::Priority::INFO, "reopen log");
    if (appender->reopen())                                                        //reopenAll()
    {
        root.info("log reopened");
    }
    else
    {
        root.warn("could not reopen log");
    }
    root.log(log4cpp::Priority::ERROR, "reopen log");
    if (appender->reopen())                                                        //reopenAll()
    {
        root.info("log reopened");
    }
    else
    {
        root.warn("could not reopen log");
    }

log4cpp::Category::shutdown();
}
void TestOstreamAppender()
{
    log4cpp::Appender* appender = new log4cpp::OstreamAppender("appender", &std::cout);
    log4cpp::Layout* layout = new log4cpp::BasicLayout();
    appender->setLayout(layout);
    log4cpp::Category& root = log4cpp::Category::getRoot();
    root.setPriority(log4cpp::Priority::ERROR);
    root.removeAllAppenders();
    root.addAppender(appender);
    root.error("root error #1");
    root.warn("root error #2");        //不能被输出
    log4cpp::Appender* subappender = new log4cpp::OstreamAppender("appender", &std::cout);
    log4cpp::Category& sub = log4cpp::Category::getInstance(std::string("sub1"));
    sub.addAppender(subappender);
    sub.error("root error #3");        //输出两次
    std::cout<<(root.getAppender() == appender)<<std::endl;
    std::cout<<(root.getAppender() == subappender)<<std::endl;
    root.removeAllAppenders();
    sub.removeAllAppenders();
    root.error("root error #4");    //不能被输出
    sub.error("root error #5");        //不能被输出
    std::cout<<(root.getAppender() == subappender)<<std::endl;
    //root.addAppender(appender);    //直接中断错误
    root.error("root error #6");    //不能被显示
    root.removeAllAppenders();
    appender = new log4cpp::OstreamAppender("appender", &std::cout);
    root.addAppender(appender);
    root.error("root error #6");
    subappender = new log4cpp::OstreamAppender("appender", &std::cout);
    sub.addAppender(subappender);
    sub.setAdditivity(false);        //false覆盖,true添加又一端口
    sub.error("root error #7");        //只显示一次

std::cout << " root priority = " << root.getPriority() << std::endl;
    std::cout << " sub priority = " << sub.getPriority() << std::endl;
    sub.setPriority(log4cpp::Priority::INFO);
    std::cout << " root priority = " << root.getPriority() << std::endl;
    std::cout << " sub priority = " << sub.getPriority() << std::endl;

testLogva(root, log4cpp::Priority::EMERG, "This contains %d %s", 2, "variable arguments");
    testLogva(root, log4cpp::Priority::ALERT, "This contains %d %s", 2, "variable arguments");
    testLogva(root, log4cpp::Priority::CRIT, "This contains %d %s", 2, "variable arguments");
    testLogva(root, log4cpp::Priority::ERROR, "This contains %d %s", 2, "variable arguments");
    testLogva(root, log4cpp::Priority::WARN, "This contains %d %s", 2, "variable arguments");
    testLogva(root, log4cpp::Priority::INFO, "This contains %d %s", 2, "variable arguments");
    testLogva(root, log4cpp::Priority::NOTICE, "This contains %d %s", 2, "variable arguments");
    testLogva(root, log4cpp::Priority::DEBUG, "This contains %d %s", 2, "variable arguments");

sub.warn("sub2 warn");
    sub.error("%s %s %d", "test", "vform", 123);
    sub.warnStream() << "streamed warn";

sub << log4cpp::Priority::WARN << "warn2.." << "..warn3..value=" << 0
        << log4cpp::eol << "..warn4";

/*root.removeAllAppenders();
    sub.removeAllAppenders();*/
    std::vector<log4cpp::Category*>* allCats;
    std::vector<log4cpp::Category*>::const_iterator iter;
    allCats = log4cpp::Category::getCurrentCategories();
    // remove all appenders from all categories
    for (iter = allCats->begin(); iter != allCats->end(); iter++) {
        (*iter)->removeAllAppenders();
    }

log4cpp::Category::shutdown();
}

void TestSyslogAppender()
{
    /*log4cpp::SyslogAppender* syslogAppender;
    syslogAppender = new log4cpp::SyslogAppender("syslog", "log4cpp");*/
    log4cpp::Appender*syslogAppender = new log4cpp::FileAppender("default", "fileappender");
    syslogAppender->setLayout(new log4cpp::BasicLayout());
    log4cpp::Category& root = log4cpp::Category::getRoot();
    root.addAppender(syslogAppender);
    root.setPriority(log4cpp::Priority::ERROR);
    root.log(log4cpp::Priority::NOTICE, "reopen log");
    if (log4cpp::Appender::reopenAll())
    {
        root.info("log reopened");
    }
    else
    {
        root.warn("could not reopen log");
    }
    log4cpp::Category::shutdown();
}

void Ndc()
{
    std::cout << "1. empty NDC: " << NDC::get() << std::endl;
    NDC::push("context1");
    std::cout << "2. push context1: " << NDC::get() << std::endl;
    NDC::push("context2");
    std::cout << "3. push context2: " << NDC::get() << std::endl;
    NDC::push("context3");
    std::cout << "4. push context3: " << NDC::get() << std::endl;
    std::cout << "5. get depth: " << NDC::getDepth() << std::endl;
    std::cout << "6. pop: " << NDC::pop() << std::endl;
    NDC::clear();
    std::cout << "7. clear: " << NDC::get() << std::endl;

for(int i = 0; i < 100; i++)
    {
        char ndc2[20];
        sprintf(ndc2, "i=%d", i);
        log4cpp::NDC::push(ndc2);
        std::cout << i<<" : " << NDC::get() << std::endl;
        log4cpp::NDC::pop();
    }
    log4cpp::Category::shutdown();
}

void TestNTEventAppender()
{
    log4cpp::Appender* appender = new log4cpp::NTEventLogAppender("default", "testNTEventAppender.Log");
    log4cpp::Category& sub1 = log4cpp::Category::getInstance(std::string("sub1"));
    sub1.addAppender(appender);
    sub1.setPriority(log4cpp::Priority::DEBUG);

sub1.emerg("sub1 emerg");
    sub1.fatal("sub1 fatal");
    sub1.alert("sub1 alert");
    sub1.crit("sub1 crit");
    sub1.error("sub1 error");
    sub1.warn("sub1 warn");
    sub1.notice("sub1 notice");
    sub1.info("sub1 info");
    sub1.debug("sub1 debug");
    sub1.log(log4cpp::Priority::NOTSET, "sub1 notset");
    sub1.log(log4cpp::Priority::ERROR, "sub1 error");

log4cpp::Category::shutdown();

}

void TestPatternLayout()
{
    /*log4cpp::Appender* appender = new log4cpp::OstreamAppender("appender", &std::cout);
    log4cpp::PatternLayout* layout = new log4cpp::PatternLayout();
    appender->setLayout(layout);
    log4cpp::Category& root = log4cpp::Category::getRoot();
    root.addAppender(appender);*/

log4cpp::BasicConfigurator::configure();                                                //初始化
    log4cpp::PatternLayout* layout = new log4cpp::PatternLayout();

layout->setConversionPattern("%d: %p %c %x: %m%n");
    log4cpp::Category::getRoot().getAppender()->setLayout(layout);

log4cpp::Category& root = log4cpp::Category::getRoot();
    log4cpp::NDC::push("ndc1");
    root.error("message");
    std::string format = "%% %r %c:%d (%R / %r) [%p] %x %m %% (%u) %n";
    layout->setConversionPattern(format);
    root.error("message");

// test date format
    format = "%d{%d %b %Y %H:%M:%S.%l} %m %n";
    layout->setConversionPattern(format);
    root.error("message");
    format = "%d{%d %b %Y %H:%M:%S.%l";
    layout->setConversionPattern(format);
    root.error("message");
    format = "%d%n";
    layout->setConversionPattern(format);
    root.error("message");

format = "%.32m %d%n";
    layout->setConversionPattern(format);
    int i;
    for (i = 0; i < 10; i++)
    {
        root.error("%d message", i);
    }

format = "%32m %d{%H:%M:%S.%l %d %b %Y}%n";
    layout->setConversionPattern(format);
    for (i = 0; i < 10; i++)
    {
        root.error("%d message", i);
    }
}

void TestPropConfig()
{
    std::string initFileName;
    initFileName = ".\\include\\tests\\log4cpp.nt.property";
    log4cpp::PropertyConfigurator::configure(initFileName);

log4cpp::Category& root = log4cpp::Category::getRoot();

log4cpp::Category& sub1 =
        log4cpp::Category::getInstance(std::string("sub1"));

log4cpp::Category& sub2 =
        log4cpp::Category::getInstance(std::string("sub1.sub2"));

root.error("root error");
    root.warn("root warn");
    sub1.error("sub1 error");
    sub1.warn("sub1 warn");
    // the following 2 should only be in A1, not A2
    sub1.info("sub1 info");
    sub1.debug("sub1 debug");

// do a few so that the log rolls over
    sub2.warn("sub2 warn 1");
    sub2.warn("sub2 warn 2");
    sub2.warn("sub2 warn 3");
    sub2.warn("sub2 warn 4");
    sub2.warn("sub2 warn 5");
    sub2.warn("sub2 warn 6");
    sub2.warn("sub2 warn 7");

#if defined(WIN32)
    log4cpp::Category& nt = log4cpp::Category::getInstance(std::string("subNT"));
    nt.error("subNT error");
    nt.warn("subNT warn");
    nt.debug("subNT debug");
#endif

log4cpp::Category::shutdown();

}

void TestStringQueue()
{
    log4cpp::StringQueueAppender* strQAppender = new log4cpp::StringQueueAppender("strQAppender");
    strQAppender->setLayout(new log4cpp::BasicLayout());
    log4cpp::Category& root = log4cpp::Category::getRoot();
    root.addAppender(strQAppender);
    root.setPriority(log4cpp::Priority::DEBUG);
    root.error("Hello log4cpp in a Error Message!");
    root.warn("Hello log4cpp in a Warning Message!");
    cout<<"Get message from Memory Queue!"<<endl;
    cout<<"-------------------------------------------"<<endl;
    queue<string>& myStrQ = strQAppender->getQueue();
    while(!myStrQ.empty())
    {
        cout<<myStrQ.front();
        myStrQ.pop();
    }
}

void TestRollingFile()
{
    log4cpp::PatternLayout* pLayout1 = new log4cpp::PatternLayout();
    pLayout1->setConversionPattern("%d: %p %c %x: %m%n");
    log4cpp::PatternLayout* pLayout2 = new log4cpp::PatternLayout();
    pLayout2->setConversionPattern("%d: %p %c %x: %m%n");
    log4cpp::Appender* fileAppender = new log4cpp::FileAppender("fileAppender","wxb.log");
    fileAppender->setLayout(pLayout1);
    log4cpp::RollingFileAppender* rollfileAppender = new log4cpp::RollingFileAppender( "rollfileAppender","rollwxb.log",5*1024,1);
    rollfileAppender->setLayout(pLayout2);
    log4cpp::Category& root = log4cpp::Category::getRoot().getInstance("RootName");
    root.addAppender(fileAppender);
    root.addAppender(rollfileAppender);
    root.setPriority(log4cpp::Priority::DEBUG);
    for (int i = 0; i < 100; i++)
    {
        string strError;
        ostringstream oss;
        oss<<i<<":Root Error Message!";
        strError = oss.str();
        root.error(strError);
    }
    log4cpp::Category::shutdown();
}

/*

# property configurator test file

log4cpp.rootCategory=DEBUG, rootAppender
log4cpp.category.sub1=DEBUG, A1, A2
log4cpp.category.sub1.sub2=DEBUG, A3

log4cpp.appender.rootAppender=ConsoleAppender
log4cpp.appender.rootAppender.layout=BasicLayout

log4cpp.appender.A1=FileAppender
log4cpp.appender.A1.fileName=A1.log
log4cpp.appender.A1.layout=PatternLayout
log4cpp.appender.A1.layout.ConversionPattern=The message %m at time %d%n

log4cpp.appender.A2=FileAppender
log4cpp.appender.A2.threshold=WARN
log4cpp.appender.A2.fileName=A2.log
log4cpp.appender.A2.layout=PatternLayout
log4cpp.appender.A2.layout.ConversionPattern=The message %m at time %d%n

log4cpp.appender.A3=RollingFileAppender
log4cpp.appender.A3.fileName=A3.log
log4cpp.appender.A3.maxFileSize=80
log4cpp.appender.A3.maxBackupIndex=1
log4cpp.appender.A3.layout=PatternLayout
log4cpp.appender.A3.layout.ConversionPattern=The message %m at time %d{%H:%M}%n

log4cpp.category.subNT=INFO, subNT
log4cpp.appender.subNT=NTEventLogAppender
log4cpp.appender.subNT.source=log4cpp-test

*/

/*
    log4cpp::FileAppender                       // 输出到文件

log4cpp::RollingFileAppender                // 输出到回卷文件,即当文件到达某个大小后回卷

log4cpp::OstreamAppender                    // 输出到一个ostream类

log4cpp::StringQueueAppender                // 内存队列

log4cpp::Win32DebugAppender                    // 发送到缺省系统调试器

log4cpp::NTEventLogAppender                    // 发送到win 事件日志

*/

log4cpp基础测试的更多相关文章

  1. myBatis 基础测试 表关联关系配置 集合 测试

    myBatis 基础测试 表关联关系配置 集合 测试 测试myelipse项目源码 sql 下载 http://download.csdn.net/detail/liangrui1988/599388 ...

  2. mysql基础测试

    mysql基础测试 测试原因   为什么需要做性能测试 模拟比当前系统更高的负载,找出性能瓶颈 重现线上异常 测试不同硬件软件配置 规划未来的业务增长   测试分类   性能测试的分类 设备层的测试 ...

  3. 基础测试jmeter5.0+badboy(从小白到入门)

    1]测试工具jmeter环境安装 1.1]安装jdk环境 1:必须安装jdk8.0(我尝试安装最新版本不行,好像当时没有配置好.之后安装8.0遍可以正常运行)下载地址:单击此段 配置jdk环境:鼠标右 ...

  4. 性能测试基础---测试流程,LR安装

    ·性能测试流程详解: 一般来说,性能测试通常可以分为以下过程: ·前期分析.测试计划.测试方案.测试环境的搭建.测试数据的准备.测试脚本的开发.测试场景的设计.测试场景的实现和执行.资源的监控.分析结 ...

  5. Kubeasz部署K8s基础测试环境简介

    下面介绍使用Kubeasz部署K8s集群环境. https://github.com/easzlab/kubeasz在需要使用kubeeasz项目安装的k8s时,需要将所有需要它来部署的节点上,都安装 ...

  6. weed-fs 基础测试

    =================== 启动 master 端口:9333 =================== sunsl@test-server:~$ weed master I0102 15: ...

  7. Linux基础测试--11道题

    000.创建一个目录/data mkdir /data 001.在/data 下面创建一个文件oldboy.txt touch /data/oldboy.txt 002.为oldboy.txt 增加内 ...

  8. Hive基础测试操作

    一.Hive测试 1.查看数据库 show databases; 2.使用某个数据库,如默认数据库 user default; 3.创建表 create table if not exist itst ...

  9. Python基础测试有关联的接口

    test_guanlian.py放在case文件夹下 test_guanlian.pyimport unittest import requestsfrom urllib.parse import u ...

随机推荐

  1. [Projet] Module NFC

    http://www.f4grx.net/projet-module-nfc/ The NFC is a contactless communication technology, which is ...

  2. LwIP buffer management, memory configuration options

    http://www.st.com/st-web-ui/static/active/cn/resource/technical/document/application_note/DM00036052 ...

  3. TurboLinux11system»adjtimex简介

    Adjtimex介绍 linux 系统有两个时钟:一个是由主板电池驱动的“Real Time Clock”也叫做RTC或者叫CMOS时钟,硬件时钟.当操作系统关机的时候,用这个来记录时间,但是对于运行 ...

  4. pytest文档2-用例运行规则

    用例设计原则 文件名以test_*.py文件和*_test.py 以test_开头的函数 以Test开头的类 以test_开头的方法 所有的包pakege必须要有__init__.py文件 help帮 ...

  5. 7)Linux程序设计入门--网络编程

    )Linux程序设计入门--网络编程 Linux系统的一个主要特点是他的网络功能非常强大.随着网络的日益普及,基于网络的 应用也将越来越多. 在这个网络时代,掌握了Linux的网络编程技术,将令每一个 ...

  6. QT源码之Qt信号槽机制与事件机制的联系

    QT源码之Qt信号槽机制与事件机制的联系是本文要介绍的内容,通过解决一个问题,从中分析出的理论,先来看内容. 本文就是来解决一个问题,就是当signal和slot的连接为Qt::QueuedConne ...

  7. SPSS Clementine 数据挖掘入门3

    转摘:http://www.cnblogs.com/dekevin/archive/2012/04/27/2473683.html 了解SPSS Clementine的基本应用后,再对比微软的SSAS ...

  8. Power Desginer系列02【转载】

    在概念模型中主要有以下几个操作和设置的对象:实体(Entity).实体属性 (Attribute).实体标识(Identifiers).关系(Relationship).继承(Inheritance) ...

  9. Javascript前端思维导图

    思维导图小tips: 思维导图又叫心智图,是表达发射性思维的有效的图形思维工具 ,它简单却又极其有效,是一种革命性的思维工具.思维导图运用图文并重的技巧,把各级主题的关系用相互隶属与相关的层级图表现出 ...

  10. Servlet学习笔记(一):生命周期

    一.Servlet 生命周期: Servlet 生命周期可被定义为从创建直到毁灭的整个过程.以下是 Servlet 遵循的过程:初始化——响应请求——终止——回收 Servlet 通过调用 init ...