日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

日志库EasyLogging++学习系列(5)—— 辅助配置功能

發(fā)布時(shí)間:2025/3/12 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 日志库EasyLogging++学习系列(5)—— 辅助配置功能 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

正如前面《日志庫(kù)EasyLogging++學(xué)習(xí)系列(3)—— 配置功能》文中最后提到的,在某些應(yīng)用場(chǎng)景下,我們還需要通過(guò)其他的一些配置手段來(lái)輔助我們完成某些特殊功能,這些輔助配置手段包括設(shè)置命令行參數(shù)、設(shè)置日志標(biāo)記、配置宏定義。本文中就對(duì)這幾個(gè)輔助配置功能逐一進(jìn)行簡(jiǎn)要介紹。


命令行參數(shù)


在 Easylogging++ 中可以通過(guò)START_EASYLOGGINGPP(argc, argv)?來(lái)完成命令行參數(shù)的設(shè)置,下面的表格列舉了GitHub上給出的 Easylogging++ 支持的命令行參數(shù):


ArgumentDescription
-vActivates maximum verbosity
--v=2Activates verbosity upto verbose level 2 (valid range: 0-9)
--verboseActivates maximum verbosity
-vmodule=MODULE_NAMEActivates verbosity for files starting with main to level 1, the rest of the files depend on logging flag?AllowVerboseIfModuleNotSpecified?Please see Logging Flags section above. Two modules can be separated by comma. Please note vmodules are last in order of precedence of checking arguments for verbose logging, e.g, if we have -v in application arguments before vmodules, vmodules will be ignored.
--logging-flags=3Sets logging flag. In example?i.e, 3, it sets logging flag toNewLineForContainer?and?AllowVerboseIfModuleNotSpecified. See logging flags section above for further details and values. See macros section to disable this function.
--default-log-file=FILESets default log file for existing and future loggers. You may want to consider defining?ELPP_NO_DEFAULT_LOG_FILE?to prevent creation of default empty log file during pre-processing. See macros section to disable this function.
對(duì)于上述命令行參數(shù),有必要簡(jiǎn)單說(shuō)明一下:

  • 其中的 -v 、 --v 、 --verbose 、 -vmodule 都是用來(lái)設(shè)置 Verbose 詳細(xì)日志的,而且這幾個(gè)參數(shù)之間是有優(yōu)先級(jí)順序的,如下:-v ?優(yōu)先于 --verbose 優(yōu)先于 --v 優(yōu)先于 -vmodule。但是,在效果上 -v 和 --verbose 是一樣的。請(qǐng)看下面的例子:

命令行參數(shù)例一:--v=2 -v ,在這里參數(shù) --v=2 會(huì)被參數(shù) -v 覆蓋,因?yàn)?-v 優(yōu)先級(jí)最高。

命令行參數(shù)例二:--verbose -vmodule=main=3 ,在這里參數(shù) -vmodule=main=3?會(huì)被參數(shù) --verbose 覆蓋,因?yàn)?--verbose?優(yōu)先于 -vmodule?。

命令行參數(shù)例三:-vmodule=main*=3 --v=5 -v --verbose ,在這里參數(shù) -v 會(huì)覆蓋其他所有的參數(shù),因?yàn)?-v 優(yōu)先級(jí)最高。

  • 其中的 --logging-flags 是用來(lái)設(shè)置日志標(biāo)記的,而且必須定義?ELPP_LOGGING_FLAGS_FROM_ARG?這個(gè)宏定義。
  • 其中的 --default-log-file 是用來(lái)設(shè)置日志默認(rèn)保存文件名的。如果沒(méi)用這個(gè)命令行參數(shù)設(shè)置文件名,那么默認(rèn)的文件名就是?logs\\myeasylog.log 。另外,我們也可以用宏定義?ELPP_DEFAULT_LOG_FILE?來(lái)達(dá)到相同的效果。如:#defineELPP_DEFAULT_LOG_FILE "logs\\tem\\test.log" 。
下面的代碼演示了設(shè)置命令行參數(shù) --v=2 ,其最終效果是 VLOG(3) 將會(huì)被屏蔽: [cpp] view plaincopy print?
  • #include?"easylogging++.h"??
  • ??
  • INITIALIZE_EASYLOGGINGPP??
  • ??
  • int?main(int?argc,?char**?argv)??
  • {??
  • ????///?請(qǐng)自行加上命令行參數(shù)?--v=2,否則達(dá)不到演示的效果??
  • ????START_EASYLOGGINGPP(argc,?argv);??
  • ??
  • ????VLOG(0);??
  • ????VLOG(1);??
  • ????VLOG(2);??
  • ????VLOG(3);??
  • ??
  • ????system("pause");??
  • ????return?0;??
  • }??
  • #include "easylogging++.h"INITIALIZE_EASYLOGGINGPPint main(int argc, char** argv) {/// 請(qǐng)自行加上命令行參數(shù) --v=2,否則達(dá)不到演示的效果START_EASYLOGGINGPP(argc, argv);VLOG(0);VLOG(1);VLOG(2);VLOG(3);system("pause");return 0; }更多關(guān)于Verbose日志的詳細(xì)信息請(qǐng)參考《日志庫(kù)EasyLogging++學(xué)習(xí)系列(8)—— Verbose日志詳解》。

    日志標(biāo)記


    在學(xué)習(xí)日志標(biāo)記之前,我們先來(lái)看看 Easylogging++ 提供出來(lái)的三個(gè)和日志標(biāo)記緊密相關(guān)的功能接口函數(shù):

    • 增加標(biāo)記函數(shù):?el::Loggers::addFlag?
    • 刪除標(biāo)記函數(shù):el::Loggers::removeFlag?
    • 檢查標(biāo)記函數(shù):el::Loggers::hasFlag?

    下面的表格列舉了GitHub上給出的 Easylogging++ 支持的日志標(biāo)記:


    FlagDescription
    NewLineForContainer (1)Makes sure we have new line for each Container log entry
    AllowVerboseIfModuleNotSpecified (2)Makes sure if -vmodule is used and does not specifies a module, then verbose logging is allowed via that module. Say param was -vmodule=main*=3 and a verbose log is being written from a file called something.cpp then if this flag is enabled, log will be written otherwise it will be disallowed. Note: having this defeats purpose of -vmodule
    LogDetailedCrashReason (4)When handling crashes by default, detailed crash reason will be logged as well (Disabled by default) (issue #90)
    DisableApplicationAbortOnFatalLog (8)Allows to disable application abortion when logged using FATAL level. Note that this does not apply to default crash handlers as application should be aborted after crash signal is handled. (Not added by default) (issue #119)
    ImmediateFlush (16)Flushes log with every log-entry (performance sensative) - Disabled by default
    StrictLogFileSizeCheck (32)Makes sure log file size is checked with every log
    ColoredTerminalOutput (64)Terminal output will be colorful if supported by terminal.
    MultiLoggerSupport (128)Enables support for using multiple loggers to log single message. (E.g,?CLOG(INFO, "default", "network") << This will be logged using default and network loggers;)
    DisablePerformanceTrackingCheckpointComparison (256)Disables checkpoint comparison
    DisableVModules (512)Disables usage of vmodules
    DisableVModulesExtensions (1024)Disables vmodules extension. This means if you have a vmodule -vmodule=main*=4 it will cover everything starting with main, where as if you do not have this defined you will be covered for any file starting with main and ending with one of the following extensions; .h .c .cpp .cc .cxx .-inl-.h .hxx .hpp. Please note following vmodule is not correct -vmodule=main.=4 with this macro not defined because this will check for main..c, notice double dots. If you want this to be valid, have a look at logging flag above: AllowVerboseIfModuleNotSpecified '?' and '' wildcards are supported
    HierarchicalLogging (2048)Enables hierarchical logging. This is not applicable to verbose logging.
    CreateLoggerAutomatically (4096)Creates logger automatically when not available.
    AutoSpacing (8192)Automatically adds spaces. E.g,LOG(INFO) << "DODGE" << "THIS!";?will output "DODGE THIS!"
    FixedTimeFormat (16384)Applicable to performace tracking only - this prevents formatting time. E.g,?1001 ms?will be logged as is, instead of formatting it as?1.01 sec
    對(duì)于上述的日志標(biāo)記,可在 Easylogging++ 源碼中查看枚舉值el::LoggingFlag?,每個(gè)標(biāo)記的含義在表格和源碼中都描述得比較清楚了,不過(guò)還是建議大家都親自動(dòng)手試一試,下面的代碼演示了標(biāo)記?NewLineForContainer?的作用:

    [cpp] view plaincopy print?
  • #define?ELPP_STL_LOGGING??
  • #include?"easylogging++.h"??
  • ??
  • INITIALIZE_EASYLOGGINGPP??
  • ??
  • int?main(int?argc,?char**?argv)??
  • {??
  • ????std::vector<int>?vecNum;??
  • ????vecNum.push_back(1);??
  • ????vecNum.push_back(2);??
  • ????vecNum.push_back(3);??
  • ??
  • ????///?增加標(biāo)記?NewLineForContainer,注意查看輸出STL容器的效果??
  • ????el::Loggers::addFlag(el::LoggingFlag::NewLineForContainer);??
  • ????LOG(INFO)?<<?vecNum;??
  • ??
  • ????///?刪除標(biāo)記?NewLineForContainer,注意查看輸出STL容器的效果??
  • ????el::Loggers::removeFlag(el::LoggingFlag::NewLineForContainer);??
  • ????LOG(INFO)?<<?vecNum;??
  • ??????
  • ????system("pause");??
  • ????return?0;??
  • }??
  • #define ELPP_STL_LOGGING #include "easylogging++.h"INITIALIZE_EASYLOGGINGPPint main(int argc, char** argv) {std::vector<int> vecNum;vecNum.push_back(1);vecNum.push_back(2);vecNum.push_back(3);/// 增加標(biāo)記 NewLineForContainer,注意查看輸出STL容器的效果el::Loggers::addFlag(el::LoggingFlag::NewLineForContainer);LOG(INFO) << vecNum;/// 刪除標(biāo)記 NewLineForContainer,注意查看輸出STL容器的效果el::Loggers::removeFlag(el::LoggingFlag::NewLineForContainer);LOG(INFO) << vecNum;system("pause");return 0; }

    配置宏定義


    在 Easylogging++ 中,有些功能必須定義相應(yīng)的宏定義才能開啟。為了方便記憶,Easylogging++ 中所有的宏定義都是以 ELPP_ 開頭的,所以可以在源碼中搜索 ELPP_ 來(lái)查看都定義了哪些宏定義。下面的表格列舉了GitHub上給出的 Easylogging++ 支持的宏定義(只列舉了部分):


    Macro NameDescription
    ELPP_DEBUG_ASSERT_FAILUREAborts application on first assertion failure. This assertion is due to invalid input e.g, invalid configuration file etc.
    ELPP_UNICODEEnables Unicode support when logging. RequiresSTART_EASYLOGGINGPP
    ELPP_THREAD_SAFEEnables thread-safety - make sure -lpthread linking for Linux.
    ELPP_FORCE_USE_STD_THREADForces to use C++ standard library for threading (Only useful when using?ELPP_THREAD_SAFE
    ELPP_STACKTRACE_ON_CRASHApplicable to GCC only. Enables stacktrace on application crash
    ELPP_DISABLE_DEFAULT_CRASH_HANDLINGDisables default crash handling. You can use el::Helpers::setCrashHandler to use your own handler.
    ELPP_DISABLE_LOGSDisables all logs - (preprocessing)
    ELPP_DISABLE_DEBUG_LOGSDisables debug logs - (preprocessing)
    ELPP_DISABLE_INFO_LOGSDisables info logs - (preprocessing)
    ELPP_DISABLE_WARNING_LOGSDisables warning logs - (preprocessing)
    ELPP_DISABLE_ERROR_LOGSDisables error logs - (preprocessing)
    ELPP_DISABLE_FATAL_LOGSDisables fatal logs - (preprocessing)
    ELPP_DISABLE_VERBOSE_LOGSDisables verbose logs - (preprocessing)
    ELPP_DISABLE_TRACE_LOGSDisables trace logs - (preprocessing)
    ELPP_FORCE_ENV_VAR_FROM_BASHIf environment variable could not be found, force using alternative bash command to find value, e.g,whoami?for username. (DO NOT USE THIS MACRO WITH?LD_PRELOAD?FOR LIBRARIES THAT ARE ALREADY USING Easylogging++ OR YOU WILL END UP IN STACK OVERFLOW FOR PROCESSES (popen) (see?issue #87?for details))
    ELPP_DEFAULT_LOG_FILEFull filename where you want initial files to be created. You need to embed value of this macro with quotes, e.g,?-DELPP_DEFAULT_LOG_FILE='"logs/el.gtest.log"'Note the double quotes inside single quotes, double quotes are the values for?const char*?and single quotes specifies value of macro
    ELPP_NO_DEFAULT_LOG_FILEIf you dont want to initialize library with default log file, define this macro. But be sure to configure your logger with propery log filename or you will end up getting heaps of errors when trying to log to file (andTO_FILE?is configured to?true)
    ELPP_DEBUG_ERRORSIf you wish to find out internal errors raised by Easylogging++ that can be because of configuration or something else, you can enable them by defining this macro. You will get your errors on standard output i.e, terminal or command prompt.
    ELPP_DISABLE_CUSTOM_FORMAT_SPECIFIERSForcefully disables custom format specifiers
    ELPP_DISABLE_LOGGING_FLAGS_FROM_ARGForcefully disables ability to set logging flags using command-line arguments
    ELPP_DISABLE_LOG_FILE_FROM_ARGForcefully disables ability to set default log file from command-line arguments
    ELPP_WINSOCK2On windows system force to use?winsock2.hinstead of?winsock.h?when?WIN32_LEAN_AND_MEANis defined

    利用在上述的宏定義,可以完成一些配置文件中無(wú)法完成的配置。下面簡(jiǎn)單介紹幾個(gè)比較實(shí)用的宏定義:

    • ELPP_DEBUG_ASSERT_FAILURE?宏定義,可以幫助我們檢測(cè)配置文件中的配置項(xiàng)名稱是否正確,假如配置項(xiàng)名稱不是Easylogging++ 支持的,那么就會(huì)出現(xiàn)斷言中斷。
    • ELPP_UNICODE?宏定義,默認(rèn)的日志記錄是只支持多字節(jié)字符串的,同時(shí)使用這個(gè)宏定義和?START_EASYLOGGINGPP(argc, argv) 可以開啟 Unicode 字符串記錄日志的功能。
    • ELPP_THREAD_SAFE?宏定義,在默認(rèn)情況下,考慮到性能,多線程安全功能是關(guān)閉的,使用這個(gè)宏定義則可以開啟。
    • ELPP_STL_LOGGING?宏定義,使 Easylogging++ 支持STL模板和容器類型的日志輸出,比如上面第二部分介紹日志標(biāo)記最后給出例子。考慮到性能,每個(gè)容器最大容量是 100,可以通過(guò)修改源碼改變這個(gè)限制,但不建議這么做,除非你可以無(wú)視性能和效率。
    當(dāng)然,其他的宏定義也很實(shí)用,建議大家也可以親自試一試,看看其實(shí)際的作用。需要注意的是,有些宏必須在包含?Easylogging++ 頭文件之前,即必須在代碼?#include "easylogging++.h" 之前完成定義才有意義。下面的代碼演示了宏定義 ELPP_UNICODE?的作用: [cpp] view plaincopy print?
  • #define?ELPP_UNICODE??
  • #include?"easylogging++.h"??
  • ??
  • INITIALIZE_EASYLOGGINGPP??
  • ??
  • int?main(int?argc,?char**?argv)??
  • {??
  • ????///?同時(shí)使用?START_EASYLOGGINGPP?才能使用Unicode???
  • ????START_EASYLOGGINGPP(argc,?argv);??
  • ??
  • ????LOG(INFO)?<<?L"宏定義演示。";??
  • ??
  • ????system("pause");??
  • ????return?0;??
  • }??
  • #define ELPP_UNICODE #include "easylogging++.h"INITIALIZE_EASYLOGGINGPPint main(int argc, char** argv) {/// 同時(shí)使用 START_EASYLOGGINGPP 才能使用Unicode START_EASYLOGGINGPP(argc, argv);LOG(INFO) << L"宏定義演示。";system("pause");return 0; }

    總結(jié)

    以上是生活随笔為你收集整理的日志库EasyLogging++学习系列(5)—— 辅助配置功能的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

    如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。