Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 1 | /* OpenBSC logging helper for the VTY */ |
| 2 | /* (C) 2009-2010 by Harald Welte <laforge@gnumonks.org> |
Holger Hans Peter Freyther | 2d6ad13 | 2014-12-05 09:35:30 +0100 | [diff] [blame] | 3 | * (C) 2009-2014 by Holger Hans Peter Freyther |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 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 <stdlib.h> |
| 23 | #include <string.h> |
| 24 | |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 25 | #include "../../config.h" |
| 26 | |
Pablo Neira Ayuso | 8341934 | 2011-03-22 16:36:13 +0100 | [diff] [blame] | 27 | #include <osmocom/core/talloc.h> |
| 28 | #include <osmocom/core/logging.h> |
| 29 | #include <osmocom/core/utils.h> |
Katerina Barone-Adesi | 3309a43 | 2013-02-21 05:16:29 +0000 | [diff] [blame] | 30 | #include <osmocom/core/strrb.h> |
| 31 | #include <osmocom/core/loggingrb.h> |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 32 | |
| 33 | #include <osmocom/vty/command.h> |
| 34 | #include <osmocom/vty/buffer.h> |
| 35 | #include <osmocom/vty/vty.h> |
| 36 | #include <osmocom/vty/telnet_interface.h> |
| 37 | #include <osmocom/vty/logging.h> |
| 38 | |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 39 | #define LOG_STR "Configure logging sub-system\n" |
| 40 | |
Harald Welte | 4ebdf74 | 2010-05-19 19:54:00 +0200 | [diff] [blame] | 41 | extern const struct log_info *osmo_log_info; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 42 | |
Harald Welte | 76e72ab | 2011-02-17 15:52:39 +0100 | [diff] [blame] | 43 | static void _vty_output(struct log_target *tgt, |
| 44 | unsigned int level, const char *line) |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 45 | { |
| 46 | struct vty *vty = tgt->tgt_vty.vty; |
| 47 | vty_out(vty, "%s", line); |
| 48 | /* This is an ugly hack, but there is no easy way... */ |
| 49 | if (strchr(line, '\n')) |
| 50 | vty_out(vty, "\r"); |
| 51 | } |
| 52 | |
| 53 | struct log_target *log_target_create_vty(struct vty *vty) |
| 54 | { |
| 55 | struct log_target *target; |
| 56 | |
| 57 | target = log_target_create(); |
| 58 | if (!target) |
| 59 | return NULL; |
| 60 | |
| 61 | target->tgt_vty.vty = vty; |
| 62 | target->output = _vty_output; |
| 63 | return target; |
| 64 | } |
| 65 | |
| 66 | DEFUN(enable_logging, |
| 67 | enable_logging_cmd, |
| 68 | "logging enable", |
| 69 | LOGGING_STR |
| 70 | "Enables logging to this vty\n") |
| 71 | { |
| 72 | struct telnet_connection *conn; |
| 73 | |
| 74 | conn = (struct telnet_connection *) vty->priv; |
| 75 | if (conn->dbg) { |
| 76 | vty_out(vty, "Logging already enabled.%s", VTY_NEWLINE); |
| 77 | return CMD_WARNING; |
| 78 | } |
| 79 | |
| 80 | conn->dbg = log_target_create_vty(vty); |
| 81 | if (!conn->dbg) |
| 82 | return CMD_WARNING; |
| 83 | |
| 84 | log_add_target(conn->dbg); |
| 85 | return CMD_SUCCESS; |
| 86 | } |
| 87 | |
Harald Welte | a62648b | 2011-02-18 21:03:27 +0100 | [diff] [blame] | 88 | struct log_target *osmo_log_vty2tgt(struct vty *vty) |
| 89 | { |
| 90 | struct telnet_connection *conn; |
| 91 | |
| 92 | if (vty->node == CFG_LOG_NODE) |
| 93 | return vty->index; |
| 94 | |
| 95 | |
| 96 | conn = (struct telnet_connection *) vty->priv; |
| 97 | if (!conn->dbg) |
| 98 | vty_out(vty, "Logging was not enabled.%s", VTY_NEWLINE); |
| 99 | |
| 100 | return conn->dbg; |
| 101 | } |
| 102 | |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 103 | DEFUN(logging_fltr_all, |
| 104 | logging_fltr_all_cmd, |
| 105 | "logging filter all (0|1)", |
| 106 | LOGGING_STR FILTER_STR |
| 107 | "Do you want to log all messages?\n" |
| 108 | "Only print messages matched by other filters\n" |
| 109 | "Bypass filter and print all messages\n") |
| 110 | { |
Harald Welte | a62648b | 2011-02-18 21:03:27 +0100 | [diff] [blame] | 111 | struct log_target *tgt = osmo_log_vty2tgt(vty); |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 112 | |
Harald Welte | a62648b | 2011-02-18 21:03:27 +0100 | [diff] [blame] | 113 | if (!tgt) |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 114 | return CMD_WARNING; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 115 | |
Harald Welte | a62648b | 2011-02-18 21:03:27 +0100 | [diff] [blame] | 116 | log_set_all_filter(tgt, atoi(argv[0])); |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 117 | return CMD_SUCCESS; |
| 118 | } |
| 119 | |
| 120 | DEFUN(logging_use_clr, |
| 121 | logging_use_clr_cmd, |
| 122 | "logging color (0|1)", |
| 123 | LOGGING_STR "Configure color-printing for log messages\n" |
| 124 | "Don't use color for printing messages\n" |
| 125 | "Use color for printing messages\n") |
| 126 | { |
Harald Welte | a62648b | 2011-02-18 21:03:27 +0100 | [diff] [blame] | 127 | struct log_target *tgt = osmo_log_vty2tgt(vty); |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 128 | |
Harald Welte | a62648b | 2011-02-18 21:03:27 +0100 | [diff] [blame] | 129 | if (!tgt) |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 130 | return CMD_WARNING; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 131 | |
Harald Welte | a62648b | 2011-02-18 21:03:27 +0100 | [diff] [blame] | 132 | log_set_use_color(tgt, atoi(argv[0])); |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 133 | return CMD_SUCCESS; |
| 134 | } |
| 135 | |
| 136 | DEFUN(logging_prnt_timestamp, |
| 137 | logging_prnt_timestamp_cmd, |
| 138 | "logging timestamp (0|1)", |
| 139 | LOGGING_STR "Configure log message timestamping\n" |
| 140 | "Don't prefix each log message\n" |
| 141 | "Prefix each log message with current timestamp\n") |
| 142 | { |
Harald Welte | a62648b | 2011-02-18 21:03:27 +0100 | [diff] [blame] | 143 | struct log_target *tgt = osmo_log_vty2tgt(vty); |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 144 | |
Harald Welte | a62648b | 2011-02-18 21:03:27 +0100 | [diff] [blame] | 145 | if (!tgt) |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 146 | return CMD_WARNING; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 147 | |
Harald Welte | a62648b | 2011-02-18 21:03:27 +0100 | [diff] [blame] | 148 | log_set_print_timestamp(tgt, atoi(argv[0])); |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 149 | return CMD_SUCCESS; |
| 150 | } |
| 151 | |
Holger Hans Peter Freyther | 2d6ad13 | 2014-12-05 09:35:30 +0100 | [diff] [blame] | 152 | DEFUN(logging_prnt_ext_timestamp, |
| 153 | logging_prnt_ext_timestamp_cmd, |
| 154 | "logging print extended-timestamp (0|1)", |
| 155 | LOGGING_STR "Log output settings\n" |
| 156 | "Configure log message timestamping\n" |
| 157 | "Don't prefix each log message\n" |
| 158 | "Prefix each log message with current timestamp with YYYYMMDDhhmmssnnn\n") |
| 159 | { |
| 160 | struct log_target *tgt = osmo_log_vty2tgt(vty); |
| 161 | |
| 162 | if (!tgt) |
| 163 | return CMD_WARNING; |
| 164 | |
| 165 | log_set_print_extended_timestamp(tgt, atoi(argv[0])); |
| 166 | return CMD_SUCCESS; |
| 167 | } |
| 168 | |
| 169 | DEFUN(logging_prnt_cat, |
| 170 | logging_prnt_cat_cmd, |
| 171 | "logging print category (0|1)", |
| 172 | LOGGING_STR "Log output settings\n" |
| 173 | "Configure log message\n" |
| 174 | "Don't prefix each log message\n" |
| 175 | "Prefix each log message with category/subsystem name\n") |
| 176 | { |
| 177 | struct log_target *tgt = osmo_log_vty2tgt(vty); |
| 178 | |
| 179 | if (!tgt) |
| 180 | return CMD_WARNING; |
| 181 | |
| 182 | log_set_print_category(tgt, atoi(argv[0])); |
| 183 | return CMD_SUCCESS; |
| 184 | } |
| 185 | |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 186 | DEFUN(logging_level, |
| 187 | logging_level_cmd, |
Pablo Neira Ayuso | 04139f1 | 2011-03-09 13:05:08 +0100 | [diff] [blame] | 188 | NULL, /* cmdstr is dynamically set in logging_vty_add_cmds(). */ |
| 189 | NULL) /* same thing for helpstr. */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 190 | { |
Harald Welte | a62648b | 2011-02-18 21:03:27 +0100 | [diff] [blame] | 191 | int category = log_parse_category(argv[0]); |
| 192 | int level = log_parse_level(argv[1]); |
| 193 | struct log_target *tgt = osmo_log_vty2tgt(vty); |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 194 | |
Harald Welte | a62648b | 2011-02-18 21:03:27 +0100 | [diff] [blame] | 195 | if (!tgt) |
| 196 | return CMD_WARNING; |
| 197 | |
| 198 | if (level < 0) { |
| 199 | vty_out(vty, "Invalid level `%s'%s", argv[1], VTY_NEWLINE); |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 200 | return CMD_WARNING; |
| 201 | } |
| 202 | |
Harald Welte | a62648b | 2011-02-18 21:03:27 +0100 | [diff] [blame] | 203 | /* Check for special case where we want to set global log level */ |
| 204 | if (!strcmp(argv[0], "all")) { |
| 205 | log_set_log_level(tgt, level); |
| 206 | return CMD_SUCCESS; |
| 207 | } |
| 208 | |
| 209 | if (category < 0) { |
| 210 | vty_out(vty, "Invalid category `%s'%s", argv[0], VTY_NEWLINE); |
| 211 | return CMD_WARNING; |
| 212 | } |
| 213 | |
| 214 | tgt->categories[category].enabled = 1; |
| 215 | tgt->categories[category].loglevel = level; |
| 216 | |
| 217 | return CMD_SUCCESS; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | DEFUN(logging_set_category_mask, |
| 221 | logging_set_category_mask_cmd, |
Holger Hans Peter Freyther | 146d1d3 | 2011-10-03 23:15:41 +0200 | [diff] [blame] | 222 | "logging set-log-mask MASK", |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 223 | LOGGING_STR |
Holger Hans Peter Freyther | 146d1d3 | 2011-10-03 23:15:41 +0200 | [diff] [blame] | 224 | "Set the logmask of this logging target\n" |
| 225 | "The logmask to use\n") |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 226 | { |
Harald Welte | a62648b | 2011-02-18 21:03:27 +0100 | [diff] [blame] | 227 | struct log_target *tgt = osmo_log_vty2tgt(vty); |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 228 | |
Harald Welte | a62648b | 2011-02-18 21:03:27 +0100 | [diff] [blame] | 229 | if (!tgt) |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 230 | return CMD_WARNING; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 231 | |
Harald Welte | a62648b | 2011-02-18 21:03:27 +0100 | [diff] [blame] | 232 | log_parse_category_mask(tgt, argv[0]); |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 233 | return CMD_SUCCESS; |
| 234 | } |
| 235 | |
Holger Hans Peter Freyther | 146d1d3 | 2011-10-03 23:15:41 +0200 | [diff] [blame] | 236 | ALIAS_DEPRECATED(logging_set_category_mask, |
| 237 | logging_set_category_mask_old_cmd, |
| 238 | "logging set log mask MASK", |
| 239 | LOGGING_STR |
| 240 | "Decide which categories to output.\n" |
| 241 | "Log commands\n" "Mask commands\n" "The logmask to use\n"); |
| 242 | |
| 243 | |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 244 | DEFUN(diable_logging, |
| 245 | disable_logging_cmd, |
| 246 | "logging disable", |
| 247 | LOGGING_STR |
| 248 | "Disables logging to this vty\n") |
| 249 | { |
Harald Welte | a62648b | 2011-02-18 21:03:27 +0100 | [diff] [blame] | 250 | struct log_target *tgt = osmo_log_vty2tgt(vty); |
| 251 | struct telnet_connection *conn = (struct telnet_connection *) vty->priv; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 252 | |
Harald Welte | a62648b | 2011-02-18 21:03:27 +0100 | [diff] [blame] | 253 | if (!tgt) |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 254 | return CMD_WARNING; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 255 | |
Harald Welte | a62648b | 2011-02-18 21:03:27 +0100 | [diff] [blame] | 256 | log_del_target(tgt); |
| 257 | talloc_free(tgt); |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 258 | conn->dbg = NULL; |
Harald Welte | a62648b | 2011-02-18 21:03:27 +0100 | [diff] [blame] | 259 | |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 260 | return CMD_SUCCESS; |
| 261 | } |
| 262 | |
| 263 | static void vty_print_logtarget(struct vty *vty, const struct log_info *info, |
| 264 | const struct log_target *tgt) |
| 265 | { |
| 266 | unsigned int i; |
| 267 | |
| 268 | vty_out(vty, " Global Loglevel: %s%s", |
| 269 | log_level_str(tgt->loglevel), VTY_NEWLINE); |
| 270 | vty_out(vty, " Use color: %s, Print Timestamp: %s%s", |
| 271 | tgt->use_color ? "On" : "Off", |
| 272 | tgt->print_timestamp ? "On" : "Off", VTY_NEWLINE); |
| 273 | |
| 274 | vty_out(vty, " Log Level specific information:%s", VTY_NEWLINE); |
| 275 | |
| 276 | for (i = 0; i < info->num_cat; i++) { |
| 277 | const struct log_category *cat = &tgt->categories[i]; |
| 278 | vty_out(vty, " %-10s %-10s %-8s %s%s", |
| 279 | info->cat[i].name+1, log_level_str(cat->loglevel), |
| 280 | cat->enabled ? "Enabled" : "Disabled", |
| 281 | info->cat[i].description, |
| 282 | VTY_NEWLINE); |
| 283 | } |
Harald Welte | 6d2d4d6 | 2013-03-10 09:53:24 +0000 | [diff] [blame] | 284 | |
| 285 | vty_out(vty, " Log Filter 'ALL': %s%s", |
| 286 | tgt->filter_map & LOG_FILTER_ALL ? "Enabled" : "Disabled", |
| 287 | VTY_NEWLINE); |
| 288 | |
Harald Welte | fb84f32 | 2013-06-06 07:33:54 +0200 | [diff] [blame] | 289 | /* print application specific filters */ |
| 290 | if (info->print_fn) |
| 291 | info->print_fn(vty, info, tgt); |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | #define SHOW_LOG_STR "Show current logging configuration\n" |
| 295 | |
| 296 | DEFUN(show_logging_vty, |
Katerina Barone-Adesi | 3309a43 | 2013-02-21 05:16:29 +0000 | [diff] [blame] | 297 | show_logging_vty_cmd, |
| 298 | "show logging vty", |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 299 | SHOW_STR SHOW_LOG_STR |
| 300 | "Show current logging configuration for this vty\n") |
| 301 | { |
Harald Welte | a62648b | 2011-02-18 21:03:27 +0100 | [diff] [blame] | 302 | struct log_target *tgt = osmo_log_vty2tgt(vty); |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 303 | |
Harald Welte | a62648b | 2011-02-18 21:03:27 +0100 | [diff] [blame] | 304 | if (!tgt) |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 305 | return CMD_WARNING; |
Harald Welte | a62648b | 2011-02-18 21:03:27 +0100 | [diff] [blame] | 306 | |
| 307 | vty_print_logtarget(vty, osmo_log_info, tgt); |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 308 | |
| 309 | return CMD_SUCCESS; |
| 310 | } |
| 311 | |
Katerina Barone-Adesi | 3309a43 | 2013-02-21 05:16:29 +0000 | [diff] [blame] | 312 | DEFUN(show_alarms, |
| 313 | show_alarms_cmd, |
| 314 | "show alarms", |
| 315 | SHOW_STR SHOW_LOG_STR |
| 316 | "Show the contents of the logging ringbuffer\n") |
| 317 | { |
| 318 | int i, num_alarms; |
| 319 | struct osmo_strrb *rb; |
| 320 | struct log_target *tgt = log_target_find(LOG_TGT_TYPE_STRRB, NULL); |
| 321 | if (!tgt) { |
| 322 | vty_out(vty, "%% No alarms, run 'log alarms <2-32700>'%s", |
| 323 | VTY_NEWLINE); |
| 324 | return CMD_WARNING; |
| 325 | } |
| 326 | |
| 327 | rb = tgt->tgt_rb.rb; |
| 328 | num_alarms = osmo_strrb_elements(rb); |
| 329 | |
| 330 | vty_out(vty, "%% Showing %i alarms%s", num_alarms, VTY_NEWLINE); |
| 331 | |
| 332 | for (i = 0; i < num_alarms; i++) |
| 333 | vty_out(vty, "%% %s%s", osmo_strrb_get_nth(rb, i), |
| 334 | VTY_NEWLINE); |
| 335 | |
| 336 | return CMD_SUCCESS; |
| 337 | } |
| 338 | |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 339 | gDEFUN(cfg_description, cfg_description_cmd, |
| 340 | "description .TEXT", |
Holger Hans Peter Freyther | c9b3e06 | 2012-07-25 13:02:49 +0200 | [diff] [blame] | 341 | "Save human-readable decription of the object\n" |
| 342 | "Text until the end of the line\n") |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 343 | { |
| 344 | char **dptr = vty->index_sub; |
| 345 | |
| 346 | if (!dptr) { |
| 347 | vty_out(vty, "vty->index_sub == NULL%s", VTY_NEWLINE); |
| 348 | return CMD_WARNING; |
| 349 | } |
| 350 | |
Holger Hans Peter Freyther | ff0670e | 2011-02-24 14:20:41 +0100 | [diff] [blame] | 351 | if (*dptr) |
| 352 | talloc_free(*dptr); |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 353 | *dptr = argv_concat(argv, argc, 0); |
Holger Hans Peter Freyther | 6a75d16 | 2013-07-14 09:07:11 +0200 | [diff] [blame] | 354 | if (!*dptr) |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 355 | return CMD_WARNING; |
| 356 | |
| 357 | return CMD_SUCCESS; |
| 358 | } |
| 359 | |
| 360 | gDEFUN(cfg_no_description, cfg_no_description_cmd, |
| 361 | "no description", |
| 362 | NO_STR |
| 363 | "Remove description of the object\n") |
| 364 | { |
| 365 | char **dptr = vty->index_sub; |
| 366 | |
| 367 | if (!dptr) { |
| 368 | vty_out(vty, "vty->index_sub == NULL%s", VTY_NEWLINE); |
| 369 | return CMD_WARNING; |
| 370 | } |
| 371 | |
| 372 | if (*dptr) { |
| 373 | talloc_free(*dptr); |
| 374 | *dptr = NULL; |
| 375 | } |
| 376 | |
| 377 | return CMD_SUCCESS; |
| 378 | } |
| 379 | |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 380 | /* Support for configuration of log targets != the current vty */ |
| 381 | |
| 382 | struct cmd_node cfg_log_node = { |
| 383 | CFG_LOG_NODE, |
| 384 | "%s(config-log)# ", |
| 385 | 1 |
| 386 | }; |
| 387 | |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 388 | #ifdef HAVE_SYSLOG_H |
| 389 | |
| 390 | #include <syslog.h> |
| 391 | |
| 392 | static const int local_sysl_map[] = { |
| 393 | [0] = LOG_LOCAL0, |
| 394 | [1] = LOG_LOCAL1, |
| 395 | [2] = LOG_LOCAL2, |
| 396 | [3] = LOG_LOCAL3, |
| 397 | [4] = LOG_LOCAL4, |
| 398 | [5] = LOG_LOCAL5, |
| 399 | [6] = LOG_LOCAL6, |
| 400 | [7] = LOG_LOCAL7 |
| 401 | }; |
| 402 | |
Harald Welte | de79cee | 2011-02-24 23:47:57 +0100 | [diff] [blame] | 403 | /* From VTY core code */ |
| 404 | extern struct host host; |
| 405 | |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 406 | static int _cfg_log_syslog(struct vty *vty, int facility) |
| 407 | { |
| 408 | struct log_target *tgt; |
| 409 | |
| 410 | /* First delete the old syslog target, if any */ |
| 411 | tgt = log_target_find(LOG_TGT_TYPE_SYSLOG, NULL); |
| 412 | if (tgt) |
| 413 | log_target_destroy(tgt); |
| 414 | |
Harald Welte | de79cee | 2011-02-24 23:47:57 +0100 | [diff] [blame] | 415 | tgt = log_target_create_syslog(host.app_info->name, 0, facility); |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 416 | if (!tgt) { |
| 417 | vty_out(vty, "%% Unable to open syslog%s", VTY_NEWLINE); |
| 418 | return CMD_WARNING; |
| 419 | } |
| 420 | log_add_target(tgt); |
| 421 | |
| 422 | vty->index = tgt; |
| 423 | vty->node = CFG_LOG_NODE; |
| 424 | |
| 425 | return CMD_SUCCESS; |
| 426 | } |
| 427 | |
| 428 | DEFUN(cfg_log_syslog_local, cfg_log_syslog_local_cmd, |
| 429 | "log syslog local <0-7>", |
| 430 | LOG_STR "Logging via syslog\n" "Syslog LOCAL facility\n" |
| 431 | "Local facility number\n") |
| 432 | { |
| 433 | int local = atoi(argv[0]); |
| 434 | int facility = local_sysl_map[local]; |
| 435 | |
| 436 | return _cfg_log_syslog(vty, facility); |
| 437 | } |
| 438 | |
| 439 | static struct value_string sysl_level_names[] = { |
| 440 | { LOG_AUTHPRIV, "authpriv" }, |
| 441 | { LOG_CRON, "cron" }, |
| 442 | { LOG_DAEMON, "daemon" }, |
| 443 | { LOG_FTP, "ftp" }, |
| 444 | { LOG_LPR, "lpr" }, |
| 445 | { LOG_MAIL, "mail" }, |
| 446 | { LOG_NEWS, "news" }, |
| 447 | { LOG_USER, "user" }, |
| 448 | { LOG_UUCP, "uucp" }, |
| 449 | /* only for value -> string conversion */ |
| 450 | { LOG_LOCAL0, "local 0" }, |
| 451 | { LOG_LOCAL1, "local 1" }, |
| 452 | { LOG_LOCAL2, "local 2" }, |
| 453 | { LOG_LOCAL3, "local 3" }, |
| 454 | { LOG_LOCAL4, "local 4" }, |
| 455 | { LOG_LOCAL5, "local 5" }, |
| 456 | { LOG_LOCAL6, "local 6" }, |
| 457 | { LOG_LOCAL7, "local 7" }, |
| 458 | { 0, NULL } |
| 459 | }; |
| 460 | |
| 461 | DEFUN(cfg_log_syslog, cfg_log_syslog_cmd, |
| 462 | "log syslog (authpriv|cron|daemon|ftp|lpr|mail|news|user|uucp)", |
Holger Hans Peter Freyther | a4463fd | 2011-10-03 23:17:36 +0200 | [diff] [blame] | 463 | LOG_STR "Logging via syslog\n" |
| 464 | "Security/authorization messages facility\n" |
| 465 | "Clock daemon (cron/at) facility\n" |
| 466 | "General system daemon facility\n" |
| 467 | "Ftp daemon facility\n" |
| 468 | "Line printer facility\n" |
| 469 | "Mail facility\n" |
| 470 | "News facility\n" |
| 471 | "Generic facility\n" |
| 472 | "UUCP facility\n") |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 473 | { |
| 474 | int facility = get_string_value(sysl_level_names, argv[0]); |
| 475 | |
| 476 | return _cfg_log_syslog(vty, facility); |
| 477 | } |
| 478 | |
| 479 | DEFUN(cfg_no_log_syslog, cfg_no_log_syslog_cmd, |
| 480 | "no log syslog", |
| 481 | NO_STR LOG_STR "Logging via syslog\n") |
| 482 | { |
| 483 | struct log_target *tgt; |
| 484 | |
| 485 | tgt = log_target_find(LOG_TGT_TYPE_SYSLOG, NULL); |
| 486 | if (!tgt) { |
| 487 | vty_out(vty, "%% No syslog target found%s", |
| 488 | VTY_NEWLINE); |
| 489 | return CMD_WARNING; |
| 490 | } |
| 491 | |
| 492 | log_target_destroy(tgt); |
| 493 | |
| 494 | return CMD_SUCCESS; |
| 495 | } |
| 496 | #endif /* HAVE_SYSLOG_H */ |
| 497 | |
| 498 | DEFUN(cfg_log_stderr, cfg_log_stderr_cmd, |
| 499 | "log stderr", |
| 500 | LOG_STR "Logging via STDERR of the process\n") |
| 501 | { |
| 502 | struct log_target *tgt; |
| 503 | |
| 504 | tgt = log_target_find(LOG_TGT_TYPE_STDERR, NULL); |
| 505 | if (!tgt) { |
| 506 | tgt = log_target_create_stderr(); |
| 507 | if (!tgt) { |
| 508 | vty_out(vty, "%% Unable to create stderr log%s", |
| 509 | VTY_NEWLINE); |
| 510 | return CMD_WARNING; |
| 511 | } |
| 512 | log_add_target(tgt); |
| 513 | } |
| 514 | |
| 515 | vty->index = tgt; |
| 516 | vty->node = CFG_LOG_NODE; |
| 517 | |
| 518 | return CMD_SUCCESS; |
| 519 | } |
| 520 | |
| 521 | DEFUN(cfg_no_log_stderr, cfg_no_log_stderr_cmd, |
| 522 | "no log stderr", |
| 523 | NO_STR LOG_STR "Logging via STDERR of the process\n") |
| 524 | { |
| 525 | struct log_target *tgt; |
| 526 | |
| 527 | tgt = log_target_find(LOG_TGT_TYPE_STDERR, NULL); |
| 528 | if (!tgt) { |
| 529 | vty_out(vty, "%% No stderr logging active%s", VTY_NEWLINE); |
| 530 | return CMD_WARNING; |
| 531 | } |
| 532 | |
| 533 | log_target_destroy(tgt); |
| 534 | |
| 535 | return CMD_SUCCESS; |
| 536 | } |
| 537 | |
| 538 | DEFUN(cfg_log_file, cfg_log_file_cmd, |
| 539 | "log file .FILENAME", |
| 540 | LOG_STR "Logging to text file\n" "Filename\n") |
| 541 | { |
| 542 | const char *fname = argv[0]; |
| 543 | struct log_target *tgt; |
| 544 | |
| 545 | tgt = log_target_find(LOG_TGT_TYPE_FILE, fname); |
| 546 | if (!tgt) { |
| 547 | tgt = log_target_create_file(fname); |
| 548 | if (!tgt) { |
| 549 | vty_out(vty, "%% Unable to create file `%s'%s", |
| 550 | fname, VTY_NEWLINE); |
| 551 | return CMD_WARNING; |
| 552 | } |
| 553 | log_add_target(tgt); |
| 554 | } |
| 555 | |
| 556 | vty->index = tgt; |
| 557 | vty->node = CFG_LOG_NODE; |
| 558 | |
| 559 | return CMD_SUCCESS; |
| 560 | } |
| 561 | |
| 562 | |
| 563 | DEFUN(cfg_no_log_file, cfg_no_log_file_cmd, |
| 564 | "no log file .FILENAME", |
| 565 | NO_STR LOG_STR "Logging to text file\n" "Filename\n") |
| 566 | { |
| 567 | const char *fname = argv[0]; |
| 568 | struct log_target *tgt; |
| 569 | |
| 570 | tgt = log_target_find(LOG_TGT_TYPE_FILE, fname); |
| 571 | if (!tgt) { |
| 572 | vty_out(vty, "%% No such log file `%s'%s", |
| 573 | fname, VTY_NEWLINE); |
| 574 | return CMD_WARNING; |
| 575 | } |
| 576 | |
| 577 | log_target_destroy(tgt); |
| 578 | |
| 579 | return CMD_SUCCESS; |
| 580 | } |
| 581 | |
Katerina Barone-Adesi | 3309a43 | 2013-02-21 05:16:29 +0000 | [diff] [blame] | 582 | DEFUN(cfg_log_alarms, cfg_log_alarms_cmd, |
| 583 | "log alarms <2-32700>", |
| 584 | LOG_STR "Logging alarms to osmo_strrb\n" |
| 585 | "Maximum number of messages to log\n") |
| 586 | { |
| 587 | struct log_target *tgt; |
| 588 | unsigned int rbsize = atoi(argv[0]); |
| 589 | |
| 590 | tgt = log_target_find(LOG_TGT_TYPE_STRRB, NULL); |
| 591 | if (tgt) |
| 592 | log_target_destroy(tgt); |
| 593 | |
| 594 | tgt = log_target_create_rb(rbsize); |
| 595 | if (!tgt) { |
| 596 | vty_out(vty, "%% Unable to create osmo_strrb (size %u)%s", |
| 597 | rbsize, VTY_NEWLINE); |
| 598 | return CMD_WARNING; |
| 599 | } |
| 600 | log_add_target(tgt); |
| 601 | |
| 602 | vty->index = tgt; |
| 603 | vty->node = CFG_LOG_NODE; |
| 604 | |
| 605 | return CMD_SUCCESS; |
| 606 | } |
| 607 | |
| 608 | DEFUN(cfg_no_log_alarms, cfg_no_log_alarms_cmd, |
| 609 | "no log alarms", |
| 610 | NO_STR LOG_STR "Logging alarms to osmo_strrb\n") |
| 611 | { |
| 612 | struct log_target *tgt; |
| 613 | |
| 614 | tgt = log_target_find(LOG_TGT_TYPE_STRRB, NULL); |
| 615 | if (!tgt) { |
| 616 | vty_out(vty, "%% No osmo_strrb target found%s", VTY_NEWLINE); |
| 617 | return CMD_WARNING; |
| 618 | } |
| 619 | |
| 620 | log_target_destroy(tgt); |
| 621 | |
| 622 | return CMD_SUCCESS; |
| 623 | } |
| 624 | |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 625 | static int config_write_log_single(struct vty *vty, struct log_target *tgt) |
| 626 | { |
| 627 | int i; |
| 628 | char level_lower[32]; |
| 629 | |
| 630 | switch (tgt->type) { |
| 631 | case LOG_TGT_TYPE_VTY: |
| 632 | return 1; |
| 633 | break; |
| 634 | case LOG_TGT_TYPE_STDERR: |
| 635 | vty_out(vty, "log stderr%s", VTY_NEWLINE); |
| 636 | break; |
| 637 | case LOG_TGT_TYPE_SYSLOG: |
| 638 | #ifdef HAVE_SYSLOG_H |
| 639 | vty_out(vty, "log syslog %s%s", |
| 640 | get_value_string(sysl_level_names, |
| 641 | tgt->tgt_syslog.facility), |
| 642 | VTY_NEWLINE); |
| 643 | #endif |
| 644 | break; |
| 645 | case LOG_TGT_TYPE_FILE: |
| 646 | vty_out(vty, "log file %s%s", tgt->tgt_file.fname, VTY_NEWLINE); |
| 647 | break; |
Katerina Barone-Adesi | 3309a43 | 2013-02-21 05:16:29 +0000 | [diff] [blame] | 648 | case LOG_TGT_TYPE_STRRB: |
| 649 | vty_out(vty, "log alarms %zu%s", |
| 650 | log_target_rb_avail_size(tgt), VTY_NEWLINE); |
| 651 | break; |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 652 | } |
| 653 | |
Harald Welte | 2da47f1 | 2012-10-22 19:31:54 +0200 | [diff] [blame] | 654 | vty_out(vty, " logging filter all %u%s", |
| 655 | tgt->filter_map & LOG_FILTER_ALL ? 1 : 0, VTY_NEWLINE); |
Harald Welte | fb84f32 | 2013-06-06 07:33:54 +0200 | [diff] [blame] | 656 | /* save filters outside of libosmocore, i.e. in app code */ |
| 657 | if (osmo_log_info->save_fn) |
| 658 | osmo_log_info->save_fn(vty, osmo_log_info, tgt); |
Harald Welte | 2da47f1 | 2012-10-22 19:31:54 +0200 | [diff] [blame] | 659 | |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 660 | vty_out(vty, " logging color %u%s", tgt->use_color ? 1 : 0, |
| 661 | VTY_NEWLINE); |
Holger Hans Peter Freyther | 879acef | 2015-01-27 11:06:51 +0100 | [diff] [blame] | 662 | vty_out(vty, " logging print category %d%s", |
Michael McTernan | 7893346 | 2015-03-20 15:29:25 +0100 | [diff] [blame] | 663 | tgt->print_category ? 1 : 0, VTY_NEWLINE); |
Holger Hans Peter Freyther | 2d6ad13 | 2014-12-05 09:35:30 +0100 | [diff] [blame] | 664 | if (tgt->print_ext_timestamp) |
| 665 | vty_out(vty, " logging print extended-timestamp 1%s", VTY_NEWLINE); |
| 666 | else |
| 667 | vty_out(vty, " logging timestamp %u%s", |
| 668 | tgt->print_timestamp ? 1 : 0, VTY_NEWLINE); |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 669 | |
| 670 | /* stupid old osmo logging API uses uppercase strings... */ |
| 671 | osmo_str2lower(level_lower, log_level_str(tgt->loglevel)); |
| 672 | vty_out(vty, " logging level all %s%s", level_lower, VTY_NEWLINE); |
| 673 | |
| 674 | for (i = 0; i < osmo_log_info->num_cat; i++) { |
| 675 | const struct log_category *cat = &tgt->categories[i]; |
| 676 | char cat_lower[32]; |
| 677 | |
Harald Welte | 1a02cfc | 2013-03-19 10:37:39 +0100 | [diff] [blame] | 678 | /* skip empty entries in the array */ |
| 679 | if (!osmo_log_info->cat[i].name) |
| 680 | continue; |
| 681 | |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 682 | /* stupid old osmo logging API uses uppercase strings... */ |
| 683 | osmo_str2lower(cat_lower, osmo_log_info->cat[i].name+1); |
| 684 | osmo_str2lower(level_lower, log_level_str(cat->loglevel)); |
| 685 | |
| 686 | vty_out(vty, " logging level %s %s%s", cat_lower, level_lower, |
| 687 | VTY_NEWLINE); |
| 688 | } |
| 689 | |
| 690 | /* FIXME: levels */ |
| 691 | |
| 692 | return 1; |
| 693 | } |
| 694 | |
| 695 | static int config_write_log(struct vty *vty) |
| 696 | { |
| 697 | struct log_target *dbg = vty->index; |
| 698 | |
| 699 | llist_for_each_entry(dbg, &osmo_log_target_list, entry) |
| 700 | config_write_log_single(vty, dbg); |
| 701 | |
| 702 | return 1; |
| 703 | } |
| 704 | |
Pablo Neira Ayuso | 04139f1 | 2011-03-09 13:05:08 +0100 | [diff] [blame] | 705 | void logging_vty_add_cmds(const struct log_info *cat) |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 706 | { |
| 707 | install_element_ve(&enable_logging_cmd); |
| 708 | install_element_ve(&disable_logging_cmd); |
| 709 | install_element_ve(&logging_fltr_all_cmd); |
| 710 | install_element_ve(&logging_use_clr_cmd); |
| 711 | install_element_ve(&logging_prnt_timestamp_cmd); |
Holger Hans Peter Freyther | 2d6ad13 | 2014-12-05 09:35:30 +0100 | [diff] [blame] | 712 | install_element_ve(&logging_prnt_ext_timestamp_cmd); |
| 713 | install_element_ve(&logging_prnt_cat_cmd); |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 714 | install_element_ve(&logging_set_category_mask_cmd); |
Holger Hans Peter Freyther | 146d1d3 | 2011-10-03 23:15:41 +0200 | [diff] [blame] | 715 | install_element_ve(&logging_set_category_mask_old_cmd); |
Pablo Neira Ayuso | 04139f1 | 2011-03-09 13:05:08 +0100 | [diff] [blame] | 716 | |
| 717 | /* Logging level strings are generated dynamically. */ |
| 718 | logging_level_cmd.string = log_vty_command_string(cat); |
| 719 | logging_level_cmd.doc = log_vty_command_description(cat); |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 720 | install_element_ve(&logging_level_cmd); |
| 721 | install_element_ve(&show_logging_vty_cmd); |
Katerina Barone-Adesi | 3309a43 | 2013-02-21 05:16:29 +0000 | [diff] [blame] | 722 | install_element_ve(&show_alarms_cmd); |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 723 | |
| 724 | install_node(&cfg_log_node, config_write_log); |
Jacob Erlbeck | 0c987bd | 2013-09-06 16:52:00 +0200 | [diff] [blame] | 725 | vty_install_default(CFG_LOG_NODE); |
Harald Welte | a62648b | 2011-02-18 21:03:27 +0100 | [diff] [blame] | 726 | install_element(CFG_LOG_NODE, &logging_fltr_all_cmd); |
| 727 | install_element(CFG_LOG_NODE, &logging_use_clr_cmd); |
| 728 | install_element(CFG_LOG_NODE, &logging_prnt_timestamp_cmd); |
Holger Hans Peter Freyther | 2d6ad13 | 2014-12-05 09:35:30 +0100 | [diff] [blame] | 729 | install_element(CFG_LOG_NODE, &logging_prnt_ext_timestamp_cmd); |
| 730 | install_element(CFG_LOG_NODE, &logging_prnt_cat_cmd); |
Harald Welte | a62648b | 2011-02-18 21:03:27 +0100 | [diff] [blame] | 731 | install_element(CFG_LOG_NODE, &logging_level_cmd); |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 732 | |
| 733 | install_element(CONFIG_NODE, &cfg_log_stderr_cmd); |
| 734 | install_element(CONFIG_NODE, &cfg_no_log_stderr_cmd); |
| 735 | install_element(CONFIG_NODE, &cfg_log_file_cmd); |
| 736 | install_element(CONFIG_NODE, &cfg_no_log_file_cmd); |
Katerina Barone-Adesi | 3309a43 | 2013-02-21 05:16:29 +0000 | [diff] [blame] | 737 | install_element(CONFIG_NODE, &cfg_log_alarms_cmd); |
| 738 | install_element(CONFIG_NODE, &cfg_no_log_alarms_cmd); |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 739 | #ifdef HAVE_SYSLOG_H |
| 740 | install_element(CONFIG_NODE, &cfg_log_syslog_cmd); |
| 741 | install_element(CONFIG_NODE, &cfg_log_syslog_local_cmd); |
| 742 | install_element(CONFIG_NODE, &cfg_no_log_syslog_cmd); |
| 743 | #endif |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 744 | } |