1, 删除 HxSql HxLog 扩展库

2. HxBroadcase 扩展库增加发送广播JSON消息扩展方法
3. HxTask扩展库任务ID修改为字符串
4. HxTrace扩展库修改输出格式
5. 取消再linux下生成 debug和release模式的选择
This commit is contained in:
hehaoyang 2024-02-01 01:03:55 +08:00
parent be7dd0da25
commit b8cd983825
10 changed files with 35 additions and 141 deletions

View File

@ -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()

View File

@ -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格式)
*

View File

@ -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
View File

@ -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

View File

@ -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
View File

@ -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

View File

@ -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;

View File

@ -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

View File

@ -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
}

View File

@ -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 \