亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

c++編譯使用log4cplus的詳細步驟

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

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

前言

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

一、log4cplus是什么?

看名字就明白了,為c++開發(fā)地日志庫。接下來引用開發(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.下載源代碼

這個地方需要注意地是現(xiàn)在master是3.x版本了,這個版本基于C++ 20以后,使用C++ 11會直接編譯報錯。如果你是C++ 11的話請在分支里找到2.x的版本(包括2.0.x和2.1.x)。

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

log4cplus下載地址

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

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

在這里插入圖片描述

進入到項目目錄后執(zhí)行以下命令:

git submodule update --init --recursive

一定要確認所有操作成功了才行,否則編譯時一定失敗。

2.開始配置

1.配置介紹

log4cplus配置項眾多,可以根據(jù)需要來配置。

請注意,不同的版本分支配置項可能不一樣,請注意區(qū)分。這個東西在配置文件里可以看到,這里不細說了。

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.開始編譯

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

這里還是建議安裝到/usr/local;一方面,因為/usr本身包含很多操作系統(tǒng)預裝的應用,相比來說/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(
			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 ()

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

這個時候你就可以使用log4cplus了。下面列出幾個簡單的例子,其它的用法可以看下開發(fā)者的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的使用有很多需要研究的地方

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

相關文章

  • 軟件構建工具makefile基礎講解

    軟件構建工具makefile基礎講解

    這篇文章介紹了軟件構建工具makefile,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-12-12
  • 淺析C++11中的右值引用、轉移語義和完美轉發(fā)

    淺析C++11中的右值引用、轉移語義和完美轉發(fā)

    對于c++11來說移動語義是一個重要的概念,一直以來我對這個概念都似懂非懂。最近翻翻資料感覺突然開竅,因此順便記錄下C++11中的右值引用、轉移語義和完美轉發(fā),方便大家查閱參考。
    2016-08-08
  • c語言函數(shù)如何求兩個數(shù)的最大值

    c語言函數(shù)如何求兩個數(shù)的最大值

    這篇文章主要介紹了c語言函數(shù)如何求兩個數(shù)的最大值問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-12-12
  • C++中的const和constexpr詳解

    C++中的const和constexpr詳解

    C++ const 和 constexpr 的區(qū)別呢,constexpr表示這玩意兒在編譯期就可以算出來(前提是為了算出它所依賴的東西也是在編譯期可以算出來的)。而const只保證了運行時不直接被修改(但這個東西仍然可能是個動態(tài)變量)。下面我們來詳細講解下。
    2016-01-01
  • 在C++中關于友元函數(shù)的進一步理解

    在C++中關于友元函數(shù)的進一步理解

    今天小編就為大家分享一篇關于在C++中關于友元函數(shù)的進一步理解,小編覺得內容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2018-12-12
  • C語言字符串壓縮之ZSTD算法詳解

    C語言字符串壓縮之ZSTD算法詳解

    快速壓縮工具zstd(zstandard)是由facebook開源的快速無損壓縮算法,主要應用于zlib級別的實時壓縮場景,并且具有更好的壓縮比。本文將來講講ZSTD算法的使用,需要的可以參考一下
    2022-08-08
  • xxx_cast類型轉換的實現(xiàn)方法

    xxx_cast類型轉換的實現(xiàn)方法

    下面小編就為大家?guī)硪黄獂xx_cast類型轉換的實現(xiàn)方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-12-12
  • sdl顯示一張bmp圖片示例

    sdl顯示一張bmp圖片示例

    這篇文章主要介紹了sdl顯示一張bmp圖片示例,需要的朋友可以參考下
    2014-04-04
  • C語言讀取文件流的相關函數(shù)用法簡介

    C語言讀取文件流的相關函數(shù)用法簡介

    這篇文章主要介紹了C語言讀取文件流的相關函數(shù)用法簡介,包括fread()函數(shù)和feof()函數(shù)的使用,需要的朋友可以參考下
    2015-08-08
  • C語言實現(xiàn)簡單的貪吃蛇游戲

    C語言實現(xiàn)簡單的貪吃蛇游戲

    這篇文章主要為大家詳細介紹了C語言實現(xiàn)簡單的貪吃蛇游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-07-07

最新評論