C++ 日志库 log4cpp 编译、压测及其范例代码 [全流程手工实践]

admin2024-05-15  1

文章目录

    • 一、 log4cpp官网
    • 二、下载
    • 三、编译
      • 1.目录结构如下
      • 2.configure 编译
      • 3.cmake 编译
    • 四、测试
    • 五、压测源码及结果
      • 1.运行环境信息
      • 2.压测源码
      • 3.压测结果

文章内容:包含了对其linux上的完整使用流程,下载、编译、安装、测试用例尝试、以及一份自己写好的压测用例供大家参考。

关于压测:压测代码和结果见文章末尾,能证明的是多线程的开销影响很大的。


一、 log4cpp官网

https://log4cpp.sourceforge.net/


二、下载

1.1.4版本下载地址
https://cytranet.dl.sourceforge.net/project/log4cpp/log4cpp-1.1.x%20%28new%29/log4cpp-1.1/log4cpp-1.1.4.tar.gz?viasf=1


三、编译

1.目录结构如下

[jn@jn log4cpp]$ tar -xvf log4cpp-1.1.4.tar.gz; cd log4cpp
[jn@jn log4cpp]$ ls
aclocal.m4  bcb5            config         configure     doc      jamfile     log4cpp.cfg        log4cpp.m4     log4cpp.spec     Makefile     msvc10  NEWS     src     TODO
AUTHORS     ChangeLog       config.log     configure.in  include  liblog4cpp  log4cpp-config     log4cpp.pc     log4cpp.spec.in  Makefile.am  msvc6   openvms  tests
autogen.sh  CMakeLists.txt  config.status  COPYING       INSTALL  libtool     log4cpp-config.in  log4cpp.pc.in  m4               Makefile.in  msvc7   README   THANKS
[jn@jn log4cpp]$

2.configure 编译

configure编译默认 动态库 和 静态库 都会编译

tar -xvf log4cpp-1.1.4.tar.gz; cd log4cpp
./configure --prefix=$PWD/liblog4cpp
make install
ls liblog4cpp/

3.cmake 编译

cmake 编译需要先运行 ./configure 生成 config.h,另外 cmake 编译默认只编译静态库(即-DBUILD_SHARED_LIBS=OFF),动态库需要打开-DBUILD_SHARED_LIBS=ON选项,同时支支持编译一种

./configure
tar -xvf log4cpp-1.1.4.tar.gz; cd log4cpp
mkdir build;cd build
cmake -DCMAKE_INSTALL_PREFIX=$PWD/liblog4cpp -DBUILD_SHARED_LIBS=ON  ..
make
make install
ls liblog4cpp

四、测试

源码目录下的tests目录有基本上所有示例代码,如testRollingFileAppender.cpp回滚写日志文件

编译方式也很简单:

  • testRollingFileAppender.cpp
g++ testRollingFileAppender.cpp -I../include -L../lib -l:liblog4cpp.a
  • testPattern.cpp
[jn@jn tests]$ g++ testPattern.cpp -I../include -L../lib -l:liblog4cpp.a
[jn@jn tests]$ ./a.out
% 0 cat1:2024-05-11 00:42:03,855 (1715359323 / 0) [ERROR] ndc1 message % (1534)
>   message<
>message   <
>messa<
>messa<
>c3.c4<
11 May 2024 00:42:03.855 message
11 May 2024 00:42:03.8552024-05-11 00:42:03,855
message 2024-05-11 00:42:03,855
0 message 2024-05-11 00:42:03,855
1 message 2024-05-11 00:42:03,855
2 message 2024-05-11 00:42:03,855
3 message 2024-05-11 00:42:03,855
4 message 2024-05-11 00:42:03,855
5 message 2024-05-11 00:42:03,855
6 message 2024-05-11 00:42:03,855
7 message 2024-05-11 00:42:03,855
8 message 2024-05-11 00:42:03,855
9 message 2024-05-11 00:42:03,855
message 00:42:03.855 11 May 2024
0 message 00:42:03.855 11 May 2024
1 message 00:42:03.855 11 May 2024
2 message 00:42:03.855 11 May 2024
3 message 00:42:03.855 11 May 2024
4 message 00:42:03.855 11 May 2024
5 message 00:42:03.855 11 May 2024
6 message 00:42:03.855 11 May 2024
7 message 00:42:03.855 11 May 2024
8 message 00:42:03.855 11 May 2024
9 message 00:42:03.855 11 May 2024
message[jn@jn tests]$
  • 其他测试,同上
[jn@jn liblog4cpp]$ ls tests/
Clock.cpp  log4cpp.init         Makefile.am  testbench.cpp                            testConfig.log4cpp.properties     testFilter.cpp                testNDC.cpp         testPropConfig.cpp
Clock.hh   log4cpp.nt.property  Makefile.in  testbench.o                              test_convenience.cpp              testFixedContextCategory.cpp  testNDCMain.cpp     testProperties.cpp
Clock.o    log4cpp.properties   NDCTest.hh   testCategory.cpp                         testDailyRollingFileAppender.cpp  testmain                      testNTEventLog.cpp  testProperties.properties
jamfile    log4cpp.property     nesteddir    testConfig.cpp                           testDLL.cpp                       testmain.cpp                  testPattern.cpp     testPropertyConfig.cpp
jn_test    Makefile             testbench    testConfig.log4cpp.dailyroll.properties  testErrorCollision.cpp            testmain.o                    testPriority.cpp    testRollingFileAppender.cpp
[jn@jn liblog4cpp]$

五、压测源码及结果

1.运行环境信息

环境:虚拟机VMware
系统:Ubuntu 22.04.1-Desktop
内存:分配16G ------------------ [主机内存:48G]
CPU:分配16核 ----------------- [主机处理器:Intel® Core™ i7-14700KF]
磁盘:300G ---------------------- [主机磁盘:7000MHz 固态]

2.压测源码

#include <log4cpp/Category.hh>
#include <log4cpp/Appender.hh>
#include <log4cpp/Priority.hh>
#include <log4cpp/NDC.hh>
#include <log4cpp/PatternLayout.hh>
#include <log4cpp/BasicConfigurator.hh>
#include <log4cpp/RollingFileAppender.hh>
#include <iostream>

#include <unistd.h>
#include <thread>

#define CPP4LOG_CATEGORY "jnServer"

class log4cppHelper
{
public:
    ~log4cppHelper()
    try {	
        log4cpp::Category::shutdown();
        std::cerr << "log4cpp destory!!!"  << std::endl;
    } catch(log4cpp::ConfigureFailure& f) {
        std::cerr << "configure failure: " << f.what() << std::endl;
    }	

    // don't catch error,let it done by exception or any if;
    void init(log4cpp::Priority::PriorityLevel logLevel = log4cpp::Priority::DEBUG) 
    {
        // create layout pattern
        log4cpp::PatternLayout* layout = new log4cpp::PatternLayout();
        layout->setConversionPattern("%d{%Y-%m-%d %H:%M:%S,%l} [%p]%m%n"); 
        // create Appender and add layout
        log4cpp::RollingFileAppender *fileAppender = new log4cpp::RollingFileAppender(CPP4LOG_CATEGORY, CPP4LOG_CATEGORY".log");
        fileAppender->setMaximumFileSize( 50*1024*1024 /*byte*/ );
        fileAppender->setMaxBackupIndex( 50 );
        fileAppender->setLayout(layout);

        // add appender to root
        log4cpp::Category::getRoot().addAppender(fileAppender);

        // set log level
        log4cpp::Category& cat = log4cpp::Category::getInstance(CPP4LOG_CATEGORY);
        cat.setPriority(logLevel);
        
        log4cpp::NDC::push("ndc1");
    }
};


#define EXPAND(x) x
#define IS_EMPTY(...) EXPAND(IS_EMPTY_(__VA_ARGS__, 0, 1))
#define IS_EMPTY_(a, b, ...) b

#define LOG_I(fmt, ...) \
    do { \
        if (IS_EMPTY(__VA_ARGS__)) { \
            log4cpp::Category::getInstance(CPP4LOG_CATEGORY).info("[%s:%s:%d] %s " fmt, __FILE__, __func__, __LINE__, ##__VA_ARGS__); \
        } else { \
            log4cpp::Category::getInstance(CPP4LOG_CATEGORY).info("[%s:%s:%d] %s", __FILE__, __func__, __LINE__, fmt); \
        } \
    } while (0)
    
    

void log_thread_func(int n) {
    for (int i = 0; i < n; ++i) {
        LOG_I("this message is from LOG_DEBUG, and is native log4cpp API for output to file.");
        LOG_I("this message is from LOG_DEBUG, and is native log4cpp API for output to file.%s.", "end bus");
        //LOG_I("message");
    }
}


int main(int argc, char* argv[])
{
    log4cppHelper log;
    log.init();

    // threads test
    constexpr int NUM_THREADS = 10;
    constexpr int NUM_Line_Per_thread = 3000000/NUM_THREADS;
    std::vector<std::thread> threads;
    for (int i = 0; i < NUM_THREADS; ++i) {
        threads.emplace_back(log_thread_func, NUM_Line_Per_thread);
    }
    for (auto& thread : threads) {
        thread.join();
    }

    return 0;
}

3.压测结果

①大日志多线程7万每秒
C++ 日志库 log4cpp 编译、压测及其范例代码 [全流程手工实践],在这里插入图片描述,第1张

②大日志单线程30万每秒

C++ 日志库 log4cpp 编译、压测及其范例代码 [全流程手工实践],在这里插入图片描述,第2张

③小日志单线程固态57万每秒
C++ 日志库 log4cpp 编译、压测及其范例代码 [全流程手工实践],在这里插入图片描述,第3张

④小日志多线程18万每秒
C++ 日志库 log4cpp 编译、压测及其范例代码 [全流程手工实践],在这里插入图片描述,第4张

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明原文出处。如若内容造成侵权/违法违规/事实不符,请联系SD编程学习网:675289112@qq.com进行投诉反馈,一经查实,立即删除!