Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in / Register
Toggle navigation
J
jlib
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
captainwong
jlib
Commits
ddd2bb81
Commit
ddd2bb81
authored
Sep 14, 2019
by
captainwong
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix curl wrapper
parent
c3491ed4
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
62 additions
and
23 deletions
+62
-23
.gitignore
.gitignore
+2
-0
log2.h
jlib/log2.h
+21
-17
curl_wrapper.h
jlib/util/curl_wrapper.h
+2
-2
win32.h
jlib/win32.h
+2
-2
test_curl_wrapper.cpp
test/test_curl_wrapper/test_curl_wrapper.cpp
+34
-1
test_curl_wrapper.vcxproj
test/test_curl_wrapper/test_curl_wrapper.vcxproj
+1
-1
No files found.
.gitignore
View file @
ddd2bb81
.vscode/
.vscode/
.vs/
.vs/
*.log*
# Compiled Object files
# Compiled Object files
*.slo
*.slo
*.lo
*.lo
...
...
jlib/log2.h
View file @
ddd2bb81
...
@@ -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
);
...
...
jlib/util/curl_wrapper.h
View file @
ddd2bb81
...
@@ -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
;
}
}
}
}
...
...
jlib/win32.h
View file @
ddd2bb81
#
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
;
...
...
test/test_curl_wrapper/test_curl_wrapper.cpp
View file @
ddd2bb81
#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
());
...
...
test/test_curl_wrapper/test_curl_wrapper.vcxproj
View file @
ddd2bb81
...
@@ -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>
MultiByt
e
</CharacterSet>
<CharacterSet>
Unicod
e
</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>
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment