Holger Hans Peter Freyther | b70d45b | 2010-04-06 11:55:37 +0200 | [diff] [blame] | 1 | /* OpenBSC logging helper for the VTY */ |
| 2 | /* (C) 2009-2010 by Harald Welte <laforge@gnumonks.org> |
| 3 | * (C) 2009-2010 by Holger Hans Peter Freyther |
| 4 | * All Rights Reserved |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License along |
| 17 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 18 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | #include <openbsc/vty.h> |
| 23 | #include <openbsc/telnet_interface.h> |
| 24 | |
| 25 | #include <osmocore/talloc.h> |
| 26 | |
| 27 | #include <vty/command.h> |
| 28 | #include <vty/buffer.h> |
| 29 | #include <vty/vty.h> |
| 30 | |
| 31 | #include <stdlib.h> |
| 32 | |
| 33 | static void _vty_output(struct log_target *tgt, const char *line) |
| 34 | { |
| 35 | struct vty *vty = tgt->tgt_vty.vty; |
| 36 | vty_out(vty, "%s", line); |
| 37 | /* This is an ugly hack, but there is no easy way... */ |
| 38 | if (strchr(line, '\n')) |
| 39 | vty_out(vty, "\r"); |
| 40 | } |
| 41 | |
| 42 | struct log_target *log_target_create_vty(struct vty *vty) |
| 43 | { |
| 44 | struct log_target *target; |
| 45 | |
| 46 | target = log_target_create(); |
| 47 | if (!target) |
| 48 | return NULL; |
| 49 | |
| 50 | target->tgt_vty.vty = vty; |
| 51 | target->output = _vty_output; |
| 52 | return target; |
| 53 | } |
| 54 | |
| 55 | DEFUN(enable_logging, |
| 56 | enable_logging_cmd, |
| 57 | "logging enable", |
| 58 | "Enables logging to this vty\n") |
| 59 | { |
| 60 | struct telnet_connection *conn; |
| 61 | |
| 62 | conn = (struct telnet_connection *) vty->priv; |
| 63 | if (conn->dbg) { |
| 64 | vty_out(vty, "Logging already enabled.%s", VTY_NEWLINE); |
| 65 | return CMD_WARNING; |
| 66 | } |
| 67 | |
| 68 | conn->dbg = log_target_create_vty(vty); |
| 69 | if (!conn->dbg) |
| 70 | return CMD_WARNING; |
| 71 | |
| 72 | log_add_target(conn->dbg); |
| 73 | return CMD_SUCCESS; |
| 74 | } |
| 75 | |
| 76 | DEFUN(logging_fltr_imsi, |
| 77 | logging_fltr_imsi_cmd, |
| 78 | "logging filter imsi IMSI", |
| 79 | "Print all messages related to a IMSI\n") |
| 80 | { |
| 81 | struct telnet_connection *conn; |
| 82 | |
| 83 | conn = (struct telnet_connection *) vty->priv; |
| 84 | if (!conn->dbg) { |
| 85 | vty_out(vty, "Logging was not enabled.%s", VTY_NEWLINE); |
| 86 | return CMD_WARNING; |
| 87 | } |
| 88 | |
| 89 | log_set_imsi_filter(conn->dbg, argv[0]); |
| 90 | return CMD_SUCCESS; |
| 91 | } |
| 92 | |
| 93 | DEFUN(logging_fltr_all, |
| 94 | logging_fltr_all_cmd, |
| 95 | "logging filter all <0-1>", |
| 96 | "Print all messages to the console\n") |
| 97 | { |
| 98 | struct telnet_connection *conn; |
| 99 | |
| 100 | conn = (struct telnet_connection *) vty->priv; |
| 101 | if (!conn->dbg) { |
| 102 | vty_out(vty, "Logging was not enabled.%s", VTY_NEWLINE); |
| 103 | return CMD_WARNING; |
| 104 | } |
| 105 | |
| 106 | log_set_all_filter(conn->dbg, atoi(argv[0])); |
| 107 | return CMD_SUCCESS; |
| 108 | } |
| 109 | |
| 110 | DEFUN(logging_use_clr, |
| 111 | logging_use_clr_cmd, |
| 112 | "logging color <0-1>", |
| 113 | "Use color for printing messages\n") |
| 114 | { |
| 115 | struct telnet_connection *conn; |
| 116 | |
| 117 | conn = (struct telnet_connection *) vty->priv; |
| 118 | if (!conn->dbg) { |
| 119 | vty_out(vty, "Logging was not enabled.%s", VTY_NEWLINE); |
| 120 | return CMD_WARNING; |
| 121 | } |
| 122 | |
| 123 | log_set_use_color(conn->dbg, atoi(argv[0])); |
| 124 | return CMD_SUCCESS; |
| 125 | } |
| 126 | |
| 127 | DEFUN(logging_prnt_timestamp, |
| 128 | logging_prnt_timestamp_cmd, |
| 129 | "logging timestamp <0-1>", |
| 130 | "Print the timestamp of each message\n") |
| 131 | { |
| 132 | struct telnet_connection *conn; |
| 133 | |
| 134 | conn = (struct telnet_connection *) vty->priv; |
| 135 | if (!conn->dbg) { |
| 136 | vty_out(vty, "Logging was not enabled.%s", VTY_NEWLINE); |
| 137 | return CMD_WARNING; |
| 138 | } |
| 139 | |
| 140 | log_set_print_timestamp(conn->dbg, atoi(argv[0])); |
| 141 | return CMD_SUCCESS; |
| 142 | } |
| 143 | |
| 144 | /* FIXME: those have to be kept in sync with the log levels and categories */ |
| 145 | #define VTY_DEBUG_CATEGORIES "(rll|cc|mm|rr|rsl|nm|sms|pag|mncc|inp|mi|mib|mux|meas|sccp|msc|mgcp|ho|db|ref)" |
| 146 | #define VTY_DEBUG_LEVELS "(everything|debug|info|notice|error|fatal)" |
| 147 | DEFUN(logging_level, |
| 148 | logging_level_cmd, |
| 149 | "logging level " VTY_DEBUG_CATEGORIES " " VTY_DEBUG_LEVELS, |
| 150 | "Set the log level for a specified category\n") |
| 151 | { |
| 152 | struct telnet_connection *conn; |
| 153 | int category = log_parse_category(argv[0]); |
| 154 | int level = log_parse_level(argv[1]); |
| 155 | |
| 156 | conn = (struct telnet_connection *) vty->priv; |
| 157 | if (!conn->dbg) { |
| 158 | vty_out(vty, "Logging was not enabled.%s", VTY_NEWLINE); |
| 159 | return CMD_WARNING; |
| 160 | } |
| 161 | |
| 162 | if (category < 0) { |
| 163 | vty_out(vty, "Invalid category `%s'%s", argv[0], VTY_NEWLINE); |
| 164 | return CMD_WARNING; |
| 165 | } |
| 166 | |
| 167 | if (level < 0) { |
| 168 | vty_out(vty, "Invalid level `%s'%s", argv[1], VTY_NEWLINE); |
| 169 | return CMD_WARNING; |
| 170 | } |
| 171 | |
| 172 | conn->dbg->categories[category].enabled = 1; |
| 173 | conn->dbg->categories[category].loglevel = level; |
| 174 | |
| 175 | return CMD_SUCCESS; |
| 176 | } |
| 177 | |
| 178 | DEFUN(logging_set_category_mask, |
| 179 | logging_set_category_mask_cmd, |
| 180 | "logging set log mask MASK", |
| 181 | "Decide which categories to output.\n") |
| 182 | { |
| 183 | struct telnet_connection *conn; |
| 184 | |
| 185 | conn = (struct telnet_connection *) vty->priv; |
| 186 | if (!conn->dbg) { |
| 187 | vty_out(vty, "Logging was not enabled.%s", VTY_NEWLINE); |
| 188 | return CMD_WARNING; |
| 189 | } |
| 190 | |
| 191 | log_parse_category_mask(conn->dbg, argv[0]); |
| 192 | return CMD_SUCCESS; |
| 193 | } |
| 194 | |
| 195 | DEFUN(logging_set_log_level, |
| 196 | logging_set_log_level_cmd, |
| 197 | "logging set log level <0-8>", |
| 198 | "Set the global log level. The value 0 implies no filtering.\n") |
| 199 | { |
| 200 | struct telnet_connection *conn; |
| 201 | |
| 202 | conn = (struct telnet_connection *) vty->priv; |
| 203 | if (!conn->dbg) { |
| 204 | vty_out(vty, "Logging was not enabled.%s", VTY_NEWLINE); |
| 205 | return CMD_WARNING; |
| 206 | } |
| 207 | |
| 208 | log_set_log_level(conn->dbg, atoi(argv[0])); |
| 209 | return CMD_SUCCESS; |
| 210 | } |
| 211 | |
| 212 | DEFUN(diable_logging, |
| 213 | disable_logging_cmd, |
| 214 | "logging disable", |
| 215 | "Disables logging to this vty\n") |
| 216 | { |
| 217 | struct telnet_connection *conn; |
| 218 | |
| 219 | conn = (struct telnet_connection *) vty->priv; |
| 220 | if (!conn->dbg) { |
| 221 | vty_out(vty, "Logging was not enabled.%s", VTY_NEWLINE); |
| 222 | return CMD_WARNING; |
| 223 | } |
| 224 | |
| 225 | log_del_target(conn->dbg); |
| 226 | talloc_free(conn->dbg); |
| 227 | conn->dbg = NULL; |
| 228 | return CMD_SUCCESS; |
| 229 | } |
| 230 | |
Holger Hans Peter Freyther | e789b21 | 2010-04-15 11:28:14 +0200 | [diff] [blame] | 231 | void openbsc_vty_print_statistics(struct vty *vty, struct gsm_network *net) |
| 232 | { |
| 233 | vty_out(vty, "Channel Requests : %lu total, %lu no channel%s", |
| 234 | counter_get(net->stats.chreq.total), |
| 235 | counter_get(net->stats.chreq.no_channel), VTY_NEWLINE); |
Holger Hans Peter Freyther | 27942e9 | 2010-04-17 06:48:29 +0200 | [diff] [blame] | 236 | vty_out(vty, "Channel Failures : %lu rf_failures, %lu rll failures%s", |
| 237 | counter_get(net->stats.chan.rf_fail), |
| 238 | counter_get(net->stats.chan.rll_err), VTY_NEWLINE); |
Holger Hans Peter Freyther | e789b21 | 2010-04-15 11:28:14 +0200 | [diff] [blame] | 239 | vty_out(vty, "Paging : %lu attempted, %lu complete, %lu expired%s", |
| 240 | counter_get(net->stats.paging.attempted), |
| 241 | counter_get(net->stats.paging.completed), |
| 242 | counter_get(net->stats.paging.expired), VTY_NEWLINE); |
Holger Hans Peter Freyther | 3e09b3f | 2010-04-12 10:45:52 +0200 | [diff] [blame] | 243 | vty_out(vty, "BTS failures : %lu OML, %lu RSL%s", |
| 244 | counter_get(net->stats.bts.oml_fail), |
| 245 | counter_get(net->stats.bts.rsl_fail), VTY_NEWLINE); |
Holger Hans Peter Freyther | e789b21 | 2010-04-15 11:28:14 +0200 | [diff] [blame] | 246 | } |
| 247 | |
Holger Hans Peter Freyther | b70d45b | 2010-04-06 11:55:37 +0200 | [diff] [blame] | 248 | void openbsc_vty_add_cmds() |
| 249 | { |
| 250 | install_element(VIEW_NODE, &enable_logging_cmd); |
| 251 | install_element(VIEW_NODE, &disable_logging_cmd); |
| 252 | install_element(VIEW_NODE, &logging_fltr_imsi_cmd); |
| 253 | install_element(VIEW_NODE, &logging_fltr_all_cmd); |
| 254 | install_element(VIEW_NODE, &logging_use_clr_cmd); |
| 255 | install_element(VIEW_NODE, &logging_prnt_timestamp_cmd); |
| 256 | install_element(VIEW_NODE, &logging_set_category_mask_cmd); |
| 257 | install_element(VIEW_NODE, &logging_level_cmd); |
| 258 | install_element(VIEW_NODE, &logging_set_log_level_cmd); |
| 259 | |
| 260 | } |