最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

c++編譯使用log4cplus的詳細(xì)步驟

 更新時(shí)間:2023年10月28日 14:59:54   作者:洛克希德馬丁  
C++很強(qiáng)大,但是仍然有很多不盡如人意的地方,比如打印日志方面就沒有java的log4j那種信手拈來(lái),自然而然地東西,今天我們說(shuō)一說(shuō)一個(gè)很強(qiáng)大地日志庫(kù)log4cplus在c++項(xiàng)目中地使用,感興趣的朋友一起看看吧

提示:文章寫完后,目錄可以自動(dòng)生成,如何生成可參考幫助文檔

前言

C++很強(qiáng)大,但是仍然有很多不盡如人意的地方,比如打印日志方面就沒有java的log4j那種信手拈來(lái),自然而然地東西。目前官方?jīng)]有推出這個(gè)東西,只能借助第三方開源項(xiàng)目實(shí)現(xiàn),或者干脆自己實(shí)現(xiàn)。但是,今天我們說(shuō)一說(shuō)一個(gè)很強(qiáng)大地日志庫(kù)log4cplus在c++項(xiàng)目中地使用。

一、log4cplus是什么?

看名字就明白了,為c++開發(fā)地日志庫(kù)。接下來(lái)引用開發(fā)者的話:

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.下載源代碼

這個(gè)地方需要注意地是現(xiàn)在master是3.x版本了,這個(gè)版本基于C++ 20以后,使用C++ 11會(huì)直接編譯報(bào)錯(cuò)。如果你是C++ 11的話請(qǐng)?jiān)诜种Ю镎业?.x的版本(包括2.0.x和2.1.x)。

由于這兩個(gè)版本編譯上沒有顯著區(qū)別,今天就以2.0.x版本為基礎(chǔ)講一下log4cplus的編譯使用。

log4cplus下載地址

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

這里克隆完了還不能拿來(lái)直接用,還需要同步下引用的子項(xiàng)目。直接克隆的代碼很小,包括子項(xiàng)目之后的大概是不到60MB,請(qǐng)留一下項(xiàng)目大小。

在這里插入圖片描述

進(jìn)入到項(xiàng)目目錄后執(zhí)行以下命令:

git submodule update --init --recursive

一定要確認(rèn)所有操作成功了才行,否則編譯時(shí)一定失敗。

2.開始配置

1.配置介紹

log4cplus配置項(xiàng)眾多,可以根據(jù)需要來(lái)配置。

請(qǐng)注意,不同的版本分支配置項(xiàng)可能不一樣,請(qǐng)注意區(qū)分。這個(gè)東西在配置文件里可以看到,這里不細(xì)說(shuō)了。

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.

主要包括調(diào)試,so版本號(hào)支持,寬字符支持和本地化等。如果看不懂英文就維持原樣。

2.開始編譯

編譯方法原作者已經(jīng)給出了,這里著重說(shuō)一下LInux上的編譯。

這里還是建議安裝到/usr/local;一方面,因?yàn)?usr本身包含很多操作系統(tǒng)預(yù)裝的應(yīng)用,相比來(lái)說(shuō)/usr/local基本上空空如也,管理起來(lái)方便。另一方面,也是為了以后使用pkgconfig查找方便,這個(gè)后面再說(shuō)。

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

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

make -j6 && sudo make install

安裝好之后會(huì)在/usr/local下面找到,重點(diǎn)是/usr/local/lib/pkgconfig/log4cplus.pc,一會(huì)配置要用到這個(gè)文件。

3.cmake引用

這里選用的是cmake,不為了別的就是因?yàn)楹?jiǎn)單。這里需要先通過(guò)cmake找到pkgconf這個(gè)包管理器,再通過(guò)pkgconf進(jìn)一步定位log4cplus這個(gè)最終需要的包。
請(qǐng)看配置:

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(
			log4cplus
			REQUIRED
			log4cplus
			IMPORTED_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 ()

重點(diǎn)就是find_package(PkgConfig REQUIRED)pkg_search_module,前者找到Linux上面安裝的pkgconf,后者通過(guò)pkgconf找到它管理的module,也就是log4cplus。

這個(gè)時(shí)候你就可以使用log4cplus了。下面列出幾個(gè)簡(jiǎn)單的例子,其它的用法可以看下開發(fā)者的tests或wiki。

4.示例

簡(jiǎn)單實(shí)用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;
}

簡(jiǎn)單實(shí)用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;
//帶時(shí)間格式的日志
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;
}

總結(jié)

1、蠻簡(jiǎn)單的,倒是log4cplus的使用有很多需要研究的地方

到此這篇關(guān)于c++編譯使用log4cplus的文章就介紹到這了,更多相關(guān)c++編譯使用log4cplus內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • C++內(nèi)存越界問題及解決過(guò)程

    C++內(nèi)存越界問題及解決過(guò)程

    內(nèi)存越界是C++常見危險(xiǎn)錯(cuò)誤,屬未定義行為,可能導(dǎo)致崩潰、數(shù)據(jù)損壞和安全漏洞,檢測(cè)手段包括AddressSanitizer等工具,預(yù)防需使用容器替代原生數(shù)組、嚴(yán)格邊界檢查、避免裸指針及代碼審查
    2025-09-09
  • C++中fork()函數(shù)

    C++中fork()函數(shù)

    fork()是Unix/Linux下創(chuàng)建子進(jìn)程的核心系統(tǒng)調(diào)用,調(diào)用一次返回兩次,本文就來(lái)詳細(xì)的介紹一下C++中fork()函數(shù)的使用,感興趣的可以了解一下
    2026-03-03
  • 一文搞懂C++多態(tài)的用法

    一文搞懂C++多態(tài)的用法

    C++多態(tài)是在繼承的基礎(chǔ)上實(shí)現(xiàn)的,了解多態(tài)之前我們需要掌握一定的C++繼承的知識(shí),本文將介紹C++中多態(tài)的概念,構(gòu)成條件以及用法,感興趣的可以學(xué)習(xí)一下
    2022-04-04
  • c++ 函數(shù)指針相關(guān)總結(jié)

    c++ 函數(shù)指針相關(guān)總結(jié)

    這篇文章主要介紹了c++ 函數(shù)指針的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)使用c++,感興趣的朋友可以了解下
    2021-02-02
  • C++全密碼生成的實(shí)現(xiàn)代碼

    C++全密碼生成的實(shí)現(xiàn)代碼

    這篇文章主要為大家詳細(xì)介紹了C++全密碼生成的實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-10-10
  • C語(yǔ)言新建臨時(shí)文件和臨時(shí)文件名的方法

    C語(yǔ)言新建臨時(shí)文件和臨時(shí)文件名的方法

    這篇文章主要介紹了C語(yǔ)言新建臨時(shí)文件和臨時(shí)文件名的方法,分別是mkstemp()函數(shù)和mktemp()函數(shù)的使用,需要的朋友可以參考下
    2015-08-08
  • C語(yǔ)言中字符串的內(nèi)存地址操作的相關(guān)函數(shù)簡(jiǎn)介

    C語(yǔ)言中字符串的內(nèi)存地址操作的相關(guān)函數(shù)簡(jiǎn)介

    這篇文章主要介紹了C語(yǔ)言中字符串的內(nèi)存地址操作的相關(guān)函數(shù),包括bcopy()函數(shù)和bzero()函數(shù)以及bcmp()函數(shù),需要的朋友可以參考下
    2015-08-08
  • 使用C++實(shí)現(xiàn)Range序列生成器的示例代碼

    使用C++實(shí)現(xiàn)Range序列生成器的示例代碼

    在C++編程中,經(jīng)常需要迭代一系列數(shù)字或其他可迭代對(duì)象,本文將使用C++來(lái)實(shí)現(xiàn)一個(gè)簡(jiǎn)單的Range封裝,文中的示例代碼講解詳細(xì),感興趣的可以了解下
    2023-11-11
  • 詳解C語(yǔ)言基礎(chǔ)的類型轉(zhuǎn)換

    詳解C語(yǔ)言基礎(chǔ)的類型轉(zhuǎn)換

    這篇文章主要為大家介紹了C語(yǔ)言基礎(chǔ)的類型轉(zhuǎn)換,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助
    2021-11-11
  • c語(yǔ)言 深入理解函數(shù)的遞歸

    c語(yǔ)言 深入理解函數(shù)的遞歸

    這一章講解的是函數(shù)的遞歸,因?yàn)檫f歸函數(shù)是一個(gè)非常重要求解復(fù)雜問題的方法之一,在學(xué)習(xí)算法的過(guò)程之中我們也會(huì)遇到他,所以我想對(duì)它進(jìn)行一次講解,希望能幫助其他人,也能幫助我自己來(lái)梳理一遍。下面我會(huì)通過(guò)一些題目的講解去認(rèn)識(shí)遞歸函數(shù)
    2022-02-02

最新評(píng)論

理塘县| 盱眙县| 门源| 凤庆县| 深水埗区| 麻城市| 辛集市| 赣榆县| 阳城县| 竹溪县| 清丰县| 申扎县| 蕲春县| 鹤岗市| 富顺县| 资源县| 靖宇县| 北票市| 苍梧县| 应用必备| 保亭| 平原县| 京山县| 舒兰市| 张家界市| 会昌县| 稷山县| 青龙| 大方县| 行唐县| 肃宁县| 鸡东县| 介休市| 文山县| 靖宇县| 林甸县| 休宁县| 安国市| 江西省| 那坡县| 巴里|