open5gs/lib/app/ogs-init.c

135 lines
3.7 KiB
C
Raw Normal View History

/*
* Copyright (C) 2019 by Sukchan Lee <acetcom@gmail.com>
*
* This file is part of Open5GS.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
2019-09-13 12:07:47 +00:00
#include "ogs-app.h"
2019-04-27 14:54:30 +00:00
2019-09-13 12:07:47 +00:00
int __ogs_app_domain;
2019-04-27 14:54:30 +00:00
int ogs_app_initialize(const char *default_config, const char *const argv[])
2019-04-27 14:54:30 +00:00
{
int rv, opt;
ogs_getopt_t options;
struct {
char *config_file;
char *log_file;
char *log_level;
char *domain_mask;
bool enable_debug;
bool enable_trace;
} optarg;
2019-04-27 14:54:30 +00:00
2019-09-13 12:07:47 +00:00
ogs_core_initialize();
ogs_app_setup_log();
ogs_config_init();
2019-04-27 14:54:30 +00:00
/**************************************************************************
* Stage 1 : Command Line Options
*/
memset(&optarg, 0, sizeof(optarg));
ogs_getopt_init(&options, (char**)argv);
while ((opt = ogs_getopt(&options, "c:l:e:m:")) != -1) {
switch (opt) {
case 'c':
optarg.config_file = options.optarg;
break;
case 'l':
optarg.log_file = options.optarg;
break;
case 'e':
optarg.log_level = options.optarg;
break;
case 'm':
optarg.domain_mask = options.optarg;
break;
case '?':
default:
ogs_assert_if_reached();
return OGS_ERROR;
}
2019-04-27 14:54:30 +00:00
}
/**************************************************************************
* Stage 2 : Load Configuration File
*/
if (optarg.config_file)
2019-09-13 12:07:47 +00:00
ogs_config()->file = optarg.config_file;
else
2019-09-13 12:07:47 +00:00
ogs_config()->file = default_config;
2019-04-27 14:54:30 +00:00
2019-09-13 12:07:47 +00:00
rv = ogs_config_read();
2019-04-27 14:54:30 +00:00
if (rv != OGS_OK) return rv;
2019-09-13 12:07:47 +00:00
rv = ogs_config_parse();
2019-04-27 14:54:30 +00:00
if (rv != OGS_OK) return rv;
/**************************************************************************
* Stage 3 : Initialize Default Memory Pool
*/
2019-09-13 12:07:47 +00:00
ogs_pkbuf_default_create(&ogs_config()->pool.defconfig);
/**************************************************************************
* Stage 4 : Setup LOG Module
*/
if (optarg.log_file)
2019-09-13 12:07:47 +00:00
ogs_config()->logger.file = optarg.log_file;
2019-04-27 14:54:30 +00:00
2019-09-13 12:07:47 +00:00
if (ogs_config()->logger.file) {
if (ogs_log_add_file(ogs_config()->logger.file) == NULL) {
ogs_fatal("cannot open log file : %s",
2019-09-13 12:07:47 +00:00
ogs_config()->logger.file);
2019-04-27 14:54:30 +00:00
return OGS_ERROR;
}
}
if (optarg.domain_mask)
2019-09-13 12:07:47 +00:00
ogs_config()->logger.domain = optarg.domain_mask;
2019-04-27 14:54:30 +00:00
if (optarg.log_level)
2019-09-13 12:07:47 +00:00
ogs_config()->logger.level = optarg.log_level;
2019-04-27 14:54:30 +00:00
rv = ogs_log_config_domain(
2019-09-13 12:07:47 +00:00
ogs_config()->logger.domain, ogs_config()->logger.level);
2019-04-27 14:54:30 +00:00
if (rv != OGS_OK) return rv;
/**************************************************************************
2019-09-13 12:07:47 +00:00
* Stage 5 : Setup Database Module
*/
if (ogs_env_get("DB_URI"))
2019-09-13 12:07:47 +00:00
ogs_config()->db_uri = ogs_env_get("DB_URI");
2019-04-27 14:54:30 +00:00
return rv;
}
2019-09-13 12:07:47 +00:00
void ogs_app_terminate(void)
2019-04-27 14:54:30 +00:00
{
2019-09-13 12:07:47 +00:00
ogs_config_final();
2019-04-27 14:54:30 +00:00
2019-09-13 12:07:47 +00:00
ogs_pkbuf_default_destroy();
2019-04-27 14:54:30 +00:00
2019-09-13 12:07:47 +00:00
ogs_core_terminate();
2019-04-27 14:54:30 +00:00
}
2019-09-13 12:07:47 +00:00
void ogs_app_setup_log(void)
2019-04-27 14:54:30 +00:00
{
2019-09-13 12:07:47 +00:00
ogs_log_install_domain(&__ogs_app_domain, "app", ogs_core()->log.level);
2019-04-27 14:54:30 +00:00
}