Commit ddd2bb81 authored by captainwong's avatar captainwong

fix curl wrapper

parent c3491ed4
.vscode/
.vs/
*.log*
# Compiled Object files
*.slo
*.lo
......
......@@ -30,16 +30,19 @@ public:
#define JLIB_LOG2_ENABLED
#include <iostream>
#include "3rdparty/spdlog/spdlog.h"
#ifdef WIN32
#ifdef _WIN32
#define SPDLOG_WCHAR_FILENAMES
#ifndef _CRT_SECURE_NO_WARNINGS
#define _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 "win32.h"
#endif // WIN32
#include "utf8.h"
#endif // _WIN32
#include "utf8.h"
......@@ -47,34 +50,35 @@ namespace jlib {
static constexpr char g_logger_name[] = "jlogger";
inline void init_logger(const
#ifdef WIN32
inline void init_logger(
#ifdef _WIN32
std::wstring
#else
std::string
#endif
& file_name)
file_name)
{
#ifdef _WIN32
file_name += L".log";
#else
file_name += ".log";
#endif
try {
std::vector<spdlog::sink_ptr> sinks;
#ifdef WIN32
#ifdef _WIN32
sinks.push_back(std::make_shared<spdlog::sinks::msvc_sink_mt>());
#endif
sinks.push_back(std::make_shared<spdlog::sinks::stdout_sink_mt>());
sinks.push_back(std::make_shared<spdlog::sinks::daily_file_sink_mt>(
#ifdef WIN32
file_name + L".log"
#else
file_name + ".log"
#endif
, 23, 59));
file_name , 23, 59));
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");
spdlog::register_logger(combined_logger);
combined_logger->flush_on(spdlog::level::trace);
} catch (const spdlog::spdlog_ex& ex) {
#ifdef WIN32
#ifdef _WIN32
char msg[1024] = { 0 };
sprintf_s(msg, "Log initialization failed: %s\n", ex.what());
MessageBoxA(nullptr, msg, "Error", MB_ICONERROR);
......
......@@ -75,7 +75,7 @@ struct Curl
curl_easy_setopt(curl_, CURLOPT_POSTFIELDS, json.data());
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);
auto_free<curl_slist> auto_free_headers(headers, [](curl_slist* p) { curl_slist_free_all(p); });
......@@ -90,7 +90,7 @@ struct Curl
static void dump_slist(curl_slist* list) {
JLOG_INFO("dumping curl_slist:");
while (list) {
JLOG_INFO(" {:x}", list->data);
JLOG_INFO(" {}", list->data);
list = list->next;
}
}
......
#pragma once
#include <boost/noncopyable.hpp>
#include <windows.h>
#include <objbase.h>
#include <Shobjidl.h>
#include <string>
#include <vector>
#include "base/noncopyable.h"
#include "utf8.h"
#include "win32/MyWSAError.h"
#include "win32/path_op.h"
......@@ -130,7 +130,7 @@ inline BOOL Win32CenterWindow(HWND hwndWindow)
return FALSE;
}
class auto_timer : public boost::noncopyable
class auto_timer : public noncopyable
{
private:
int m_timer_id;
......
#define _CRT_SECURE_NO_WARNINGS
#include "../../jlib/log2.h"
#include "../../jlib/util/curl_wrapper.h"
#include <stdio.h>
......@@ -6,9 +8,40 @@ using namespace jlib;
int main()
{
jlib::init_logger(L"curl");
Curl curl;
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("error %d\n", curl.lastError());
printf("error msg %s\n", curl.lastErrorMsg().data());
......
......@@ -29,7 +29,7 @@
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<CharacterSet>MultiByte</CharacterSet>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<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