-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjam_node.cpp
More file actions
194 lines (156 loc) · 5.12 KB
/
Copy pathjam_node.cpp
File metadata and controls
194 lines (156 loc) · 5.12 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
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
183
184
185
186
187
188
189
190
191
192
193
194
/**
* Copyright Quadrivium LLC
* All Rights Reserved
* SPDX-License-Identifier: Apache-2.0
*/
#include <chrono>
#include <iostream>
#include <memory>
#include <optional>
#include <variant>
#include <qtils/final_action.hpp>
#include <soralog/impl/configurator_from_yaml.hpp>
#include <soralog/logging_system.hpp>
#include "app/application.hpp"
#include "app/configuration.hpp"
#include "app/configurator.hpp"
#include "injector/node_injector.hpp"
#include "loaders/impl/example_loader.hpp"
#include "log/logger.hpp"
#include "modules/module_loader.hpp"
#include "se/subscription.hpp"
using std::string_view_literals::operator""sv;
// NOLINTBEGIN(cppcoreguidelines-pro-bounds-pointer-arithmetic)
namespace {
void wrong_usage() {
std::cerr << "Wrong usage.\n"
"Run with `--help' argument to print usage\n";
}
using jam::app::Application;
using jam::app::Configuration;
using jam::injector::NodeInjector;
using jam::log::LoggingSystem;
int run_node(std::shared_ptr<LoggingSystem> logsys,
std::shared_ptr<Configuration> appcfg) {
auto injector = std::make_unique<NodeInjector>(logsys, appcfg);
// Load modules
std::deque<std::unique_ptr<jam::loaders::Loader>> loaders;
{
auto logger = logsys->getLogger("Modules", "jam");
const std::string path("modules");
jam::modules::ModuleLoader module_loader(path);
auto modules = module_loader.get_modules();
if (modules.has_error()) {
SL_CRITICAL(logger, "Failed to load modules from path: {}", path);
return EXIT_FAILURE;
}
for (const auto &module : modules.value()) {
auto loader = injector->register_loader(module);
if (loader) {
loaders.emplace_back(std::move(loader));
}
}
}
auto logger = logsys->getLogger("Main", jam::log::defaultGroupName);
auto app = injector->injectApplication();
SL_INFO(logger, "Node started. Version: {} ", appcfg->nodeVersion());
app->run();
SL_INFO(logger, "Node stopped");
logger->flush();
return EXIT_SUCCESS;
}
} // namespace
int main(int argc, const char **argv, const char **env) {
soralog::util::setThreadName("jam-node");
qtils::FinalAction flush_std_streams_at_exit([] {
std::cout.flush();
std::cerr.flush();
});
if (argc == 0) {
// Abnormal run
wrong_usage();
return EXIT_FAILURE;
}
if (argc == 1) {
// Run without arguments
wrong_usage();
return EXIT_FAILURE;
}
auto app_configurator =
std::make_unique<jam::app::Configurator>(argc, argv, env);
// Parse CLI args for help, version and config
if (auto res = app_configurator->step1(); res.has_value()) {
if (res.value()) {
return EXIT_SUCCESS;
}
} else {
return EXIT_FAILURE;
}
// Setup logging system
auto logging_system = ({
auto log_config = app_configurator->getLoggingConfig();
if (log_config.has_error()) {
std::cerr << "Logging config is empty.\n";
return EXIT_FAILURE;
}
auto log_configurator = std::make_shared<soralog::ConfiguratorFromYAML>(
std::shared_ptr<soralog::Configurator>(nullptr), log_config.value());
auto logging_system =
std::make_shared<soralog::LoggingSystem>(std::move(log_configurator));
auto config_result = logging_system->configure();
if (not config_result.message.empty()) {
(config_result.has_error ? std::cerr : std::cout)
<< config_result.message << '\n';
}
if (config_result.has_error) {
return EXIT_FAILURE;
}
std::make_shared<jam::log::LoggingSystem>(std::move(logging_system));
});
// Parse CLI args for help, version and config
if (auto res = app_configurator->step2(); res.has_value()) {
if (res.value()) {
return EXIT_SUCCESS;
}
} else {
return EXIT_FAILURE;
}
// Setup config
auto configuration = ({
auto logger = logging_system->getLogger("Configurator", "jam");
auto config_res = app_configurator->calculateConfig(logger);
if (config_res.has_error()) {
auto error = config_res.error();
SL_CRITICAL(logger, "Failed to calculate config: {}", error);
fmt::println(std::cerr, "Failed to calculate config: {}", error);
fmt::println(std::cerr, "See more details in the log");
return EXIT_FAILURE;
}
config_res.value();
});
int exit_code;
{
std::string_view name{argv[1]};
if (name.substr(0, 1) == "-") {
// The first argument isn't subcommand, run as node
exit_code = run_node(logging_system, configuration);
}
// else if (false and name == "subcommand-1"s) {
// exit_code = execute_subcommend_1(argc - 1, argv + 1);
// }
//
// else if (false and name == "subcommand-2"s) {
// exit_code = execute_subcommend_2(argc - 1, argv + 1);
// }
else {
// No subcommand, but argument is not a valid option: begins not with dash
wrong_usage();
return EXIT_FAILURE;
}
}
auto logger = logging_system->getLogger("Main", jam::log::defaultGroupName);
SL_INFO(logger, "All components are stopped");
logger->flush();
return exit_code;
}
// NOLINTEND(cppcoreguidelines-pro-bounds-pointer-arithmetic)