OSVR Framework (Internal Development Docs)  0.6-1962-g59773924
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
osvr_server.cpp
Go to the documentation of this file.
1 
12 // Copyright 2014 Sensics, Inc.
13 //
14 // Licensed under the Apache License, Version 2.0 (the "License");
15 // you may not use this file except in compliance with the License.
16 // You may obtain a copy of the License at
17 //
18 // http://www.apache.org/licenses/LICENSE-2.0
19 //
20 // Unless required by applicable law or agreed to in writing, software
21 // distributed under the License is distributed on an "AS IS" BASIS,
22 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
23 // See the License for the specific language governing permissions and
24 // limitations under the License.
25 
26 // Internal Includes
29 #include <osvr/Util/Logger.h>
30 #include <osvr/Util/LogNames.h>
31 #include <osvr/Util/LogRegistry.h>
33 
34 // Library/third-party includes
35 #include <boost/program_options.hpp>
36 
37 // Standard includes
38 #include <vector>
39 #include <iostream>
40 
41 namespace opt = boost::program_options;
42 
43 static osvr::server::ServerPtr server;
44 using ::osvr::util::log::OSVR_SERVER_LOG;
45 
48  auto log = ::osvr::util::log::make_logger(OSVR_SERVER_LOG);
49  log->info() << "Received shutdown signal...";
50  server->signalStop();
51 }
52 
53 int main(int argc, char *argv[]) {
54  auto log = ::osvr::util::log::make_logger(OSVR_SERVER_LOG);
55 
56  std::vector<std::string> configPaths;
57 
58  opt::options_description optionsAll("All Options");
59  opt::options_description optionsVisible("Command Line Options");
60  opt::positional_options_description optionsPositional;
61 
62  optionsPositional.add("config", -1);
63  optionsVisible.add_options()
64  ("config", "server configuration filename")
65  ("help", "display this help message")
66  ("verbose,v", "enable verbose logging")
67  ("debug,d", "enable debug logging");
68  optionsAll.add(optionsVisible);
69 
70  opt::variables_map values;
71  try {
72  opt::store(opt::command_line_parser(argc, argv)
73  .options(optionsAll)
74  .positional(optionsPositional)
75  .run(),
76  values);
77  opt::notify(values);
78  } catch (opt::invalid_command_line_syntax &e) {
79  log->error() << e.what();
80  return 1;
81  } catch (opt::unknown_option &e) {
82  log->error() << e.what(); // may want to replace boost msg
83  return 1;
84  }
85 
86  if (values.count("help")) {
87  std::cout << optionsVisible << std::endl;
88  return 0;
89  }
90 
91  if (values.count("debug")) {
92  osvr::util::log::LogRegistry::instance().setLevel(osvr::util::log::LogLevel::trace);
93  osvr::util::log::LogRegistry::instance().setConsoleLevel(osvr::util::log::LogLevel::trace);
94  log->trace("Debug logging enabled.");
95  } else if (values.count("verbose")) {
96  osvr::util::log::LogRegistry::instance().setLevel(osvr::util::log::LogLevel::debug);
97  osvr::util::log::LogRegistry::instance().setConsoleLevel(osvr::util::log::LogLevel::debug);
98  log->debug("Verbose logging enabled.");
99  }
100 
101  if (values.count("config")) {
102  std::string configFileArgument = values["config"].as<std::string>();
103  log->info() << "Using config file " << configFileArgument << " from command line argument.";
104  configPaths = { configFileArgument };
105  } else {
106  log->info() << "Using default config file - pass a filename on the command "
107  "line to use a different one.";
109  }
110 
112  if (!server) {
113  // only attempt to load the empty config if no arguments are passed.
114  if (!values.count("config")) {
115  log->info() << "Could not find a valid config file in the default search paths. Using default config object.";
116  server = osvr::server::configureServerFromString("{ }");
117  } else {
118  return -1;
119  }
120  }
121 
122  if (!server) {
123  log->error() << "Unknown error while creating server.";
124  return -1;
125  }
126 
127  log->info() << "Registering shutdown handler...";
128  osvr::server::registerShutdownHandler<&handleShutdown>();
129 
130  log->info() << "Starting server mainloop: OSVR Server is ready to go!";
131  server->startAndAwaitShutdown();
132 
133  log->info() << "OSVR Server exited.";
134 
135  return 0;
136 }
void handleShutdown()
Shutdown handler function - forcing the server pointer to be global.
Definition: osvr_server.cpp:47
Header to register a handler for cross-platform shutdown/terminate signals. WARNING: includes windows...
void setConsoleLevel(LogLevel severity)
Sets the minimum level of messages to be logged to the console.
Regstry to maintain instantiated loggers and global settings.
Platform specific search paths for osvr server config files.
OSVR_SERVER_EXPORT ServerPtr configureServerFromFirstFileInList(std::vector< std::string > const &configNames)
This iterates over a vector that contains a list of potential config files, and uses the first workin...
shared_ptr< Server > ServerPtr
How one should hold a Server.
Definition: ServerPtr.h:39
Header to include for OSVR-internal usage of the logging mechanism: provides the needed definition of...
void setLevel(LogLevel severity)
Sets the minimum level of messages to be logged on all registered loggers.
OSVR_SERVER_EXPORT std::vector< std::string > getDefaultConfigFilePaths()
this returns a vector of default server configuration file paths.
Header.