handleBaseCmdLineArgs() refactor

This commit is contained in:
Ryan Tharp 2018-07-26 03:52:23 -07:00
parent 554834425d
commit bd229be656
3 changed files with 58 additions and 2 deletions

View File

@ -1,8 +1,6 @@
#include <getopt.h>
#include <llarp.h>
#include <llarp/logger.h>
#include <signal.h>
#include <sys/param.h> // for MIN
#include <string>
struct llarp_main *ctx = 0;
@ -23,6 +21,8 @@ main(int argc, char *argv[])
{
multiThreaded = false;
}
const char *conffname = handleBaseCmdLineArgs(argc, argv);
/*
const char *conffname = "daemon.ini";
int c;
while(1)
@ -67,6 +67,7 @@ main(int argc, char *argv[])
abort();
}
}
*/
ctx = llarp_main_init(conffname, multiThreaded);
int code = 1;

View File

@ -78,6 +78,9 @@ extern "C"
void
llarp_main_free(struct llarp_main *ptr);
const char *
handleBaseCmdLineArgs(int argc, char *argv[]);
#ifdef __cplusplus
}
#endif

View File

@ -5,6 +5,8 @@
#include "logger.hpp"
#include "math.h"
#include "router.hpp"
#include <getopt.h>
#include <sys/param.h> // for MIN
#if(__FreeBSD__) || (__OpenBSD__) || (__NetBSD__)
#include <pthread_np.h>
@ -436,3 +438,53 @@ llarp_main_free(struct llarp_main *ptr)
{
delete ptr;
}
const char *
handleBaseCmdLineArgs(int argc, char *argv[])
{
const char *conffname = "daemon.ini";
int c;
while(1)
{
static struct option long_options[] = {
{"config", required_argument, 0, 'c'},
{"logLevel", required_argument, 0, 'o'},
{0, 0, 0, 0}};
int option_index = 0;
c = getopt_long(argc, argv, "c:o:", long_options, &option_index);
if(c == -1)
break;
switch(c)
{
case 0:
break;
case 'c':
conffname = optarg;
break;
case 'o':
if(strncmp(optarg, "debug", MIN(strlen(optarg), (unsigned long)5)) == 0)
{
cSetLogLevel(eLogDebug);
}
else if(strncmp(optarg, "info", MIN(strlen(optarg), (unsigned long)4))
== 0)
{
cSetLogLevel(eLogInfo);
}
else if(strncmp(optarg, "warn", MIN(strlen(optarg), (unsigned long)4))
== 0)
{
cSetLogLevel(eLogWarn);
}
else if(strncmp(optarg, "error", MIN(strlen(optarg), (unsigned long)5))
== 0)
{
cSetLogLevel(eLogError);
}
break;
default:
abort();
}
}
return conffname;
}