This commit is contained in:
Andreas Eversberg 2024-01-24 21:04:22 +01:00
parent e39db4cffd
commit 71cc533e8e
5 changed files with 46 additions and 35 deletions

View File

@ -6,6 +6,11 @@
/* All logging categories used by this project. */
struct log_info_cat log_categories[] = {
[DLCC] = {
.name = "DLCC",
.description = "libosmo-cc CC Layer",
.color = "\033[0;37m",
},
[DOPTIONS] = {
.name = "DOPTIONS",
.description = "config options",

View File

@ -1,5 +1,6 @@
enum {
DLCC,
DOPTIONS,
DJITTER,
DISDN,

View File

@ -22,12 +22,11 @@
#include <errno.h>
#include <osmocom/core/utils.h>
#include <osmocom/core/application.h>
#include <osmocom/cc/misc.h>
#include "logging.h"
int loglevel = LOGL_INFO;
extern int num_kanal;
static int scroll_window_start = 0;
static int scroll_window_end = 0;
static int scroll_window_height = 0;
@ -171,14 +170,14 @@ static void list_cat(void)
printf("\n");
printf("Give name(s) of debug category:\n");
for (i = 0; i < (int)osmo_log_info->num_cat; i++) {
if (!osmo_log_info->cat[i].name)
for (i = 0; i < (int)log_categories_size; i++) {
if (!log_categories[i].name)
continue;
printf(" ");
if (osmo_log_info->cat[i].color)
printf("%s", osmo_log_info->cat[i].color);
if (osmo_log_info->cat[i].name)
printf("%s\033[0;39m = %s\n", osmo_log_info->cat[i].name, osmo_log_info->cat[i].description);
if (log_categories[i].color)
printf("%s", log_categories[i].color);
if (log_categories[i].name)
printf("%s\033[0;39m = %s\n", log_categories[i].name, log_categories[i].description);
}
printf("\n");
}
@ -218,27 +217,22 @@ int parse_logging_opt(const char *optarg)
return -EINVAL;
}
/* Set loglevel and enable all categories, if dstring is not set. Else set loglevel and disable all categories. */
for (i = 0; i < (int)osmo_log_info->num_cat; i++) {
if (!osmo_stderr_target->categories[i].loglevel)
continue;
osmo_stderr_target->categories[i].loglevel = loglevel;
osmo_stderr_target->categories[i].enabled = (!dstring);
}
for (i = 0; i < (int)log_categories_size; i++)
log_set_category_filter(osmo_stderr_target, i, (!dstring), loglevel);
/* Enable each given category. */
while((p = strsep(&dstring, ","))) {
for (i = 0; i < (int)osmo_log_info->num_cat; i++) {
for (i = 0; i < (int)log_categories_size; i++) {
if (!log_category_name(i))
continue;
if (!strcasecmp(p, log_category_name(i)))
break;
}
if (i == (int)osmo_log_info->num_cat) {
if (i == (int)log_categories_size) {
fprintf(stderr, "Given logging category '%s' unknown, use '-v list' to show available categories!\n", p);
free(dup);
return -EINVAL;
}
osmo_stderr_target->categories[i].loglevel = loglevel;
osmo_stderr_target->categories[i].enabled = 1;
log_set_category_filter(osmo_stderr_target, i, 1, loglevel);
}
free(dup);
@ -255,6 +249,8 @@ void logging_init(void)
.num_cat = log_categories_size,
};
osmo_cc_set_log_cat(DLCC);
osmo_init_logging2(NULL, &log_info);
log_set_print_timestamp(osmo_stderr_target, 0);
log_set_print_level(osmo_stderr_target, 1);
@ -262,11 +258,7 @@ void logging_init(void)
log_set_print_category(osmo_stderr_target, 1);
/* Set loglevel and enable all categories. */
for (i = 0; i < (int)osmo_log_info->num_cat; i++) {
if (!osmo_stderr_target->categories[i].loglevel)
continue;
osmo_stderr_target->categories[i].loglevel = loglevel;
osmo_stderr_target->categories[i].enabled = 1;
}
for (i = 0; i < (int)log_categories_size; i++)
log_set_category_filter(osmo_stderr_target, i, 1, loglevel);
}

View File

@ -25,6 +25,8 @@
#include "options.h"
#include "../liblogging/logging.h"
const char *selected_config_file = NULL;
typedef struct option {
struct option *next;
int short_option;
@ -62,11 +64,11 @@ void option_add(int short_option, const char *long_option, int parameter_count)
option_t *option;
/* check if option already exists or is not allowed */
if (!strcmp(long_option, "config") || !strcmp(long_option, "no-config")) {
LOGP(DOPTIONS, LOGL_ERROR, "Option '%s' is not allowed to add, please fix!\n", option->long_option);
abort();
}
for (option = option_head; option; option = option->next) {
if (!strcmp(option->long_option, "config")) {
LOGP(DOPTIONS, LOGL_ERROR, "Option '%s' is not allowed to add, please fix!\n", option->long_option);
abort();
}
if (option->short_option == short_option
|| !strcmp(option->long_option, long_option)) {
LOGP(DOPTIONS, LOGL_ERROR, "Option '%s' added twice, please fix!\n", option->long_option);
@ -87,7 +89,7 @@ void option_add(int short_option, const char *long_option, int parameter_count)
option_tailp = &(option->next);
}
int options_config_file(int argc, char *argv[], const char *config_file, int (*handle_options)(int short_option, int argi, char *argv[]))
int options_config_file(int argc, char *argv[], const char *_config_file, int (*handle_options)(int short_option, int argi, char *argv[]))
{
static const char *home;
char config[256];
@ -101,16 +103,24 @@ int options_config_file(int argc, char *argv[], const char *config_file, int (*h
/* select for alternative config file */
if (argc > 2 && !strcmp(argv[1], "--config"))
config_file = argv[2];
selected_config_file = argv[2];
else
selected_config_file = _config_file;
/* select for alternative config file */
if (argc > 1 && !strcmp(argv[1], "--no-config")) {
selected_config_file = NULL;
return 1;
}
/* add home directory */
if (config_file[0] == '~' && config_file[1] == '/') {
if (selected_config_file[0] == '~' && selected_config_file[1] == '/') {
home = getenv("HOME");
if (home == NULL)
return 1;
sprintf(config, "%s/%s", home, config_file + 2);
sprintf(config, "%s/%s", home, selected_config_file + 2);
} else
strcpy(config, config_file);
strcpy(config, selected_config_file);
/* open config file */
fp = fopen(config, "r");
@ -210,12 +220,12 @@ int options_config_file(int argc, char *argv[], const char *config_file, int (*h
}
}
if (!option) {
LOGP(DOPTIONS, LOGL_ERROR, "Given option '%s' in config file '%s' at line %d is not a valid option, use '-h' for help!\n", opt, config_file, line);
LOGP(DOPTIONS, LOGL_ERROR, "Given option '%s' in config file '%s' at line %d is not a valid option, use '-h' for help!\n", opt, selected_config_file, line);
rc = -EINVAL;
goto done;
}
if (option->parameter_count != i) {
LOGP(DOPTIONS, LOGL_ERROR, "Given option '%s' in config file '%s' at line %d requires %d parameter(s), use '-h' for help!\n", opt, config_file, line, option->parameter_count);
LOGP(DOPTIONS, LOGL_ERROR, "Given option '%s' in config file '%s' at line %d requires %d parameter(s), use '-h' for help!\n", opt, selected_config_file, line, option->parameter_count);
return -EINVAL;
}
rc = handle_options(option->short_option, 0, args);
@ -252,6 +262,8 @@ int options_command_line(int argc, char *argv[], int (*handle_options)(int short
argi += 1;
continue;
}
if (!strcmp(argv[argi], "--no-config"))
continue;
if (argv[argi][0] == '-') {
if (argv[argi][1] != '-') {
if (strlen(argv[argi]) != 2) {

View File

@ -1,4 +1,5 @@
extern const char *selected_config_file;
char *options_strdup(const char *s);
void option_add(int short_option, const char *long_option, int parameter_count);
int options_config_file(int argc, char *argv[], const char *config_file, int (*handle_options)(int short_option, int argi, char *argv[]));