Tag Archives: Log4cplus

CMake: How to Complie Log4cplus

 

1. Download the official source code from
git clone –recurse https://github.com/log4cplus/log4cplus.git
log4cplus relies on catch and threadpool, if one of them fails, you can delete the corresponding file in the root of the source code and download it separately
As an example:
rm -rf threadpool
git clone https://github.com/log4cplus/ThreadPool.git threadpool
rm -rf catch
git clone https://github.com/philsquared/Catch.git catch
2. Compile the msvc repository
(1). Switch to the source root, and create a temporary folder for the cmake cache
mkdir .build_msvc
cd .build_msvc
(2). To compile the release version, execute the following command, where CMAKE_INSTALL_PREFIX specifies the library and file output path, in this case to the same level as the source code
# cmake -DCMAKE_INSTALL_PREFIX=… /… /thirdparties/log4cplus/msvc -DCMAKE_DEBUG_POSTFIX=d .
cmake -DCMAKE_INSTALL_PREFIX=. /… /thirdparties/log4cplus/msvc …
cmake –build . –config release -j8
cmake –install .
If the build is successful, there are three subdirectories in the directory, as follows.
— thirdparties/log4cplus/msvc
— bin
— include
— lib
(3). To compile the debug version, execute the following command
# cmake -DCMAKE_INSTALL_PREFIX=… /… /thirdparties/log4cplus/msvc -DCMAKE_DEBUG_POSTFIX=d -DCMAKE_BUILD_TYPE=Debug …
# cmake -DCMAKE_INSTALL_PREFIX=… /… /thirdparties/log4cplus/msvc -DCMAKE_DEBUG_POSTFIX=d …
cmake –build . –config debug -j8
cmake –install . –config debug

3. Compile the mingw32 repository
(1). Add the mingw32/bin directory to the environment variable
PATH=D:/Qt/Qt5.15.2/Tools/mingw810_32/bin/;%PATH%
(2). Switch to the source root, create a temporary folder for the cmake cache
mkdir .mingw32
cd .mingw32
(3). Compile the release version, execute the following command, where CMAKE_INSTALL_PREFIX specifies the library and file output path
cmake.exe -G “MinGW Makefiles” “-DCMAKE_INSTALL_PREFIX:STRING=… /… /thirdparties/log4cplus/mingw32” “-DCMAKE_MAKE_PROGRAM=mingw32-make.exe” “-DCMAKE_C_COMPILER:STRING=gcc.exe” “-DCMAKE_CXX_COMPILER: STRING=g++.exe” …
cmake –build . -j8
cmake –install .
(4). To compile the debug version, execute the following command
cmake.exe -G “MinGW Makefiles” “-DCMAKE_BUILD_TYPE:STRING=Debug” “-DCMAKE_INSTALL_PREFIX:STRING=. /… /thirdparties/log4cplus/mingw32” “-DCMAKE_MAKE_PROGRAM=mingw32-make.exe” “-DCMAKE_C_COMPILER:STRING=gcc.exe” “-DCMAKE_CXX_COMPILER: STRING=g++.exe” …
cmake –build . –config debug -j8
cmake –install . –config debug

4. Using log4cplus in Qt
(1). You can add an environment variable directly from Qt creator to the output path specified during compilation, or you can copy the entire compiled folder to the project’s third-party library path
qt creator add env:
LOG4CPLUS_DIR with a value of . /… /thirdparties/log4cplus/msvc
Or add the path to the cmake file in CMakeLists.txt:
set(log4cplus_DIR … /thirdparties/msvc/lib/cmake/log4cplus)
(2). Add log4plus library
find_package(log4cplus REQUIRED)
target_link_libraries(TestLog4cplus log4cplus::log4cplusU)

cmake_minimum_required(VERSION 3.14)

project(TestLog4cplus LANGUAGES CXX)

set(CMAKE_INCLUDE_CURRENT_DIR ON)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

if (${MSVC})
    message("MSVC")
    set(log4cplus_DIR ../thirdparties/msvc/lib/cmake/log4cplus)
endif()

if (${MINGW})
    message("MINGW")
    if (not ${CMAKE_CL_64})
    else()
        set(log4cplus_DIR ../thirdparties/mingw32/lib/cmake/log4cplus)
    endif()
endif()

find_package(log4cplus REQUIRED)

add_executable(TestLog4cplus
    main.cpp
)

target_link_libraries(TestLog4cplus log4cplus::log4cplusU)
#include <map>
#include <string>
#include <log4cplus/logger.h>
#include <log4cplus/configurator.h>
#include <log4cplus/loggingmacros.h>
#include <log4cplus/log4cplus.h>


int main(int argc, char *argv[])
{
    (void)(argc);
    (void)(argv);

    using namespace log4cplus;
    const int LOOP_COUNT = 20;

    Initializer initializer;

    Logger logger = Logger::getInstance(LOG4CPLUS_TEXT("test"));
    // logger.setLogLevel(INFO_LOG_LEVEL);
    logger.setLogLevel(ALL_LOG_LEVEL);

    tstring pattern = LOG4CPLUS_TEXT("%D{%Y-%m-%d %H:%M:%S.%q} %t %p %l %m%n");

    SharedAppenderPtr consoleAppender(new ConsoleAppender);
    consoleAppender->setName(LOG4CPLUS_TEXT("console"));
    // consoleAppender->setLayout(std::unique_ptr<Layout>(new SimpleLayout));
    consoleAppender->setLayout(std::unique_ptr<Layout>(new PatternLayout(pattern)));
    logger.addAppender(consoleAppender);

    // Create objects with a maximum of 200K and cache up to 5 log files (total is 6 files)
    SharedFileAppenderPtr fileAppender(new RollingFileAppender(LOG4CPLUS_TEXT("logs/Test.log"), 200 * 1024, 5, false, true));
    fileAppender->setName(LOG4CPLUS_TEXT("FirstFile"));
    // fileAppender->setLayout( std::unique_ptr<Layout>(new TTCCLayout()));
    fileAppender->setLayout(std::unique_ptr<Layout>(new PatternLayout(pattern)));
    // fileAppender->getloc();
    logger.addAppender(SharedAppenderPtr(fileAppender.get()));

    for (int i = 0; i < LOOP_COUNT; ++i) {
        LOG4CPLUS_DEBUG(logger, "Entering loop #" << i);
    }

    LOG4CPLUS_TRACE(logger, ("Hello trace"));
    LOG4CPLUS_DEBUG(logger, ("Hello debug"));
    LOG4CPLUS_INFO(logger, ("Hello info"));
    LOG4CPLUS_WARN(logger, ("Hello warn"));
    LOG4CPLUS_ERROR(logger, ("Hello error"));
    LOG4CPLUS_FATAL(logger, ("Hello fatal"));
    LOG4CPLUS_ASSERT(logger, ("Hello assert"));

    std::string str("Hello std::string info");
    LOG4CPLUS_INFO(logger, str.data());

    return 0;
}