日志库EasyLogging++学习系列(5)—— 辅助配置功能
正如前面《日志庫EasyLogging++學習系列(3)—— 配置功能》文中最后提到的,在某些應用場景下,我們還需要通過其他的一些配置手段來輔助我們完成某些特殊功能,這些輔助配置手段包括設置命令行參數、設置日志標記、配置宏定義。本文中就對這幾個輔助配置功能逐一進行簡要介紹。
命令行參數
在 Easylogging++ 中可以通過START_EASYLOGGINGPP(argc, argv)?來完成命令行參數的設置,下面的表格列舉了GitHub上給出的 Easylogging++ 支持的命令行參數:
| -v | Activates maximum verbosity |
| --v=2 | Activates verbosity upto verbose level 2 (valid range: 0-9) |
| --verbose | Activates maximum verbosity |
| -vmodule=MODULE_NAME | Activates 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=3 | Sets 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=FILE | Sets 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. |
- 其中的 -v 、 --v 、 --verbose 、 -vmodule 都是用來設置 Verbose 詳細日志的,而且這幾個參數之間是有優先級順序的,如下:-v ?優先于 --verbose 優先于 --v 優先于 -vmodule。但是,在效果上 -v 和 --verbose 是一樣的。請看下面的例子:
命令行參數例一:--v=2 -v ,在這里參數 --v=2 會被參數 -v 覆蓋,因為 -v 優先級最高。
命令行參數例二:--verbose -vmodule=main=3 ,在這里參數 -vmodule=main=3?會被參數 --verbose 覆蓋,因為 --verbose?優先于 -vmodule?。
命令行參數例三:-vmodule=main*=3 --v=5 -v --verbose ,在這里參數 -v 會覆蓋其他所有的參數,因為 -v 優先級最高。
- 其中的 --logging-flags 是用來設置日志標記的,而且必須定義?ELPP_LOGGING_FLAGS_FROM_ARG?這個宏定義。
- 其中的 --default-log-file 是用來設置日志默認保存文件名的。如果沒用這個命令行參數設置文件名,那么默認的文件名就是?logs\\myeasylog.log 。另外,我們也可以用宏定義?ELPP_DEFAULT_LOG_FILE?來達到相同的效果。如:#defineELPP_DEFAULT_LOG_FILE "logs\\tem\\test.log" 。
日志標記
在學習日志標記之前,我們先來看看 Easylogging++ 提供出來的三個和日志標記緊密相關的功能接口函數:
- 增加標記函數:?el::Loggers::addFlag?
- 刪除標記函數:el::Loggers::removeFlag?
- 檢查標記函數:el::Loggers::hasFlag?
下面的表格列舉了GitHub上給出的 Easylogging++ 支持的日志標記:
| 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 |
配置宏定義
在 Easylogging++ 中,有些功能必須定義相應的宏定義才能開啟。為了方便記憶,Easylogging++ 中所有的宏定義都是以 ELPP_ 開頭的,所以可以在源碼中搜索 ELPP_ 來查看都定義了哪些宏定義。下面的表格列舉了GitHub上給出的 Easylogging++ 支持的宏定義(只列舉了部分):
| ELPP_DEBUG_ASSERT_FAILURE | Aborts application on first assertion failure. This assertion is due to invalid input e.g, invalid configuration file etc. |
| ELPP_UNICODE | Enables Unicode support when logging. RequiresSTART_EASYLOGGINGPP |
| ELPP_THREAD_SAFE | Enables thread-safety - make sure -lpthread linking for Linux. |
| ELPP_FORCE_USE_STD_THREAD | Forces to use C++ standard library for threading (Only useful when using?ELPP_THREAD_SAFE |
| ELPP_STACKTRACE_ON_CRASH | Applicable to GCC only. Enables stacktrace on application crash |
| ELPP_DISABLE_DEFAULT_CRASH_HANDLING | Disables default crash handling. You can use el::Helpers::setCrashHandler to use your own handler. |
| ELPP_DISABLE_LOGS | Disables all logs - (preprocessing) |
| ELPP_DISABLE_DEBUG_LOGS | Disables debug logs - (preprocessing) |
| ELPP_DISABLE_INFO_LOGS | Disables info logs - (preprocessing) |
| ELPP_DISABLE_WARNING_LOGS | Disables warning logs - (preprocessing) |
| ELPP_DISABLE_ERROR_LOGS | Disables error logs - (preprocessing) |
| ELPP_DISABLE_FATAL_LOGS | Disables fatal logs - (preprocessing) |
| ELPP_DISABLE_VERBOSE_LOGS | Disables verbose logs - (preprocessing) |
| ELPP_DISABLE_TRACE_LOGS | Disables trace logs - (preprocessing) |
| ELPP_FORCE_ENV_VAR_FROM_BASH | If 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_FILE | Full 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_FILE | If 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_ERRORS | If 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_SPECIFIERS | Forcefully disables custom format specifiers |
| ELPP_DISABLE_LOGGING_FLAGS_FROM_ARG | Forcefully disables ability to set logging flags using command-line arguments |
| ELPP_DISABLE_LOG_FILE_FROM_ARG | Forcefully disables ability to set default log file from command-line arguments |
| ELPP_WINSOCK2 | On windows system force to use?winsock2.hinstead of?winsock.h?when?WIN32_LEAN_AND_MEANis defined |
利用在上述的宏定義,可以完成一些配置文件中無法完成的配置。下面簡單介紹幾個比較實用的宏定義:
- ELPP_DEBUG_ASSERT_FAILURE?宏定義,可以幫助我們檢測配置文件中的配置項名稱是否正確,假如配置項名稱不是Easylogging++ 支持的,那么就會出現斷言中斷。
- ELPP_UNICODE?宏定義,默認的日志記錄是只支持多字節字符串的,同時使用這個宏定義和?START_EASYLOGGINGPP(argc, argv) 可以開啟 Unicode 字符串記錄日志的功能。
- ELPP_THREAD_SAFE?宏定義,在默認情況下,考慮到性能,多線程安全功能是關閉的,使用這個宏定義則可以開啟。
- ELPP_STL_LOGGING?宏定義,使 Easylogging++ 支持STL模板和容器類型的日志輸出,比如上面第二部分介紹日志標記最后給出例子。考慮到性能,每個容器最大容量是 100,可以通過修改源碼改變這個限制,但不建議這么做,除非你可以無視性能和效率。
總結
以上是生活随笔為你收集整理的日志库EasyLogging++学习系列(5)—— 辅助配置功能的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 日志库EasyLogging++学习系列
- 下一篇: matlab中有哪些输出函数,MATLA