forked from dxFeed/dxfeed-c-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfiguration.hpp
182 lines (142 loc) · 4.7 KB
/
Configuration.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
* The contents of this file are subject to the Mozilla Public License Version
* 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Initial Developer of the Original Code is Devexperts LLC.
* Portions created by the Initial Developer are Copyright (C) 2010
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
*/
#pragma once
#include <iostream>
#include <memory>
#include <mutex>
#include <sstream>
#include <string>
#include <toml.hpp>
#include "StringUtils.hpp"
extern "C" {
#include "DXErrorCodes.h"
#include "DXErrorHandling.h"
#include "Logger.h"
}
namespace dx {
struct Configuration : std::enable_shared_from_this<Configuration> {
enum class Type { None, String, File };
private:
Type type_;
std::string config_;
mutable std::recursive_mutex mutex_;
bool loaded_ = false;
toml::value properties_;
Configuration() : type_{Type::None}, config_{}, mutex_{}, properties_{} {}
public:
static std::shared_ptr<Configuration> getInstance() {
static std::shared_ptr<Configuration> instance{new Configuration()};
return instance;
}
bool isLoaded() const {
std::lock_guard<std::recursive_mutex> lock(mutex_);
return loaded_;
}
void dump() {
std::lock_guard<std::recursive_mutex> lock(mutex_);
std::cerr << "\nC-API Configuration:\n" << toml::format(properties_, 120) << std::endl;
std::cerr << "Loaded defaults:\n";
std::cerr << "dump = " << std::boolalpha << getDump() << std::endl;
std::cerr << "network.heartbeatPeriod = " << getNetworkHeartbeatPeriod() << std::endl;
std::cerr << "network.heartbeatTimeout = " << getNetworkHeartbeatTimeout() << std::endl;
std::cerr << "network.reestablishConnections = " << getNetworkReestablishConnections() << std::endl
<< std::endl;
}
bool loadFromFile(const std::string& fileName) {
std::lock_guard<std::recursive_mutex> lock(mutex_);
if (loaded_) {
return true;
}
if (StringUtils::trimCopy(fileName).empty()) {
dx_set_error_code(dx_cfgec_empty_config_file_name);
return false;
}
try {
properties_ = toml::parse(fileName);
loaded_ = true;
type_ = Type::File;
config_ = fileName;
if (getDump()) {
dump();
}
return true;
} catch (const std::exception& e) {
dx_set_error_code(dx_cfgec_toml_parser_error);
dx_logging_verbose(dx_ll_warn, L"Configuration::loadFromFile: %hs", e.what());
loaded_ = false;
}
return false;
}
bool loadFromString(const std::string& config) {
std::lock_guard<std::recursive_mutex> lock(mutex_);
if (loaded_) {
return true;
}
if (StringUtils::trimCopy(config).empty()) {
dx_set_error_code(dx_cfgec_empty_config_string);
return false;
}
try {
std::istringstream iss(config);
properties_ = toml::parse(iss);
loaded_ = true;
type_ = Type::String;
config_ = config;
if (getDump()) {
dump();
}
return true;
} catch (const std::exception& e) {
dx_set_error_code(dx_cfgec_toml_parser_error);
dx_logging_verbose(dx_ll_warn, L"Configuration::loadFromString: %hs", e.what());
loaded_ = false;
}
return false;
}
template <typename T>
T getProperty(const std::string& groupName, const std::string& fieldName, T defaultValue) const {
std::lock_guard<std::recursive_mutex> lock(mutex_);
if (!loaded_) {
return defaultValue;
}
try {
return toml::find_or((groupName.empty()) ? properties_ : toml::find(properties_, groupName), fieldName,
defaultValue);
} catch (const std::exception&) {
return defaultValue;
}
}
int getNetworkHeartbeatPeriod(int defaultValue = 10) const {
return getProperty("network", "heartbeatPeriod", defaultValue);
}
int getNetworkHeartbeatTimeout(int defaultValue = 120) const {
return getProperty("network", "heartbeatTimeout", defaultValue);
}
bool getDump(bool defaultValue = false) const { return getProperty("", "dump", defaultValue); }
dx_log_level_t getMinimumLoggingLevel(dx_log_level_t defaultValue = dx_ll_info) const {
return StringUtils::stringToLoggingLevel<dx_log_level_t>(getProperty("logger", "level", StringUtils::loggingLevelToString(defaultValue)));
}
bool getNetworkReestablishConnections(bool defaultValue = true) const {
return getProperty("network", "reestablishConnections", defaultValue);
}
bool getSubscriptionsDisableLastEventStorage(bool defaultValue = true) const {
return getProperty("subscriptions", "disableLastEventStorage", defaultValue);
}
};
} // namespace dx