如何在基于 Qt qmake 的项目中使用 CuteLogger?

How to use CuteLogger in Qt qmake based project?

我能够使用 cmake 构建 Qt CuteLogger 项目。以下是代码,几乎是 https://github.com/dept2/CuteLogger.

中解释的内容

我想我也可以在非 cmake 项目中使用 cutelogger,比如 qmake 甚至 visual studio。我可以请求有关如何操作的说明吗?

我对这两个 cmake 都是新手。

main.cpp

#include <QCoreApplication>
#include <Logger.h>
#include <ConsoleAppender.h>

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

  ConsoleAppender* consoleAppender = new ConsoleAppender;
  consoleAppender->setFormat("[%{type:-7}] <%{Function}> %{message}\n");
  cuteLogger->registerAppender(consoleAppender);

  LOG_INFO("Starting the application");
  int result = app.exec();

  if (result)
    LOG_WARNING() << "Something went wrong." << "Result code is" << result;
  return result;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.14)

project(untitled3 LANGUAGES CXX)

set(CMAKE_INCLUDE_CURRENT_DIR ON)

set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(Qt5Core)

add_executable(untitled3
  main.cpp
)

target_link_libraries(untitled3 Qt${QT_VERSION_MAJOR}::Core)

ADD_SUBDIRECTORY(Logger)

TARGET_LINK_LIBRARIES(untitled3 CuteLogger)

一个简单的解决方案是创建一个包含 .cpp 和 .h 的 .pri:

├── 3rdParty
│   ├── CuteLogger
│   │   ├── CMakeLists.txt
│   │   ├── CuteLogger.pro
│   │   ├── CuteLogger.qbs
│   │   ├── Doxyfile
│   │   ├── include
│   │   │   ├── AbstractAppender.h
│   │   │   ├── AbstractStringAppender.h
│   │   │   ├── AndroidAppender.h
│   │   │   ├── ConsoleAppender.h
│   │   │   ├── CuteLogger_global.h
│   │   │   ├── FileAppender.h
│   │   │   ├── Logger.h
│   │   │   ├── OutputDebugAppender.h
│   │   │   └── RollingFileAppender.h
│   │   ├── LICENSE.LGPL
│   │   ├── README.md
│   │   ├── src
│   │   │   ├── AbstractAppender.cpp
│   │   │   ├── AbstractStringAppender.cpp
│   │   │   ├── AndroidAppender.cpp
│   │   │   ├── ConsoleAppender.cpp
│   │   │   ├── FileAppender.cpp
│   │   │   ├── Logger.cpp
│   │   │   ├── OutputDebugAppender.cpp
│   │   │   └── RollingFileAppender.cpp
│   │   └── test
│   │       └── basictest.cpp
│   └── CuteLogger.pri
├── 65231036.pro
└── main.cpp

CuteLogger.pri

INCLUDEPATH += $$PWD/CuteLogger/include

SOURCES += $$PWD/CuteLogger/src/Logger.cpp \
           $$PWD/CuteLogger/src/AbstractAppender.cpp \
           $$PWD/CuteLogger/src/AbstractStringAppender.cpp \
           $$PWD/CuteLogger/src/ConsoleAppender.cpp \
           $$PWD/CuteLogger/src/FileAppender.cpp \
           $$PWD/CuteLogger/src/RollingFileAppender.cpp

HEADERS += $$PWD/CuteLogger/include/Logger.h \
           $$PWD/CuteLogger/include/CuteLogger_global.h \
           $$PWD/CuteLogger/include/AbstractAppender.h \
           $$PWD/CuteLogger/include/AbstractStringAppender.h \
           $$PWD/CuteLogger/include/ConsoleAppender.h \
           $$PWD/CuteLogger/include/FileAppender.h \
           $$PWD/CuteLogger/include/RollingFileAppender.h

win32 {
    SOURCES += $$PWD/CuteLogger/src/OutputDebugAppender.cpp
    HEADERS += $$PWD/CuteLogger/include/OutputDebugAppender.h
}

android {
    SOURCES += $$PWD/CuteLogger/src/AndroidAppender.cpp
    HEADERS += $$PWD/CuteLogger/include/AndroidAppender.h
}

*.pro

QT -= gui

CONFIG += c++11 console
CONFIG -= app_bundle

SOURCES += main.cpp

include(3rdParty/CuteLogger.pri)