add: spdlog and basic client code.
This commit is contained in:
17
Util/CMakeLists.txt
Normal file
17
Util/CMakeLists.txt
Normal file
@@ -0,0 +1,17 @@
|
||||
cmake_minimum_required(VERSION 3.16)
|
||||
|
||||
project(Util LANGUAGES CXX)
|
||||
|
||||
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(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Core)
|
||||
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Core)
|
||||
|
||||
add_library(Util STATIC Util.h Util.cpp)
|
||||
target_link_libraries(Util PRIVATE Qt${QT_VERSION_MAJOR}::Core)
|
||||
target_include_directories(Util PUBLIC ${CMAKE_CURRENT_LIST_DIR})
|
||||
60
Util/Util.cpp
Normal file
60
Util/Util.cpp
Normal file
@@ -0,0 +1,60 @@
|
||||
#include "Util.h"
|
||||
|
||||
#include <QDateTime>
|
||||
#include <QDebug>
|
||||
#include <QMutex>
|
||||
#include <iostream>
|
||||
#include <spdlog/fmt/bundled/color.h>
|
||||
#include <spdlog/fmt/fmt.h>
|
||||
#include <spdlog/sinks/rotating_file_sink.h>
|
||||
#include <spdlog/sinks/stdout_color_sinks.h>
|
||||
#include <spdlog/spdlog.h>
|
||||
|
||||
#ifdef Q_OS_WIN
|
||||
#include <windows.h>
|
||||
#endif
|
||||
|
||||
static QMutex msgMutex;
|
||||
static std::shared_ptr<spdlog::logger> logger;
|
||||
|
||||
Util::Util()
|
||||
{
|
||||
}
|
||||
|
||||
void Util::InitLogger(const QString& logPath, const QString& mark)
|
||||
{
|
||||
auto file_sink = std::make_shared<spdlog::sinks::rotating_file_sink_mt>(logPath.toStdString(), 1024 * 1024 * 50, 3);
|
||||
auto console_sink = std::make_shared<spdlog::sinks::stdout_color_sink_mt>();
|
||||
file_sink->set_pattern("[%Y-%m-%d %H:%M:%S.%e][%l]: %v");
|
||||
console_sink->set_pattern("%^[%Y-%m-%d %H:%M:%S.%e][%l]: %v%$");
|
||||
std::vector<spdlog::sink_ptr> sinks{file_sink, console_sink};
|
||||
logger = std::make_shared<spdlog::logger>(mark.toStdString(), sinks.begin(), sinks.end());
|
||||
logger->set_level(spdlog::level::debug);
|
||||
spdlog::register_logger(logger);
|
||||
}
|
||||
|
||||
void Util::ConsoleMsgHander(QtMsgType type, const QMessageLogContext& context, const QString& msg)
|
||||
{
|
||||
Q_UNUSED(context);
|
||||
|
||||
switch (type) {
|
||||
case QtDebugMsg:
|
||||
logger->debug(msg.toStdString());
|
||||
break;
|
||||
case QtInfoMsg:
|
||||
logger->info(msg.toStdString());
|
||||
break;
|
||||
case QtWarningMsg:
|
||||
logger->warn(msg.toStdString());
|
||||
break;
|
||||
case QtCriticalMsg:
|
||||
logger->error(msg.toStdString());
|
||||
break;
|
||||
case QtFatalMsg:
|
||||
logger->critical(msg.toStdString());
|
||||
break;
|
||||
default:
|
||||
logger->warn("Unknown QtMsgType type.");
|
||||
break;
|
||||
}
|
||||
}
|
||||
17
Util/Util.h
Normal file
17
Util/Util.h
Normal file
@@ -0,0 +1,17 @@
|
||||
#ifndef UTIL_H
|
||||
#define UTIL_H
|
||||
|
||||
#include <QObject>
|
||||
|
||||
class Util : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
Util();
|
||||
|
||||
public:
|
||||
static void InitLogger(const QString& logPath, const QString& mark);
|
||||
static void ConsoleMsgHander(QtMsgType type, const QMessageLogContext& context, const QString& msg);
|
||||
};
|
||||
|
||||
#endif // UTIL_H
|
||||
Reference in New Issue
Block a user