From 63c0e6d1996733d107434585fbef5e888b3075ed Mon Sep 17 00:00:00 2001 From: Holger Hans Peter Freyther Date: Mon, 27 Dec 2010 08:58:57 +0100 Subject: [PATCH] layer23: Make the logfile configurable with cell_log Be able to add extra (short) options from the 'applet' to the main application. Use this to print the help mentioning app specific options, pass the getopt string and handle the command line parsing for it. Change cell_log to keep the logname in the app_cell_log.c and then access it from the cell_log.c implementation. --- .../include/osmocom/bb/common/l23_app.h | 3 ++ src/host/layer23/src/common/main.c | 51 ++++++++++++++----- src/host/layer23/src/misc/app_cell_log.c | 26 ++++++++++ src/host/layer23/src/misc/cell_log.c | 2 +- 4 files changed, 69 insertions(+), 13 deletions(-) diff --git a/src/host/layer23/include/osmocom/bb/common/l23_app.h b/src/host/layer23/include/osmocom/bb/common/l23_app.h index 46141e1..fcfd4bd 100644 --- a/src/host/layer23/include/osmocom/bb/common/l23_app.h +++ b/src/host/layer23/include/osmocom/bb/common/l23_app.h @@ -21,7 +21,10 @@ struct l23_app_info { const char *copyright; const char *contribution; + char *getopt_string; int (*cfg_supported)(); + int (*cfg_print_help)(); + int (*cfg_handle_opt)(int c,const char *optarg); }; extern struct l23_app_info *l23_app_info(); diff --git a/src/host/layer23/src/common/main.c b/src/host/layer23/src/common/main.c index 1f9afaf..a791ccf 100644 --- a/src/host/layer23/src/common/main.c +++ b/src/host/layer23/src/common/main.c @@ -35,6 +35,7 @@ #include #include #include +#include #include @@ -101,25 +102,46 @@ static void print_help() if (options & L23_OPT_DBG) printf(" -d --debug Change debug flags.\n"); + + if (app && app->cfg_print_help) + app->cfg_print_help(); +} + +static void build_config(char **opt, struct option **option) +{ + struct l23_app_info *app; + static struct option long_options[] = { + {"help", 0, 0, 'h'}, + {"socket", 1, 0, 's'}, + {"sap", 1, 0, 'S'}, + {"arfcn", 1, 0, 'a'}, + {"gsmtap-ip", 1, 0, 'i'}, + {"vty-port", 1, 0, 'v'}, + {"debug", 1, 0, 'd'}, + }; + + + app = l23_app_info(); + *opt = talloc_asprintf(l23_ctx, "hs:S:a:i:v:d:%s", + app && app->getopt_string ? app->getopt_string : ""); + *option = talloc_zero_array(l23_ctx, struct option, + ARRAY_SIZE(long_options) + 1); + memcpy(*option, long_options, sizeof(long_options)); } static void handle_options(int argc, char **argv) { struct sockaddr_in gsmtap; + struct l23_app_info *app = l23_app_info(); + struct option *long_options; + char *opt; + + build_config(&opt, &long_options); + while (1) { int option_index = 0, c; - static struct option long_options[] = { - {"help", 0, 0, 'h'}, - {"socket", 1, 0, 's'}, - {"sap", 1, 0, 'S'}, - {"arfcn", 1, 0, 'a'}, - {"gsmtap-ip", 1, 0, 'i'}, - {"vty-port", 1, 0, 'v'}, - {"debug", 1, 0, 'd'}, - {0, 0, 0, 0}, - }; - - c = getopt_long(argc, argv, "hs:S:a:i:v:d:", + + c = getopt_long(argc, argv, opt, long_options, &option_index); if (c == -1) break; @@ -153,9 +175,14 @@ static void handle_options(int argc, char **argv) log_parse_category_mask(stderr_target, optarg); break; default: + if (app && app->cfg_handle_opt) + app->cfg_handle_opt(c, optarg); break; } } + + talloc_free(opt); + talloc_free(long_options); } void sighandler(int sigset) diff --git a/src/host/layer23/src/misc/app_cell_log.c b/src/host/layer23/src/misc/app_cell_log.c index a72588f..e80a7b2 100644 --- a/src/host/layer23/src/misc/app_cell_log.c +++ b/src/host/layer23/src/misc/app_cell_log.c @@ -30,7 +30,12 @@ #include #include +#include + extern struct log_target *stderr_target; +extern void *l23_ctx; + +char *logname = "/var/log/osmocom.log"; int _scan_work(struct osmocom_ms *ms) { @@ -77,9 +82,30 @@ static int l23_cfg_supported() return L23_OPT_TAP | L23_OPT_DBG; } +static int l23_cfg_print_help() +{ + printf("\nApplication specific\n"); + printf(" -l --logfile LOGFILE Logfile for the cell log.\n"); + return 0; +} + +static int l23_cfg_handle(int c, const char *optarg) +{ + switch (c) { + case 'l': + logname = talloc_strdup(l23_ctx, optarg); + break; + } + + return 0; +} + static struct l23_app_info info = { .copyright = "Copyright (C) 2010 Andreas Eversberg\n", + .getopt_string = "l:", .cfg_supported = l23_cfg_supported, + .cfg_handle_opt = l23_cfg_handle, + .cfg_print_help = l23_cfg_print_help, }; struct l23_app_info *l23_app_info() diff --git a/src/host/layer23/src/misc/cell_log.c b/src/host/layer23/src/misc/cell_log.c index 1034ffa..ce68ef0 100644 --- a/src/host/layer23/src/misc/cell_log.c +++ b/src/host/layer23/src/misc/cell_log.c @@ -88,7 +88,7 @@ static double pm_gps_x, pm_gps_y, pm_gps_z; static int arfcn; static int rach_count; static FILE *logfp = NULL; -static char *logname = "/var/log/osmocom.log"; +extern char *logname; static struct gsm48_sysinfo sysinfo; -- 2.20.1