-
Notifications
You must be signed in to change notification settings - Fork 130
Added osvr_server command-line options #529
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
4f79020
d2bbb3b
cea5f20
3edd956
33a3984
130c9d4
1cea73b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,15 +28,22 @@ | |
| #include <osvr/Server/RegisterShutdownHandler.h> | ||
| #include <osvr/Util/Logger.h> | ||
| #include <osvr/Util/LogNames.h> | ||
| #include <osvr/Util/LogRegistry.h> | ||
|
|
||
| // Library/third-party includes | ||
| // - none | ||
| #include <boost/program_options.hpp> | ||
| #include <boost/filesystem.hpp> | ||
| #include <boost/filesystem/fstream.hpp> | ||
| #include <boost/optional.hpp> | ||
|
|
||
| // Standard includes | ||
| #include <exception> | ||
| #include <fstream> | ||
| #include <iostream> | ||
|
|
||
| namespace opt = boost::program_options; | ||
| namespace fs = boost::filesystem; | ||
|
|
||
| static osvr::server::ServerPtr server; | ||
| using ::osvr::util::log::OSVR_SERVER_LOG; | ||
|
|
||
|
|
@@ -50,16 +57,84 @@ void handleShutdown() { | |
| int main(int argc, char *argv[]) { | ||
| auto log = ::osvr::util::log::make_logger(OSVR_SERVER_LOG); | ||
|
|
||
| std::string configName(osvr::server::getDefaultConfigFilename()); | ||
| if (argc > 1) { | ||
| configName = argv[1]; | ||
| std::string configName; // server configuration filename | ||
|
|
||
| opt::options_description optionsAll("All Options"); | ||
| opt::options_description optionsVisible("Command Line Options"); | ||
| opt::positional_options_description optionsPositional; | ||
|
|
||
| optionsPositional.add("config", -1); | ||
| optionsAll.add_options()( | ||
| "config", | ||
| opt::value<std::string>(&configName) | ||
| ->default_value(osvr::server::getDefaultConfigFilename()), | ||
| "server configuration filename"); | ||
| optionsVisible.add_options() | ||
| ("help", "display this help message") | ||
| ("verbose,v", "enable verbose logging") | ||
| ("debug,d", "enable debug logging"); | ||
| optionsAll.add(optionsVisible); | ||
|
|
||
| opt::variables_map values; | ||
| try { | ||
| opt::store(opt::command_line_parser(argc, argv) | ||
| .options(optionsAll) | ||
| .positional(optionsPositional) | ||
| .run(), | ||
| values); | ||
| opt::notify(values); | ||
| } catch (opt::invalid_command_line_syntax &e) { | ||
| log->error() << e.what(); | ||
| return 1; | ||
| } catch (opt::unknown_option &e) { | ||
| log->error() << e.what(); // may want to replace boost msg | ||
| return 1; | ||
| } | ||
|
|
||
| if (values.count("help")) { | ||
| std::cout << optionsVisible << std::endl; | ||
| return 0; | ||
| } | ||
|
|
||
| if (values.count("debug")) { | ||
| osvr::util::log::LogRegistry::instance().setLevel(osvr::util::log::LogLevel::trace); | ||
| osvr::util::log::LogRegistry::instance().setConsoleLevel(osvr::util::log::LogLevel::trace); | ||
| log->trace("Debug logging enabled."); | ||
| } else if (values.count("verbose")) { | ||
| osvr::util::log::LogRegistry::instance().setLevel(osvr::util::log::LogLevel::debug); | ||
| osvr::util::log::LogRegistry::instance().setConsoleLevel(osvr::util::log::LogLevel::debug); | ||
| log->debug("Verbose logging enabled."); | ||
| } | ||
|
|
||
| configName = values["config"].as<std::string>(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what does this line do if no command line arguments are passed?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @JeroMiya If no configuration file is specified, it will check to see if the file returned by |
||
|
|
||
| boost::optional<fs::path> configPath(configName); | ||
| try { | ||
| if (!fs::exists(*configPath)) { | ||
| log->warn() << "File '" << configName | ||
| << "' not found. Using empty configuration."; | ||
| configPath = boost::none; | ||
| } else { | ||
| if (fs::is_directory(*configPath)) { | ||
| log->error() << "'" << configName << "' is a directory."; | ||
| return -1; | ||
| } else if (!fs::is_regular_file(*configPath)) { | ||
| log->error() << "'" << configName << "' is special file."; | ||
| return -1; | ||
| } | ||
| } | ||
| } catch (fs::filesystem_error &e) { | ||
| log->error() << "Could not open config file at '" << configName << "'."; | ||
| log->error() << "Reason " << e.what() << "."; | ||
| configPath = boost::none; | ||
| } | ||
|
|
||
| if (configPath) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm assuming this check fails if you provide an empty string to
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @mars979 I think so, since I would expect
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, sounds good. |
||
| server = osvr::server::configureServerFromFile(configName); | ||
| } else { | ||
| log->info() | ||
| << "Using default config file - pass a filename on the command " | ||
| "line to use a different one."; | ||
| server = osvr::server::configureServerFromString("{ }"); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need to also check for a file called
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If no configuration file is specified on the command line, we assume the user specified the filename returned by |
||
| } | ||
|
|
||
| server = osvr::server::configureServerFromFile(configName); | ||
| if (!server) { | ||
| return -1; | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -38,6 +38,7 @@ | |
| #include <exception> | ||
| #include <fstream> | ||
| #include <iostream> | ||
| #include <sstream> | ||
|
|
||
| namespace osvr { | ||
| namespace server { | ||
|
|
@@ -48,24 +49,15 @@ namespace server { | |
| /// @brief This is the basic common code of a server app's setup, ripped out | ||
| /// of the main server app to make alternate server-acting apps simpler to | ||
| /// develop. | ||
| inline ServerPtr configureServerFromFile(std::string const &configName) { | ||
| inline ServerPtr configureServerFromString(std::string const &json) { | ||
| auto log = | ||
| ::osvr::util::log::make_logger(::osvr::util::log::OSVR_SERVER_LOG); | ||
|
|
||
| ServerPtr ret; | ||
| log->info() << "Using config file '" << configName << "'."; | ||
| std::ifstream config(configName); | ||
| if (!config.good()) { | ||
| log->error() << "Could not open config file!"; | ||
| log->error() << "Searched in the current directory; file may be " | ||
| "misspelled, missing, or in a different directory."; | ||
| return nullptr; | ||
| } | ||
|
|
||
| osvr::server::ConfigureServer srvConfig; | ||
| log->info() << "Constructing server as configured..."; | ||
| try { | ||
| srvConfig.loadConfig(config); | ||
| srvConfig.loadConfig(json); | ||
| ret = srvConfig.constructServer(); | ||
| } catch (std::exception &e) { | ||
| log->error() | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we log the std::exception error here ?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @mars979 We are logging the exception here. (Or are you suggesting we shouldn't log it?)
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @godbyk , I meant to ask if
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @mars979 Ah, gotcha. We're logging it a couple lines down. (clang-format's line-wrapping hinders the readability of the line.)
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, it was just wrapped to next line. My bad. |
||
|
|
@@ -151,6 +143,26 @@ namespace server { | |
| return ret; | ||
| } | ||
|
|
||
| /// @Brief Convenience wrapper for configureServerFromString(). | ||
| inline ServerPtr configureServerFromFile(std::string const &configName) { | ||
| auto log = | ||
| ::osvr::util::log::make_logger(::osvr::util::log::OSVR_SERVER_LOG); | ||
|
|
||
| ServerPtr ret; | ||
| log->info() << "Using config file '" << configName << "'."; | ||
| std::ifstream config(configName); | ||
| if (!config.good()) { | ||
| log->error() << "Could not open config file!"; | ||
| log->error() << "Searched in the current directory; file may be " | ||
| "misspelled, missing, or in a different directory."; | ||
| return nullptr; | ||
| } | ||
|
|
||
| std::stringstream sstr; | ||
| sstr << config.rdbuf(); | ||
| return configureServerFromString(sstr.str()); | ||
| } | ||
|
|
||
| } // namespace server | ||
| } // namespace osvr | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this make the config option the default option when only one argument is passed (like when you drag a config file over the server in windows explorer)? Just making sure we don't change the current behavior in that case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. So if you run
osvr_server blah.jsonthen it will useblah.jsonas the configuration file. The same thing happens if you drag and dropblah.jsononto theosvr_server.exeprogram in Windows.