gsm/gsm48_ie: Fix frequency list decoding
[osmocom-bb.git] / src / vty / logging_vty.c
index b51be7d..b037a5b 100644 (file)
@@ -24,9 +24,9 @@
 
 #include "../../config.h"
 
-#include <osmocore/talloc.h>
-#include <osmocore/logging.h>
-#include <osmocore/utils.h>
+#include <osmocom/core/talloc.h>
+#include <osmocom/core/logging.h>
+#include <osmocom/core/utils.h>
 
 //#include <openbsc/vty.h>
 
@@ -85,6 +85,21 @@ DEFUN(enable_logging,
        return CMD_SUCCESS;
 }
 
+struct log_target *osmo_log_vty2tgt(struct vty *vty)
+{
+       struct telnet_connection *conn;
+
+       if (vty->node == CFG_LOG_NODE)
+               return vty->index;
+
+
+       conn = (struct telnet_connection *) vty->priv;
+       if (!conn->dbg)
+               vty_out(vty, "Logging was not enabled.%s", VTY_NEWLINE);
+
+       return conn->dbg;
+}
+
 DEFUN(logging_fltr_all,
       logging_fltr_all_cmd,
       "logging filter all (0|1)",
@@ -93,15 +108,12 @@ DEFUN(logging_fltr_all,
        "Only print messages matched by other filters\n"
        "Bypass filter and print all messages\n")
 {
-       struct telnet_connection *conn;
+       struct log_target *tgt = osmo_log_vty2tgt(vty);
 
-       conn = (struct telnet_connection *) vty->priv;
-       if (!conn->dbg) {
-               vty_out(vty, "Logging was not enabled.%s", VTY_NEWLINE);
+       if (!tgt)
                return CMD_WARNING;
-       }
 
-       log_set_all_filter(conn->dbg, atoi(argv[0]));
+       log_set_all_filter(tgt, atoi(argv[0]));
        return CMD_SUCCESS;
 }
 
@@ -112,15 +124,12 @@ DEFUN(logging_use_clr,
       "Don't use color for printing messages\n"
       "Use color for printing messages\n")
 {
-       struct telnet_connection *conn;
+       struct log_target *tgt = osmo_log_vty2tgt(vty);
 
-       conn = (struct telnet_connection *) vty->priv;
-       if (!conn->dbg) {
-               vty_out(vty, "Logging was not enabled.%s", VTY_NEWLINE);
+       if (!tgt)
                return CMD_WARNING;
-       }
 
-       log_set_use_color(conn->dbg, atoi(argv[0]));
+       log_set_use_color(tgt, atoi(argv[0]));
        return CMD_SUCCESS;
 }
 
@@ -131,122 +140,61 @@ DEFUN(logging_prnt_timestamp,
        "Don't prefix each log message\n"
        "Prefix each log message with current timestamp\n")
 {
-       struct telnet_connection *conn;
+       struct log_target *tgt = osmo_log_vty2tgt(vty);
 
-       conn = (struct telnet_connection *) vty->priv;
-       if (!conn->dbg) {
-               vty_out(vty, "Logging was not enabled.%s", VTY_NEWLINE);
+       if (!tgt)
                return CMD_WARNING;
-       }
 
-       log_set_print_timestamp(conn->dbg, atoi(argv[0]));
+       log_set_print_timestamp(tgt, atoi(argv[0]));
        return CMD_SUCCESS;
 }
 
-/* FIXME: those have to be kept in sync with the log levels and categories */
-#define VTY_DEBUG_CATEGORIES "(rll|cc|mm|rr|rsl|nm|sms|pag|mncc|inp|mi|mib|mux|meas|sccp|msc|mgcp|ho|db|ref|gprs|ns|bssgp|llc|sndcp|isup|m2ua|pcap|all)"
-#define CATEGORIES_HELP        \
-       "A-bis Radio Link Layer (RLL)\n"                        \
-       "Layer3 Call Control (CC)\n"                            \
-       "Layer3 Mobility Management (MM)\n"                     \
-       "Layer3 Radio Resource (RR)\n"                          \
-       "A-bis Radio Signalling Link (RSL)\n"                   \
-       "A-bis Network Management / O&M (NM/OML)\n"             \
-       "Layer3 Short Messagaging Service (SMS)\n"              \
-       "Paging Subsystem\n"                                    \
-       "MNCC API for Call Control application\n"               \
-       "A-bis Input Subsystem\n"                               \
-       "A-bis Input Driver for Signalling\n"                   \
-       "A-bis Input Driver for B-Channel (voice data)\n"       \
-       "A-bis B-Channel / Sub-channel Multiplexer\n"           \
-       "Radio Measurements\n"                                  \
-       "SCCP\n"                                                \
-       "Mobile Switching Center\n"                             \
-       "Media Gateway Control Protocol\n"                      \
-       "Hand-over\n"                                           \
-       "Database Layer\n"                                      \
-       "Reference Counting\n"                                  \
-       "GPRS Core\n"                                           \
-       "GPRS Network Service (NS)\n"                           \
-       "GPRS BSS Gateway Protocol (BSSGP)\n"                   \
-       "GPRS Logical Link Control Protocol (LLC)\n"            \
-       "GPRS Sub-Network Dependent Control Protocol (SNDCP)\n" \
-       "ISDN User Part (ISUP)\n"                               \
-       "SCTP M2UA\n"                                           \
-       "Trace message IO\n"                                    \
-       "Global setting for all subsytems\n"
-
-#define VTY_DEBUG_LEVELS "(everything|debug|info|notice|error|fatal)"
-#define LEVELS_HELP    \
-       "Log simply everything\n"                               \
-       "Log debug messages and higher levels\n"                \
-       "Log informational messages and higher levels\n"        \
-       "Log noticable messages and higher levels\n"            \
-       "Log error messages and higher levels\n"                \
-       "Log only fatal messages\n"
-
-static int _logging_level(struct vty *vty, struct log_target *dbg,
-                         const char *cat_str, const char *lvl_str)
+DEFUN(logging_level,
+      logging_level_cmd,
+      NULL, /* cmdstr is dynamically set in logging_vty_add_cmds(). */
+      NULL) /* same thing for helpstr. */
 {
-       int category = log_parse_category(cat_str);
-       int level = log_parse_level(lvl_str);
+       int category = log_parse_category(argv[0]);
+       int level = log_parse_level(argv[1]);
+       struct log_target *tgt = osmo_log_vty2tgt(vty);
+
+       if (!tgt)
+               return CMD_WARNING;
 
        if (level < 0) {
-               vty_out(vty, "Invalid level `%s'%s", lvl_str, VTY_NEWLINE);
+               vty_out(vty, "Invalid level `%s'%s", argv[1], VTY_NEWLINE);
                return CMD_WARNING;
        }
 
        /* Check for special case where we want to set global log level */
-       if (!strcmp(cat_str, "all")) {
-               log_set_log_level(dbg, level);
+       if (!strcmp(argv[0], "all")) {
+               log_set_log_level(tgt, level);
                return CMD_SUCCESS;
        }
 
        if (category < 0) {
-               vty_out(vty, "Invalid category `%s'%s", cat_str, VTY_NEWLINE);
+               vty_out(vty, "Invalid category `%s'%s", argv[0], VTY_NEWLINE);
                return CMD_WARNING;
        }
 
-       dbg->categories[category].enabled = 1;
-       dbg->categories[category].loglevel = level;
+       tgt->categories[category].enabled = 1;
+       tgt->categories[category].loglevel = level;
 
        return CMD_SUCCESS;
 }
 
-DEFUN(logging_level,
-      logging_level_cmd,
-      "logging level " VTY_DEBUG_CATEGORIES " " VTY_DEBUG_LEVELS,
-      LOGGING_STR
-      "Set the log level for a specified category\n"
-      CATEGORIES_HELP
-      LEVELS_HELP)
-{
-       struct telnet_connection *conn;
-
-       conn = (struct telnet_connection *) vty->priv;
-       if (!conn->dbg) {
-               vty_out(vty, "Logging was not enabled.%s", VTY_NEWLINE);
-               return CMD_WARNING;
-       }
-
-       return _logging_level(vty, conn->dbg, argv[0], argv[1]);
-}
-
 DEFUN(logging_set_category_mask,
       logging_set_category_mask_cmd,
       "logging set log mask MASK",
        LOGGING_STR
       "Decide which categories to output.\n")
 {
-       struct telnet_connection *conn;
+       struct log_target *tgt = osmo_log_vty2tgt(vty);
 
-       conn = (struct telnet_connection *) vty->priv;
-       if (!conn->dbg) {
-               vty_out(vty, "Logging was not enabled.%s", VTY_NEWLINE);
+       if (!tgt)
                return CMD_WARNING;
-       }
 
-       log_parse_category_mask(conn->dbg, argv[0]);
+       log_parse_category_mask(tgt, argv[0]);
        return CMD_SUCCESS;
 }
 
@@ -256,17 +204,16 @@ DEFUN(diable_logging,
        LOGGING_STR
       "Disables logging to this vty\n")
 {
-       struct telnet_connection *conn;
+       struct log_target *tgt = osmo_log_vty2tgt(vty);
+       struct telnet_connection *conn = (struct telnet_connection *) vty->priv;
 
-       conn = (struct telnet_connection *) vty->priv;
-       if (!conn->dbg) {
-               vty_out(vty, "Logging was not enabled.%s", VTY_NEWLINE);
+       if (!tgt)
                return CMD_WARNING;
-       }
 
-       log_del_target(conn->dbg);
-       talloc_free(conn->dbg);
+       log_del_target(tgt);
+       talloc_free(tgt);
        conn->dbg = NULL;
+
        return CMD_SUCCESS;
 }
 
@@ -301,14 +248,12 @@ DEFUN(show_logging_vty,
        SHOW_STR SHOW_LOG_STR
        "Show current logging configuration for this vty\n")
 {
-       struct telnet_connection *conn;
+       struct log_target *tgt = osmo_log_vty2tgt(vty);
 
-       conn = (struct telnet_connection *) vty->priv;
-       if (!conn->dbg) {
-               vty_out(vty, "Logging was not enabled.%s", VTY_NEWLINE);
+       if (!tgt)
                return CMD_WARNING;
-       }
-       vty_print_logtarget(vty, osmo_log_info, conn->dbg);
+
+       vty_print_logtarget(vty, osmo_log_info, tgt);
 
        return CMD_SUCCESS;
 }
@@ -324,6 +269,8 @@ gDEFUN(cfg_description, cfg_description_cmd,
                return CMD_WARNING;
        }
 
+       if (*dptr)
+               talloc_free(*dptr);
        *dptr = argv_concat(argv, argc, 0);
        if (!dptr)
                return CMD_WARNING;
@@ -359,59 +306,6 @@ struct cmd_node cfg_log_node = {
        1
 };
 
-DEFUN(cfg_log_fltr_all,
-      cfg_log_fltr_all_cmd,
-      "logging filter all (0|1)",
-       LOGGING_STR FILTER_STR
-       "Do you want to log all messages?\n"
-       "Only print messages matched by other filters\n"
-       "Bypass filter and print all messages\n")
-{
-       struct log_target *dbg = vty->index;
-
-       log_set_all_filter(dbg, atoi(argv[0]));
-       return CMD_SUCCESS;
-}
-
-DEFUN(cfg_log_use_clr,
-      cfg_log_use_clr_cmd,
-      "logging color (0|1)",
-       LOGGING_STR "Configure color-printing for log messages\n"
-      "Don't use color for printing messages\n"
-      "Use color for printing messages\n")
-{
-       struct log_target *dbg = vty->index;
-
-       log_set_use_color(dbg, atoi(argv[0]));
-       return CMD_SUCCESS;
-}
-
-DEFUN(cfg_log_timestamp,
-      cfg_log_timestamp_cmd,
-      "logging timestamp (0|1)",
-       LOGGING_STR "Configure log message timestamping\n"
-       "Don't prefix each log message\n"
-       "Prefix each log message with current timestamp\n")
-{
-       struct log_target *dbg = vty->index;
-
-       log_set_print_timestamp(dbg, atoi(argv[0]));
-       return CMD_SUCCESS;
-}
-
-DEFUN(cfg_log_level,
-      cfg_log_level_cmd,
-      "logging level " VTY_DEBUG_CATEGORIES " " VTY_DEBUG_LEVELS,
-      LOGGING_STR
-      "Set the log level for a specified category\n"
-      CATEGORIES_HELP
-      LEVELS_HELP)
-{
-       struct log_target *dbg = vty->index;
-
-       return _logging_level(vty, dbg, argv[0], argv[1]);
-}
-
 #ifdef HAVE_SYSLOG_H
 
 #include <syslog.h>
@@ -427,6 +321,9 @@ static const int local_sysl_map[] = {
        [7] = LOG_LOCAL7
 };
 
+/* From VTY core code */
+extern struct host host;
+
 static int _cfg_log_syslog(struct vty *vty, int facility)
 {
        struct log_target *tgt;
@@ -436,7 +333,7 @@ static int _cfg_log_syslog(struct vty *vty, int facility)
        if (tgt)
                log_target_destroy(tgt);
 
-       tgt = log_target_create_syslog("FIXME", 0, facility);
+       tgt = log_target_create_syslog(host.app_info->name, 0, facility);
        if (!tgt) {
                vty_out(vty, "%% Unable to open syslog%s", VTY_NEWLINE);
                return CMD_WARNING;
@@ -655,7 +552,7 @@ static int config_write_log(struct vty *vty)
        return 1;
 }
 
-void logging_vty_add_cmds()
+void logging_vty_add_cmds(const struct log_info *cat)
 {
        install_element_ve(&enable_logging_cmd);
        install_element_ve(&disable_logging_cmd);
@@ -663,14 +560,18 @@ void logging_vty_add_cmds()
        install_element_ve(&logging_use_clr_cmd);
        install_element_ve(&logging_prnt_timestamp_cmd);
        install_element_ve(&logging_set_category_mask_cmd);
+
+       /* Logging level strings are generated dynamically. */
+       logging_level_cmd.string = log_vty_command_string(cat);
+       logging_level_cmd.doc = log_vty_command_description(cat);
        install_element_ve(&logging_level_cmd);
        install_element_ve(&show_logging_vty_cmd);
 
        install_node(&cfg_log_node, config_write_log);
-       install_element(CFG_LOG_NODE, &cfg_log_fltr_all_cmd);
-       install_element(CFG_LOG_NODE, &cfg_log_use_clr_cmd);
-       install_element(CFG_LOG_NODE, &cfg_log_timestamp_cmd);
-       install_element(CFG_LOG_NODE, &cfg_log_level_cmd);
+       install_element(CFG_LOG_NODE, &logging_fltr_all_cmd);
+       install_element(CFG_LOG_NODE, &logging_use_clr_cmd);
+       install_element(CFG_LOG_NODE, &logging_prnt_timestamp_cmd);
+       install_element(CFG_LOG_NODE, &logging_level_cmd);
 
        install_element(CONFIG_NODE, &cfg_log_stderr_cmd);
        install_element(CONFIG_NODE, &cfg_no_log_stderr_cmd);