Pages - Menu

標籤

AWS (1) bash (1) Boost (2) C (2) CMake (2) Concurrency_Programming (3) CPP (37) Database (2) DNS (1) Docker (4) Docker-Compose (1) ELK (1) emacs (4) gcp (1) gdrive (1) git (1) gitbash (2) gitlab (1) kvm (4) Linux (5) MT4 (4) MT5 (4) Multicast (2) MySQL (2) Nijatrader8 (1) OpenCV (1) Python (4) QT5 (1) R (1) rdp (3) screenshot (1) ssh (3) Tabnine (1) TCP (1) TensorFlow (1) Tools (12) Ubuntu_1904 (11) Ubuntu_20_04 (5) UDP (1) VS2010 (1) VS2015 (1) VS2019 (1) WebServer (1) Win10 (1) winmerge (1) WSL (1) xrdp (1)

搜尋此網誌

2020年4月30日星期四

Logger

Aim

In cpp project, we may need a way to handle log message. This class is an example to show how to do that. This logger is aimed running on Linux and Windows. This is a singleton class.

Source

#include <iostream>
#include "Logger.h"

// usage: ./Logger
class Foo
{
public:
    Foo() { LOGMSG_CLASS_NAME("Foo"); }
    ~Foo() {}

    void PrintLog()
    {
        LOGMSG_MSG("Multi thread function--------------\n");
        LOGMSG_DBG("Log inside class\n");
        LOGMSG_WRN("Log inside class\n");
        LOGMSG_MSG("Log inside class\n");
        LOGMSG_ERR("Log inside class\n");
        LOGMSG_MSG("\n");

        LOGMSG_MSG_S() << "Single thread function-----------------\n";
        LOGMSG_MSG_S() << "Log inside class\n";
        LOGMSG_DBG_S() << "Log inside class\n";
        LOGMSG_WRN_S() << "Log inside class\n";
        LOGMSG_ERR_S() << "Log inside class\n";
    }
};
int main (int argc, char *argv[])
{
    Logger::LoggerConfig config;
    config.logLevel = Logger::LogLevel::DEBUG;
    config.logPath = "./tempLog";
    config.fileSize = 0;
    config.fileSizeLimit = 4 * 1024 * 1024; // 4 MByte
    config.isToConsole = true;
    config.isToFile = true;

    LOGMSG_INIT(config);
    LOGMSG_MSG_C("Multi thread function--------------\n");
    LOGMSG_DBG_C("Log outside class\n");
    LOGMSG_WRN_C("Log outside class\n");
    LOGMSG_MSG_C("Log outside class\n");
    LOGMSG_ERR_C("Log outside class\n");
    LOGMSG_MSG_C("\n");

    LOGMSG_MSG_S_C() << "Single thread function-----------------\n";
    LOGMSG_MSG_S_C() << "Log outside class\n";
    LOGMSG_DBG_S_C() << "Log outside class\n";
    LOGMSG_WRN_S_C() << "Log outside class\n";
    LOGMSG_ERR_S_C() << "Log outside class\n";

    Foo xx;
    xx.PrintLog();
    return 0;
}

Output

This logger can output to console and files. Following is the example of the output. The file size and the location can be controlled by a config class.
20200430_152749_293 [MSG]                      main:    38, 13163,Multi thread function--------------
20200430_152749_293 [DBG]                      main:    39, 13163,Log outside class
20200430_152749_293 [WRN]                      main:    40, 13163,Log outside class
20200430_152749_293 [MSG]                      main:    41, 13163,Log outside class
20200430_152749_293 [ERR]                      main:    42, 13163,Log outside class
20200430_152749_293 [MSG]                      main:    43, 13163,
20200430_152749_293 [MSG]                      main:    45, 13163,Single thread function-----------------
20200430_152749_293 [MSG]                      main:    46, 13163,Log outside class
20200430_152749_293 [DBG]                      main:    47, 13163,Log outside class
20200430_152749_293 [WRN]                      main:    48, 13163,Log outside class
20200430_152749_293 [ERR]                      main:    49, 13163,Log outside class
20200430_152749_293 [MSG]                       Foo::                 PrintLog:    13, 13163,Multi thread function--------------
20200430_152749_293 [DBG]                       Foo::                 PrintLog:    14, 13163,Log inside class
20200430_152749_293 [WRN]                       Foo::                 PrintLog:    15, 13163,Log inside class
20200430_152749_293 [MSG]                       Foo::                 PrintLog:    16, 13163,Log inside class
20200430_152749_293 [ERR]                       Foo::                 PrintLog:    17, 13163,Log inside class
20200430_152749_293 [MSG]                       Foo::                 PrintLog:    18, 13163,
20200430_152749_293 [MSG]                       Foo::                 PrintLog:    20, 13163,Single thread function-----------------
20200430_152749_293 [MSG]                       Foo::                 PrintLog:    21, 13163,Log inside class
20200430_152749_293 [DBG]                       Foo::                 PrintLog:    22, 13163,Log inside class
20200430_152749_293 [WRN]                       Foo::                 PrintLog:    23, 13163,Log inside class
20200430_152749_293 [ERR]                       Foo::                 PrintLog:    24, 13163,Log inside class

CMakeLists.txt

if (UNIX)
else ()
  list(REMOVE_ITEM ${folderName}_inc ${CMAKE_CURRENT_SOURCE_DIR}/DefaultMutex.h)
  list(REMOVE_ITEM ${folderName}_inc ${CMAKE_CURRENT_SOURCE_DIR}/NLFileSys.h)
  list(REMOVE_ITEM ${folderName}_src ${CMAKE_CURRENT_SOURCE_DIR}/DefaultMutex.cpp)
  list(REMOVE_ITEM ${folderName}_src ${CMAKE_CURRENT_SOURCE_DIR}/NLFileSys.cpp)
endif ()
We remove some files during building binary if we are not using linux.

# Handle extra Flags
if (Use_Linux_Lock)
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DUse_Linux_Lock")
  message(STATUS "Use linux lock")
elseif (Use_Windows_Lock)
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DUse_Windows_Lock")
  message(STATUS "Use window lock")
else ()
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DUse_Std_Lock")
  message(STATUS "Use std lock")
endif ()
We can select which mutex lock to use while using cmake.

Example:

cmake -G Ninja ../Logger/ -DCMAKE_BUILD_TYPE=Debug -DUse_Linux_Lock=True

Logger.h

#ifdef Use_Linux_Lock
#include "DefaultMutex.h"
#elif Use_Windows_Lock
#elif Use_Std_Lock
#include <mutex>
#endif
Here shows how to select library files in cpp.

沒有留言:

發佈留言