From b8cd983825d081f89af51d1b5615400c62004ffe Mon Sep 17 00:00:00 2001 From: hehaoyang <1109196436@qq.com> Date: Thu, 1 Feb 2024 01:03:55 +0800 Subject: [PATCH] =?UTF-8?q?1,=20=E5=88=A0=E9=99=A4=20HxSql=20HxLog=20?= =?UTF-8?q?=E6=89=A9=E5=B1=95=E5=BA=93=202.=20HxBroadcase=20=E6=89=A9?= =?UTF-8?q?=E5=B1=95=E5=BA=93=E5=A2=9E=E5=8A=A0=E5=8F=91=E9=80=81=E5=B9=BF?= =?UTF-8?q?=E6=92=ADJSON=E6=B6=88=E6=81=AF=E6=89=A9=E5=B1=95=E6=96=B9?= =?UTF-8?q?=E6=B3=95=203.=20HxTask=E6=89=A9=E5=B1=95=E5=BA=93=E4=BB=BB?= =?UTF-8?q?=E5=8A=A1ID=E4=BF=AE=E6=94=B9=E4=B8=BA=E5=AD=97=E7=AC=A6?= =?UTF-8?q?=E4=B8=B2=204.=20HxTrace=E6=89=A9=E5=B1=95=E5=BA=93=E4=BF=AE?= =?UTF-8?q?=E6=94=B9=E8=BE=93=E5=87=BA=E6=A0=BC=E5=BC=8F=205.=20=E5=8F=96?= =?UTF-8?q?=E6=B6=88=E5=86=8Dlinux=E4=B8=8B=E7=94=9F=E6=88=90=20debug?= =?UTF-8?q?=E5=92=8Crelease=E6=A8=A1=E5=BC=8F=E7=9A=84=E9=80=89=E6=8B=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- HxBroadcast.cpp | 17 ++++++++++++----- HxBroadcast.h | 8 ++++++++ HxLog.cpp | 26 -------------------------- HxLog.h | 15 --------------- HxSql.cpp | 38 -------------------------------------- HxSql.h | 24 ------------------------ HxTask.cpp | 4 ++-- HxTask.h | 20 ++++++++++---------- HxTrace.cpp | 10 +++------- HxUtils.pro | 14 -------------- 10 files changed, 35 insertions(+), 141 deletions(-) delete mode 100644 HxLog.cpp delete mode 100644 HxLog.h delete mode 100644 HxSql.cpp delete mode 100644 HxSql.h diff --git a/HxBroadcast.cpp b/HxBroadcast.cpp index 9d2bfb0..5c89bd5 100644 --- a/HxBroadcast.cpp +++ b/HxBroadcast.cpp @@ -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> args) +void HxBroadcast::publish_json(int action_type, QJsonObject msginfo) { - QJsonObject root, msginfo; - - for (std::initializer_list>::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> args) +{ + QJsonObject msginfo; + + for (std::initializer_list>::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() diff --git a/HxBroadcast.h b/HxBroadcast.h index 8b9ea55..73a25c5 100644 --- a/HxBroadcast.h +++ b/HxBroadcast.h @@ -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格式) * diff --git a/HxLog.cpp b/HxLog.cpp deleted file mode 100644 index f8b7cf8..0000000 --- a/HxLog.cpp +++ /dev/null @@ -1,26 +0,0 @@ -#include "HxLog.h" - -#include -#include - -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(); -} diff --git a/HxLog.h b/HxLog.h deleted file mode 100644 index c6d7f3f..0000000 --- a/HxLog.h +++ /dev/null @@ -1,15 +0,0 @@ -#ifndef HXLOG_H -#define HXLOG_H - -#include - -class HxLog -{ -public: - static void append(QString title, QString message); - -private: - static QMutex mutex; -}; - -#endif // HXLOG_H diff --git a/HxSql.cpp b/HxSql.cpp deleted file mode 100644 index 01ae018..0000000 --- a/HxSql.cpp +++ /dev/null @@ -1,38 +0,0 @@ -#include "HxSql.h" -#include "HxTrace.h" - -#include -#include -#include -#include - -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); -} diff --git a/HxSql.h b/HxSql.h deleted file mode 100644 index be24331..0000000 --- a/HxSql.h +++ /dev/null @@ -1,24 +0,0 @@ -#ifndef HXSQL_H -#define HXSQL_H - -#include - -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 diff --git a/HxTask.cpp b/HxTask.cpp index 9776178..ec41ab3 100644 --- a/HxTask.cpp +++ b/HxTask.cpp @@ -1,7 +1,7 @@ -#include "HxTask.h" +#include "HxTask.h" #include #include QMutex HxTask::mutex; -QMap HxTask::dispatchers; +QMap HxTask::dispatchers; diff --git a/HxTask.h b/HxTask.h index 5fe0e7f..a65b4c0 100644 --- a/HxTask.h +++ b/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 - 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 - 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 dispatchers; + static QMap dispatchers; }; #endif // HXTASK_H diff --git a/HxTrace.cpp b/HxTrace.cpp index 20b6eda..8b3b480 100644 --- a/HxTrace.cpp +++ b/HxTrace.cpp @@ -1,20 +1,17 @@ -#include "HxTrace.h" +#include "HxTrace.h" #include 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 } diff --git a/HxUtils.pro b/HxUtils.pro index 8afd286..71f41d1 100644 --- a/HxUtils.pro +++ b/HxUtils.pro @@ -7,16 +7,6 @@ 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 @@ -32,10 +22,8 @@ SOURCES += \ HxBroadcast.cpp \ HxDisk.cpp \ HxJson.cpp \ - HxLog.cpp \ HxProcess.cpp \ HxSocket.cpp \ - HxSql.cpp \ HxSystem.cpp \ HxTask.cpp \ HxThread.cpp \ @@ -45,10 +33,8 @@ HEADERS += \ HxBroadcast.h \ HxDisk.h \ HxJson.h \ - HxLog.h \ HxProcess.h \ HxSocket.h \ - HxSql.h \ HxSystem.h \ HxThread.h \ HxTask.h \