常用C++庫(1)日誌庫

    ~~~~做C++好幾年了,記錄一個自己用的C++日誌庫吧,自己不記得也可以看看:log4cpp

下載

    ~~~~這是資源路徑:log4cpp 1.1.3
    ~~~~如果資源失效,就去官網下載吧,官網吧!http://log4cpp.sourceforge.net/#download。
    ~~~~下不到的可以找我幫忙下載,也可以找我要。

編譯安裝

    ~~~~下載好後,找個路徑解壓,編譯就可以了,我是在ubuntu上使用,make check是測試一下編譯有沒有問題:

./configure
make
make check 
sudo make install

    ~~~~windows上:需要使用cmake編譯,安裝cmake直接去官網下載,安裝就行了,沒有什麼難度的東西(附上官網下載頁面地址:https://cmake.org/download/)。
    ~~~~windows使用cmake就差不多了,解壓壓縮包,進入目錄,創建build目錄,進入目錄,cmake …/。

E:\boost\log4cpp-1.1.3\log4cpp>mkdir build

E:\boost\log4cpp-1.1.3\log4cpp>cd build

E:\boost\log4cpp-1.1.3\log4cpp\build>cmake ../
-- Building for: Visual Studio 15 2017
-- The C compiler identification is MSVC 19.15.26726.0
-- The CXX compiler identification is MSVC 19.15.26726.0
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2017/Professional/VC/Tools/MSVC/14.15.26726/bin/Hostx86/x86/cl.exe
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2017/Professional/VC/Tools/MSVC/14.15.26726/bin/Hostx86/x86/cl.exe -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2017/Professional/VC/Tools/MSVC/14.15.26726/bin/Hostx86/x86/cl.exe
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2017/Professional/VC/Tools/MSVC/14.15.26726/bin/Hostx86/x86/cl.exe -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
CMake Warning (dev) in CMakeLists.txt:
  No cmake_minimum_required command is present.  A line of code such as

    cmake_minimum_required(VERSION 3.13)

  should be added at the top of the file.  The version specified may be lower
  if you wish to support older CMake versions for this project.  For more
  information run "cmake --help-policy CMP0000".
This warning is for project developers.  Use -Wno-dev to suppress it.

-- Configuring done
-- Generating done
-- Build files have been written to: E:/boost/log4cpp-1.1.3/log4cpp/build

E:\boost\log4cpp-1.1.3\log4cpp\build>

    ~~~~然後用vs打開編譯即可。

例子

例子源碼:demo.

    ~~~~剛剛編譯好的庫,我們可以寫一個例子,記錄下來怎麼使用,這樣下次不記得可以快速想起來怎麼用。我這裏主要還是在ubuntu上,版本是16.04。
   ~~~創建文件Clog4Util.h

#ifndef CLOG4UTIL_INCLUDE
#define CLOG4UTIL_INCLUDE

#include "log4cpp/Category.hh"
#include "log4cpp/Appender.hh"
#include "log4cpp/FileAppender.hh"
#include "log4cpp/OstreamAppender.hh"
#include "log4cpp/Layout.hh"
#include "log4cpp/BasicLayout.hh"
#include "log4cpp/Priority.hh"
#include "log4cpp/PropertyConfigurator.hh"

#include <iostream>
#include <string>

using namespace std;

namespace log4Util {

        int Init(string initfilename);

        void close();

        void Debug(const char* tag, const char* msg);

        void Debug(const char* msg);

        void Info(const char* tag, const char* msg);

        void Info(const char* msg);

        void Error(const char* tag, const char* msg);

        void Error(const char* msg);
}
#endif

    ~~~~創建文件Clog4Util.cpp

#include "Clog4Util.h"

namespace log4Util {

	int Init(string initfilename) {
		try {
			log4cpp::PropertyConfigurator::configure(initfilename);
		}
		catch (log4cpp::ConfigureFailure&f) {
			std::cout << "Configure Problem " << f.what() << std::endl;//失敗
			return -1;
		}
		return 0;
	}

	void close() {
		try {
			log4cpp::Category::shutdown();
		}
		catch (...) {

		}
	}

	void Debug(const char* tag, const char* msg) {
		try {
			log4cpp::Category& t_debug = log4cpp::Category::getInstance(tag);
			t_debug.debug(msg);
		}
		catch (...) {

		}
	}

	void Debug(const char* msg) {
		try {
			log4cpp::Category& t_debug = log4cpp::Category::getInstance(string("Debug"));
			t_debug.debug(msg);
		}
		catch (...) {

		}
	}

	void Info(const char* tag, const char* msg) {
		try {
			log4cpp::Category& t_debug = log4cpp::Category::getInstance(tag);
			t_debug.info(msg);
		}
		catch (...) {

		}
	}

	void Info(const char* msg) {
		try {
			log4cpp::Category& t_info = log4cpp::Category::getInstance(string("Info"));
			t_info.info(msg);
		}
		catch (...) {

		}
	}

	void Error(const char* tag, const char* msg) {
		try {
			log4cpp::Category& t_debug = log4cpp::Category::getInstance(tag);
			t_debug.error(msg);
		}
		catch (...) {

		}
	}

	void Error(const char* msg) {
		try {
			log4cpp::Category& t_error = log4cpp::Category::getInstance(string("Error"));
			t_error.error(msg);
		}
		catch (...) {

		}
	}
}

    ~~~~創建文件main.cpp:

#include <stdio.h>

#include <ctype.h>
#include <signal.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>  
#include <thread>
#include <time.h>

#include "Clog4Util.h"

int main(int argc, char *argv[])
{
    if(argc < 2){
        printf("Usage: %s base exponent \n", argv[0]);
        return 1;
    }

    log4Util::Init(argv[1]);

    printf("test log4cpp\n");

    //存放日誌信息
    char strLog_[1024] = { 0 };

    //建日誌寫到strLog_中
    sprintf(strLog_, "test error log, %s:%d.", __FILE__, __LINE__);

    //寫入日誌
    log4Util::Error(strLog_);

    //賦空
    memset(strLog_, 0, 1024);

    //建日誌寫到strLog_中
    sprintf(strLog_, "test error log, %s:%d.", __FILE__, __LINE__);

    //寫入日誌
    log4Util::Debug(strLog_);

    //賦空
    memset(strLog_, 0, 1024);

    //建日誌寫到strLog_中
    sprintf(strLog_, "test error log, %s:%d.", __FILE__, __LINE__);

    //寫入日誌
    log4Util::Info(strLog_);
	
    //賦空
    memset(strLog_, 0, 1024);

    //建日誌寫到strLog_中
    sprintf(strLog_, "test error log,tag:1 %s:%d.", __FILE__, __LINE__);

    //寫入日誌
    log4Util::Error("1", strLog_);

    //賦空
	memset(strLog_, 0, 1024);

    //建日誌寫到strLog_中
	sprintf(strLog_, "test error log,tag:2 %s:%d.", __FILE__, __LINE__);

    //寫入日誌
    log4Util::Debug("2", strLog_);

    //賦空
    memset(strLog_, 0, 1024);

    //建日誌寫到strLog_中
    sprintf(strLog_, "test error log,tag:3 %s:%d.", __FILE__, __LINE__);

    //寫入日誌
    log4Util::Info("3", strLog_);

	log4Util::close();
	
    return 0;
}

    ~~~~創建CMakeList.txt文件:

project(test)

#添加C++11支持
add_definitions(-std=c++11)

#添加頭文件搜索路徑
include_directories(/usr/local/include)

#添加庫文件搜索路徑
link_directories(/usr/local/lib)

#用於將當前目錄下的所有源文件的名字保存在變量 DIR_SRCS 中
aux_source_directory(. DIR_SRCS)

add_executable(test ${DIR_SRCS})

#在這裏根據名字log4cpp尋找文件
target_link_libraries(test log4cpp pthread)

    ~~~~創建目錄build,進入目錄編譯

qilimi@sharenew-desktop:~/test/log4cpp/1-test/build$ cmake ../
-- The C compiler identification is GNU 5.4.0
-- The CXX compiler identification is GNU 5.4.0
...
-- Configuring done
-- Generating done
-- Build files have been written to: /home/qilimi/test/log4cpp/1-test/build
qilimi@sharenew-desktop:~/test/log4cpp/1-test/build$ make
Scanning dependencies of target test
[ 33%] Building CXX object CMakeFiles/test.dir/main.o
[ 66%] Linking CXX executable test
[100%] Built target test
qilimi@sharenew-desktop:~/test/log4cpp/1-test/build$

    ~~~~現在還需要一個日誌文件配置文件,我們創建文件log4cpp.conf:

#-------定義rootCategory的屬性-------

#指定rootCategory的log優先級是ERROR,其Appenders有兩個,分別是console,TESTAppender
log4cpp.rootCategory=DEBUG, console, TESTAppender

#-------定義console屬性-------

#consoleAppender類型:控制檯輸出
#下面這三條語句表示控制檯輸出的log輸出的佈局按照指定的格式;輸出格式是:[%p] %d{%H:%M:%S.%l} (%c): %m%n
log4cpp.appender.console=ConsoleAppender
log4cpp.appender.console.layout=PatternLayout
log4cpp.appender.console.layout.ConversionPattern=[%p] %d{%H:%M:%S.%l} (%c): %m%n

#-------定義TESTAppender的屬性-------

#RollingFileAppender類型:輸出到回捲文件,即文件到達某個大小的時候產生一個新的文件
#下面的語句表示文件輸出到指定的log文件,輸出的佈局按照指定的格式,輸出的格式是:[%d{%Y-%m-%d %H:%M:%S.%l} - %p] (%c): %m%n
log4cpp.appender.TESTAppender=RollingFileAppender

#當日志文件到達maxFileSize大小時,將會自動滾動
log4cpp.appender.TESTAppender.maxFileSize=1024000

#maxBackupIndex指定可以產生的滾動文件的最大數
log4cpp.appender.TESTAppender.maxBackupIndex=40

#fileName指定信息輸出到logs/irismatch.log文件
log4cpp.appender.TESTAppender.fileName=/home/qilimi/log.log

#PatternLayout 表示可以靈活指定佈局模式
log4cpp.appender.TESTAppender.layout=PatternLayout

#append=true 信息追加到上面指定的日誌文件中,false表示將信息覆蓋指定文件內容
log4cpp.appender.TESTAppender.append=true
log4cpp.appender.TESTAppender.layout.ConversionPattern=[%d{%Y-%m-%d %H:%M:%S.%l} - %p] (%c): %m%n

    ~~~~讓我們直接運行程序:

qilimi@sharenew-desktop:~/test/log4cpp/1-test/build$ ./test log4cpp.conf
test log4cpp
[ERROR] 18:42:34.604 (Error): test error log, /home/qilimi/test/log4cpp/1-test/main.cpp:27.
[DEBUG] 18:42:34.604 (Debug): test error log, /home/qilimi/test/log4cpp/1-test/main.cpp:36.
[INFO] 18:42:34.604 (Info): test error log, /home/qilimi/test/log4cpp/1-test/main.cpp:45.
[ERROR] 18:42:34.604 (1): test error log,tag:1 /home/qilimi/test/log4cpp/1-test/main.cpp:54.
[DEBUG] 18:42:34.604 (2): test error log,tag:2 /home/qilimi/test/log4cpp/1-test/main.cpp:63.
[INFO] 18:42:34.604 (3): test error log,tag:3 /home/qilimi/test/log4cpp/1-test/main.cpp:72.
qilimi@sharenew-desktop:~/test/log4cpp/1-test/build$ cat ~/log.log
[2019-02-15 18:34:58.868 - ERROR] (Error): test error log, /home/qilimi/test/log4cpp/1-test/main.cpp:28.
[2019-02-15 18:34:58.868 - DEBUG] (Debug): test error log, /home/qilimi/test/log4cpp/1-test/main.cpp:37.
[2019-02-15 18:34:58.868 - INFO] (Info): test error log, /home/qilimi/test/log4cpp/1-test/main.cpp:46.
[2019-02-15 18:34:58.868 - ERROR] (1): test error log,tag:1 /home/qilimi/test/log4cpp/1-test/main.cpp:55.
[2019-02-15 18:34:58.868 - DEBUG] (2): test error log,tag:2 /home/qilimi/test/log4cpp/1-test/main.cpp:64.
[2019-02-15 18:34:58.868 - INFO] (3): test error log,tag:3 /home/qilimi/test/log4cpp/1-test/main.cpp:73.
[2019-02-15 18:40:26.625 - ERROR] (Error): test error log, /home/qilimi/test/log4cpp/1-test/main.cpp:28.
[2019-02-15 18:40:26.625 - DEBUG] (Debug): test error log, /home/qilimi/test/log4cpp/1-test/main.cpp:37.
[2019-02-15 18:40:26.625 - INFO] (Info): test error log, /home/qilimi/test/log4cpp/1-test/main.cpp:46.
[2019-02-15 18:40:26.625 - ERROR] (1): test error log,tag:1 /home/qilimi/test/log4cpp/1-test/main.cpp:55.
[2019-02-15 18:40:26.625 - DEBUG] (2): test error log,tag:2 /home/qilimi/test/log4cpp/1-test/main.cpp:64.
[2019-02-15 18:40:26.626 - INFO] (3): test error log,tag:3 /home/qilimi/test/log4cpp/1-test/main.cpp:73.
[2019-02-15 18:42:34.604 - ERROR] (Error): test error log, /home/qilimi/test/log4cpp/1-test/main.cpp:27.
[2019-02-15 18:42:34.604 - DEBUG] (Debug): test error log, /home/qilimi/test/log4cpp/1-test/main.cpp:36.
[2019-02-15 18:42:34.604 - INFO] (Info): test error log, /home/qilimi/test/log4cpp/1-test/main.cpp:45.
[2019-02-15 18:42:34.604 - ERROR] (1): test error log,tag:1 /home/qilimi/test/log4cpp/1-test/main.cpp:54.
[2019-02-15 18:42:34.604 - DEBUG] (2): test error log,tag:2 /home/qilimi/test/log4cpp/1-test/main.cpp:63.
[2019-02-15 18:42:34.604 - INFO] (3): test error log,tag:3 /home/qilimi/test/log4cpp/1-test/main.cpp:72.
qilimi@sharenew-desktop:~/test/log4cpp/1-test/build$

    ~~~~完成。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章