Prechádzať zdrojové kódy

V0.2.6
1、添加了fmt的轻量级log库
2、修改了QtFtp的下载到文件的存储方式,将获取到的数据以100MB为单位逐步写入

Apple 6 mesiacov pred
rodič
commit
ed9cce0196
2 zmenil súbory, kde vykonal 103 pridanie a 3 odobranie
  1. 79 0
      common/FmtLog/fmtlog.h
  2. 24 3
      common/ftp/QtFtp.cpp

+ 79 - 0
common/FmtLog/fmtlog.h

@@ -0,0 +1,79 @@
+#ifndef FMTLOG_H
+#define FMTLOG_H
+
+#include <chrono>
+
+#include "fmt/core.h"
+#include "fmt/chrono.h"
+#include "fmt/color.h"
+#include <regex>
+
+
+/**
+ * 说明:使用方式和使用fmt的print方式一样
+ *      输出格式如下
+ *      [2024-09-01 23:10:50][widget.cpp:52] 你好
+ */
+
+
+/* 取出文件名的正则表达式 */
+const std::regex _reg_file(R"(.*/([\S]*[\.][\S]*)$)");
+
+/* Debug输出 */
+#define FMTLOG_DEBUG(...) \
+    do{ \
+        auto _now = std::chrono::system_clock::now(); \
+        std::time_t _now_c = std::chrono::system_clock::to_time_t(_now); \
+        std::string _log_time = fmt::format("{:%Y-%m-%d %H:%M:%S}", fmt::localtime(_now_c)); \
+        std::string _log_file_src = __FILE__; \
+        auto _log_file = std::regex_replace(_log_file_src, _reg_file, "$1"); \
+        std::string _log_str = fmt::format(__VA_ARGS__); \
+        fmt::print(fg(fmt::color::blue), "[{}][{}:{}] {}\n",_log_time,_log_file ,__LINE__ ,_log_str); \
+    }while(0)
+
+/* 正常输出 */
+#define FMTLOG_INFO(...) \
+    do{ \
+        auto _now = std::chrono::system_clock::now(); \
+        std::time_t _now_c = std::chrono::system_clock::to_time_t(_now); \
+        std::string _log_time = fmt::format("{:%Y-%m-%d %H:%M:%S}", fmt::localtime(_now_c)); \
+        std::string _log_file_src = __FILE__; \
+        auto _log_file = std::regex_replace(_log_file_src, _reg_file, "$1"); \
+        std::string _log_str = fmt::format(__VA_ARGS__); \
+        fmt::print(fg(fmt::color::green), "[{}][{}:{}] {}\n",_log_time,_log_file ,__LINE__ ,_log_str); \
+    }while(0)
+
+/* Warn输出 */
+#define FMTLOG_WARN(...) \
+    do{ \
+        auto _now = std::chrono::system_clock::now(); \
+        std::time_t _now_c = std::chrono::system_clock::to_time_t(_now); \
+        std::string _log_time = fmt::format("{:%Y-%m-%d %H:%M:%S}", fmt::localtime(_now_c)); \
+        std::string _log_file_src = __FILE__; \
+        auto _log_file = std::regex_replace(_log_file_src, _reg_file, "$1"); \
+        std::string _log_str = fmt::format(__VA_ARGS__); \
+        fmt::print(fg(fmt::color::yellow), "[{}][{}:{}] {}\n",_log_time,_log_file ,__LINE__ ,_log_str); \
+    }while(0)
+
+/* 错误输出 */
+#define FMTLOG_ERROR(...) \
+    do{ \
+        auto _now = std::chrono::system_clock::now(); \
+        std::time_t _now_c = std::chrono::system_clock::to_time_t(_now); \
+        std::string _log_time = fmt::format("{:%Y-%m-%d %H:%M:%S}", fmt::localtime(_now_c)); \
+        std::string _log_file_src = __FILE__; \
+        auto _log_file = std::regex_replace(_log_file_src, _reg_file, "$1"); \
+        std::string _log_str = fmt::format(__VA_ARGS__); \
+        fmt::print(fg(fmt::color::red), "[{}][{}:{}] {}\n",_log_time,_log_file ,__LINE__ ,_log_str); \
+    }while(0)
+
+// void hello()
+// {
+//     auto now = std::chrono::system_clock::now();
+//     std::time_t now_c = std::chrono::system_clock::to_time_t(now);
+//     std::string _log_time = fmt::format("{:%Y-%m-%d %H:%M:%S}", fmt::localtime(now_c));
+//     fmt::print(fg(fmt::color::green), "Hello, {}!\n", _log_time);
+//     std::regex_replace("hello", _reg_file, "$1");
+// }
+
+#endif /* FMTLOG_H */

+ 24 - 3
common/ftp/QtFtp.cpp

@@ -264,17 +264,38 @@ bool QtFtp::createDir(const QString& path)
 }
 
 
-/* 完成,保存文件 */
+/**
+ * @brief 完成,保存文件
+ *        这里采用逐步读取文件内容,因为QByteArray最多存储大约2GB的数据
+ * 
+ */
 void QtFtp::do_downloadFinished()
 {
     if(m_timer.isActive())
     {
         m_timer.stop();
     }
+    if(!m_file.isOpen())
+    {
+        m_file.open(QFile::WriteOnly | QFile::Truncate);
+    }
     if(m_reply->error() == QNetworkReply::NoError)
     {
-        m_file.write(m_reply->readAll());
-        m_file.flush();  /* 刷新文件,写入硬盘 */
+        while(!m_reply->atEnd())
+        {
+            /* 每次读取100MB */
+            QByteArray data = m_reply->read(104857600);
+            quint64 writenBytes = m_file.write(data);
+            LOG_DEBUG("write file : " + QString::number(writenBytes));
+            if(writenBytes != data.size())
+            {
+                LOG_WARN("FTP download failed: write file failed");
+                emit signal_downloadState(false);
+                m_result = false;
+                break;
+            }
+        }
+        m_file.flush();
         emit signal_downloadState(true);
         m_result = true;
         LOG_DEBUG("----- FTP download success -----");