c++编译使用log4cplus

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

文章目录

  • 前言
  • 一、log4cplus是什么?
  • 二、使用步骤
    • 1.下载源代码
    • 2.开始配置
      • 1.配置介绍
      • 2.开始编译
    • 3.cmake引用
    • 4.示例
  • 总结


前言

C++很强大,但是仍然有很多不尽如人意的地方,比如打印日志方面就没有java的log4j那种信手拈来,自然而然地东西。目前官方没有推出这个东西,只能借助第三方开源项目实现,或者干脆自己实现。但是,今天我们说一说一个很强大地日志库log4cplus在c++项目中地使用。


一、log4cplus是什么?

看名字就明白了,为c++开发地日志库。接下来引用开发者的话:

log4cplus is a simple to use C++ logging API providing thread–safe, flexible, and arbitrarily granular control over log management and configuration. It is modeled after the Java log4j API.

二、使用步骤

1.下载源代码

这个地方需要注意地是现在master是3.x版本了,这个版本基于C++ 20以后,使用C++ 11会直接编译报错。如果你是C++ 11的话请在分支里找到2.x的版本(包括2.0.x和2.1.x)。

由于这两个版本编译上没有显著区别,今天就以2.0.x版本为基础讲一下log4cplus的编译使用。

log4cplus下载地址

git clone https://gitee.com/anold/log4cplus.git -b 2.0.x

这里克隆完了还不能拿来直接用,还需要同步下引用的子项目。直接克隆的代码很小,包括子项目之后的大概是不到60MB,请留一下项目大小。

在这里插入图片描述
进入到项目目录后执行以下命令:

git submodule update --init --recursive

一定要确认所有操作成功了才行,否则编译时一定失败。

2.开始配置

1.配置介绍

log4cplus配置项众多,可以根据需要来配置。

请注意,不同的版本分支配置项可能不一样,请注意区分。这个东西在配置文件里可以看到,这里不细说了。

Configure script options
--enable-debugging
This option is disabled by default. This option mainly affects GCC builds but it also has some limited effect on non-GCC builds. It turns on debugging information generation, undefines NDEBUG symbol and adds -fstack-check (GCC).--enable-warnings
This option is enabled by default. It adds platform / compiler dependent warning options to compiler command line.--enable-so-version
This option is enabled by default. It enables SO version decoration on resulting library file, e.g., the .2.0.0 in liblog4cplus-1.2.so.2.0.0.--enable-release-version
This option is enabled by default. It enables release version decoration on the resulting library file, e.g., the -1.2 in liblog4cplus-1.2.so.2.0.0.--enable-symbols-visibility-options
This option is enabled by default. It enables use of compiler and platform specific option for symbols visibility. See also the Visibility page on GCC Wiki.--enable-profiling
This option is disabled by default. This option adds profiling information generation compiler option -pg to GCC and Sun CC / Solaris Studio builds.--enable-threads
This option is enabled by default. It turns on detection of necessary compiler and linker flags that enable POSIX threading support.While this detection usually works well, some platforms still need help with configuration by supplying additional flags to the configure script. One of the know deficiencies is Solaris Studio on Linux. See one of the later note for details.--with-wchar_t-support
This option is enabled by default. When enabled, additional binaries will be built, marked with U suffix in file name and compiled with -DUNICODE=1 flag. In effect, these binaries assume that log4cplus::tchar is wchar_t.--with-working-locale
This is one of three locale and wchar_t↔char conversion related options. It is disabled by default.It is know to work well with GCC on Linux. Other platforms generally have lesser locale support in their implementations of the C++ standard library. It is known not to work well on any BSDs.See also docs/unicode.txt.--with-working-c-locale
This is second of wchar_t↔char conversion related options. It is disabled by default.It is known to work well on most Unix--like platforms, including recent Cygwin.--with-iconv
This is third of wchar_t↔char conversion related options. It is disabled by default.The conversion using iconv() function always uses "UTF-8" and "WCHAR_T" as source/target encoding. It is known to work well on platforms with GNU iconv. Different implementations of iconv() might not support "WCHAR_T" encoding selector.Either system provided iconv() or library provided libiconv() are detected and accepted. Also both SUSv3 and GNU iconv() function signatures are accepted.--with-qt
This option is disabled by default. It enables compilation of a separate shared library (liblog4cplusqt4debugappender) that implements Qt4DebugAppender. It requires Qt4 and pkg-config to be installed.--enable-tests
This option is enabled by default. It enables compilation of test executables.--enable-unit-tests
This option is disabled by default. It enables compilation of unit tests along their units. These unit tests then can be executed through unit_tests test executable that is built during compilation.

主要包括调试,so版本号支持,宽字符支持和本地化等。如果看不懂英文就维持原样。

2.开始编译

编译方法原作者已经给出了,这里着重说一下LInux上的编译。

这里还是建议安装到/usr/local;一方面,因为/usr本身包含很多操作系统预装的应用,相比来说/usr/local基本上空空如也,管理起来方便。另一方面,也是为了以后使用pkgconfig查找方便,这个后面再说。

./configure --prefix=/usr/local --enable-so-version=yes --enable-release-version=yes \
--enable-threads=yes

配置好之后使用下面的命令:

make -j6 && sudo make install

安装好之后会在/usr/local下面找到,重点是/usr/local/lib/pkgconfig/log4cplus.pc,一会配置要用到这个文件。

3.cmake引用

这里选用的是cmake,不为了别的就是因为简单。这里需要先通过cmake找到pkgconf这个包管理器,再通过pkgconf进一步定位log4cplus这个最终需要的包。
请看配置:

cmake_minimum_required(VERSION 3.10)
project(log_4_cplus)set(CMAKE_CXX_STANDARD 11)
find_package(PkgConfig REQUIRED)
if (PKG_CONFIG_FOUND)message(STATUS "PkgConfig Found")pkg_search_module(log4cplusREQUIREDlog4cplusIMPORTED_TARGET)if (TARGET PkgConfig::log4cplus)message(STATUS "log4cplus Found")add_executable(log_4_cplus main.cpp)target_link_libraries(log_4_cplus PkgConfig::log4cplus)endif ()
endif ()

重点就是find_package(PkgConfig REQUIRED)pkg_search_module,前者找到Linux上面安装的pkgconf,后者通过pkgconf找到它管理的module,也就是log4cplus。

这个时候你就可以使用log4cplus了。下面列出几个简单的例子,其它的用法可以看下开发者的tests或wiki。

4.示例

简单实用1:

#include <iostream>
#include <iomanip>
#include <log4cplus/logger.h>
#include <log4cplus/loggingmacros.h>
#include <log4cplus/configurator.h>
#include <log4cplus/initializer.h>using namespace std;
using namespace log4cplus;
using namespace log4cplus::helpers;void printTest(log4cplus::Logger const &logger) {LOG4CPLUS_INFO(logger,LOG4CPLUS_TEXT("This is")<< LOG4CPLUS_TEXT(" a reall")<< LOG4CPLUS_TEXT("y long message.") << std::endl<< LOG4CPLUS_TEXT("Just testing it out") << std::endl<< LOG4CPLUS_TEXT("What do you think?"));LOG4CPLUS_INFO(logger, LOG4CPLUS_TEXT("This is a bool: ") << true);LOG4CPLUS_INFO(logger, LOG4CPLUS_TEXT("This is a char: ")<< LOG4CPLUS_TEXT('x'));LOG4CPLUS_INFO(logger, LOG4CPLUS_TEXT("This is a short: ")<< static_cast<short>(-100));LOG4CPLUS_INFO(logger, LOG4CPLUS_TEXT("This is a unsigned short: ")<< static_cast<unsigned short>(100));LOG4CPLUS_INFO(logger, LOG4CPLUS_TEXT("This is a int: ") << 1000);LOG4CPLUS_INFO(logger, LOG4CPLUS_TEXT("This is a unsigned int: ") << 1000U);LOG4CPLUS_INFO(logger, LOG4CPLUS_TEXT("This is a long(hex): ")<< std::hex << 100000000L);LOG4CPLUS_INFO(logger, LOG4CPLUS_TEXT("This is a unsigned long: ")<< static_cast<unsigned long>(100000000U));LOG4CPLUS_INFO(logger, LOG4CPLUS_TEXT("This is a float: ") << 1.2345f);LOG4CPLUS_INFO(logger, LOG4CPLUS_TEXT("This is a double: ")<< std::setprecision(15)<< 1.2345234234);LOG4CPLUS_INFO(logger, LOG4CPLUS_TEXT("This is a long double: ")<< std::setprecision(15)<< 123452342342.342L);
}int main(){log4cplus::Initializer initializer;log4cplus::BasicConfigurator config;config.configure(logger);log4cplus::Logger logger = log4cplus::Logger::getInstance(LOG4CPLUS_TEXT("main"));printTest();return 0;
}

简单实用2:

#include <iostream>
#include <iomanip>
#include <log4cplus/logger.h>
#include <log4cplus/loggingmacros.h>
#include <log4cplus/configurator.h>
#include <log4cplus/initializer.h>using namespace std;
using namespace log4cplus;
using namespace log4cplus::helpers;//带时间格式的日志
void time_format_test() {log4cplus::tchar const fmtstr[] =LOG4CPLUS_TEXT("%s, %Q%%q%q %%Q %%q=%%%q%%;%%q, %%Q=%Q");std::cout << "Entering main()..." << std::endl;log4cplus::Initializer initializer;try {Time time;log4cplus::tstring str;time = now();str = getFormattedTime(fmtstr, time);log4cplus::tcout << LOG4CPLUS_TEXT ("now: ") << str << std::endl;time = time_from_parts(0, 7);str = getFormattedTime(fmtstr, time);log4cplus::tcout << str << std::endl;time = time_from_parts(0, 17);str = getFormattedTime(fmtstr, time);log4cplus::tcout << str << std::endl;time = time_from_parts(0, 123);str = getFormattedTime(fmtstr, time);log4cplus::tcout << str << std::endl;time = time_from_parts(0, 1234);str = getFormattedTime(fmtstr, time);log4cplus::tcout << str << std::endl;time = time_from_parts(0, 12345);str = getFormattedTime(fmtstr, time);log4cplus::tcout << str << std::endl;time = time_from_parts(0, 123456);str = getFormattedTime(fmtstr, time);log4cplus::tcout << str << std::endl;time = time_from_parts(0, 0);str = getFormattedTime(fmtstr, time);log4cplus::tcout << str << std::endl;}catch (std::exception const &e) {std::cout << "Exception: " << e.what() << std::endl;}catch (...) {std::cout << "Exception..." << std::endl;}std::cout << "Exiting main()..." << std::endl;
}int main(){time_format_test();return 0;
}

总结

1、蛮简单的,倒是log4cplus的使用有很多需要研究的地方

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/118045.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

SOFAStack软件供应链安全产品解析——SCA软件成分分析

近年来&#xff0c;软件供应链安全相关攻击事件呈快速增长态势&#xff0c;造成的危害也越来越严重&#xff0c;为了保障软件供应链安全&#xff0c;各行业主管单位也出台了诸多政策及技术标准。基于内部多年的实践&#xff0c;蚂蚁数科金融级云原生PaaS平台SOFAStack发布完整的…

GEE案例——一个完整的火灾监测案例dNBR差异化归一化烧毁指数

差异化归一化烧毁指数 dNBR是"差异化归一化烧毁指数"的缩写。它是一种用于评估卫星图像中烧毁区域严重程度的遥感指数。dNBR值通过将火灾前的归一化烧毁指数(NBR)减去火灾后的NBR来计算得出。该指数常用于野火监测和评估。 dNBR(差异化归一化烧毁指数)是一种用…

linux安装node(含npm命令) 并配置淘宝镜像源

1. 下载压缩包 wget https://nodejs.org/dist/v16.14.0/node-v16.14.0-linux-x64.tar.xz # node14 https://nodejs.org/dist/v14.15.4/node-v14.15.4-linux-x64.tar.xz # 推荐将压缩包放置到/usr/local/node文件夹中安装 mv node-v16.14.0-linux-x64.tar.xz /usr/local/node …

MySQL几种方法的数据库备份

MySQL几种方法的数据库备份_mysql备份的几种方式-CSDN博客 MySQL有几个方法来备份 最近博客一直想写点。可是不知道写什么&#xff0c;感觉自己近期的知识没有什么添加&#xff0c;今天想到了一篇能够写的博客。曾经试过依据data目录备份MySQL。可是从来没有成功过。前几天帮助…

maven-default-http-blocker (http://0.0.0.0/): Blocked mirror for repositories

前言 略 说明 新设备上安装了mvn 3.8.5&#xff0c;编译新项目出错&#xff1a; [ERROR] Non-resolvable parent POM for com.admin.project:1.0: Could not transfer artifact com.extend.parent:pom:1.6.9 from/to maven-default-http-blocker (http://0.0.0.0/): Bl…

【网安大模型专题10.19】※论文5:ChatGPT+漏洞定位+补丁生成+补丁验证+APR方法+ChatRepair+不同修复场景+修复效果(韦恩图展示)

Keep the Conversation Going: Fixing 162 out of 337 bugs for $0.42 each using ChatGPT 写在最前面背景介绍自动程序修复流程Process of APR (automated program repair)1、漏洞程序2、漏洞定位模块3、补丁生成4、补丁验证 &#xff08;可以学习的PPT设计&#xff09;经典的…

k8s---深入解析pod

pod 1.为何使用pod?2.容器设计模式3. pod基本概念3.1 pod仍需关注的字段3.2 pod的生命周期 4.深入解析pod(四种Projected Volume&#xff0c;“投射数据卷)4.1 Secret4.2 ConfigMap4.3 Downward API4.4 ServiceAccountToken&#xff08;特殊secret&#xff09;4.5 pod 的健康检…

回归预测 | MATLAB实现BO-LSTM贝叶斯优化长短期神经网络多输入单输出回归预测

回归预测 | MATLAB实现BO-LSTM贝叶斯优化长短期神经网络多输入单输出回归预测 目录 回归预测 | MATLAB实现BO-LSTM贝叶斯优化长短期神经网络多输入单输出回归预测效果一览基本介绍模型搭建程序设计参考资料 效果一览 基本介绍 MATLAB实现BO-LSTM贝叶斯优化长短期神经网络多输入…

SOLIDWORKS 2024新功能 3D CAD三维机械设计10大新功能

SOLIDWORKS 2024新增功能 - 3D CAD三维机械设计 10大新增功能 1. 先前版本的兼容性 •利用您订阅的 SOLIDWORKS&#xff0c;可将您的 SOLIDWORKS 设计作品保存为旧版本&#xff0c;与使用旧版本 SOLIDWORKS 的供应商无缝协作。 •可将零件、装配体和工程图保存为最新版本…

c++中 多线程执行时 线程的执行顺序不固定

C中多线程执行时&#xff0c;线程的执行顺序是不确定的。这是由于多线程的并发性质导致的。 在多线程程序中&#xff0c;多个线程可以同时执行&#xff0c;并且它们的执行顺序是由系统调度器决定的。系统调度器根据各种因素&#xff08;如线程的优先级、线程的状态等&#xff0…

ElasticSearch中关于Nasted嵌套查询的介绍:生动案例,通俗易懂,彻底吸收

题注&#xff1a;随着对ES接触的越来越深入&#xff0c;发现此前了解的ES知识点有点单薄&#xff0c;特此寻来ES知识点汇总成的一个思维导图&#xff0c;全面了解自己掌握了哪些&#xff0c;未掌握哪些。此外&#xff0c;作者斌并没有足够的精力学习ES全部的知识点&#xff0c;…

运行报错(三)git bash报错fatal: detected dubious ownership in repository at

报错现象 在运行git 命令时&#xff0c;出现报错 “fatal: detected dubious ownership in repository at” 报错原因 文件夹的所有者和现在的用户不一致 栗子&#xff1a; 文件夹的所有者是root&#xff0c;而当前用户是admin 解决方案 方法一、 将文件夹的所有者替换成ad…

网络原理之UDP协议

文章目录 前言应用层协议常见的几种数据格式1. xml2. JSON3. protobuffer 端口号传输层UDP 报文协议格式源端口号和目的端口号UDP 长度校验和 前言 前面我们学习了如何使用 UDP 数据报 和 TCP 流实现网络编程一个回显服务器&#xff0c;在知道了 UDP 和 TCP 协议的基本原理之后…

Unity性能优化一本通

文章目录 关于Unity性能优化一、资源部分&#xff1a;1、图片1.1、 图片尺寸越小越好1.2、使用2N次幂大小1.3、取消勾选Read/Write Enabled1.4、图片压缩1.5、禁用多余的Mip Map1.6、合并图集 2、模型2.1.限制模型面数2.2.限制贴图的大小2.3.禁用Read/Write Enables2.4.不勾选其…

行情分析——加密货币市场大盘走势(10.25)

目前大饼继续上涨&#xff0c;还没有看到震荡盘整的迹象。从MACD日线来看&#xff0c;连续绿色实心柱已经10天。现在有点上涨无力了&#xff0c;而现在入场做空性价比更高&#xff0c;看反弹到33000-32000。如果谨慎点&#xff0c;可以继续等待。 以太目前来看和大饼一样那个也…

Rabbitmq消息积压问题如何解决?

一、增加处理能力 优化系统架构、增加服务器资源、采用负载均衡等手段&#xff0c;以提高系统的处理能力和并发处理能力。通过增加服务器数量或者优化代码&#xff0c;确保系统能够及时处理所有的消息。 二、异步处理 将消息的处理过程设计为异步执行&#xff0c;即接收到消息…

Pandas 数据分析系列1--SeriesDataFrame数据结构详解

Pandas 概述 Pandas 是一个开源的数据分析和数据处理库,是基于 NumPy 开发的。它提供了灵活且高效的数据结构,使得处理和分析结构化、缺失和时间序列数据变得更加容易。其在数据分析和数据处理领域广泛应用,在金融、社交媒体、科学研究等领域都有很高的使用率和广泛的应用场…

深度学习使用Keras进行迁移学习提升网络性能

上一篇文章我们用自己定义的模型来解决了二分类问题,在20个回合的训练之后得到了大约74%的准确率,一方面是我们的epoch太小的原因,另外一方面也是由于模型太简单,结构简单,故而不能做太复杂的事情,那么怎么提升预测的准确率了?一个有效的方法就是迁移学习。 迁移学习其…

LeetCode88——合并两个有序数组

LeetCode88——合并两个有序数组 1.题目描述&#xff1a; 给你两个按 非递减顺序 排列的整数数组 nums1 和 nums2&#xff0c;另有两个整数 m 和 n &#xff0c;分别表示 nums1 和 nums2 中的元素数目。 请你 合并 nums2 到 nums1 中&#xff0c;使合并后的数组同样按 非递减…

SQL预编译中order by后为什么不能参数化原因

SQL预编译中order by后为什么不能参数化原因 以Golang为例进行说明 package mainimport ("database/sql""fmt""log"_ "github.com/go-sql-driver/mysql" // Import MySQL driver )func main() {// 数据库连接信息db, err : sql.Open…