-
-
Notifications
You must be signed in to change notification settings - Fork 990
/
options.cpp
132 lines (112 loc) · 4.44 KB
/
options.cpp
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
/*
Copyright (C) 2020 by Iris Morelle <shadowm@wesnoth.org>
Part of the Battle for Wesnoth Project https://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
#include "server/campaignd/options.hpp"
#include "commandline_argv.hpp"
#include "formatter.hpp"
#include "log.hpp"
#include "serialization/string_utils.hpp"
#include <boost/program_options/cmdline.hpp>
#include <boost/program_options/parsers.hpp>
#include <boost/program_options/variables_map.hpp>
namespace po = boost::program_options;
namespace campaignd {
command_line::command_line(int argc, char** argv)
: command_line(read_argv(argc, argv))
{
}
command_line::command_line(const std::vector<std::string>& args)
: help(false)
, version(false)
, config_file()
, server_dir()
, port()
, show_log_domains(false)
, log_domain_levels()
, log_precise_timestamps(false)
, report_timings(false)
, argv0_(args.at(0))
, args_(args.begin() + 1, args.end())
, help_text_()
{
po::options_description opts_general{"General options"};
opts_general.add_options()
("help,h", "prints this message and exits.")
("version,v", "displays the server version and exits.")
;
po::options_description opts_server{"Server configuration"};
opts_server.add_options()
("config,c", po::value<std::string>(), "specifies the path to the server configuration file. By default this is server.cfg in the server directory.")
("server-dir,s", po::value<std::string>(), "specifies the path to the server directory. By default this is the process working directory.")
("port,p", po::value<unsigned short>(), "specifies the port number on which the server will listen for client connections.")
;
po::options_description opts_log{"Logging options"};
opts_log.add_options()
("logdomains", "lists defined log domains and exits")
("log-error", po::value<std::string>(), "sets the severity level of the specified log domain(s) to 'error'. <arg> should be given as a comma-separated list of domains.")
("log-warning", po::value<std::string>(), "sets the severity level of the specified log domain(s) to 'warning'. This is the default for all log domains other than 'campaignd' and 'server'.")
("log-info", po::value<std::string>(), "sets the severity level of the specified log domain(s) to 'info'. This is the default for the 'campaignd' and 'server' log domains.")
("log-debug", po::value<std::string>(), "sets the severity level of the specified log domain(s) to 'debug'.")
("log-none", po::value<std::string>(), "disables logging for the specified log domain(s).")
("log-precise", "shows the timestamps in log output with more precision.")
("timings", "outputs timings for serviced requests to stderr.")
;
po::options_description opts;
opts.add(opts_general).add(opts_server).add(opts_log);
static const int style = po::command_line_style::default_style ^ po::command_line_style::allow_guessing;
po::variables_map vm;
po::store(po::command_line_parser(args_).options(opts).style(style).run(), vm);
static const std::map<std::string, int> log_levels = {
{ "error", lg::err().get_severity() },
{ "warning", lg::warn().get_severity() },
{ "info", lg::info().get_severity() },
{ "debug", lg::debug().get_severity() },
{ "none", -1 }
};
if(vm.count("help")) {
if(!help) {
help_text_ = formatter() << "Usage: " << argv0_ << " [<options>]\n" << opts;
}
help = true;
}
if(vm.count("version")) {
version = true;
}
if(vm.count("config")) {
config_file = vm["config"].as<std::string>();
}
if(vm.count("server-dir")) {
server_dir = vm["server-dir"].as<std::string>();
}
if(vm.count("port")) {
port = vm["port"].as<unsigned short>();
}
if(vm.count("logdomains")) {
show_log_domains = true;
}
for(const auto& lvl : log_levels) {
const auto& swtch = std::string{"log-"} + lvl.first;
const auto severity = lvl.second;
if(vm.count(swtch)) {
const auto& domains = utils::split(vm[swtch].as<std::string>());
for(const auto& d : domains) {
log_domain_levels[d] = severity;
}
}
}
if(vm.count("log-precise")) {
log_precise_timestamps = true;
}
if(vm.count("timings")) {
report_timings = true;
}
}
} // end namespace campaignd