Commit ddd2bb81 authored by captainwong's avatar captainwong

fix curl wrapper

parent c3491ed4
.vscode/ .vscode/
.vs/ .vs/
*.log*
# Compiled Object files # Compiled Object files
*.slo *.slo
*.lo *.lo
......
...@@ -30,16 +30,19 @@ public: ...@@ -30,16 +30,19 @@ public:
#define JLIB_LOG2_ENABLED #define JLIB_LOG2_ENABLED
#include <iostream> #ifdef _WIN32
#include "3rdparty/spdlog/spdlog.h" #define SPDLOG_WCHAR_FILENAMES
#ifdef WIN32
#ifndef _CRT_SECURE_NO_WARNINGS #ifndef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS #define _CRT_SECURE_NO_WARNINGS
#endif // _CRT_SECURE_NO_WARNINGS #endif // _CRT_SECURE_NO_WARNINGS
#endif // _WIN32
#include "3rdparty/spdlog/spdlog.h"
#ifdef _WIN32
#include "3rdparty/spdlog/sinks/msvc_sink.h" #include "3rdparty/spdlog/sinks/msvc_sink.h"
#include "win32.h" #include "utf8.h"
#endif // WIN32 #endif // _WIN32
#include "utf8.h" #include "utf8.h"
...@@ -47,34 +50,35 @@ namespace jlib { ...@@ -47,34 +50,35 @@ namespace jlib {
static constexpr char g_logger_name[] = "jlogger"; static constexpr char g_logger_name[] = "jlogger";
inline void init_logger(const inline void init_logger(
#ifdef WIN32 #ifdef _WIN32
std::wstring std::wstring
#else #else
std::string std::string
#endif #endif
& file_name) file_name)
{ {
#ifdef _WIN32
file_name += L".log";
#else
file_name += ".log";
#endif
try { try {
std::vector<spdlog::sink_ptr> sinks; std::vector<spdlog::sink_ptr> sinks;
#ifdef WIN32 #ifdef _WIN32
sinks.push_back(std::make_shared<spdlog::sinks::msvc_sink_mt>()); sinks.push_back(std::make_shared<spdlog::sinks::msvc_sink_mt>());
#endif #endif
sinks.push_back(std::make_shared<spdlog::sinks::stdout_sink_mt>()); sinks.push_back(std::make_shared<spdlog::sinks::stdout_sink_mt>());
sinks.push_back(std::make_shared<spdlog::sinks::daily_file_sink_mt>( sinks.push_back(std::make_shared<spdlog::sinks::daily_file_sink_mt>(
#ifdef WIN32 file_name , 23, 59));
file_name + L".log"
#else
file_name + ".log"
#endif
, 23, 59));
auto combined_logger = std::make_shared<spdlog::logger>(g_logger_name, begin(sinks), end(sinks)); auto combined_logger = std::make_shared<spdlog::logger>(g_logger_name, begin(sinks), end(sinks));
combined_logger->set_pattern("[%Y-%m-%d %H:%M:%S.%e] [tid %t] [%L] %v"); combined_logger->set_pattern("[%Y-%m-%d %H:%M:%S.%e] [tid %t] [%L] %v");
spdlog::register_logger(combined_logger); spdlog::register_logger(combined_logger);
combined_logger->flush_on(spdlog::level::trace); combined_logger->flush_on(spdlog::level::trace);
} catch (const spdlog::spdlog_ex& ex) { } catch (const spdlog::spdlog_ex& ex) {
#ifdef WIN32 #ifdef _WIN32
char msg[1024] = { 0 }; char msg[1024] = { 0 };
sprintf_s(msg, "Log initialization failed: %s\n", ex.what()); sprintf_s(msg, "Log initialization failed: %s\n", ex.what());
MessageBoxA(nullptr, msg, "Error", MB_ICONERROR); MessageBoxA(nullptr, msg, "Error", MB_ICONERROR);
......
...@@ -75,7 +75,7 @@ struct Curl ...@@ -75,7 +75,7 @@ struct Curl
curl_easy_setopt(curl_, CURLOPT_POSTFIELDS, json.data()); curl_easy_setopt(curl_, CURLOPT_POSTFIELDS, json.data());
curl_slist* headers = nullptr; curl_slist* headers = nullptr;
headers = curl_slist_append(headers, "application/json"); headers = curl_slist_append(headers, "Content-Type: application/json");
curl_easy_setopt(curl_, CURLOPT_HTTPHEADER, headers); curl_easy_setopt(curl_, CURLOPT_HTTPHEADER, headers);
auto_free<curl_slist> auto_free_headers(headers, [](curl_slist* p) { curl_slist_free_all(p); }); auto_free<curl_slist> auto_free_headers(headers, [](curl_slist* p) { curl_slist_free_all(p); });
...@@ -90,7 +90,7 @@ struct Curl ...@@ -90,7 +90,7 @@ struct Curl
static void dump_slist(curl_slist* list) { static void dump_slist(curl_slist* list) {
JLOG_INFO("dumping curl_slist:"); JLOG_INFO("dumping curl_slist:");
while (list) { while (list) {
JLOG_INFO(" {:x}", list->data); JLOG_INFO(" {}", list->data);
list = list->next; list = list->next;
} }
} }
......
#pragma once #pragma once
#include <boost/noncopyable.hpp>
#include <windows.h> #include <windows.h>
#include <objbase.h> #include <objbase.h>
#include <Shobjidl.h> #include <Shobjidl.h>
#include <string> #include <string>
#include <vector> #include <vector>
#include "base/noncopyable.h"
#include "utf8.h" #include "utf8.h"
#include "win32/MyWSAError.h" #include "win32/MyWSAError.h"
#include "win32/path_op.h" #include "win32/path_op.h"
...@@ -130,7 +130,7 @@ inline BOOL Win32CenterWindow(HWND hwndWindow) ...@@ -130,7 +130,7 @@ inline BOOL Win32CenterWindow(HWND hwndWindow)
return FALSE; return FALSE;
} }
class auto_timer : public boost::noncopyable class auto_timer : public noncopyable
{ {
private: private:
int m_timer_id; int m_timer_id;
......
#define _CRT_SECURE_NO_WARNINGS
#include "../../jlib/log2.h"
#include "../../jlib/util/curl_wrapper.h" #include "../../jlib/util/curl_wrapper.h"
#include <stdio.h> #include <stdio.h>
...@@ -6,9 +8,40 @@ using namespace jlib; ...@@ -6,9 +8,40 @@ using namespace jlib;
int main() int main()
{ {
jlib::init_logger(L"curl");
Curl curl; Curl curl;
curl.init(); curl.init();
curl.get("https://baidu.com");
/*curl.get("https://baidu.com");
printf("HTTP %ld\n", curl.lastHttpCode());
printf("error %d\n", curl.lastError());
printf("error msg %s\n", curl.lastErrorMsg().data());
printf("%s\n", curl.lastHttpContent().data());*/
const char* json = R"({
"items": [
{
"account": "861234567890",
"online": true,
"ops": [
{
"event": 3400,
"source": 0,
"timestamp": 1568401523
}
],
"passwd": "123456",
"status": 0,
"type": 7
}
],
"sign": "sss",
"ver": "1.0"
})";
auto url = "192.168.1.168:10086";
curl.postJson(url, json);
printf("HTTP %ld\n", curl.lastHttpCode()); printf("HTTP %ld\n", curl.lastHttpCode());
printf("error %d\n", curl.lastError()); printf("error %d\n", curl.lastError());
printf("error msg %s\n", curl.lastErrorMsg().data()); printf("error msg %s\n", curl.lastErrorMsg().data());
......
...@@ -29,7 +29,7 @@ ...@@ -29,7 +29,7 @@
<ConfigurationType>Application</ConfigurationType> <ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries> <UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset> <PlatformToolset>v142</PlatformToolset>
<CharacterSet>MultiByte</CharacterSet> <CharacterSet>Unicode</CharacterSet>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType> <ConfigurationType>Application</ConfigurationType>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment