50 const int DEFAULT_LOG_LEVEL{2};
52 const int DEFAULT_PRIORITY_LEVEL{50};
54 void usage(
const std::string & sAppName,std::vector<std::string> additionalOptions)
56 std::cout<<
"usage: "<<sAppName <<
" [OPTIONS]... CONFIG_URL"<<std::endl;
58 std::cout<<
" CONFIG_URL URL of XML configuration file."<<std::endl;
60 std::cout<<
"options:"<<std::endl;
61 std::cout<<
" -d, --daemonize Run in the background."<<std::endl;
62 std::cout<<
" -h, --help Print this message and exit."<<std::endl;
63 std::cout<<
" -f, --logfile FILE Log to a file instead of stdout."<<std::endl;
64 std::cout<<
" -l, --loglevel [0,4] Set initial log level."<<std::endl;
65 std::cout<<
" default: "<<DEFAULT_LOG_LEVEL<<std::endl;
66 std::cout<<
" --pidfile FILE Write application pid to file."<<std::endl;
67 std::cout<<
" -p, --priority [0,99] Set realtime priority level."<<std::endl;
68 std::cout<<
" Only used with -r, --realtime."<<std::endl;
69 std::cout<<
" default: "<<DEFAULT_PRIORITY_LEVEL<<std::endl;
70 std::cout<<
" -r, --realtime Set realtime scheduling."<<std::endl;
71 std::cout<<
" --syslog Log to syslog instead of stdout."<<std::endl;
72 std::cout<<
" --uuidfile FILE Write the application instance UUID to file."<<std::endl;
73 std::cout<<
" -v, --version Print version and exit."<<std::endl;
76 if(!additionalOptions.empty())
78 std::cout<<
"additional options:"<<std::endl;
80 for(
const auto & option : additionalOptions)
82 std::cerr<<option<<std::endl;
121 std::vector<option> options =
123 {
"help", 0,
nullptr,
'h'},
124 {
"realtime", 0,
nullptr,
'r'},
125 {
"loglevel", 1,
nullptr,
'l'},
126 {
"logfile", 1,
nullptr,
'f'},
127 {
"daemonize",0,
nullptr,
'd'},
128 {
"syslog", 0,
nullptr, 1},
129 {
"version" , 0,
nullptr,
'v'},
130 {
"pidfile" , 1,
nullptr, 2},
131 {
"uuidfile", 1,
nullptr, 3},
132 {
"priority", 1,
nullptr,
'p'},
135 std::string sOptString{
"hrvdf:l:p:"};
142 int iLogLevel{DEFAULT_LOG_LEVEL};
143 int iPriority{DEFAULT_PRIORITY_LEVEL};
144 std::string sLogFile{};
145 std::string sPIDFile{};
146 std::string sUUIDFile{};
151 options.insert(options.end(),
152 additionalOptions.begin(),
153 additionalOptions.end());
159 options.push_back({0, 0,
nullptr,0 });
161 while((iOption = getopt_long(argc,argv,sOptString.c_str(), &options[0],&iOptionIndex)) != -1)
174 std::cout<<optarg<<std::endl;
187 std::cout<<VERSION<<std::endl;
209 std::cerr<<
"invalid priority: "<<optarg<<std::endl;
222 std::cerr<<
"invalid log level: "<<optarg<<std::endl;
232 std::cerr<<
"option -"<<
static_cast<char>(optopt)<<
" requires an argument."<<std::endl;
234 else if(isprint(optopt))
236 std::cerr<<
"unknown option -"<<
static_cast<char>(optopt)<<
"."<<std::endl;
240 std::cerr<<
"bad option"<<std::endl;
252 std::string sConfigurationXML{};
256 std::cerr<<
"Missing CONFIG_URL"<<std::endl;
261 sConfigurationXML = argv[optind];
269 if(sLogFile.empty() && !bSysLog && iLogLevel != 0)
271 std::cerr<<
"unable to daemonize log level must be 0 when logging to stdout"<<std::endl;;
277 std::cerr<<
"unable to daemonize"<<std::endl;
282 if(!sLogFile.empty())
284 logger.redirectLogsToFile(sLogFile.c_str());
289 logger.setLogLevel(static_cast<EMANE::LogLevel>(iLogLevel));
299 std::stringstream ss;
300 for(
int i = 0; i < argc; ++i)
306 uuid_unparse(uuid_,uuidBuf);
313 struct sched_param schedParam{iPriority};
315 if(sched_setscheduler(0,SCHED_RR,&schedParam))
317 if(bSysLog || !sLogFile.empty() || !iLogLevel)
319 std::cerr<<
"unable to set realtime scheduler"<<std::endl;
329 if(bSysLog || !sLogFile.empty() || iLogLevel < 2)
331 std::cerr<<
"Please consider using the realtime scheduler to improve fidelity."<<std::endl;
334 logger.log(
EMANE::ERROR_LEVEL,
"Please consider using the realtime scheduler to improve fidelity.");
338 if(!sPIDFile.empty())
340 std::ofstream pidFile{sPIDFile.c_str(), std::ios::out};
344 pidFile<<getpid()<<std::endl;
352 if(!sUUIDFile.empty())
354 std::ofstream uuidFile{sUUIDFile.c_str(), std::ios::out};
358 uuidFile<<uuidBuf<<std::endl;
370 struct sigaction action;
372 memset(&action,0,
sizeof(action));
374 action.sa_handler = sighandler;
376 sigaction(SIGINT,&action,
nullptr);
377 sigaction(SIGQUIT,&action,
nullptr);
394 catch(
const std::exception & exp)
Main(const std::string &sName)
virtual const char * type() const
Exception base class that allows for type and description information.
virtual void doDestroy()=0
virtual void doConfigure(const std::string &)=0
An instance of the EMANE logger. Provides methods for logger configuration and logging.
const char * what() const
virtual std::vector< std::string > doGetOptionsUsage() const
virtual std::vector< option > doGetOptions() const
virtual const uuid_t & getUUID() const
int main(int argc, char *argv[])
virtual std::string doGetOptString() const
virtual bool doProcessOption(int iOptOpt, const char *pzOptArg)
std::int32_t toINT32(std::int32_t i32Min=std::numeric_limits< std::int32_t >::min(), std::int32_t i32Max=std::numeric_limits< std::int32_t >::max()) const
Parameter conversion class with range checks.