forked from lei.chen/myxce
update controller code
This commit is contained in:
parent
2836d196da
commit
41f56fa6ed
54 changed files with 9688 additions and 214 deletions
162
src/controller/main.c
Normal file
162
src/controller/main.c
Normal file
|
|
@ -0,0 +1,162 @@
|
|||
#include <argp.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <tlog.h>
|
||||
|
||||
#include "myconf.h"
|
||||
#include "utils.h"
|
||||
|
||||
extern int myapp_run(myconf_t*);
|
||||
|
||||
static char doc[] = "xce_controller";
|
||||
static char args_doc[] = "";
|
||||
const char* argp_program_version = MK_VERSION("xce v0.0.1-", STR(VERSION_COMMIT_ID));
|
||||
const char* argp_program_bug_address = BUG_EMAIL;
|
||||
static char* default_config = "/tmp/xce/xce.yaml";
|
||||
|
||||
struct arguments {
|
||||
char* config;
|
||||
char* logdir;
|
||||
char* clusterconf;
|
||||
int verbose;
|
||||
};
|
||||
|
||||
static struct argp_option options[] = {
|
||||
{ "config", 'c', "FILE", 0, "Load initial configuration from FILE" },
|
||||
{ "logdir", 'd', "DIR", 0, "Directory for log file" },
|
||||
{ "verbose", 'v', 0, 0, "Produce verbose logs" },
|
||||
{ "clusterconf", 'f', "FILE", 0, "Load Cluster Configuration from FILE" },
|
||||
{ 0 }
|
||||
};
|
||||
|
||||
static error_t parse_opt(int key,
|
||||
char* arg,
|
||||
struct argp_state* state)
|
||||
{
|
||||
struct arguments* arguments = state->input;
|
||||
|
||||
switch (key) {
|
||||
case 'c':
|
||||
arguments->config = arg;
|
||||
break;
|
||||
case 'd':
|
||||
arguments->logdir = arg;
|
||||
break;
|
||||
case 'v':
|
||||
arguments->verbose++;
|
||||
break;
|
||||
case 'f':
|
||||
arguments->clusterconf = arg;
|
||||
break;
|
||||
default:
|
||||
return ARGP_ERR_UNKNOWN;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static struct argp argp = {
|
||||
options,
|
||||
parse_opt,
|
||||
args_doc,
|
||||
doc
|
||||
};
|
||||
|
||||
static char* verbose_to_loglevel(int verbose)
|
||||
{
|
||||
char* loglevel = "info";
|
||||
|
||||
switch (verbose) {
|
||||
case 1:
|
||||
loglevel = "debug";
|
||||
break;
|
||||
case 2:
|
||||
loglevel = "debug2";
|
||||
break;
|
||||
case 3:
|
||||
loglevel = "debug3";
|
||||
break;
|
||||
default:
|
||||
if (verbose > 3) {
|
||||
loglevel = "debug3";
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
return loglevel;
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
struct arguments arguments;
|
||||
initial_config_t* init_config;
|
||||
myconf_t myconf;
|
||||
char* logdir = "/tmp";
|
||||
char* loglevel = "info";
|
||||
int logsize = 64 * 1024 * 1024;
|
||||
int logcount = 10;
|
||||
char* myname = "xce_controller";
|
||||
|
||||
memset(&arguments, 0x0, sizeof(arguments));
|
||||
|
||||
argp_parse(&argp, argc, argv, 0, 0, &arguments);
|
||||
|
||||
if (arguments.clusterconf == NULL) {
|
||||
tlog_error("no cluster configuration specified. please use -f to specify cluster configuration file");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (arguments.config == NULL) {
|
||||
arguments.config = default_config;
|
||||
}
|
||||
|
||||
if (arguments.logdir != NULL) {
|
||||
logdir = arguments.logdir;
|
||||
}
|
||||
|
||||
if (arguments.verbose != 0) {
|
||||
loglevel = verbose_to_loglevel(arguments.verbose);
|
||||
}
|
||||
|
||||
mylog_init(logdir, myname, loglevel, logsize, logcount);
|
||||
|
||||
load_initial_config(arguments.config,
|
||||
&init_config,
|
||||
arguments.verbose);
|
||||
|
||||
if (init_config == NULL) {
|
||||
tlog_error("Fail to read initial log");
|
||||
return -1;
|
||||
}
|
||||
myconf.init_config = init_config;
|
||||
myconf.cluster_config = NULL;
|
||||
myconf.conffile = arguments.clusterconf;
|
||||
|
||||
if (init_config != NULL) {
|
||||
if (init_config->logging != NULL) {
|
||||
if ((init_config->logging->logdir != NULL) && (arguments.logdir == NULL)) {
|
||||
char logfile[XCE_MAXPATH_LEN + 1];
|
||||
|
||||
logdir = init_config->logging->logdir;
|
||||
snprintf(logfile, sizeof(logfile) - 1,
|
||||
"%s/%s.log", logdir, myname);
|
||||
|
||||
tlog_set_logfile(logfile);
|
||||
}
|
||||
|
||||
if ((init_config->logging->loglevel != NULL) && (arguments.verbose == 0)) {
|
||||
loglevel = init_config->logging->loglevel;
|
||||
|
||||
mylog_setlevel(loglevel);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
myapp_run(&myconf);
|
||||
|
||||
free_initial_config(init_config);
|
||||
mylog_fini();
|
||||
|
||||
return 0;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue