Skip to content

Commit af96a4b

Browse files
committed
Merge branch 'dev_token_chengxin' into 'feat_token'
【FIX】解决easylog实例引起的编译问题 See merge request server/openapi/openapi-cpp-sdk!56
2 parents 69bbe77 + cc8d63e commit af96a4b

File tree

9 files changed

+302
-223
lines changed

9 files changed

+302
-223
lines changed

demo/openapi_cpp_test/openapi_cpp_test.cpp

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
#include "tigerapi/utils.h"
1111
#include "cpprest/details/basic_types.h"
1212
#include "tigerapi/price_util.h"
13-
#include "tigerapi/easylogging++.h"
1413
#include <chrono>
1514
#include <thread>
15+
#include "tigerapi/logger.h"
1616

1717
using namespace std;
1818
using namespace web;
@@ -595,25 +595,13 @@ class TestPushClient {
595595
};
596596

597597
int main(int argc, char* argv[]) {
598-
599-
// log settings
600-
el::Configurations defaultConf;
601-
defaultConf.setToDefault();
602-
603-
defaultConf.set(el::Level::Global, el::ConfigurationType::Enabled, "true");
604-
defaultConf.set(el::Level::Debug, el::ConfigurationType::Enabled, "true");
605-
defaultConf.set(el::Level::Info, el::ConfigurationType::Enabled, "true");
606-
defaultConf.set(el::Level::Warning, el::ConfigurationType::Enabled, "true");
607-
defaultConf.set(el::Level::Error, el::ConfigurationType::Enabled, "true");
608-
609-
el::Loggers::reconfigureLogger("default", defaultConf);
610-
598+
LoggerConfig::set_log_level(el::Level::Debug);
611599
//Set Tiger OpenAPI SDK configuration
612600
bool sand_box = false;
613-
// ClientConfig config = ClientConfig(false, U("../openapi_cpp_test/"));
614-
// config.set_server_url(U("http://127.0.0.1:8085/gateway"));
615-
// config.set_server_public_key(SANDBOX_TIGER_PUBLIC_KEY);
616-
ClientConfig config = ClientConfig(false);
601+
ClientConfig config = ClientConfig(false, U("D:/test package/"));
602+
//config.set_server_url(U("http://127.0.0.1:8085/gateway"));
603+
//config.set_server_public_key(SANDBOX_TIGER_PUBLIC_KEY);
604+
//ClientConfig config = ClientConfig(false);
617605

618606
// config.private_key = U("");
619607
// config.tiger_id = U("");

include/tigerapi/client_config.h

Lines changed: 33 additions & 200 deletions
Original file line numberDiff line numberDiff line change
@@ -8,55 +8,20 @@
88
#include <string>
99
#include <utility>
1010
#include "constants.h"
11-
#include "easylogging++.h"
11+
#include "logger.h"
1212
#include "utils.h"
1313
#include "win32.h"
14-
#include <fstream>
15-
#include "properties.h"
1614

1715
using namespace std;
1816

1917
namespace TIGER_API {
2018
class OPENAPI_EXPORT ClientConfig {
2119
public:
22-
explicit ClientConfig(bool sandbox_debug = false) :
23-
sandbox_debug(sandbox_debug) {
24-
if (sandbox_debug) {
25-
LOG(WARNING) << U("SANDBOX IS NOT SUPPORTED") << endl;
26-
// server_url = SANDBOX_TIGER_SERVER_URL;
27-
// server_public_key = SANDBOX_TIGER_PUBLIC_KEY;
28-
}
29-
};
30-
31-
// ClientConfig() : sandbox_debug(false) {};
32-
33-
ClientConfig(utility::string_t tiger_id, utility::string_t private_key, utility::string_t account) :
34-
tiger_id(std::move(tiger_id)),
35-
private_key(std::move(private_key)),
36-
account(std::move(account)) {};
37-
20+
ClientConfig(bool sandbox_debug = false);
21+
ClientConfig(utility::string_t tiger_id, utility::string_t private_key, utility::string_t account);
3822
ClientConfig(utility::string_t tiger_id, utility::string_t private_key, utility::string_t account,
39-
bool sandbox_debug = false, utility::string_t lang = U("en_US")) :
40-
tiger_id(std::move(tiger_id)),
41-
private_key(std::move(private_key)),
42-
account(std::move(account)),
43-
sandbox_debug(sandbox_debug),
44-
lang(lang) {
45-
if (sandbox_debug) {
46-
LOG(WARNING) << U("SANDBOX IS NOT SUPPORTED") << endl;
47-
// server_url = SANDBOX_TIGER_SERVER_URL;
48-
// server_public_key = SANDBOX_TIGER_PUBLIC_KEY;
49-
// socket_url = SANDBOX_TIGER_SOCKET_HOST;
50-
// socket_port = SANDBOX_TIGER_SOCKET_PORT;
51-
}
52-
};
53-
54-
explicit ClientConfig(bool sandbox_debug, const utility::string_t props_path) :
55-
sandbox_debug(sandbox_debug),
56-
props_path(props_path) {
57-
load_props();
58-
load_token();
59-
};
23+
bool sandbox_debug = false, utility::string_t lang = U("en_US"));
24+
ClientConfig(bool sandbox_debug, const utility::string_t props_path);
6025

6126
utility::string_t tiger_id;
6227
utility::string_t private_key;
@@ -74,60 +39,26 @@ namespace TIGER_API {
7439
utility::string_t token;
7540
utility::string_t props_path;
7641

77-
void check() {
78-
if (this->tiger_id.empty()) {
79-
LOG(ERROR) << U("Client Config error: tiger_id can't be empty") << endl;
80-
throw std::runtime_error("Client Config error: tiger_id can't be empty");
81-
}
82-
if (this->private_key.empty()) {
83-
LOG(ERROR) << U("Client Config error: private_key can't be empty") << endl;
84-
throw std::runtime_error("Client Config error: private_key can't be empty");
85-
}
86-
}
87-
88-
void check_account() {
89-
if (this->account.empty()) {
90-
LOG(ERROR) << U("Client Config error: account can't be empty") << endl;
91-
throw std::runtime_error("Client Config error: account can't be empty");
92-
}
93-
}
94-
95-
void set_server_url(const utility::string_t &url) {
96-
this->server_url = url;
97-
}
98-
99-
void set_socket_url(const utility::string_t &url) {
100-
this->socket_url = url;
101-
}
102-
103-
void set_socket_port(const utility::string_t &port) {
104-
this->socket_port = port;
105-
}
106-
107-
void set_server_public_key(const utility::string_t &key) {
108-
this->server_public_key = key;
109-
}
110-
111-
void set_token(const utility::string_t &token) {
112-
this->token = token;
113-
}
114-
115-
const utility::string_t &get_server_url() {
116-
return this->server_url;
117-
}
118-
119-
const utility::string_t &get_server_pub_key() {
120-
return this->server_public_key;
121-
}
122-
123-
const utility::string_t &get_socket_url() {
124-
return this->socket_url;
125-
}
126-
127-
const utility::string_t &get_socket_port() {
128-
return this->socket_port;
129-
}
42+
void check();
43+
void check_account();
44+
45+
void set_server_url(const utility::string_t& url);
46+
47+
void set_socket_url(const utility::string_t& url);
48+
49+
void set_socket_port(const utility::string_t& port);
50+
51+
void set_server_public_key(const utility::string_t& key);
52+
53+
void set_token(const utility::string_t& token);
54+
55+
const utility::string_t& get_server_url();
13056

57+
const utility::string_t& get_server_pub_key();
58+
59+
const utility::string_t& get_socket_url();
60+
61+
const utility::string_t& get_socket_port();
13162

13263
private:
13364
bool sandbox_debug = false;
@@ -136,113 +67,15 @@ namespace TIGER_API {
13667
utility::string_t socket_url = TIGER_SOCKET_HOST;
13768
utility::string_t socket_port = TIGER_SOCKET_PORT;
13869

139-
void load_props() {
140-
utility::string_t full_path = get_props_path(DEFAULT_PROPS_FILE);
141-
if (full_path.empty()) {
142-
return;
143-
}
144-
LOG(INFO) << U("Loading properties file from: ") << full_path << endl;
145-
146-
try {
147-
std::ifstream file(Utils::str16to8(full_path));
148-
if (!file.is_open()) {
149-
LOG(ERROR) << U("Failed to open properties file: ") << full_path << endl;
150-
return;
151-
}
152-
153-
Properties props;
154-
props.load(file);
155-
156-
if (tiger_id.empty()) {
157-
tiger_id = props.get_property(U("tiger_id"));
158-
}
159-
if (private_key.empty()) {
160-
private_key = props.get_property(U("private_key_pk1"));
161-
}
162-
if (account.empty()) {
163-
account = props.get_property(U("account"));
164-
}
165-
if (license.empty()) {
166-
license = props.get_property(U("license"));
167-
}
168-
169-
utility::string_t env = props.get_property(U("env"));
170-
std::transform(env.begin(), env.end(), env.begin(), ::toupper);
171-
if (env == U("SANDBOX")) {
172-
sandbox_debug = true;
173-
server_url = SANDBOX_TIGER_SERVER_URL;
174-
server_public_key = SANDBOX_TIGER_PUBLIC_KEY;
175-
socket_url = SANDBOX_TIGER_SOCKET_HOST;
176-
socket_port = SANDBOX_TIGER_SOCKET_PORT;
177-
}
178-
179-
} catch (const std::exception &e) {
180-
LOG(ERROR) << U("Failed to load properties file: ") << Utils::str8to16(e.what()) << endl;
181-
}
182-
LOG(INFO) << U("Loaded properties file successfully, tiger_id: ") << tiger_id << " account: " << account
183-
<< endl;
184-
}
185-
186-
utility::string_t get_props_path(const utility::string_t &filename) const {
187-
if (!props_path.empty())
188-
{
189-
if (Utils::is_directory(props_path)) {
190-
return props_path + filename;
191-
}
192-
}
193-
return U("");
194-
}
195-
196-
utility::string_t get_token_path() const {
197-
return get_props_path(DEFAULT_TOKEN_FILE);
198-
}
199-
200-
void load_token() {
201-
utility::string_t full_path = get_token_path();
202-
if (full_path.empty()) {
203-
return;
204-
}
205-
try {
206-
std::ifstream file(Utils::str16to8(full_path));
207-
if (!file.is_open()) {
208-
LOG(ERROR) << U("Failed to open token file: ") << full_path << endl;
209-
return;
210-
}
211-
212-
Properties props;
213-
props.load(file);
214-
215-
// get token value
216-
token = props.get_property(U("token"));
217-
LOG(INFO) << U("Loaded token successfully, token: ") << token << endl;
218-
} catch (const std::exception &e) {
219-
LOG(ERROR) << U("Failed to load token file: ") << Utils::str8to16(e.what()) << endl;
220-
}
221-
}
222-
223-
void save_token(const utility::string_t &new_token) {
224-
utility::string_t full_path = get_token_path();
225-
if (full_path.empty()) {
226-
return;
227-
}
228-
try {
229-
Properties props;
230-
props.set_property(U("token"), new_token);
231-
232-
std::ofstream file(Utils::str16to8(full_path));
233-
if (!file.is_open()) {
234-
LOG(ERROR) << U("Failed to open token file for writing: ") << full_path << endl;
235-
return;
236-
}
237-
238-
props.store(file);
239-
token = new_token;
240-
LOG(INFO) << U("Saved token successfully, token: ") << token << endl;
241-
} catch (const std::exception &e) {
242-
LOG(ERROR) << U("Failed to save token file: ") << Utils::str8to16(e.what()) << endl;
243-
}
244-
}
70+
void load_props();
71+
72+
utility::string_t get_props_path(const utility::string_t& filename) const;
73+
74+
utility::string_t get_token_path() const;
75+
76+
void load_token();
77+
78+
void save_token(const utility::string_t& new_token);
24579
};
24680
}
247-
24881
#endif //TIGERAPI_CLIENT_CONFIG_H

include/tigerapi/logger.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#ifndef LOGGER_H
2+
#define LOGGER_H
3+
#include "easylogging++.h"
4+
#include "win32.h"
5+
6+
namespace TIGER_API
7+
{
8+
class OPENAPI_EXPORT LoggerConfig
9+
{
10+
public:
11+
LoggerConfig() {};
12+
~LoggerConfig() {};
13+
14+
static void set_log_level(el::Level level);
15+
};
16+
}
17+
#endif

include/tigerapi/properties.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <map>
66
#include <fstream>
77
#include <sstream>
8+
#include <ctime>
89
#include "utils.h"
910

1011
namespace TIGER_API {
@@ -43,8 +44,9 @@ namespace TIGER_API {
4344
value = unescape(value);
4445

4546
// store as utility::string_t
46-
properties[utility::conversions::to_string_t(key)] =
47-
utility::conversions::to_string_t(value);
47+
utility::string_t key_t = utility::conversions::to_string_t(key);
48+
utility::string_t value_t = utility::conversions::to_string_t(value);
49+
properties[key_t] = value_t;
4850
}
4951
}
5052
}

openapi-cpp-sdk.vcxproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,7 @@
533533
<ClInclude Include="include\tigerapi\contract_util.h" />
534534
<ClInclude Include="include\tigerapi\easylogging++.h" />
535535
<ClInclude Include="include\tigerapi\enums.h" />
536+
<ClInclude Include="include\tigerapi\logger.h" />
536537
<ClInclude Include="include\tigerapi\model.h" />
537538
<ClInclude Include="include\tigerapi\order_util.h" />
538539
<ClInclude Include="include\tigerapi\push_client.h" />
@@ -569,8 +570,10 @@
569570
<ClCompile Include="include\openapi_pb\pb_source\TickData.pb.cc" />
570571
<ClCompile Include="include\openapi_pb\pb_source\TradeTickData.pb.cc" />
571572
<ClCompile Include="include\openssl\applink.c" />
573+
<ClCompile Include="src\client_config.cpp" />
572574
<ClCompile Include="src\contract_util.cpp" />
573575
<ClCompile Include="src\easylogging++.cpp" />
576+
<ClCompile Include="src\logger.cpp" />
574577
<ClCompile Include="src\order_util.cpp" />
575578
<ClCompile Include="src\push_socket\push_client_impl.cpp" />
576579
<ClCompile Include="src\push_socket\push_frame_serialize.cpp" />

openapi-cpp-sdk.vcxproj.filters

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,9 @@
678678
<ClInclude Include="include\tigerapi\push_socket\push_client_impl.h">
679679
<Filter>include\tigerapi\push_socket</Filter>
680680
</ClInclude>
681+
<ClInclude Include="include\tigerapi\logger.h">
682+
<Filter>include\tigerapi</Filter>
683+
</ClInclude>
681684
</ItemGroup>
682685
<ItemGroup>
683686
<ClCompile Include="src\contract_util.cpp">
@@ -767,6 +770,12 @@
767770
<ClCompile Include="src\easylogging++.cpp">
768771
<Filter>src</Filter>
769772
</ClCompile>
773+
<ClCompile Include="src\logger.cpp">
774+
<Filter>src</Filter>
775+
</ClCompile>
776+
<ClCompile Include="src\client_config.cpp">
777+
<Filter>src</Filter>
778+
</ClCompile>
770779
</ItemGroup>
771780
<ItemGroup>
772781
<None Include="include\cpprest\details\http_constants.dat">

0 commit comments

Comments
 (0)