Qt线程实现的一个简单日志打印类

Qt线程实现的一个简单日志打印类

MyLog.h

#ifndef MYLOG_H
#define MYLOG_H
#include <qglobal.h>
#include <qstring.h>
#include <thread>
#include <QObject>
#include <QQueue>
#include <QMutex>
#include <QFile>
#include <QWaitCondition>

#ifdef WIN32
    #pragma execution_character_set("utf-8")
#endif

#define WAITFORLOCKMS 5 /*互斥量lock等待时间*/

//外界写日志的宏
#define writeLog(log) (MyLog::Get()->WriteLog(log, __FILE__, __LINE__, __FUNCTION__))

//为日志添加日期时间
#define formatStr(str, file, line, func) QString("%1 [%2 %3: %4]: %5\r\n").arg(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz")).arg(file).arg(line).arg(func).arg(str)

//获取当前日期
#define getCurDate() QString("%1").arg(QDateTime::currentDateTime().toString("yyyyMMdd"))

class QPoint;
class QPointF;
class QRect;
class QRectF;
class CvPoint;
class CvPoint2D32f;
class MyDebugDlg;

class MyLog {
   
public:
    MyLog();    //构造函数, 初始话成员变量
    ~MyLog();   //析构函数, 释放资源

    void WriteLog(const QString &t, const QString &sFile = "",  const int &iLine = -1, const QString &sFunc = "");       //成员函数, 重载函数, 将打印信息加入打印队列中
    void WriteLog(const bool &t, const QString &sFile = "", const int &iLine = -1, const QString &sFunc = "");           //成员函数, 重载函数, 将打印信息加入打印队列中
    void WriteLog(const char &t, const QString &sFile = "", const int &iLine = -1, const QString &sFunc = "");           //成员函数, 重载函数, 将打印信息加入打印队列中
    void WriteLog(const signed short &t, const QString &sFile = "", const int &iLine = -1, const QString &sFunc = "");   //成员函数, 重载函数, 将打印信息加入打印队列中
    void WriteLog(const unsigned short &t, const QString &sFile = "", const int &iLine = -1, const QString &sFunc = ""); //成员函数, 重载函数, 将打印信息加入打印队列中
    void WriteLog(const signed int &t, const QString &sFile = "", const int &iLine = -1, const QString &sFunc = "");     //成员函数, 重载函数, 将打印信息加入打印队列中
    void WriteLog(const unsigned int &t, const QString &sFile = "", const int &iLine = -1, const QString &sFunc = "");   //成员函数, 重载函数, 将打印信息加入打印队列中
    void WriteLog(const signed long &t, const QString &sFile = "", const int &iLine = -1, const QString &sFunc = "");    //成员函数, 重载函数, 将打印信息加入打印队列中
    void WriteLog(const unsigned long &t, const QString &sFile = "", const int &iLine = -1, const QString &sFunc = "");  //成员函数, 重载函数, 将打印信息加入打印队列中
    void WriteLog(const qint64 &t, const QString &sFile = "", const int &iLine = -1, const QString &sFunc = "");         //成员函数, 重载函数, 将打印信息加入打印队列中
    void WriteLog(const quint64 &t, const QString &sFile = "", const int &iLine = -1, const QString &sFunc = "");        //成员函数, 重载函数, 将打印信息加入打印队列中
    void WriteLog(const float &t, const QString &sFile = "", const int &iLine = -1, const QString &sFunc = "");          //成员函数, 重载函数, 将打印信息加入打印队列中
    void WriteLog(const double &t, const QString &sFile = "", const int &iLine = -1, const QString &sFunc = "");         //成员函数, 重载函数, 将打印信息加入打印队列中
    void WriteLog(const char* t, const QString &sFile = "", const int &iLine = -1, const QString &sFunc = "");           //成员函数, 重载函数, 将打印信息加入打印队列中
    void WriteLog(const QByteArray &t, const QString &sFile = "", const int &iLine = -1, const QString &sFunc = "");     //成员函数, 重载函数, 将打印信息加入打印队列中
    void WriteLog(const QPoint &t, const QString &sFile = "", const int &iLine = -1, const QString &sFunc = "");         //成员函数, 重载函数, 将打印信息加入打印队列中
    void Write
下面是一个简单Qt 实现的多线程 HTTP 请求代理的示例代码: ```cpp #include <QtNetwork> #include <QThread> class HttpProxy : public QObject { Q_OBJECT public: HttpProxy(QObject* parent = nullptr) : QObject(parent) { // 创建一个 QNetworkAccessManager 对象作为 HTTP 请求发送器 m_manager = new QNetworkAccessManager(this); // 创建一个 QThread 对象作为 HTTP 请求处理器 m_thread = new QThread(this); m_thread->start(); // 将 HTTP 请求处理器移动到新线程中执行 moveToThread(m_thread); } ~HttpProxy() { // 在析构函数中释放 QThread 和 QNetworkAccessManager 对象 m_thread->quit(); m_thread->wait(); delete m_thread; delete m_manager; } // 发送 HTTP 请求的函数,支持 GET 和 POST 方法 QNetworkReply* sendRequest(const QUrl& url, const QByteArray& data = QByteArray(), const QString& method = "GET") { QNetworkRequest request(url); request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded"); QNetworkReply* reply = nullptr; if (method == "GET") { reply = m_manager->get(request); } else if (method == "POST") { reply = m_manager->post(request, data); } // 将 HTTP 请求的信号连接到 HTTP 请求处理器的槽函数中 QObject::connect(reply, &QNetworkReply::finished, this, &HttpProxy::handleRequestFinished); QObject::connect(reply, &QNetworkReply::sslErrors, this, &HttpProxy::handleSslErrors); // 将 HTTP 请求处理器的信号连接到代理的槽函数中 QObject::connect(this, &HttpProxy::abortRequest, reply, &QNetworkReply::abort); return reply; } signals: // 取消 HTTP 请求的信号 void abortRequest(); private slots: // 处理 HTTP 请求完成的槽函数 void handleRequestFinished() { QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender()); if (reply->error() == QNetworkReply::NoError) { QByteArray responseData = reply->readAll(); QString responseString(responseData); qDebug() << "Received response:" << responseString; } else { qDebug() << "Request failed:" << reply->errorString(); } reply->deleteLater(); } // 处理 SSL 错误的槽函数 void handleSslErrors(QList<QSslError> errors) { QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender()); reply->ignoreSslErrors(errors); } private: QNetworkAccessManager* m_manager; QThread* m_thread; }; ``` 这个示例中,HttpProxy 将 HTTP 请求发送器和 HTTP 请求处理器分别放在不同的线程中执行,以实现线程并发处理 HTTP 请求。HttpProxy 提供了一个 sendRequest 函数,支持 GET 和 POST 方法,并返回一个 QNetworkReply 对象,可以通过该对象获取 HTTP 响应结果。HttpProxy 还提供了一个 abortRequest 信号,可以用于取消正在进行的 HTTP 请求。 使用 HttpProxy ,可以这样发送 HTTP 请求: ```cpp HttpProxy proxy; QNetworkReply* reply = proxy.sendRequest(QUrl("https://www.example.com")); ``` 当 HTTP 请求完成时,会自动调用 handleRequestFinished 槽函数。如果 HTTP 请求失败,则会打印失败信息;否则,会打印响应结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值