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.