Selaa lähdekoodia

V0.2.12
1、完善了CurlHttp类
2、添加了hiredis类

Apple 6 kuukautta sitten
vanhempi
commit
dbaafffed3
4 muutettua tiedostoa jossa 109 lisäystä ja 31 poistoa
  1. 7 1
      Libraries/Libraries.cmake
  2. 85 15
      common/CurlHttp/CurlHttp.cpp
  3. 4 2
      common/CurlHttp/CurlHttp.h
  4. 13 13
      common/LHLog/LHLogInit.cpp

+ 7 - 1
Libraries/Libraries.cmake

@@ -56,6 +56,7 @@ elseif(CMAKE_SYSTEM_NAME MATCHES "Linux")
     message(STATUS "Using ${CMAKE_SYSTEM_NAME} System")
     #添加Curl库
     set(CURL_DIR ${CMAKE_CURRENT_LIST_DIR}/linux_gcc8.3/lib/cmake/CURL)
+    set(hiredis_DIR ${CMAKE_CURRENT_LIST_DIR}/linux_gcc8.3/lib/cmake/hiredis)
     if(ENABLE_SPDLOG_STATIC_LIB)
         set(fmt_DIR ${CMAKE_CURRENT_LIST_DIR}/linux_gcc8.3_static/lib/cmake/fmt)
         set(spdlog_DIR ${CMAKE_CURRENT_LIST_DIR}/linux_gcc8.3_static/lib/cmake/spdlog)
@@ -68,7 +69,7 @@ endif()
 
 
 #=========================================================
-#添加MODULE模式搜索其他库的路径
+#添加MODULE模式搜索其他库的路径,不是标准的开源库
 list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR}/sm_dll)
 list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR}/qmqtt)
 list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR}/LHQLog)
@@ -80,6 +81,8 @@ find_package(fmt REQUIRED)
 find_package(spdlog REQUIRED)
 #链接curl库,使用的是CURL::libcurl
 find_package(CURL REQUIRED)
+#hiredis库
+find_package(hiredis REQUIRED)
 
 #在Windows中可能有冲突的地方,直接指明路径
 # find_package(Freetype REQUIRED PATHS ${freetype_DIR})
@@ -120,4 +123,7 @@ message(STATUS "--------------------------------------------------")
 message(STATUS "CURL Found : ${CURL_FOUND}")
 message(STATUS "CURL Version : ${CURL_VERSION}")
 message(STATUS "--------------------------------------------------")
+message(STATUS "hiredis Found : ${hiredis_FOUND}")
+message(STATUS "hiredis Version : ${hiredis_VERSION}")
+message(STATUS "--------------------------------------------------")
 

+ 85 - 15
common/CurlHttp/CurlHttp.cpp

@@ -69,17 +69,92 @@ CurlHttp::~CurlHttp()
 bool CurlHttp::Get(const std::string& url, std::string& response)
 {
     CURL *curl;
+    CURLcode res;
     curl = curl_easy_init();
     if(curl == nullptr)
     {
         FMTLOG_ERROR("curl_easy_init() failed");
         return false;
     }
-    /* 设置用户名密码,可能有的话 */
+    /* 设置为Get请求 */
+    curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "GET");
+    /* 设置url */
+    curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
+    /* 设置重定向,遇到3xx返回值时自动重定向 */
+    curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
+    /* 设置https协议 */
+    curl_easy_setopt(curl, CURLOPT_DEFAULT_PROTOCOL, "https");
+    /* 设置写入回调函数 */
+    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteStringCallback);
+    curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
+    /* 设置超时 */
+    curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 30L);
+    curl_easy_setopt(curl, CURLOPT_TIMEOUT, 30L);
+
+    /* 发送请求 */
+    bool result = true;
+    res = curl_easy_perform(curl);
+    if(res != CURLE_OK)
+    {
+        FMTLOG_ERROR("Get failed: {}, Url: {}", curl_easy_strerror(res), url);
+        result = false;
+    }
 
+    curl_easy_cleanup(curl);
     return true;
 }
 
+/* 获取信息,带有http头 */
+bool CurlHttp::Get(const std::string& url, const std::vector<std::string>& vecHeader, std::string& response)
+{
+    CURL *curl;
+    CURLcode res;
+    curl = curl_easy_init();
+    if(curl == nullptr)
+    {
+        FMTLOG_ERROR("curl_easy_init() failed");
+        return false;
+    }
+    /* 设置为Get请求 */
+    curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "GET");
+    /* 设置url */
+    curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
+    /* 设置http头 */
+    struct curl_slist *headers = NULL;
+    if(!vecHeader.empty())
+    {
+        for(auto &header : vecHeader)
+        {
+            headers = curl_slist_append(headers, header.c_str());
+        }
+        curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
+    }
+    /* 设置重定向,遇到3xx返回值时自动重定向 */
+    curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
+    /* 设置https协议 */
+    curl_easy_setopt(curl, CURLOPT_DEFAULT_PROTOCOL, "https");
+    /* 设置写入回调函数 */
+    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteStringCallback);
+    curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
+    /* 设置超时 */
+    curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 30L);
+    curl_easy_setopt(curl, CURLOPT_TIMEOUT, 30L);
+
+    /* 发送请求 */
+    bool result = true;
+    res = curl_easy_perform(curl);
+    if(res != CURLE_OK)
+    {
+        FMTLOG_ERROR("Get failed: {}, Url:{}", curl_easy_strerror(res), url);
+        result = false;
+    }
+
+    /* 清理内存 */
+    curl_slist_free_all(headers);
+    curl_easy_cleanup(curl);
+    return result;
+}
+
 /**
  * @brief 发送信息,不携带http头
  * 
@@ -103,22 +178,15 @@ bool CurlHttp::Post(const std::string& url, const std::string& postData, std::st
 
     /* 设置动作功能和网址 */
     curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST");
-    curl_easy_setopt(curl, CURLOPT_URL, "http://172.16.36.80:30000/vos/external/broadcastToken");
+    curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
     /* 设置重定向,遇到3xx返回值时自动重定向 */
     curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
     /* 设置https协议 */
     curl_easy_setopt(curl, CURLOPT_DEFAULT_PROTOCOL, "https");
 
-    /* 设置http头 */
-    struct curl_slist *headers = NULL;
-    headers = curl_slist_append(headers, "User-Agent: Apifox/1.0.0 (https://apifox.com)");
-    headers = curl_slist_append(headers, "Content-Type: application/json");
-    curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
-
     /* 设置包体 */
-    const char *data = R"({"appSecret":"8zR9ug4WbKCtEgaQ08myRm5HqKIMwP83Ra24t921tO/mU0cTIFdm/t0C9Jxrd53x","appKey":"rili4l26"})";
-    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);
-    curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, strlen(data));
+    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postData.c_str());
+    curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, postData.size());
 
     /* 设置回调函数 */
     curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteStringCallback);
@@ -137,7 +205,7 @@ bool CurlHttp::Post(const std::string& url, const std::string& postData, std::st
     bool result = true;
     if(res != CURLE_OK)
     {
-        FMTLOG_ERROR("curl_easy_perform() failed: {}", curl_easy_strerror(res));
+        FMTLOG_ERROR("Post failed: {}, Url: {}", curl_easy_strerror(res), url);
         result = false;
     }
 
@@ -158,7 +226,7 @@ bool CurlHttp::Post(const std::string& url, const std::string& postData, std::st
  * @return true 
  * @return false 
  */
-bool CurlHttp::PostWithBody(const std::string& url,const std::vector<std::string>& vecHeader, const std::string& postData, std::string& response)
+bool CurlHttp::Post(const std::string& url,const std::vector<std::string>& vecHeader, const std::string& postData, std::string& response)
 {
     CURL *curl;
     CURLcode res;
@@ -177,13 +245,14 @@ bool CurlHttp::PostWithBody(const std::string& url,const std::vector<std::string
     curl_easy_setopt(curl, CURLOPT_DEFAULT_PROTOCOL, "https");
 
     /* 设置http头 */
+    struct curl_slist *headers = NULL;
     if(!vecHeader.empty())
     {
-        struct curl_slist *headers = NULL;
         for(auto &header : vecHeader)
         {
             headers = curl_slist_append(headers, header.c_str());
         }
+        curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
     }
 
     /* 设置包体 */
@@ -207,10 +276,11 @@ bool CurlHttp::PostWithBody(const std::string& url,const std::vector<std::string
     bool result = true;
     if(res != CURLE_OK)
     {
-        FMTLOG_ERROR("curl_easy_perform() failed: {}", curl_easy_strerror(res));
+        FMTLOG_ERROR("Post failed: {}, Url: {}", curl_easy_strerror(res), url);
         result = false;
     }
 
+    curl_slist_free_all(headers);
     /* 清理curl */
     curl_easy_cleanup(curl);
 

+ 4 - 2
common/CurlHttp/CurlHttp.h

@@ -17,10 +17,12 @@ public:
     ~CurlHttp();
     /* 获取信息 */
     static bool Get(const std::string& url, std::string& response);
-    /* 发送信息 */
+    /* 获取信息,带有http头 */
+    static bool Get(const std::string& url, const std::vector<std::string>& vecHeader, std::string& response);
+    /* 发送信息,带有包体 */
     static bool Post(const std::string& url, const std::string& postData, std::string& response);
     /* 发送带有Http头和包体的信息 */
-    static bool PostWithBody(const std::string& url, const std::vector<std::string>& vecHeader, const std::string& postData, std::string& response);
+    static bool Post(const std::string& url, const std::vector<std::string>& vecHeader, const std::string& postData, std::string& response);
 
 };
 

+ 13 - 13
common/LHLog/LHLogInit.cpp

@@ -5,12 +5,16 @@
 #include "LHLog_file_sink.h"
 
 
-#include <QString>
-#include <QApplication>
+#include <string>
+#include <filesystem>
+
 
 /* 初始化spdlog,输入的是模组名称,可以为空
  * lhQLog是一个全局函数,在库LHQLog中定义
+ * 这里获取可执行文件文件夹的路径,使用的是C++17新引入的特性
+ * GCC9会在标准库中,GCC8.3需要手动链接库stdc++fs才可以使用
  */
+
 void initLog(QString ModuleName, CLHQLogApi& lhQLog)
 {
 
@@ -20,16 +24,17 @@ void initLog(QString ModuleName, CLHQLogApi& lhQLog)
         /* 创建一个文件sink,每天一个,00:00创建新的 */
         // auto sink_file = std::make_shared<spdlog::sinks::daily_file_sink_mt>("FlowChartLog/log.txt",0,0);
         /* 自定义的sink */
+        std::filesystem::path execPath = std::filesystem::current_path();
     #if defined(Q_OS_WIN32)
         #if C_DEBUG
-            QString libName = QApplication::applicationDirPath() + "/LHQLogd.dll";
+            std::string libName = execPath.string() + "/LHQLogd.dll";
         #elif C_RELEASE
-            QString libName = QApplication::applicationDirPath() + "/LHQLog.dll";
+            std::string libName = execPath.string() + "/LHQLog.dll";
         #endif
     #elif defined(Q_OS_LINUX)
-        QString libName = QApplication::applicationDirPath() + "/libLHQLog.so";
+        std::string libName = execPath.string() + "/libLHQLog.so";
     #endif
-        auto sink_custom = std::make_shared<spdlog::sinks::LHLog_file_sink_mt>(&lhQLog, libName, ModuleName);
+        auto sink_custom = std::make_shared<spdlog::sinks::LHLog_file_sink_mt>(&lhQLog, QString(libName.c_str()), ModuleName);
         /* 给默认记录器用的sink */
         auto sink_default = std::make_shared<spdlog::sinks::stdout_color_sink_mt>();
  
@@ -59,17 +64,12 @@ void initLog(QString ModuleName, CLHQLogApi& lhQLog)
         /* 创建一个WebAPI logger */
         auto logger_WebAPI = std::make_shared<spdlog::logger>("WebAPI",begin(sinks),end(sinks));
         /* 创建一个发射机通用 Logger */
-        auto logger_Transmitter = std::make_shared<spdlog::logger>("Transmitter",begin(sinks),end(sinks));
-
-
-//        /* 创建一个设备Info logger */
-//        auto logger_Info = std::make_shared<spdlog::logger>("DevInfo",begin(sinks),end(sinks));
-
+        auto logger_SuperBrain = std::make_shared<spdlog::logger>("SuperBrain",begin(sinks),end(sinks));
 
         /* 注册到注册表 */
         spdlog::register_logger(logger_main);
         spdlog::register_logger(logger_WebAPI);
-        spdlog::register_logger(logger_Transmitter);
+        spdlog::register_logger(logger_SuperBrain);
 
         /* 设置spdlog输出级别,默认的估计不输出debug这个级别
          * 这是默认的设置,可以在外面单数设置输出方式