From fcdd364921edc26607eb171b53847a115e5855f0 Mon Sep 17 00:00:00 2001 From: pandx Date: Thu, 24 Oct 2024 19:56:25 +0800 Subject: [PATCH] modify log info. --- datatransfer/data_trans.cpp | 149 ++++++++++++++++++++++-------------- 1 file changed, 91 insertions(+), 58 deletions(-) diff --git a/datatransfer/data_trans.cpp b/datatransfer/data_trans.cpp index f538409..c79b8a3 100644 --- a/datatransfer/data_trans.cpp +++ b/datatransfer/data_trans.cpp @@ -12,7 +12,7 @@ DataTrans::~DataTrans() {} static int OnDebug(CURL *, curl_infotype itype, char *pData, size_t size, void *) { if (itype == CURLINFO_TEXT) { - // printf("[TEXT]%s\n", pData); + zlog_debug(zbt, "[TEXT]%s", pData); } else if (itype == CURLINFO_HEADER_IN) { zlog_debug(zbt, "[HEADER_IN]%s", pData); } else if (itype == CURLINFO_HEADER_OUT) { @@ -21,6 +21,8 @@ static int OnDebug(CURL *, curl_infotype itype, char *pData, size_t size, void * zlog_debug(zbt, "[DATA_IN]%s", pData); } else if (itype == CURLINFO_DATA_OUT) { zlog_debug(zbt, "[DATA_OUT]%s", pData); + } else { + zlog_error(zbt, "invalid itype:%d", itype); } return 0; } @@ -29,7 +31,7 @@ static size_t OnWriteData(void *buffer, size_t size, size_t nmemb, void *lpVoid) std::string *str = dynamic_cast((std::string *)lpVoid); if (NULL == str || NULL == buffer) { zlog_error(zct, "[OnWriteData] str:%p, buffer:%p", str, buffer); - return -1; + return 0; } char *pData = (char *)buffer; str->append(pData, size * nmemb); @@ -41,7 +43,10 @@ static size_t my_fwrite(void *buffer, size_t size, size_t nmemb, void *stream) { struct DownloadFile *out = (struct DownloadFile *)stream; if (out && !out->stream) { out->stream = fopen(out->filename, "wb"); - if (!out->stream) return -1; + if (!out->stream) { + zlog_error(zct, "[my_fwrite] fail to open:%s", out->filename); + return 0; + } } return fwrite(buffer, size, nmemb, out->stream); } @@ -49,38 +54,37 @@ static size_t my_fwrite(void *buffer, size_t size, size_t nmemb, void *stream) { int DataTrans::download(char *pFilename, std::string &strUrl, std::string &strResponse, bool bDownload) { CURL *curl = NULL; CURLcode res; - struct DownloadFile dlfile = {pFilename, //定义下载到本地的文件位置和路径 - NULL}; + struct DownloadFile dlfile = {pFilename, NULL}; //定义下载到本地的文件位置和路径 curl_global_init(CURL_GLOBAL_DEFAULT); - curl = curl_easy_init(); //初始化一个curl指针 - if (curl) { // curl对象存在的情况下执行的操作 - //设置远端地址 - curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str()); - if (bDownload) { - //执行写入文件流操作 - curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_fwrite); //当有数据被写入,回调函数被调用, - curl_easy_setopt(curl, CURLOPT_WRITEDATA, &dlfile); //设置结构体的指针传递给回调函数 - } else { - curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL); - curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, OnWriteData); - curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&strResponse); - } - curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 8); //连接超时,这个数值如果设置太短可能导致数据请求不到就断开了 - curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10); //接收数据时超时设置,如果10秒内数据未接收完,直接退出 - //启用时会汇报所有的信息,存放在STDERR或指定的CURLOPT_STDERR中 - curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); - curl_easy_setopt(curl, CURLOPT_USERPWD, "SUREN:SUREN"); - - //写入文件 - res = curl_easy_perform(curl); - //释放curl对象 - curl_easy_cleanup(curl); - if (res != CURLE_OK) { - zlog_error(zct, "curl_easy_perform ret:%d", res); - return -1; - } + curl = curl_easy_init(); + if (curl == NULL) { + zlog_error(zct, "[download] curl_easy_init failed."); + return 1; + } + + curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str()); + if (bDownload) { + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_fwrite); //当有数据被写入,回调函数被调用, + curl_easy_setopt(curl, CURLOPT_WRITEDATA, &dlfile); //设置结构体的指针传递给回调函数 + } else { + curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL); + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, OnWriteData); + curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&strResponse); } - zlog_debug(zct, "strResponse = %s", strResponse.c_str()); + curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 8); //连接超时,这个数值如果设置太短可能导致数据请求不到就断开了 + curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10); //接收数据时超时设置,如果10秒内数据未接收完,直接退出 + curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); + curl_easy_setopt(curl, CURLOPT_USERPWD, "SUREN:SUREN"); + + res = curl_easy_perform(curl); + curl_easy_cleanup(curl); + if (res != CURLE_OK) { + curl_global_cleanup(); + zlog_error(zct, "[download] curl_easy_perform ret:%s", curl_easy_strerror(res)); + return -1; + } + + zlog_debug(zct, "[download] strResponse = %s", strResponse.c_str()); if (bDownload) { if (dlfile.stream) { fclose(dlfile.stream); @@ -97,55 +101,64 @@ size_t writedata2file(void *ptr, size_t size, size_t nmemb, FILE *stream) { } int DataTrans::dl_curl_post_req(const std::string &url, const std::string &postParams, std::string &filename) { - CURL *curl; - FILE *fp; + CURL *curl = nullptr; + FILE *fp = nullptr; CURLcode res; + res = curl_global_init(CURL_GLOBAL_ALL); if (CURLE_OK != res) { - zlog_error(zct, "init libcurl failed."); + zlog_error(zct, "[dl_curl_post_req] curl_global_init failed."); curl_global_cleanup(); return 1; } curl = curl_easy_init(); if (curl == NULL) { - zlog_error(zct, "curl_easy_init failed."); + zlog_error(zct, "[dl_curl_post_req] curl_easy_init failed."); curl_global_cleanup(); return 2; - } - - fp = fopen(filename.c_str(), "wb"); + } + res = curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); - if (res != CURLE_OK) { - fclose(fp); + if (res != CURLE_OK) { + zlog_error(zct, "[dl_curl_post_req] curl_easy_setopt CURLOPT_URL failed."); curl_easy_cleanup(curl); curl_global_cleanup(); return 3; } res = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writedata2file); - if (res != CURLE_OK) { - fclose(fp); + if (res != CURLE_OK) { + zlog_error(zct, "[dl_curl_post_req] curl_easy_setopt CURLOPT_WRITEFUNCTION failed."); curl_easy_cleanup(curl); curl_global_cleanup(); return 4; } + fp = fopen(filename.c_str(), "wb"); + if (fp == nullptr) { + zlog_error(zct, "[dl_curl_post_req] fopen:%s failed", filename.c_str()); + curl_easy_cleanup(curl); + curl_global_cleanup(); + return 5; + } + res = curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp); if (res != CURLE_OK) { + zlog_error(zct, "[dl_curl_post_req] curl_easy_setopt CURLOPT_WRITEDATA failed."); fclose(fp); curl_easy_cleanup(curl); curl_global_cleanup(); - return 5; + return 6; } res = curl_easy_perform(curl); fclose(fp); if (res != CURLE_OK) { - fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res)); + zlog_error(zct, "[dl_curl_post_req] curl_easy_perform() failed: %s", curl_easy_strerror(res)); curl_easy_cleanup(curl); curl_global_cleanup(); - return 6; + return 7; } curl_easy_cleanup(curl); @@ -157,7 +170,7 @@ int DataTrans::Post(const std::string &strUrl, const std::string &strPost, std:: CURLcode res; CURL *curl = curl_easy_init(); if (NULL == curl) { - zlog_error(zct, "curl_easy_init failed."); + zlog_error(zct, "[Post] curl_easy_init failed."); return CURLE_FAILED_INIT; } if (debug_) { @@ -175,6 +188,10 @@ int DataTrans::Post(const std::string &strUrl, const std::string &strPost, std:: curl_easy_setopt(curl, CURLOPT_TIMEOUT, 3); res = curl_easy_perform(curl); curl_easy_cleanup(curl); + if (res != CURLE_OK) { + zlog_error(zct, "[Post] curl_easy_perform() failed: %s", curl_easy_strerror(res)); + return 1; + } return res; } @@ -182,7 +199,7 @@ int DataTrans::Get(const std::string &strUrl, std::string &strResponse) { CURLcode res; CURL *curl = curl_easy_init(); if (NULL == curl) { - zlog_error(zct, "curl_easy_init failed."); + zlog_error(zct, "[Get] curl_easy_init failed."); return CURLE_FAILED_INIT; } if (debug_) { @@ -203,6 +220,10 @@ int DataTrans::Get(const std::string &strUrl, std::string &strResponse) { curl_easy_setopt(curl, CURLOPT_TIMEOUT, 3); res = curl_easy_perform(curl); curl_easy_cleanup(curl); + if (res != CURLE_OK) { + zlog_error(zct, "[Get] curl_easy_perform() failed: %s", curl_easy_strerror(res)); + return 1; + } return res; } @@ -210,7 +231,7 @@ int DataTrans::Posts(const std::string &strUrl, const std::string &strPost, std: CURLcode res; CURL *curl = curl_easy_init(); if (NULL == curl) { - zlog_error(zct, "curl_easy_init failed."); + zlog_error(zct, "[Posts] curl_easy_init failed."); return CURLE_FAILED_INIT; } @@ -229,7 +250,7 @@ int DataTrans::Posts(const std::string &strUrl, const std::string &strPost, std: curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false); curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false); } else { - //缺省情况就是PEM,所以无需设置,另外支持DER + // 缺省情况就是PEM,所以无需设置,另外支持DER // curl_easy_setopt(curl,CURLOPT_SSLCERTTYPE,"PEM"); curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, true); curl_easy_setopt(curl, CURLOPT_CAINFO, pCaPath); @@ -238,6 +259,10 @@ int DataTrans::Posts(const std::string &strUrl, const std::string &strPost, std: curl_easy_setopt(curl, CURLOPT_TIMEOUT, 3); res = curl_easy_perform(curl); curl_easy_cleanup(curl); + if (res != CURLE_OK) { + zlog_error(zct, "[Posts] curl_easy_perform() failed: %s", curl_easy_strerror(res)); + return 1; + } return res; } @@ -245,7 +270,7 @@ int DataTrans::Gets(const std::string &strUrl, std::string &strResponse, const c CURLcode res; CURL *curl = curl_easy_init(); if (NULL == curl) { - zlog_error(zct, "curl_easy_init failed."); + zlog_error(zct, "[Gets] curl_easy_init failed."); return CURLE_FAILED_INIT; } if (debug_) { @@ -268,10 +293,15 @@ int DataTrans::Gets(const std::string &strUrl, std::string &strResponse, const c curl_easy_setopt(curl, CURLOPT_TIMEOUT, 3); res = curl_easy_perform(curl); curl_easy_cleanup(curl); + if (res != CURLE_OK) { + zlog_error(zct, "[Gets] curl_easy_perform() failed: %s", curl_easy_strerror(res)); + return 1; + } return res; } int DataTrans::upload_file(const std::string &strUrl, const std::string &filename, std::string &response) { + CURLcode res; CURL *curl; CURLM *multi_handle; int still_running; @@ -290,12 +320,13 @@ int DataTrans::upload_file(const std::string &strUrl, const std::string &filenam curl = curl_easy_init(); if (NULL == curl) { - zlog_error(zct, "curl_easy_init failed."); + zlog_error(zct, "[upload_file] curl_easy_init failed."); return CURLE_FAILED_INIT; } + multi_handle = curl_multi_init(); if (NULL == multi_handle) { - zlog_error(zct, "curl_multi_init failed."); + zlog_error(zct, "[upload_file] curl_multi_init failed."); curl_easy_cleanup(curl); return CURLE_FAILED_INIT; } @@ -310,7 +341,12 @@ int DataTrans::upload_file(const std::string &strUrl, const std::string &filenam curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist); curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost); curl_multi_add_handle(multi_handle, curl); - curl_multi_perform(multi_handle, &still_running); + res = curl_multi_perform(multi_handle, &still_running); + if (res != CURLE_OK) { + zlog_error(zct, "[upload_file] curl_multi_perform() failed: %s", curl_easy_strerror(res)); + return res; + } + do { struct timeval timeout; int rc; @@ -355,11 +391,8 @@ int DataTrans::upload_file(const std::string &strUrl, const std::string &filenam } while (still_running); curl_multi_cleanup(multi_handle); - /* always cleanup */ curl_easy_cleanup(curl); - /* then cleanup the formpost chain */ curl_formfree(formpost); - /* free slist */ curl_slist_free_all(headerlist); return 0; }