1. 代码重构
2. 删除HxLog日志类 3. 删除HxSql数据库类 4. HxTask任务标识由UUID修改为字符串 5. HxTrace调整输出格式 6. HxBroadcase添加发布JSON格式消息扩展方法
This commit is contained in:
parent
be7dd0da25
commit
7acb69f47b
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -1,7 +1,7 @@
|
|||
debug/*
|
||||
release/*
|
||||
.qmake.stash
|
||||
HxUtils.pro.*
|
||||
utils.pro.*
|
||||
Makefile*
|
||||
*.Debug
|
||||
*.Release
|
||||
|
|
|
@ -20,12 +20,9 @@ void HxBroadcast::initialization(int port)
|
|||
|
||||
void HxBroadcast::publish(QString message) { emit broadcast->publish_event(message); }
|
||||
|
||||
void HxBroadcast::publish_json(int action_type, std::initializer_list<QPair<QString, QJsonValue>> args)
|
||||
void HxBroadcast::publish_json(int action_type, QJsonObject msginfo)
|
||||
{
|
||||
QJsonObject root, msginfo;
|
||||
|
||||
for (std::initializer_list<QPair<QString, QJsonValue>>::const_iterator i = args.begin(); i != args.end(); ++i)
|
||||
msginfo.insert(i->first, i->second);
|
||||
QJsonObject root;
|
||||
|
||||
root.insert("action_type", action_type);
|
||||
root.insert("msgInfo", msginfo);
|
||||
|
@ -33,6 +30,16 @@ void HxBroadcast::publish_json(int action_type, std::initializer_list<QPair<QStr
|
|||
publish(QJsonDocument(root).toJson(QJsonDocument::Compact));
|
||||
}
|
||||
|
||||
void HxBroadcast::publish_json(int action_type, std::initializer_list<QPair<QString, QJsonValue>> args)
|
||||
{
|
||||
QJsonObject msginfo;
|
||||
|
||||
for (std::initializer_list<QPair<QString, QJsonValue>>::const_iterator i = args.begin(); i != args.end(); ++i)
|
||||
msginfo.insert(i->first, i->second);
|
||||
|
||||
publish_json(action_type, msginfo);
|
||||
}
|
||||
|
||||
void HxBroadcast::publish_achieve(QString message) { socket->writeDatagram(message.toUtf8(), QHostAddress::Broadcast, broadcast->port); }
|
||||
|
||||
void HxBroadcast::receive_ready_read()
|
||||
|
|
|
@ -24,6 +24,14 @@ public:
|
|||
*/
|
||||
static void publish(QString message);
|
||||
|
||||
/**
|
||||
* @brief 发布消息 (JSON格式)
|
||||
*
|
||||
* @param action_type 操作类型
|
||||
* @param args msgInfo 参数
|
||||
*/
|
||||
static void publish_json(int action_type, QJsonObject msgInfo);
|
||||
|
||||
/**
|
||||
* @brief 发布消息 (JSON格式)
|
||||
*
|
||||
|
|
26
HxLog.cpp
26
HxLog.cpp
|
@ -1,26 +0,0 @@
|
|||
#include "HxLog.h"
|
||||
|
||||
#include <QFile>
|
||||
#include <QDateTime>
|
||||
|
||||
QMutex HxLog::mutex;
|
||||
|
||||
void HxLog::append(QString title, QString message)
|
||||
{
|
||||
mutex.lock();
|
||||
|
||||
auto current_time = QDateTime::currentDateTime();
|
||||
|
||||
QFile file(QString("log/%1.log").arg(current_time.toString("yyyyMMdd")));
|
||||
|
||||
if (file.open(QIODevice::WriteOnly | QIODevice::Append))
|
||||
{
|
||||
auto data = QString("[%1] | [%2] | %3\r\n").arg(current_time.toString("yyyy-MM-dd HH:mm:ss"), title, message);
|
||||
|
||||
file.write(data.toLocal8Bit());
|
||||
|
||||
file.close();
|
||||
}
|
||||
|
||||
mutex.unlock();
|
||||
}
|
15
HxLog.h
15
HxLog.h
|
@ -1,15 +0,0 @@
|
|||
#ifndef HXLOG_H
|
||||
#define HXLOG_H
|
||||
|
||||
#include <QMutex>
|
||||
|
||||
class HxLog
|
||||
{
|
||||
public:
|
||||
static void append(QString title, QString message);
|
||||
|
||||
private:
|
||||
static QMutex mutex;
|
||||
};
|
||||
|
||||
#endif // HXLOG_H
|
38
HxSql.cpp
38
HxSql.cpp
|
@ -1,38 +0,0 @@
|
|||
#include "HxSql.h"
|
||||
#include "HxTrace.h"
|
||||
|
||||
#include <QDateTime>
|
||||
#include <QSqlQuery>
|
||||
#include <QVariant>
|
||||
#include <QStorageInfo>
|
||||
|
||||
QSqlDatabase HxSql::open(QString filepath, QString connectionName)
|
||||
{
|
||||
QSqlDatabase database;
|
||||
if (QSqlDatabase::contains(connectionName))
|
||||
database = QSqlDatabase::database(connectionName);
|
||||
else
|
||||
database = QSqlDatabase::addDatabase("QSQLITE", connectionName);
|
||||
|
||||
database.setDatabaseName(filepath);
|
||||
|
||||
if (!database.open())
|
||||
{
|
||||
QString bk_filepath = filepath + QString(".[%1].bk").arg(QDateTime::currentDateTime().toString("yyyyMMddHHmmss"));
|
||||
|
||||
QFile::copy(filepath, bk_filepath);
|
||||
|
||||
QFile::remove(filepath);
|
||||
|
||||
HxTrace::debug_write_line("database", QString("file backup success. %1 => %2").arg(filepath).arg(bk_filepath));
|
||||
|
||||
database.open();
|
||||
}
|
||||
|
||||
return database;
|
||||
}
|
||||
|
||||
void HxSql::close(QString connectionName)
|
||||
{
|
||||
QSqlDatabase::removeDatabase(connectionName);
|
||||
}
|
24
HxSql.h
24
HxSql.h
|
@ -1,24 +0,0 @@
|
|||
#ifndef HXSQL_H
|
||||
#define HXSQL_H
|
||||
|
||||
#include <QSqlDatabase>
|
||||
|
||||
class HxSql
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* @brief 打开数据库
|
||||
* @param filepath 数据库文件路径
|
||||
* @param connectionName 连接名
|
||||
* @return QSqlDatabase 数据库对象
|
||||
*/
|
||||
static QSqlDatabase open(QString filepath, QString connectionName);
|
||||
|
||||
/**
|
||||
* @brief 关闭数据库
|
||||
* @param connectionName 连接名
|
||||
*/
|
||||
static void close(QString connectionName);
|
||||
};
|
||||
|
||||
#endif // HXSQL_H
|
|
@ -1,7 +1,7 @@
|
|||
#include "HxTask.h"
|
||||
#include "HxTask.h"
|
||||
|
||||
#include <QMap>
|
||||
#include <QMutex>
|
||||
|
||||
QMutex HxTask::mutex;
|
||||
QMap<QUuid, bool> HxTask::dispatchers;
|
||||
QMap<QString, bool> HxTask::dispatchers;
|
||||
|
|
20
HxTask.h
20
HxTask.h
|
@ -11,7 +11,7 @@ public:
|
|||
* @brief 停止
|
||||
* @param uuid 任务唯一编码
|
||||
*/
|
||||
static void stop(QUuid uuid)
|
||||
static void stop(QString uuid)
|
||||
{
|
||||
if (dispatchers.contains(uuid))
|
||||
{
|
||||
|
@ -63,14 +63,14 @@ public:
|
|||
* @param uuid 任务唯一编码
|
||||
*/
|
||||
template <typename Functor>
|
||||
static void run(Functor functor, int millisecond, QUuid uuid)
|
||||
static void run(Functor functor, int millisecond, QString uuid)
|
||||
{
|
||||
dispatchers.insert(uuid, true);
|
||||
|
||||
QtConcurrent::run(
|
||||
[=](Functor functor, int _millisecond, QUuid _uuid)
|
||||
[=](Functor functor, int _millisecond, QString _uuid)
|
||||
{
|
||||
HxTrace::debug_write_line("HxTask", QString("Thread: %1, start").arg(_uuid.toString()));
|
||||
HxTrace::debug_write_line("HxTask", QString("Thread: %1, start").arg(_uuid));
|
||||
|
||||
while (dispatchers[_uuid])
|
||||
{
|
||||
|
@ -79,7 +79,7 @@ public:
|
|||
QThread::msleep(_millisecond);
|
||||
}
|
||||
|
||||
HxTrace::debug_write_line("HxTask", QString("Thread: %1, stop").arg(_uuid.toString()));
|
||||
HxTrace::debug_write_line("HxTask", QString("Thread: %1, stop").arg(_uuid));
|
||||
|
||||
dispatchers.remove(_uuid);
|
||||
},
|
||||
|
@ -110,14 +110,14 @@ public:
|
|||
* @param uuid 任务唯一编码
|
||||
*/
|
||||
template <typename T, typename Class>
|
||||
static void run(Class *object, T (Class::*fn)(), int millisecond, QUuid uuid)
|
||||
static void run(Class *object, T (Class::*fn)(), int millisecond, QString uuid)
|
||||
{
|
||||
dispatchers.insert(uuid, true);
|
||||
|
||||
QtConcurrent::run(
|
||||
[=](Class *_object, T (Class::*_fn)(), int _millisecond, QUuid _uuid)
|
||||
[=](Class *_object, T (Class::*_fn)(), int _millisecond, QString _uuid)
|
||||
{
|
||||
HxTrace::debug_write_line("HxTask", QString("Thread: %1, start").arg(_uuid.toString()));
|
||||
HxTrace::debug_write_line("HxTask", QString("Thread: %1, start").arg(_uuid));
|
||||
|
||||
while (dispatchers[_uuid])
|
||||
{
|
||||
|
@ -126,7 +126,7 @@ public:
|
|||
QThread::msleep(_millisecond);
|
||||
}
|
||||
|
||||
HxTrace::debug_write_line("HxTask", QString("Thread: %1, stop").arg(_uuid.toString()));
|
||||
HxTrace::debug_write_line("HxTask", QString("Thread: %1, stop").arg(_uuid));
|
||||
|
||||
dispatchers.remove(_uuid);
|
||||
},
|
||||
|
@ -222,7 +222,7 @@ private:
|
|||
/**
|
||||
* @brief dispatchers
|
||||
*/
|
||||
static QMap<QUuid, bool> dispatchers;
|
||||
static QMap<QString, bool> dispatchers;
|
||||
};
|
||||
|
||||
#endif // HXTASK_H
|
||||
|
|
10
HxTrace.cpp
10
HxTrace.cpp
|
@ -1,20 +1,17 @@
|
|||
#include "HxTrace.h"
|
||||
#include "HxTrace.h"
|
||||
|
||||
#include <QDateTime>
|
||||
|
||||
void HxTrace::debug_write_line(QString title, QString message)
|
||||
{
|
||||
#ifdef QT_DEBUG
|
||||
qDebug("[%s] [%s] => %s",
|
||||
qDebug("%s | %s | %s",
|
||||
qPrintable(QDateTime::currentDateTime().toString("yyyy/MM/dd HH:mm:ss")),
|
||||
qPrintable(title),
|
||||
qPrintable(message));
|
||||
#endif
|
||||
}
|
||||
|
||||
void HxTrace::debug_write_line(QString title, const char *format, ...)
|
||||
{
|
||||
#ifdef QT_DEBUG
|
||||
char output[1024];
|
||||
|
||||
va_list arg_list;
|
||||
|
@ -27,9 +24,8 @@ void HxTrace::debug_write_line(QString title, const char *format, ...)
|
|||
|
||||
va_end(arg_list);
|
||||
|
||||
qDebug("[%s] [%s] => %s",
|
||||
qDebug("%s | %s | %s",
|
||||
qPrintable(QDateTime::currentDateTime().toString("yyyy/MM/dd HH:mm:ss")),
|
||||
qPrintable(title),
|
||||
output);
|
||||
#endif
|
||||
}
|
||||
|
|
10
utils.h
Normal file
10
utils.h
Normal file
|
@ -0,0 +1,10 @@
|
|||
#ifndef UTILS_H
|
||||
#define UTILS_H
|
||||
|
||||
class utils
|
||||
{
|
||||
public:
|
||||
utils();
|
||||
};
|
||||
|
||||
#endif // UTILS_H
|
|
@ -1,61 +1,41 @@
|
|||
QT -= gui
|
||||
QT += concurrent
|
||||
QT += sql
|
||||
QT += network
|
||||
|
||||
TEMPLATE = lib
|
||||
CONFIG += staticlib
|
||||
CONFIG += c++11
|
||||
|
||||
CONFIG += debug_and_release
|
||||
|
||||
unix {
|
||||
CONFIG(debug, debug|release){
|
||||
TARGET = debug/HxUtils
|
||||
} else {
|
||||
TARGET = release/HxUtils
|
||||
}
|
||||
}
|
||||
|
||||
# The following define makes your compiler emit warnings if you use
|
||||
# any Qt feature that has been marked deprecated (the exact warnings
|
||||
# depend on your compiler). Please consult the documentation of the
|
||||
# deprecated API in order to know how to port your code away from it.
|
||||
DEFINES += QT_DEPRECATED_WARNINGS
|
||||
|
||||
# You can also make your code fail to compile if it uses deprecated APIs.
|
||||
# In order to do so, uncomment the following line.
|
||||
# You can also select to disable deprecated APIs only up to a certain version of Qt.
|
||||
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
|
||||
|
||||
SOURCES += \
|
||||
HxBroadcast.cpp \
|
||||
HxDisk.cpp \
|
||||
HxJson.cpp \
|
||||
HxLog.cpp \
|
||||
HxProcess.cpp \
|
||||
HxSocket.cpp \
|
||||
HxSql.cpp \
|
||||
HxSystem.cpp \
|
||||
HxTask.cpp \
|
||||
HxThread.cpp \
|
||||
HxTrace.cpp
|
||||
|
||||
HEADERS += \
|
||||
HxBroadcast.h \
|
||||
HxDisk.h \
|
||||
HxJson.h \
|
||||
HxLog.h \
|
||||
HxProcess.h \
|
||||
HxSocket.h \
|
||||
HxSql.h \
|
||||
HxSystem.h \
|
||||
HxThread.h \
|
||||
HxTask.h \
|
||||
HxTrace.h
|
||||
|
||||
# Default rules for deployment.
|
||||
unix {
|
||||
target.path = $$[QT_INSTALL_PLUGINS]/generic
|
||||
}
|
||||
!isEmpty(target.path): INSTALLS += target
|
||||
QT -= gui
|
||||
QT += concurrent
|
||||
QT += sql
|
||||
QT += network
|
||||
|
||||
TEMPLATE = lib
|
||||
CONFIG += staticlib
|
||||
|
||||
CONFIG += c++17
|
||||
|
||||
# You can make your code fail to compile if it uses deprecated APIs.
|
||||
# In order to do so, uncomment the following line.
|
||||
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
|
||||
|
||||
SOURCES += \
|
||||
HxBroadcast.cpp \
|
||||
HxDisk.cpp \
|
||||
HxJson.cpp \
|
||||
HxProcess.cpp \
|
||||
HxSocket.cpp \
|
||||
HxSystem.cpp \
|
||||
HxTask.cpp \
|
||||
HxThread.cpp \
|
||||
HxTrace.cpp
|
||||
|
||||
HEADERS += \
|
||||
HxBroadcast.h \
|
||||
HxDisk.h \
|
||||
HxJson.h \
|
||||
HxProcess.h \
|
||||
HxSocket.h \
|
||||
HxSystem.h \
|
||||
HxTask.h \
|
||||
HxThread.h \
|
||||
HxTrace.h
|
||||
|
||||
# Default rules for deployment.
|
||||
unix {
|
||||
target.path = $$[QT_INSTALL_PLUGINS]/generic
|
||||
}
|
||||
!isEmpty(target.path): INSTALLS += target
|
Loading…
Reference in New Issue
Block a user