forked from scylladb/seastar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app-template.cc
167 lines (151 loc) · 5.25 KB
/
app-template.cc
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
/*
* This file is open source software, licensed to you under the terms
* of the Apache License, Version 2.0 (the "License"). See the NOTICE file
* distributed with this work for additional information regarding copyright
* ownership. You may not use this file except in compliance with the License.
*
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
/*
* Copyright (C) 2014 Cloudius Systems, Ltd.
*/
#include "app-template.hh"
#include "core/reactor.hh"
#include "core/scollectd.hh"
#include "core/metrics_api.hh"
#include <boost/program_options.hpp>
#include "core/print.hh"
#include "util/log.hh"
#include "util/log-cli.hh"
#include <boost/program_options.hpp>
#include <boost/make_shared.hpp>
#include <fstream>
#include <cstdlib>
namespace seastar {
namespace bpo = boost::program_options;
app_template::app_template(app_template::config cfg)
: _cfg(std::move(cfg))
, _opts(_cfg.name + " options") {
_opts.add_options()
("help,h", "show help message")
;
_opts.add(reactor::get_options_description(cfg.default_task_quota));
_opts.add(seastar::metrics::get_options_description());
_opts.add(smp::get_options_description());
_opts.add(scollectd::get_options_description());
_opts.add(log_cli::get_options_description());
}
boost::program_options::options_description& app_template::get_options_description() {
return _opts;
}
boost::program_options::options_description_easy_init
app_template::add_options() {
return _opts.add_options();
}
void
app_template::add_positional_options(std::initializer_list<positional_option> options) {
for (auto&& o : options) {
_opts.add(boost::make_shared<bpo::option_description>(o.name, o.value_semantic, o.help));
_pos_opts.add(o.name, o.max_count);
}
}
bpo::variables_map&
app_template::configuration() {
return *_configuration;
}
int
app_template::run(int ac, char ** av, std::function<future<int> ()>&& func) {
return run_deprecated(ac, av, [func = std::move(func)] () mutable {
auto func_done = make_lw_shared<promise<>>();
engine().at_exit([func_done] { return func_done->get_future(); });
futurize_apply(func).finally([func_done] {
func_done->set_value();
}).then([] (int exit_code) {
return engine().exit(exit_code);
}).or_terminate();
});
}
int
app_template::run(int ac, char ** av, std::function<future<> ()>&& func) {
return run(ac, av, [func = std::move(func)] {
return func().then([] () {
return 0;
});
});
}
int
app_template::run_deprecated(int ac, char ** av, std::function<void ()>&& func) {
#ifdef DEBUG
print("WARNING: debug mode. Not for benchmarking or production\n");
#endif
bpo::variables_map configuration;
try {
bpo::store(bpo::command_line_parser(ac, av)
.options(_opts)
.positional(_pos_opts)
.run()
, configuration);
auto home = std::getenv("HOME");
if (home) {
std::ifstream ifs(std::string(home) + "/.config/seastar/seastar.conf");
if (ifs) {
bpo::store(bpo::parse_config_file(ifs, _opts), configuration);
}
std::ifstream ifs_io(std::string(home) + "/.config/seastar/io.conf");
if (ifs_io) {
bpo::store(bpo::parse_config_file(ifs_io, _opts), configuration);
}
}
} catch (bpo::error& e) {
print("error: %s\n\nTry --help.\n", e.what());
return 2;
}
if (configuration.count("help")) {
std::cout << _opts << "\n";
return 1;
}
if (configuration["help-loggers"].as<bool>()) {
log_cli::print_available_loggers(std::cout);
return 1;
}
bpo::notify(configuration);
// Needs to be before `smp::configure()`.
try {
apply_logging_settings(log_cli::extract_settings(configuration));
} catch (const std::runtime_error& exn) {
std::cout << "logging configuration error: " << exn.what() << '\n';
return 1;
}
configuration.emplace("argv0", boost::program_options::variable_value(std::string(av[0]), false));
smp::configure(configuration);
_configuration = {std::move(configuration)};
engine().when_started().then([this] {
seastar::metrics::configure(this->configuration()).then([this] {
// set scollectd use the metrics configuration, so the later
// need to be set first
scollectd::configure( this->configuration());
});
}).then(
std::move(func)
).then_wrapped([] (auto&& f) {
try {
f.get();
} catch (std::exception& ex) {
std::cout << "program failed with uncaught exception: " << ex.what() << "\n";
engine().exit(1);
}
});
auto exit_code = engine().run();
smp::cleanup();
return exit_code;
}
}