瀏覽代碼

V0.6.8
1、新添加了stdlog.h文件,修改CurlFTP的日志输出方式为stdlog,没有spdlog也可以使用了

Apple 3 月之前
父節點
當前提交
325a662f1f
共有 2 個文件被更改,包括 136 次插入61 次删除
  1. 59 61
      common/CurlFtp/CurlFtp.cpp
  2. 77 0
      common/StdLog/stdlog.h

+ 59 - 61
common/CurlFtp/CurlFtp.cpp

@@ -5,12 +5,8 @@
 #include <filesystem>
 #include <fstream>
 
-// #include "fmtlog.h"
-#include "spdlog/spdlog.h"
+#include "stdlog.h"
 
-#ifndef SPDLOG_ACTIVE_LEVEL
-#define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_DEBUG
-#endif
 
 #if defined(_WIN32)
 
@@ -286,7 +282,7 @@ static int progress_callback(void *clientp,
                     curl_off_t ultotal,
                     curl_off_t ulnow)
 {
-    // SPDLOG_DEBUG("dTotal: {}, dNow: {}, uTotal: {}, uNow: {}", dltotal, dlnow, ultotal, ulnow);
+    // LOG_DEBUG("dTotal: {}, dNow: {}, uTotal: {}, uNow: {}", dltotal, dlnow, ultotal, ulnow);
     if(dltotal == 0 && ultotal == 0)
     {
         dCount = 0;
@@ -297,7 +293,7 @@ static int progress_callback(void *clientp,
     }
     std::chrono::system_clock::time_point nowTime = std::chrono::system_clock::now();
     auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(nowTime - lastTime).count();
-    // SPDLOG_DEBUG("duration:{}", duration);
+    // LOG_DEBUG("duration:{}", duration);
     if((duration < 1000) && ((dltotal != dlnow) || (ultotal != ulnow)))
     {
         return 0;
@@ -346,7 +342,7 @@ const std::regex parseRegSpace(R"(( )+)");
  * (文件类型) ... (文件大小,$5) ... (文件名)
  */
 const std::regex parseReg1(R"(^([^ ]+) (\d*) (\w*) (\w*) (\d*) (.*) ([^ ]+)$)");
-// SPDLOG_INFO("\n-------------------\n");
+// LOG_INFO("\n-------------------\n");
 /* 匹配换行符,分割每一行 */
 const std::regex parseReg2(R"(\n)");
 /* 匹配文件夹 */
@@ -370,14 +366,14 @@ static bool parseFileInfo(const std::string& strSrc, std::vector<CF_FileInfo>& f
     std::string str2 = strSrc;
 #endif /* _WIN32 */
     
-    // SPDLOG_DEBUG("\n{}", str2);
+    // LOG_DEBUG("\n{}", str2);
     // /* 正则表达式,匹配多个空格 */
     // std::regex regSpace(R"(( )+)");
     // /* 匹配以非空格开头的字符,空格隔开,中间任意字符,空格隔开,非空格组成的结尾
     //  * (文件类型) ... (文件大小,$5) ... (文件名)
     //  */
     // std::regex reg1(R"(^([^ ]+) (\d*) (\w*) (\w*) (\d*) (.*) ([^ ]+)$)");
-    // // SPDLOG_INFO("\n-------------------\n");
+    // // LOG_INFO("\n-------------------\n");
     // /* 匹配换行符,分割每一行 */
     // std::regex reg2(R"(\n)");
     // /* 匹配文件夹 */
@@ -393,7 +389,7 @@ static bool parseFileInfo(const std::string& strSrc, std::vector<CF_FileInfo>& f
     {
         /* 去掉多余的空格 */
         auto line = std::regex_replace(it2->str(), parseRegSpace, " ");
-        // SPDLOG_INFO("{}", line);
+        // LOG_INFO("{}", line);
         CF_FileInfo fi;
         /* 取出文件类型 */
         std::string strFileType = std::regex_replace(line, parseReg1, "$1");
@@ -458,7 +454,7 @@ bool CurlFtp::setFtpIPAndPort(const std::string& IP, const int port)
     m_IP = IP;
     m_port = port;
     m_ftpUrl = "ftp://" + m_IP + ":" + std::to_string(m_port);
-    SPDLOG_INFO("Set ftpUrl = {}", m_ftpUrl);
+    LOG_INFO("Set ftpUrl = " << m_ftpUrl);
     m_isSftp = false;
     return true;
 }
@@ -469,7 +465,7 @@ bool CurlFtp::setSftpIPAndPort(const std::string& IP, const int port)
     m_IP = IP;
     m_port = port;
     m_ftpUrl = "sftp://" + m_IP + ":" + std::to_string(m_port);
-    SPDLOG_INFO("Set sftpUrl = {}", m_ftpUrl);
+    LOG_INFO("Set sftpUrl = " << m_ftpUrl);
     m_isSftp = true;
     return true;
 }
@@ -498,7 +494,7 @@ bool CurlFtp::getFileList(std::string dir, std::vector<std::string>& fileList)
 {
     if(m_IP.empty())
     {
-        SPDLOG_WARN("IP or port is empty");
+        LOG_WARN("IP or port is empty");
         return false;
     }
     /* 检查dir,添加前缀“/” */
@@ -508,7 +504,7 @@ bool CurlFtp::getFileList(std::string dir, std::vector<std::string>& fileList)
     // curl = curl_easy_init();
     // if(curl == nullptr)
     // {
-    //     SPDLOG_ERROR("curl init failed !");
+    //     LOG_ERROR("curl init failed !");
     //     return false;
     // }
     resetCurl(m_curl);
@@ -518,7 +514,7 @@ bool CurlFtp::getFileList(std::string dir, std::vector<std::string>& fileList)
     listAll(m_curl, dirTmp, listInfo);
     for(const CF_FileInfo& fi : listInfo)
     {
-        // SPDLOG_INFO("type = {}, size = {}, name = {}", (int)fi.type, fi.size, fi.name);
+        // LOG_INFO("type = {}, size = {}, name = {}", (int)fi.type, fi.size, fi.name);
         if(fi.type == CF_FileType::FILE)
         {
             fileList.push_back(fi.name);
@@ -535,7 +531,7 @@ bool CurlFtp::getDirList(std::string dir, std::vector<std::string>& dirList)
 {
     if(m_IP.empty())
     {
-        SPDLOG_WARN("IP or port is empty");
+        LOG_WARN("IP or port is empty");
         return false;
     }
     /* 检查dir,添加前缀“/” */
@@ -545,7 +541,7 @@ bool CurlFtp::getDirList(std::string dir, std::vector<std::string>& dirList)
     // curl = curl_easy_init();
     // if(curl == nullptr)
     // {
-    //     SPDLOG_ERROR("curl init failed !");
+    //     LOG_ERROR("curl init failed !");
     //     return false;
     // }
     resetCurl(m_curl);
@@ -555,7 +551,7 @@ bool CurlFtp::getDirList(std::string dir, std::vector<std::string>& dirList)
     listAll(m_curl, dirTmp, listInfo);
     for(const CF_FileInfo& fi : listInfo)
     {
-        // SPDLOG_INFO("type = {}, size = {}, name = {}", (int)fi.type, fi.size, fi.name);
+        // LOG_INFO("type = {}, size = {}, name = {}", (int)fi.type, fi.size, fi.name);
         if(fi.type == CF_FileType::DIR)
         {
             dirList.push_back(fi.name);
@@ -578,10 +574,10 @@ bool CurlFtp::getDirList(std::string dir, std::vector<std::string>& dirList)
  */
 bool CurlFtp::isDirExist(const std::string& dir)
 {
-    SPDLOG_DEBUG("Check remote dir: {}", dir);
+    LOG_DEBUG("Check remote dir:" << dir);
     if(m_ftpUrl.empty())
     {
-        SPDLOG_ERROR("ftpUrl is empty");
+        LOG_ERROR("ftpUrl is empty");
         return false;
     }
     if(dir == "/")
@@ -611,7 +607,7 @@ bool CurlFtp::createDirectory(const std::string& ftpDir)
 {
     if(m_ftpUrl.empty())
     {
-        SPDLOG_ERROR("ftpUrl is empty");
+        LOG_ERROR("ftpUrl is empty");
         return false;
     }
     /* 先检查FTP文件夹是否存在,如果存在直接返回true */
@@ -626,7 +622,7 @@ bool CurlFtp::createDirectory(const std::string& ftpDir)
     // CURL *curl = curl_easy_init();
     // if(curl == nullptr)
     // {
-    //     SPDLOG_ERROR("Create FTP DIR, curl init failed !");
+    //     LOG_ERROR("Create FTP DIR, curl init failed !");
     //     return false;
     // }
     resetCurl(m_curl);
@@ -665,12 +661,12 @@ bool CurlFtp::createDirectory(const std::string& ftpDir)
     // 启用持久连接
     // curl_easy_setopt(m_curl, CURLOPT_TCP_KEEPALIVE, 1L);
 
-    SPDLOG_DEBUG("Create remote dir: {}", dirTmp);
+    LOG_DEBUG("Create remote dir: " << dirTmp);
     // CURLcode res = curl_easy_perform(curl);
     bool ret = performCurl(m_curl);
     if(!ret)
     {
-        SPDLOG_ERROR("Failed to create remote Dir");
+        LOG_ERROR("Failed to create remote Dir");
     }
 
     // curl_easy_cleanup(curl);
@@ -708,10 +704,10 @@ bool CurlFtp::createDirectories(const std::string& ftpDir)
     /* 逐层创建 */
     for(const std::string& dir : dirList)
     {
-        // SPDLOG_DEBUG("Create dir: {}", dir);
+        // LOG_DEBUG("Create dir: {}", dir);
         if(!createDirectory(dir))
         {
-            SPDLOG_ERROR("Failed to create dir: {}", dir);
+            LOG_ERROR("Failed to create dir: " << dir);
             return false;
         }
     }
@@ -735,7 +731,7 @@ bool CurlFtp::downloadFile(const std::string& remoteFile, const std::string& loc
 {
     if(m_ftpUrl.empty())
     {
-        SPDLOG_ERROR("ftpUrl is empty");
+        LOG_ERROR("ftpUrl is empty");
         return false;
     }
     
@@ -747,7 +743,7 @@ bool CurlFtp::downloadFile(const std::string& remoteFile, const std::string& loc
     std::string localDirTmp = localFile.substr(0, localFile.find_last_of("/"));
     if(!checkLocalDir(localDirTmp))
     {
-        SPDLOG_ERROR("Failed to create local dir: {}", localDirTmp);
+        LOG_ERROR("Failed to create local dir: " << localDirTmp);
         return false;
     }
 
@@ -755,7 +751,7 @@ bool CurlFtp::downloadFile(const std::string& remoteFile, const std::string& loc
     // curl = curl_easy_init();
     // if(curl == nullptr)
     // {
-    //     SPDLOG_ERROR("curl init failed !");
+    //     LOG_ERROR("curl init failed !");
     //     return false;
     // }
     resetCurl(m_curl);
@@ -797,7 +793,9 @@ bool CurlFtp::downloadFile(const std::string& remoteFile, const std::string& loc
     bool ret = performCurl(m_curl);
     if(!ret)
     {
-        SPDLOG_ERROR("Failed to get file list, Url = {}", ftpUrl);
+        std::stringstream ss;
+        ss << "Failed to download file: " << ftpUrl;
+        LOG_ERROR("Failed to get file list, Url = " << ftpUrl);
         /* 清理下载失败的文件 */
         ofs.close();
         std::remove(localFile.c_str());
@@ -819,7 +817,7 @@ bool CurlFtp::downloadToArray(const std::string& remoteFile, std::vector<char>&
 {
     if(m_ftpUrl.empty())
     {
-        SPDLOG_ERROR("ftpUrl is empty");
+        LOG_ERROR("ftpUrl is empty");
         return false;
     }
     
@@ -831,7 +829,7 @@ bool CurlFtp::downloadToArray(const std::string& remoteFile, std::vector<char>&
     // curl = curl_easy_init();
     // if(curl == nullptr)
     // {
-    //     SPDLOG_ERROR("curl init failed !");
+    //     LOG_ERROR("curl init failed !");
     //     return false;
     // }
     resetCurl(m_curl);
@@ -870,7 +868,7 @@ bool CurlFtp::downloadToArray(const std::string& remoteFile, std::vector<char>&
     bool ret = performCurl(m_curl);
     if(!ret)
     {
-        SPDLOG_ERROR("Failed to get file list, Url = {}", ftpUrl);
+        LOG_ERROR("Failed to get file list, Url = " << ftpUrl);
     }
     /* 清理curl */
     // curl_easy_cleanup(curl);
@@ -895,13 +893,13 @@ bool CurlFtp::uploadFile(const std::string& localFile, const std::string& remote
 {
     if(m_ftpUrl.empty())
     {
-        SPDLOG_ERROR("Url is empty");
+        LOG_ERROR("Url is empty");
         return false;
     }
     /* 检查本地文件是否存在 */
     if(!std::filesystem::exists(localFile))
     {
-        SPDLOG_ERROR("Local file is not exist: {}", localFile);
+        LOG_ERROR("Local file is not exist: " << localFile);
         return false;
     }
     /* 检查FTP文件名是否符合规范 */
@@ -914,11 +912,11 @@ bool CurlFtp::uploadFile(const std::string& localFile, const std::string& remote
         {
             if(!createDirectories(remoteDirTmp))
             {
-                // SPDLOG_ERROR("Failed to create remote dir: {}", remoteFileTmp);
+                // LOG_ERROR("Failed to create remote dir: {}", remoteFileTmp);
                 return false;
             }
         }else {
-            SPDLOG_ERROR("Remote dir is not exist: {}", remoteDirTmp);
+            LOG_ERROR("Remote dir is not exist: " << remoteDirTmp);
             return false;
         }
     }
@@ -931,7 +929,7 @@ bool CurlFtp::uploadFile(const std::string& localFile, const std::string& remote
     ifs.open(localFile, std::ios::in | std::ios::binary);
     if(!ifs.is_open())
     {
-        SPDLOG_ERROR("Failed to open local file: {}", localFile);
+        LOG_ERROR("Failed to open local file: " << localFile);
         return false;
     }
     /* 获取文件大小 */
@@ -940,13 +938,13 @@ bool CurlFtp::uploadFile(const std::string& localFile, const std::string& remote
     auto fileSize = ifs.tellg();
     /* 恢复指针到文件头 */
     ifs.seekg(0, std::ios::beg);
-    SPDLOG_DEBUG("File size: {}", (long)fileSize);
+    LOG_DEBUG("File size: " << (long)fileSize);
 
     // CURL* curl = nullptr;
     // curl = curl_easy_init();
     // if(curl == nullptr)
     // {
-    //     SPDLOG_ERROR("curl init failed !");
+    //     LOG_ERROR("curl init failed !");
     //     ifs.close();
     //     return false;
     // }
@@ -992,7 +990,7 @@ bool CurlFtp::uploadFile(const std::string& localFile, const std::string& remote
     bool ret = performCurl(m_curl);
     if(!ret)
     {
-        SPDLOG_ERROR("Upload file failed, Url = {}", ftpUrl);
+        LOG_ERROR("Upload file failed, Url = " << ftpUrl);
     }
     /* 关闭文件,清理curl */
     ifs.close();
@@ -1020,7 +1018,7 @@ bool CurlFtp::uploadData(char* srcData, size_t size, const std::string& remoteFi
 {
     if(m_ftpUrl.empty())
     {
-        SPDLOG_ERROR("Url is empty");
+        LOG_ERROR("Url is empty");
         return false;
     }
 
@@ -1039,23 +1037,23 @@ bool CurlFtp::uploadData(char* srcData, size_t size, const std::string& remoteFi
         {
             if(!createDirectories(remoteDirTmp))
             {
-                // SPDLOG_ERROR("Failed to create remote dir: {}", remoteFileTmp);
+                // LOG_ERROR("Failed to create remote dir: {}", remoteFileTmp);
                 return false;
             }
         }else {
-            SPDLOG_ERROR("Remote dir is not exist: {}", remoteDirTmp);
+            LOG_ERROR("Remote dir is not exist: " << remoteDirTmp);
             return false;
         }
     }
     /* 拼接远程文件的url */
     std::string ftpUrl = m_ftpUrl + remoteFileTmp;
-    SPDLOG_DEBUG("Data size: {}", arrayInfo.size);
+    LOG_DEBUG("Data size: " << arrayInfo.size);
 
     // CURL* curl = nullptr;
     // curl = curl_easy_init();
     // if(curl == nullptr)
     // {
-    //     SPDLOG_ERROR("curl init failed !");
+    //     LOG_ERROR("curl init failed !");
     //     return false;
     // }
     if(!resetCurl(m_curl))
@@ -1100,7 +1098,7 @@ bool CurlFtp::uploadData(char* srcData, size_t size, const std::string& remoteFi
     bool ret = performCurl(m_curl);
     if(!ret)
     {
-        SPDLOG_ERROR("Upload file failed, Url = {}", ftpUrl);
+        LOG_ERROR("Upload file failed, Url = " << ftpUrl);
     }
     /* 关闭文件,清理curl */
     // curl_easy_cleanup(curl);
@@ -1125,7 +1123,7 @@ bool CurlFtp::listAll(CURL* curl, std::string dir, std::vector<CF_FileInfo>& fil
 {
     if(m_IP.empty())
     {
-        SPDLOG_ERROR("IP is empty");
+        LOG_ERROR("IP is empty");
         return false;
     }
 
@@ -1162,7 +1160,7 @@ bool CurlFtp::listAll(CURL* curl, std::string dir, std::vector<CF_FileInfo>& fil
     bool ret = performCurl(curl);
     if(!ret)
     {
-        SPDLOG_ERROR("Failed to get file listUrl = {}", ftpUrl);
+        LOG_ERROR("Failed to get file listUrl = " << ftpUrl);
         return false;
     }
     /* 解析字符串 */
@@ -1202,7 +1200,7 @@ std::string CurlFtp::checkFilePath(const std::string& file)
     std::regex reg2(R"([.]*/$)");
     if(std::regex_match(dirTmp, reg2))
     {
-        SPDLOG_ERROR("File path is not correct, end with '/'");
+        LOG_ERROR("File path is not correct, end with '/'");
         return std::string();
     }
     
@@ -1241,7 +1239,7 @@ bool CurlFtp::checkLocalDir(const std::string& localDir)
     /* 创建文件夹 */
     if(!std::filesystem::create_directories(localDirTmp))
     {
-        SPDLOG_ERROR("Failed to create local dir: {}", localDirTmp);
+        LOG_ERROR("Failed to create local dir: " << localDirTmp);
         return false;
     }
 
@@ -1262,15 +1260,15 @@ bool CurlFtp::performCurl(CURL* curl)
         } 
         else if (res == CURLE_LOGIN_DENIED) 
         {
-            SPDLOG_ERROR("Login failed, error code: {} ,{}", (int)res, curl_easy_strerror(res));
+            LOG_ERROR("Login failed, error code: " << (int)res << ", " << curl_easy_strerror(res));
             /* 设置用户名和密码 */
             curl_easy_setopt(curl, CURLOPT_USERNAME, m_username.c_str());
             curl_easy_setopt(curl, CURLOPT_PASSWORD, m_password.c_str());
         } 
         else
         {
-            SPDLOG_ERROR("Perform curl failed, error code: {} ,{}", (int)res, curl_easy_strerror(res));
-            SPDLOG_ERROR("Retry times: {}", 4 - retry);
+            LOG_ERROR("Perform curl failed, error code: " << (int)res << ", " << curl_easy_strerror(res));
+            LOG_ERROR("Retry times: " << (4 - retry));
         }
         retry--;
     }
@@ -1286,7 +1284,7 @@ bool CurlFtp::resetCurl(CURL* curl)
         m_curl = curl_easy_init();
         if(m_curl == nullptr)
         {
-            SPDLOG_ERROR("curl init failed !");
+            LOG_ERROR("curl init failed !");
             return false;
         }
     }else {
@@ -1359,10 +1357,10 @@ bool CurlFtp::checkFtpDirExist(const std::string& dir)
         result = true;
     } else 
     {
-        SPDLOG_ERROR("Check remote dir error, error code: {} ,{}", (int)res, curl_easy_strerror(res));
+        LOG_ERROR("Check remote dir error, error code: " << (int)res << ", " << curl_easy_strerror(res));
         result = false;
     }
-    // SPDLOG_DEBUG("Check remote dir: {}, res: {}", dir, (int)res);
+    // LOG_DEBUG("Check remote dir: {}, res: {}", dir, (int)res);
     return result;
 }
 
@@ -1371,17 +1369,17 @@ bool CurlFtp::checkSftpDirExist(const std::string& dir)
 {
     /* 取出上一层文件夹路径 */
     std::string parentDir = std::filesystem::path(dir).parent_path().string();
-    // SPDLOG_DEBUG("Parent dir: {}", parentDir);
+    // LOG_DEBUG("Parent dir: {}", parentDir);
     std::vector<std::string> vecDir;
     bool ret = getDirList(parentDir, vecDir);
     if(!ret)
     {
-        SPDLOG_ERROR("Failed to check sftp dir: {}", dir);
+        LOG_ERROR("Failed to check sftp dir: " << dir);
         return false;
     }
     /* 取出本层文件夹名称 */
     std::string dirName = std::filesystem::path(dir).filename().string();
-    // SPDLOG_DEBUG("Dir name: {}", dirName);
+    // LOG_DEBUG("Dir name: {}", dirName);
     /* 判断是否存在 */
     bool result = false;
     for(const std::string& str : vecDir)

+ 77 - 0
common/StdLog/stdlog.h

@@ -0,0 +1,77 @@
+
+#include <string>
+#include <iostream>
+
+
+#ifdef SPDLOG_ACTIVE_LEVEL
+
+#include "spdlog/spdlog.h"
+
+#endif /* SPDLOG_ACTIVE_LEVEL */
+
+
+#ifdef SPDLOG_ACTIVE_LEVEL 
+    #define LOG_DEBUG(_msg) \
+        do { \
+            std::ostringstream _oss; \
+            _oss << _msg; \
+            SPDLOG_DEBUG(_oss.str()); \
+        } while(0)
+#else
+    #define LOG_DEBUG(_msg)
+        do { \
+            std::ostringstream _oss; \
+            _oss << _msg; \
+            std::cout << _oss.str() << std::endl; \
+        } while(0)
+
+#endif /* SPDLOG_ACTIVE_LEVEL */
+
+#ifdef SPDLOG_ACTIVE_LEVEL 
+    #define LOG_INFO(_msg) \
+        do { \
+            std::ostringstream _oss; \
+            _oss << _msg; \
+            SPDLOG_INFO(_oss.str()); \
+        } while(0)
+#else
+    #define LOG_INFO(_msg) \
+        do { \
+            std::ostringstream _oss; \
+            _oss << _msg; \
+            std::cout << _oss.str() << std::endl; \
+        } while(0)
+#endif /* SPDLOG_ACTIVE_LEVEL */
+
+
+#ifdef SPDLOG_ACTIVE_LEVEL 
+    #define LOG_WARN(_msg) \
+        do { \
+            std::ostringstream _oss; \
+            _oss << _msg; \
+            SPDLOG_WARN(_oss.str()); \
+        } while(0)
+#else
+    #define LOG_WARN(_msg)
+        do { \
+            std::ostringstream _oss; \
+            _oss << _msg; \
+            std::cout << _oss.str() << std::endl; \
+        } while(0)
+#endif /* SPDLOG_ACTIVE_LEVEL */
+
+#ifdef SPDLOG_ACTIVE_LEVEL 
+    #define LOG_ERROR(_msg) \
+        do { \
+            std::ostringstream _oss; \
+            _oss << _msg; \
+            SPDLOG_ERROR(_oss.str()); \
+        } while(0)
+#else
+    #define LOG_ERROR(_msg)
+        do { \
+            std::ostringstream _oss; \
+            _oss << _msg; \
+            std::cout << _oss.str() << std::endl; \
+        } while(0)
+#endif /* SPDLOG_ACTIVE_LEVEL */