Skip to content

Commit cc8d63e

Browse files
author
chengxin
committed
【ADD】添加文件
1 parent 42fad45 commit cc8d63e

File tree

3 files changed

+246
-0
lines changed

3 files changed

+246
-0
lines changed

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

src/client_config.cpp

Lines changed: 221 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,221 @@
1+
#include "../include/tigerapi/client_config.h"
2+
#include "../include/tigerapi/properties.h"
3+
4+
TIGER_API::ClientConfig::ClientConfig(bool sandbox_debug /*= false*/)
5+
: sandbox_debug(sandbox_debug) {
6+
if (sandbox_debug) {
7+
LOG(WARNING) << U("SANDBOX IS NOT SUPPORTED") << endl;
8+
// server_url = SANDBOX_TIGER_SERVER_URL;
9+
// server_public_key = SANDBOX_TIGER_PUBLIC_KEY;
10+
}
11+
};
12+
13+
TIGER_API::ClientConfig::ClientConfig(utility::string_t tiger_id, utility::string_t private_key, utility::string_t account)
14+
: tiger_id(std::move(tiger_id)),
15+
private_key(std::move(private_key)),
16+
account(std::move(account)) {
17+
18+
};
19+
20+
TIGER_API::ClientConfig::ClientConfig(utility::string_t tiger_id, utility::string_t private_key, utility::string_t account, bool sandbox_debug /*= false*/, utility::string_t lang /*= U("en_US")*/)
21+
: tiger_id(std::move(tiger_id)),
22+
private_key(std::move(private_key)),
23+
account(std::move(account)),
24+
sandbox_debug(sandbox_debug),
25+
lang(lang) {
26+
if (sandbox_debug) {
27+
LOG(WARNING) << U("SANDBOX IS NOT SUPPORTED") << endl;
28+
// server_url = SANDBOX_TIGER_SERVER_URL;
29+
// server_public_key = SANDBOX_TIGER_PUBLIC_KEY;
30+
// socket_url = SANDBOX_TIGER_SOCKET_HOST;
31+
// socket_port = SANDBOX_TIGER_SOCKET_PORT;
32+
}
33+
};
34+
35+
TIGER_API::ClientConfig::ClientConfig(bool sandbox_debug, const utility::string_t props_path)
36+
: sandbox_debug(sandbox_debug),
37+
props_path(props_path) {
38+
load_props();
39+
load_token();
40+
};
41+
42+
void TIGER_API::ClientConfig::check()
43+
{
44+
if (this->tiger_id.empty()) {
45+
LOG(ERROR) << U("Client Config error: tiger_id can't be empty") << endl;
46+
throw std::runtime_error("Client Config error: tiger_id can't be empty");
47+
}
48+
if (this->private_key.empty()) {
49+
LOG(ERROR) << U("Client Config error: private_key can't be empty") << endl;
50+
throw std::runtime_error("Client Config error: private_key can't be empty");
51+
}
52+
}
53+
54+
void TIGER_API::ClientConfig::check_account()
55+
{
56+
if (this->account.empty()) {
57+
LOG(ERROR) << U("Client Config error: account can't be empty") << endl;
58+
throw std::runtime_error("Client Config error: account can't be empty");
59+
}
60+
}
61+
62+
void TIGER_API::ClientConfig::set_server_url(const utility::string_t& url)
63+
{
64+
this->server_url = url;
65+
}
66+
67+
void TIGER_API::ClientConfig::set_socket_url(const utility::string_t& url)
68+
{
69+
this->socket_url = url;
70+
}
71+
72+
void TIGER_API::ClientConfig::set_socket_port(const utility::string_t& port)
73+
{
74+
this->socket_port = port;
75+
}
76+
77+
void TIGER_API::ClientConfig::set_server_public_key(const utility::string_t& key)
78+
{
79+
this->server_public_key = key;
80+
}
81+
82+
void TIGER_API::ClientConfig::set_token(const utility::string_t& token)
83+
{
84+
this->token = token;
85+
}
86+
87+
const utility::string_t& TIGER_API::ClientConfig::get_server_url()
88+
{
89+
return this->server_url;
90+
}
91+
92+
const utility::string_t& TIGER_API::ClientConfig::get_server_pub_key()
93+
{
94+
return this->server_public_key;
95+
}
96+
97+
const utility::string_t& TIGER_API::ClientConfig::get_socket_url()
98+
{
99+
return this->socket_url;
100+
}
101+
102+
const utility::string_t& TIGER_API::ClientConfig::get_socket_port()
103+
{
104+
return this->socket_port;
105+
}
106+
107+
void TIGER_API::ClientConfig::load_props()
108+
{
109+
utility::string_t full_path = get_props_path(DEFAULT_PROPS_FILE);
110+
if (full_path.empty()) {
111+
return;
112+
}
113+
LOG(INFO) << U("Loading properties file from: ") << full_path << endl;
114+
115+
try {
116+
auto u8_path = Utils::str16to8(full_path);
117+
std::ifstream file(u8_path);
118+
if (!file.is_open()) {
119+
LOG(ERROR) << U("Failed to open properties file: ") << full_path << endl;
120+
return;
121+
}
122+
123+
Properties props;
124+
props.load(file);
125+
126+
if (tiger_id.empty()) {
127+
tiger_id = props.get_property(U("tiger_id"));
128+
}
129+
if (private_key.empty()) {
130+
private_key = props.get_property(U("private_key_pk1"));
131+
}
132+
if (account.empty()) {
133+
account = props.get_property(U("account"));
134+
}
135+
if (license.empty()) {
136+
license = props.get_property(U("license"));
137+
}
138+
139+
utility::string_t env = props.get_property(U("env"));
140+
std::transform(env.begin(), env.end(), env.begin(), ::toupper);
141+
if (env == U("SANDBOX")) {
142+
sandbox_debug = true;
143+
server_url = SANDBOX_TIGER_SERVER_URL;
144+
server_public_key = SANDBOX_TIGER_PUBLIC_KEY;
145+
socket_url = SANDBOX_TIGER_SOCKET_HOST;
146+
socket_port = SANDBOX_TIGER_SOCKET_PORT;
147+
}
148+
149+
}
150+
catch (const std::exception& e) {
151+
LOG(ERROR) << U("Failed to load properties file: ") << Utils::str8to16(e.what()) << endl;
152+
}
153+
LOG(INFO) << U("Loaded properties file successfully, tiger_id: ") << tiger_id << " account: " << account
154+
<< endl;
155+
}
156+
157+
utility::string_t TIGER_API::ClientConfig::get_props_path(const utility::string_t& filename) const
158+
{
159+
if (!props_path.empty())
160+
{
161+
if (Utils::is_directory(props_path)) {
162+
return props_path + filename;
163+
}
164+
}
165+
return U("");
166+
}
167+
168+
utility::string_t TIGER_API::ClientConfig::get_token_path() const
169+
{
170+
return get_props_path(DEFAULT_TOKEN_FILE);
171+
}
172+
173+
void TIGER_API::ClientConfig::load_token()
174+
{
175+
utility::string_t full_path = get_token_path();
176+
if (full_path.empty()) {
177+
return;
178+
}
179+
try {
180+
std::ifstream file(Utils::str16to8(full_path));
181+
if (!file.is_open()) {
182+
LOG(ERROR) << U("Failed to open token file: ") << full_path << endl;
183+
return;
184+
}
185+
186+
Properties props;
187+
props.load(file);
188+
189+
// get token value
190+
token = props.get_property(U("token"));
191+
LOG(INFO) << U("Loaded token successfully, token: ") << token << endl;
192+
}
193+
catch (const std::exception& e) {
194+
LOG(ERROR) << U("Failed to load token file: ") << Utils::str8to16(e.what()) << endl;
195+
}
196+
}
197+
198+
void TIGER_API::ClientConfig::save_token(const utility::string_t& new_token)
199+
{
200+
utility::string_t full_path = get_token_path();
201+
if (full_path.empty()) {
202+
return;
203+
}
204+
try {
205+
Properties props;
206+
props.set_property(U("token"), new_token);
207+
208+
std::ofstream file(Utils::str16to8(full_path));
209+
if (!file.is_open()) {
210+
LOG(ERROR) << U("Failed to open token file for writing: ") << full_path << endl;
211+
return;
212+
}
213+
214+
props.store(file);
215+
token = new_token;
216+
LOG(INFO) << U("Saved token successfully, token: ") << token << endl;
217+
}
218+
catch (const std::exception& e) {
219+
LOG(ERROR) << U("Failed to save token file: ") << Utils::str8to16(e.what()) << endl;
220+
}
221+
}

src/logger.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#include "../include/tigerapi/logger.h"
2+
3+
INITIALIZE_EASYLOGGINGPP
4+
5+
void TIGER_API::LoggerConfig::set_log_level(el::Level level)
6+
{
7+
el::Loggers::setLoggingLevel(level);
8+
}

0 commit comments

Comments
 (0)