-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfileoverridedatasource.cpp
More file actions
52 lines (44 loc) · 2.04 KB
/
fileoverridedatasource.cpp
File metadata and controls
52 lines (44 loc) · 2.04 KB
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
#include "configcat/fileoverridedatasource.h"
#include "configcatlogger.h"
using namespace std;
namespace configcat {
FileFlagOverrides::FileFlagOverrides(const std::string& filePath, OverrideBehaviour behaviour):
filePath(filePath),
behaviour(behaviour) {
}
std::shared_ptr<OverrideDataSource> FileFlagOverrides::createDataSource(const std::shared_ptr<ConfigCatLogger>& logger) {
return make_shared<FileOverrideDataSource>(filePath, behaviour, logger);
}
FileOverrideDataSource::FileOverrideDataSource(const string& filePath, OverrideBehaviour behaviour, const std::shared_ptr<ConfigCatLogger>& logger):
OverrideDataSource(behaviour),
overrides(make_shared<unordered_map<string, Setting>>()),
filePath(filePath),
logger(logger) {
if (!filesystem::exists(filePath)) {
LOG_ERROR(1300) <<
"Cannot find the local config file '" << filePath << "'. "
"This is a path that your application provided to the ConfigCat SDK by passing it to the constructor of the `FileFlagOverrides` class. "
"Read more: https://configcat.com/docs/sdk-reference/cpp/#json-file";
}
}
shared_ptr<unordered_map<string, Setting>> FileOverrideDataSource::getOverrides() {
reloadFileContent();
return overrides;
}
void FileOverrideDataSource::reloadFileContent() {
try {
auto lastWriteTime = std::filesystem::last_write_time(filePath);
if (fileLastWriteTime != lastWriteTime) {
fileLastWriteTime = lastWriteTime;
auto config = Config::fromFile(filePath);
overrides = config->getSettingsOrEmpty();
}
} catch (const filesystem::filesystem_error&) {
LogEntry logEntry(logger, configcat::LOG_LEVEL_ERROR, 1302, current_exception());
logEntry << "Failed to read the local config file '" << filePath << "'.";
} catch (...) {
LogEntry logEntry(logger, configcat::LOG_LEVEL_ERROR, 2302, current_exception());
logEntry << "Failed to decode JSON from the local config file '" << filePath << "'.";
}
}
} // namespace configcat