CMake 编译 Log4cplus
1.从官方下载源码:
git clone --recurse https://github.com/log4cplus/log4cplus.git
log4cplus 依赖 catch 和 threadpool, 如果其中一个失败, 可以删除源码根目录对应的文件, 单独下载即可
举个例子:
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.编译 msvc 版本库
(1).切换到源码根目录, 创建一个临时文件夹, 用于 cmake 缓存
mkdir .build_msvc
cd .build_msvc
(2).编译 release 版本, 执行如下命令, 其中 CMAKE_INSTALL_PREFIX 指定库及文件输出路径, 本例中将输出到与源码同级目录
# 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 .
如果编译成功, 该目录中有三个子目录, 如下所示:
-- thirdparties/log4cplus/msvc
-- bin
-- include
-- lib
(3).编译 debug 版本, 执行如下命令
# 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.编译 mingw32 版本库
(1).添加 mingw32/bin 目录到环境变量
PATH=D:/Qt/Qt5.15.2/Tools/mingw810_32/bin/;%PATH%
(2).切换到源码根目录, 创建一个临时文件夹, 用于 cmake 缓存
mkdir .mingw32
cd .mingw32
(3).编译 release 版本, 执行如下命令, 其中 CMAKE_INSTALL_PREFIX 指定库及文件输出路径
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).编译 debug 版本, 执行如下命令
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.Qt 中使用 log4cplus
(1).可以直接通过 Qt creator 添加一个环境变量, 其路径为编译时指定的输出路径, 也可以将编译时的整个文件夹拷贝到项目的第三方库路径下
qt creator add env:
LOG4CPLUS_DIR 值为 ../../thirdparties/log4cplus/msvc
或者在 CMakeLists.txt 中添加 cmake 文件路径:
set(log4cplus_DIR ../thirdparties/msvc/lib/cmake/log4cplus)
(2).添加 log4plus 库
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); // 创建最大为200K,最多缓存5个日志文件的对象(总共是6个文件) 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; }