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 | |
Harald Welte | 4015287 | 2010-05-16 20:52:23 +0200 | [diff] [blame] | 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
Holger Hans Peter Freyther | b70d45b | 2010-04-06 11:55:37 +0200 | [diff] [blame] | 24 | |
| 25 | #include <osmocore/talloc.h> |
| 26 | |
Harald Welte | 4015287 | 2010-05-16 20:52:23 +0200 | [diff] [blame] | 27 | #include <openbsc/vty.h> |
| 28 | #include <openbsc/telnet_interface.h> |
| 29 | #include <openbsc/gsm_data.h> |
| 30 | #include <openbsc/debug.h> |
| 31 | |
Holger Hans Peter Freyther | b70d45b | 2010-04-06 11:55:37 +0200 | [diff] [blame] | 32 | #include <vty/command.h> |
| 33 | #include <vty/buffer.h> |
| 34 | #include <vty/vty.h> |
| 35 | |
Holger Hans Peter Freyther | b70d45b | 2010-04-06 11:55:37 +0200 | [diff] [blame] | 36 | static void _vty_output(struct log_target *tgt, const char *line) |
| 37 | { |
| 38 | struct vty *vty = tgt->tgt_vty.vty; |
| 39 | vty_out(vty, "%s", line); |
| 40 | /* This is an ugly hack, but there is no easy way... */ |
| 41 | if (strchr(line, '\n')) |
| 42 | vty_out(vty, "\r"); |
| 43 | } |
| 44 | |
| 45 | struct log_target *log_target_create_vty(struct vty *vty) |
| 46 | { |
| 47 | struct log_target *target; |
| 48 | |
| 49 | target = log_target_create(); |
| 50 | if (!target) |
| 51 | return NULL; |
| 52 | |
| 53 | target->tgt_vty.vty = vty; |
| 54 | target->output = _vty_output; |
| 55 | return target; |
| 56 | } |
| 57 | |
| 58 | DEFUN(enable_logging, |
| 59 | enable_logging_cmd, |
| 60 | "logging enable", |
Harald Welte | 31cec99 | 2010-05-11 10:41:34 +0200 | [diff] [blame] | 61 | LOGGING_STR |
Holger Hans Peter Freyther | b70d45b | 2010-04-06 11:55:37 +0200 | [diff] [blame] | 62 | "Enables logging to this vty\n") |
| 63 | { |
| 64 | struct telnet_connection *conn; |
| 65 | |
| 66 | conn = (struct telnet_connection *) vty->priv; |
| 67 | if (conn->dbg) { |
| 68 | vty_out(vty, "Logging already enabled.%s", VTY_NEWLINE); |
| 69 | return CMD_WARNING; |
| 70 | } |
| 71 | |
| 72 | conn->dbg = log_target_create_vty(vty); |
| 73 | if (!conn->dbg) |
| 74 | return CMD_WARNING; |
| 75 | |
| 76 | log_add_target(conn->dbg); |
| 77 | return CMD_SUCCESS; |
| 78 | } |
| 79 | |
Holger Hans Peter Freyther | b70d45b | 2010-04-06 11:55:37 +0200 | [diff] [blame] | 80 | DEFUN(logging_fltr_all, |
| 81 | logging_fltr_all_cmd, |
Harald Welte | beb9d1e | 2010-05-16 00:00:04 +0200 | [diff] [blame] | 82 | "logging filter all (0|1)", |
| 83 | LOGGING_STR FILTER_STR |
| 84 | "Do you want to log all messages?\n" |
| 85 | "Only print messages matched by other filters\n" |
| 86 | "Bypass filter and print all messages\n") |
Holger Hans Peter Freyther | b70d45b | 2010-04-06 11:55:37 +0200 | [diff] [blame] | 87 | { |
| 88 | struct telnet_connection *conn; |
| 89 | |
| 90 | conn = (struct telnet_connection *) vty->priv; |
| 91 | if (!conn->dbg) { |
| 92 | vty_out(vty, "Logging was not enabled.%s", VTY_NEWLINE); |
| 93 | return CMD_WARNING; |
| 94 | } |
| 95 | |
| 96 | log_set_all_filter(conn->dbg, atoi(argv[0])); |
| 97 | return CMD_SUCCESS; |
| 98 | } |
| 99 | |
| 100 | DEFUN(logging_use_clr, |
| 101 | logging_use_clr_cmd, |
Harald Welte | beb9d1e | 2010-05-16 00:00:04 +0200 | [diff] [blame] | 102 | "logging color (0|1)", |
| 103 | LOGGING_STR "Configure color-printing for log messages\n" |
| 104 | "Don't use color for printing messages\n" |
Holger Hans Peter Freyther | b70d45b | 2010-04-06 11:55:37 +0200 | [diff] [blame] | 105 | "Use color for printing messages\n") |
| 106 | { |
| 107 | struct telnet_connection *conn; |
| 108 | |
| 109 | conn = (struct telnet_connection *) vty->priv; |
| 110 | if (!conn->dbg) { |
| 111 | vty_out(vty, "Logging was not enabled.%s", VTY_NEWLINE); |
| 112 | return CMD_WARNING; |
| 113 | } |
| 114 | |
| 115 | log_set_use_color(conn->dbg, atoi(argv[0])); |
| 116 | return CMD_SUCCESS; |
| 117 | } |
| 118 | |
| 119 | DEFUN(logging_prnt_timestamp, |
| 120 | logging_prnt_timestamp_cmd, |
Harald Welte | beb9d1e | 2010-05-16 00:00:04 +0200 | [diff] [blame] | 121 | "logging timestamp (0|1)", |
| 122 | LOGGING_STR "Configure log message timestamping\n" |
| 123 | "Don't prefix each log message\n" |
| 124 | "Prefix each log message with current timestamp\n") |
Holger Hans Peter Freyther | b70d45b | 2010-04-06 11:55:37 +0200 | [diff] [blame] | 125 | { |
| 126 | struct telnet_connection *conn; |
| 127 | |
| 128 | conn = (struct telnet_connection *) vty->priv; |
| 129 | if (!conn->dbg) { |
| 130 | vty_out(vty, "Logging was not enabled.%s", VTY_NEWLINE); |
| 131 | return CMD_WARNING; |
| 132 | } |
| 133 | |
| 134 | log_set_print_timestamp(conn->dbg, atoi(argv[0])); |
| 135 | return CMD_SUCCESS; |
| 136 | } |
| 137 | |
| 138 | /* FIXME: those have to be kept in sync with the log levels and categories */ |
Harald Welte | 1694079 | 2010-05-13 19:47:01 +0200 | [diff] [blame] | 139 | #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|all)" |
Harald Welte | 31cec99 | 2010-05-11 10:41:34 +0200 | [diff] [blame] | 140 | #define CATEGORIES_HELP \ |
| 141 | "A-bis Radio Link Layer (RLL)\n" \ |
| 142 | "Layer3 Call Control (CC)\n" \ |
| 143 | "Layer3 Mobility Management (MM)\n" \ |
| 144 | "Layer3 Radio Resource (RR)\n" \ |
| 145 | "A-bis Radio Signalling Link (RSL)\n" \ |
| 146 | "A-bis Network Management / O&M (NM/OML)\n" \ |
| 147 | "Layer3 Short Messagaging Service (SMS)\n" \ |
| 148 | "Paging Subsystem\n" \ |
| 149 | "MNCC API for Call Control application\n" \ |
| 150 | "A-bis Input Subsystem\n" \ |
| 151 | "A-bis Input Driver for Signalling\n" \ |
| 152 | "A-bis Input Driver for B-Channel (voice data)\n" \ |
| 153 | "A-bis B-Channel / Sub-channel Multiplexer\n" \ |
| 154 | "Radio Measurements\n" \ |
| 155 | "SCCP\n" \ |
| 156 | "Mobile Switching Center\n" \ |
| 157 | "Media Gateway Control Protocol\n" \ |
| 158 | "Hand-over\n" \ |
| 159 | "Database Layer\n" \ |
| 160 | "Reference Counting\n" \ |
| 161 | "GPRS Core\n" \ |
| 162 | "GPRS Network Service (NS)\n" \ |
| 163 | "GPRS BSS Gateway Protocol (BSSGP)\n" \ |
Harald Welte | 1694079 | 2010-05-13 19:47:01 +0200 | [diff] [blame] | 164 | "GPRS Logical Link Control Protocol (LLC)\n" \ |
| 165 | "GPRS Sub-Network Dependent Control Protocol (SNDCP)\n" \ |
Harald Welte | 31cec99 | 2010-05-11 10:41:34 +0200 | [diff] [blame] | 166 | "Global setting for all subsytems\n" |
| 167 | |
Holger Hans Peter Freyther | b70d45b | 2010-04-06 11:55:37 +0200 | [diff] [blame] | 168 | #define VTY_DEBUG_LEVELS "(everything|debug|info|notice|error|fatal)" |
Harald Welte | 31cec99 | 2010-05-11 10:41:34 +0200 | [diff] [blame] | 169 | #define LEVELS_HELP \ |
| 170 | "Log simply everything\n" \ |
| 171 | "Log debug messages and higher levels\n" \ |
| 172 | "Log informational messages and higher levels\n" \ |
| 173 | "Log noticable messages and higher levels\n" \ |
| 174 | "Log error messages and higher levels\n" \ |
| 175 | "Log only fatal messages\n" |
Holger Hans Peter Freyther | b70d45b | 2010-04-06 11:55:37 +0200 | [diff] [blame] | 176 | DEFUN(logging_level, |
| 177 | logging_level_cmd, |
| 178 | "logging level " VTY_DEBUG_CATEGORIES " " VTY_DEBUG_LEVELS, |
Harald Welte | 31cec99 | 2010-05-11 10:41:34 +0200 | [diff] [blame] | 179 | LOGGING_STR |
| 180 | "Set the log level for a specified category\n" |
| 181 | CATEGORIES_HELP |
| 182 | LEVELS_HELP) |
Holger Hans Peter Freyther | b70d45b | 2010-04-06 11:55:37 +0200 | [diff] [blame] | 183 | { |
| 184 | struct telnet_connection *conn; |
| 185 | int category = log_parse_category(argv[0]); |
| 186 | int level = log_parse_level(argv[1]); |
| 187 | |
| 188 | conn = (struct telnet_connection *) vty->priv; |
| 189 | if (!conn->dbg) { |
| 190 | vty_out(vty, "Logging was not enabled.%s", VTY_NEWLINE); |
| 191 | return CMD_WARNING; |
| 192 | } |
| 193 | |
Harald Welte | 4e5de6b | 2010-05-11 10:21:45 +0200 | [diff] [blame] | 194 | if (level < 0) { |
| 195 | vty_out(vty, "Invalid level `%s'%s", argv[1], VTY_NEWLINE); |
Holger Hans Peter Freyther | b70d45b | 2010-04-06 11:55:37 +0200 | [diff] [blame] | 196 | return CMD_WARNING; |
| 197 | } |
| 198 | |
Harald Welte | 4e5de6b | 2010-05-11 10:21:45 +0200 | [diff] [blame] | 199 | /* Check for special case where we want to set global log level */ |
| 200 | if (!strcmp(argv[0], "all")) { |
| 201 | log_set_log_level(conn->dbg, level); |
| 202 | return CMD_SUCCESS; |
| 203 | } |
| 204 | |
| 205 | if (category < 0) { |
| 206 | vty_out(vty, "Invalid category `%s'%s", argv[0], VTY_NEWLINE); |
Holger Hans Peter Freyther | b70d45b | 2010-04-06 11:55:37 +0200 | [diff] [blame] | 207 | return CMD_WARNING; |
| 208 | } |
| 209 | |
| 210 | conn->dbg->categories[category].enabled = 1; |
| 211 | conn->dbg->categories[category].loglevel = level; |
| 212 | |
| 213 | return CMD_SUCCESS; |
| 214 | } |
| 215 | |
| 216 | DEFUN(logging_set_category_mask, |
| 217 | logging_set_category_mask_cmd, |
| 218 | "logging set log mask MASK", |
Harald Welte | 31cec99 | 2010-05-11 10:41:34 +0200 | [diff] [blame] | 219 | LOGGING_STR |
Holger Hans Peter Freyther | b70d45b | 2010-04-06 11:55:37 +0200 | [diff] [blame] | 220 | "Decide which categories to output.\n") |
| 221 | { |
| 222 | struct telnet_connection *conn; |
| 223 | |
| 224 | conn = (struct telnet_connection *) vty->priv; |
| 225 | if (!conn->dbg) { |
| 226 | vty_out(vty, "Logging was not enabled.%s", VTY_NEWLINE); |
| 227 | return CMD_WARNING; |
| 228 | } |
| 229 | |
| 230 | log_parse_category_mask(conn->dbg, argv[0]); |
| 231 | return CMD_SUCCESS; |
| 232 | } |
| 233 | |
Holger Hans Peter Freyther | b70d45b | 2010-04-06 11:55:37 +0200 | [diff] [blame] | 234 | DEFUN(diable_logging, |
| 235 | disable_logging_cmd, |
| 236 | "logging disable", |
Harald Welte | 31cec99 | 2010-05-11 10:41:34 +0200 | [diff] [blame] | 237 | LOGGING_STR |
Holger Hans Peter Freyther | b70d45b | 2010-04-06 11:55:37 +0200 | [diff] [blame] | 238 | "Disables logging to this vty\n") |
| 239 | { |
| 240 | struct telnet_connection *conn; |
| 241 | |
| 242 | conn = (struct telnet_connection *) vty->priv; |
| 243 | if (!conn->dbg) { |
| 244 | vty_out(vty, "Logging was not enabled.%s", VTY_NEWLINE); |
| 245 | return CMD_WARNING; |
| 246 | } |
| 247 | |
| 248 | log_del_target(conn->dbg); |
| 249 | talloc_free(conn->dbg); |
| 250 | conn->dbg = NULL; |
| 251 | return CMD_SUCCESS; |
| 252 | } |
| 253 | |
Harald Welte | 160f42a | 2010-05-11 11:12:37 +0200 | [diff] [blame] | 254 | static void vty_print_logtarget(struct vty *vty, const struct log_info *info, |
| 255 | const struct log_target *tgt) |
| 256 | { |
| 257 | unsigned int i; |
| 258 | |
| 259 | vty_out(vty, " Global Loglevel: %s%s", |
| 260 | log_level_str(tgt->loglevel), VTY_NEWLINE); |
| 261 | vty_out(vty, " Use color: %s, Print Timestamp: %s%s", |
| 262 | tgt->use_color ? "On" : "Off", |
| 263 | tgt->print_timestamp ? "On" : "Off", VTY_NEWLINE); |
| 264 | |
| 265 | vty_out(vty, " Log Level specific information:%s", VTY_NEWLINE); |
| 266 | |
| 267 | for (i = 0; i < info->num_cat; i++) { |
| 268 | const struct log_category *cat = &tgt->categories[i]; |
| 269 | vty_out(vty, " %-10s %-10s %-8s %s%s", |
| 270 | info->cat[i].name+1, log_level_str(cat->loglevel), |
| 271 | cat->enabled ? "Enabled" : "Disabled", |
| 272 | info->cat[i].description, |
| 273 | VTY_NEWLINE); |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | #define SHOW_LOG_STR "Show current logging configuration\n" |
| 278 | |
| 279 | DEFUN(show_logging_vty, |
| 280 | show_logging_vty_cmd, |
| 281 | "show logging vty", |
| 282 | SHOW_STR SHOW_LOG_STR |
| 283 | "Show current logging configuration for this vty\n") |
| 284 | { |
| 285 | struct telnet_connection *conn; |
| 286 | |
| 287 | conn = (struct telnet_connection *) vty->priv; |
| 288 | if (!conn->dbg) { |
| 289 | vty_out(vty, "Logging was not enabled.%s", VTY_NEWLINE); |
| 290 | return CMD_WARNING; |
| 291 | } |
| 292 | vty_print_logtarget(vty, &log_info, conn->dbg); |
| 293 | |
| 294 | return CMD_SUCCESS; |
| 295 | } |
| 296 | |
Harald Welte | 8791dac | 2010-05-14 17:59:53 +0200 | [diff] [blame] | 297 | gDEFUN(cfg_description, cfg_description_cmd, |
| 298 | "description .TEXT", |
| 299 | "Save human-readable decription of the object\n") |
| 300 | { |
| 301 | char **dptr = vty->index_sub; |
Harald Welte | 8791dac | 2010-05-14 17:59:53 +0200 | [diff] [blame] | 302 | |
| 303 | if (!dptr) { |
| 304 | vty_out(vty, "vty->index_sub == NULL%s", VTY_NEWLINE); |
| 305 | return CMD_WARNING; |
| 306 | } |
| 307 | |
Harald Welte | 3699e20 | 2010-05-16 19:28:32 +0200 | [diff] [blame] | 308 | *dptr = argv_concat(argv, argc, 0); |
| 309 | if (!dptr) |
Harald Welte | 8791dac | 2010-05-14 17:59:53 +0200 | [diff] [blame] | 310 | return CMD_WARNING; |
| 311 | |
Harald Welte | 8791dac | 2010-05-14 17:59:53 +0200 | [diff] [blame] | 312 | return CMD_SUCCESS; |
| 313 | } |
| 314 | |
| 315 | gDEFUN(cfg_no_description, cfg_no_description_cmd, |
| 316 | "no description", |
| 317 | NO_STR |
| 318 | "Remove description of the object\n") |
| 319 | { |
| 320 | char **dptr = vty->index_sub; |
| 321 | |
| 322 | if (!dptr) { |
| 323 | vty_out(vty, "vty->index_sub == NULL%s", VTY_NEWLINE); |
| 324 | return CMD_WARNING; |
| 325 | } |
| 326 | |
| 327 | if (*dptr) { |
| 328 | talloc_free(*dptr); |
| 329 | *dptr = NULL; |
| 330 | } |
| 331 | |
| 332 | return CMD_SUCCESS; |
| 333 | } |
| 334 | |
Harald Welte | 682ee5f | 2010-05-16 22:02:16 +0200 | [diff] [blame^] | 335 | void logging_vty_add_cmds() |
Holger Hans Peter Freyther | b70d45b | 2010-04-06 11:55:37 +0200 | [diff] [blame] | 336 | { |
Harald Welte | 7bc28f6 | 2010-05-12 16:10:35 +0000 | [diff] [blame] | 337 | install_element_ve(&enable_logging_cmd); |
| 338 | install_element_ve(&disable_logging_cmd); |
Harald Welte | 7bc28f6 | 2010-05-12 16:10:35 +0000 | [diff] [blame] | 339 | install_element_ve(&logging_fltr_all_cmd); |
| 340 | install_element_ve(&logging_use_clr_cmd); |
| 341 | install_element_ve(&logging_prnt_timestamp_cmd); |
| 342 | install_element_ve(&logging_set_category_mask_cmd); |
| 343 | install_element_ve(&logging_level_cmd); |
| 344 | install_element_ve(&show_logging_vty_cmd); |
Holger Hans Peter Freyther | b70d45b | 2010-04-06 11:55:37 +0200 | [diff] [blame] | 345 | } |