modify log info.

This commit is contained in:
pandx 2024-10-24 19:56:25 +08:00
parent 5dd9a7506d
commit fcdd364921

View File

@ -12,7 +12,7 @@ DataTrans::~DataTrans() {}
static int OnDebug(CURL *, curl_infotype itype, char *pData, size_t size, void *) { static int OnDebug(CURL *, curl_infotype itype, char *pData, size_t size, void *) {
if (itype == CURLINFO_TEXT) { if (itype == CURLINFO_TEXT) {
// printf("[TEXT]%s\n", pData); zlog_debug(zbt, "[TEXT]%s", pData);
} else if (itype == CURLINFO_HEADER_IN) { } else if (itype == CURLINFO_HEADER_IN) {
zlog_debug(zbt, "[HEADER_IN]%s", pData); zlog_debug(zbt, "[HEADER_IN]%s", pData);
} else if (itype == CURLINFO_HEADER_OUT) { } 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); zlog_debug(zbt, "[DATA_IN]%s", pData);
} else if (itype == CURLINFO_DATA_OUT) { } else if (itype == CURLINFO_DATA_OUT) {
zlog_debug(zbt, "[DATA_OUT]%s", pData); zlog_debug(zbt, "[DATA_OUT]%s", pData);
} else {
zlog_error(zbt, "invalid itype:%d", itype);
} }
return 0; 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 *>((std::string *)lpVoid); std::string *str = dynamic_cast<std::string *>((std::string *)lpVoid);
if (NULL == str || NULL == buffer) { if (NULL == str || NULL == buffer) {
zlog_error(zct, "[OnWriteData] str:%p, buffer:%p", str, buffer); zlog_error(zct, "[OnWriteData] str:%p, buffer:%p", str, buffer);
return -1; return 0;
} }
char *pData = (char *)buffer; char *pData = (char *)buffer;
str->append(pData, size * nmemb); 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; struct DownloadFile *out = (struct DownloadFile *)stream;
if (out && !out->stream) { if (out && !out->stream) {
out->stream = fopen(out->filename, "wb"); 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); return fwrite(buffer, size, nmemb, out->stream);
} }
@ -49,15 +54,16 @@ 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) { int DataTrans::download(char *pFilename, std::string &strUrl, std::string &strResponse, bool bDownload) {
CURL *curl = NULL; CURL *curl = NULL;
CURLcode res; CURLcode res;
struct DownloadFile dlfile = {pFilename, //定义下载到本地的文件位置和路径 struct DownloadFile dlfile = {pFilename, NULL}; //定义下载到本地的文件位置和路径
NULL};
curl_global_init(CURL_GLOBAL_DEFAULT); curl_global_init(CURL_GLOBAL_DEFAULT);
curl = curl_easy_init(); //初始化一个curl指针 curl = curl_easy_init();
if (curl) { // curl对象存在的情况下执行的操作 if (curl == NULL) {
//设置远端地址 zlog_error(zct, "[download] curl_easy_init failed.");
return 1;
}
curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str()); curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str());
if (bDownload) { if (bDownload) {
//执行写入文件流操作
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_fwrite); //当有数据被写入,回调函数被调用, curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_fwrite); //当有数据被写入,回调函数被调用,
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &dlfile); //设置结构体的指针传递给回调函数 curl_easy_setopt(curl, CURLOPT_WRITEDATA, &dlfile); //设置结构体的指针传递给回调函数
} else { } else {
@ -67,20 +73,18 @@ int DataTrans::download(char *pFilename, std::string &strUrl, std::string &strRe
} }
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 8); //连接超时,这个数值如果设置太短可能导致数据请求不到就断开了 curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 8); //连接超时,这个数值如果设置太短可能导致数据请求不到就断开了
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10); //接收数据时超时设置如果10秒内数据未接收完直接退出 curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10); //接收数据时超时设置如果10秒内数据未接收完直接退出
//启用时会汇报所有的信息存放在STDERR或指定的CURLOPT_STDERR中
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
curl_easy_setopt(curl, CURLOPT_USERPWD, "SUREN:SUREN"); curl_easy_setopt(curl, CURLOPT_USERPWD, "SUREN:SUREN");
//写入文件
res = curl_easy_perform(curl); res = curl_easy_perform(curl);
//释放curl对象
curl_easy_cleanup(curl); curl_easy_cleanup(curl);
if (res != CURLE_OK) { if (res != CURLE_OK) {
zlog_error(zct, "curl_easy_perform ret:%d", res); curl_global_cleanup();
zlog_error(zct, "[download] curl_easy_perform ret:%s", curl_easy_strerror(res));
return -1; return -1;
} }
}
zlog_debug(zct, "strResponse = %s", strResponse.c_str()); zlog_debug(zct, "[download] strResponse = %s", strResponse.c_str());
if (bDownload) { if (bDownload) {
if (dlfile.stream) { if (dlfile.stream) {
fclose(dlfile.stream); fclose(dlfile.stream);
@ -97,27 +101,27 @@ 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) { int DataTrans::dl_curl_post_req(const std::string &url, const std::string &postParams, std::string &filename) {
CURL *curl; CURL *curl = nullptr;
FILE *fp; FILE *fp = nullptr;
CURLcode res; CURLcode res;
res = curl_global_init(CURL_GLOBAL_ALL); res = curl_global_init(CURL_GLOBAL_ALL);
if (CURLE_OK != res) { if (CURLE_OK != res) {
zlog_error(zct, "init libcurl failed."); zlog_error(zct, "[dl_curl_post_req] curl_global_init failed.");
curl_global_cleanup(); curl_global_cleanup();
return 1; return 1;
} }
curl = curl_easy_init(); curl = curl_easy_init();
if (curl == NULL) { if (curl == NULL) {
zlog_error(zct, "curl_easy_init failed."); zlog_error(zct, "[dl_curl_post_req] curl_easy_init failed.");
curl_global_cleanup(); curl_global_cleanup();
return 2; return 2;
} }
fp = fopen(filename.c_str(), "wb");
res = curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); res = curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
if (res != CURLE_OK) { if (res != CURLE_OK) {
fclose(fp); zlog_error(zct, "[dl_curl_post_req] curl_easy_setopt CURLOPT_URL failed.");
curl_easy_cleanup(curl); curl_easy_cleanup(curl);
curl_global_cleanup(); curl_global_cleanup();
return 3; return 3;
@ -125,29 +129,38 @@ int DataTrans::dl_curl_post_req(const std::string &url, const std::string &postP
res = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writedata2file); res = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writedata2file);
if (res != CURLE_OK) { if (res != CURLE_OK) {
fclose(fp); zlog_error(zct, "[dl_curl_post_req] curl_easy_setopt CURLOPT_WRITEFUNCTION failed.");
curl_easy_cleanup(curl); curl_easy_cleanup(curl);
curl_global_cleanup(); curl_global_cleanup();
return 4; return 4;
} }
res = curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp); fp = fopen(filename.c_str(), "wb");
if (res != CURLE_OK) { if (fp == nullptr) {
fclose(fp); zlog_error(zct, "[dl_curl_post_req] fopen:%s failed", filename.c_str());
curl_easy_cleanup(curl); curl_easy_cleanup(curl);
curl_global_cleanup(); curl_global_cleanup();
return 5; return 5;
} }
res = curl_easy_perform(curl); res = curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
fclose(fp);
if (res != CURLE_OK) { 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_setopt CURLOPT_WRITEDATA failed.");
fclose(fp);
curl_easy_cleanup(curl); curl_easy_cleanup(curl);
curl_global_cleanup(); curl_global_cleanup();
return 6; return 6;
} }
res = curl_easy_perform(curl);
fclose(fp);
if (res != CURLE_OK) {
zlog_error(zct, "[dl_curl_post_req] curl_easy_perform() failed: %s", curl_easy_strerror(res));
curl_easy_cleanup(curl);
curl_global_cleanup();
return 7;
}
curl_easy_cleanup(curl); curl_easy_cleanup(curl);
curl_global_cleanup(); curl_global_cleanup();
return 0; return 0;
@ -157,7 +170,7 @@ int DataTrans::Post(const std::string &strUrl, const std::string &strPost, std::
CURLcode res; CURLcode res;
CURL *curl = curl_easy_init(); CURL *curl = curl_easy_init();
if (NULL == curl) { if (NULL == curl) {
zlog_error(zct, "curl_easy_init failed."); zlog_error(zct, "[Post] curl_easy_init failed.");
return CURLE_FAILED_INIT; return CURLE_FAILED_INIT;
} }
if (debug_) { 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); curl_easy_setopt(curl, CURLOPT_TIMEOUT, 3);
res = curl_easy_perform(curl); res = curl_easy_perform(curl);
curl_easy_cleanup(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; return res;
} }
@ -182,7 +199,7 @@ int DataTrans::Get(const std::string &strUrl, std::string &strResponse) {
CURLcode res; CURLcode res;
CURL *curl = curl_easy_init(); CURL *curl = curl_easy_init();
if (NULL == curl) { if (NULL == curl) {
zlog_error(zct, "curl_easy_init failed."); zlog_error(zct, "[Get] curl_easy_init failed.");
return CURLE_FAILED_INIT; return CURLE_FAILED_INIT;
} }
if (debug_) { if (debug_) {
@ -203,6 +220,10 @@ int DataTrans::Get(const std::string &strUrl, std::string &strResponse) {
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 3); curl_easy_setopt(curl, CURLOPT_TIMEOUT, 3);
res = curl_easy_perform(curl); res = curl_easy_perform(curl);
curl_easy_cleanup(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; return res;
} }
@ -210,7 +231,7 @@ int DataTrans::Posts(const std::string &strUrl, const std::string &strPost, std:
CURLcode res; CURLcode res;
CURL *curl = curl_easy_init(); CURL *curl = curl_easy_init();
if (NULL == curl) { if (NULL == curl) {
zlog_error(zct, "curl_easy_init failed."); zlog_error(zct, "[Posts] curl_easy_init failed.");
return CURLE_FAILED_INIT; 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_VERIFYPEER, false);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false); curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false);
} else { } else {
//缺省情况就是PEM所以无需设置另外支持DER // 缺省情况就是PEM所以无需设置另外支持DER
// curl_easy_setopt(curl,CURLOPT_SSLCERTTYPE,"PEM"); // curl_easy_setopt(curl,CURLOPT_SSLCERTTYPE,"PEM");
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, true); curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, true);
curl_easy_setopt(curl, CURLOPT_CAINFO, pCaPath); 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); curl_easy_setopt(curl, CURLOPT_TIMEOUT, 3);
res = curl_easy_perform(curl); res = curl_easy_perform(curl);
curl_easy_cleanup(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; return res;
} }
@ -245,7 +270,7 @@ int DataTrans::Gets(const std::string &strUrl, std::string &strResponse, const c
CURLcode res; CURLcode res;
CURL *curl = curl_easy_init(); CURL *curl = curl_easy_init();
if (NULL == curl) { if (NULL == curl) {
zlog_error(zct, "curl_easy_init failed."); zlog_error(zct, "[Gets] curl_easy_init failed.");
return CURLE_FAILED_INIT; return CURLE_FAILED_INIT;
} }
if (debug_) { 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); curl_easy_setopt(curl, CURLOPT_TIMEOUT, 3);
res = curl_easy_perform(curl); res = curl_easy_perform(curl);
curl_easy_cleanup(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; return res;
} }
int DataTrans::upload_file(const std::string &strUrl, const std::string &filename, std::string &response) { int DataTrans::upload_file(const std::string &strUrl, const std::string &filename, std::string &response) {
CURLcode res;
CURL *curl; CURL *curl;
CURLM *multi_handle; CURLM *multi_handle;
int still_running; int still_running;
@ -290,12 +320,13 @@ int DataTrans::upload_file(const std::string &strUrl, const std::string &filenam
curl = curl_easy_init(); curl = curl_easy_init();
if (NULL == curl) { if (NULL == curl) {
zlog_error(zct, "curl_easy_init failed."); zlog_error(zct, "[upload_file] curl_easy_init failed.");
return CURLE_FAILED_INIT; return CURLE_FAILED_INIT;
} }
multi_handle = curl_multi_init(); multi_handle = curl_multi_init();
if (NULL == multi_handle) { if (NULL == multi_handle) {
zlog_error(zct, "curl_multi_init failed."); zlog_error(zct, "[upload_file] curl_multi_init failed.");
curl_easy_cleanup(curl); curl_easy_cleanup(curl);
return CURLE_FAILED_INIT; 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_HTTPHEADER, headerlist);
curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost); curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
curl_multi_add_handle(multi_handle, curl); 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 { do {
struct timeval timeout; struct timeval timeout;
int rc; int rc;
@ -355,11 +391,8 @@ int DataTrans::upload_file(const std::string &strUrl, const std::string &filenam
} while (still_running); } while (still_running);
curl_multi_cleanup(multi_handle); curl_multi_cleanup(multi_handle);
/* always cleanup */
curl_easy_cleanup(curl); curl_easy_cleanup(curl);
/* then cleanup the formpost chain */
curl_formfree(formpost); curl_formfree(formpost);
/* free slist */
curl_slist_free_all(headerlist); curl_slist_free_all(headerlist);
return 0; return 0;
} }