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> |
| 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 <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 | |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 152 | DEFUN(logging_level, |
| 153 | logging_level_cmd, |
Pablo Neira Ayuso | 04139f1 | 2011-03-09 13:05:08 +0100 | [diff] [blame] | 154 | NULL, /* cmdstr is dynamically set in logging_vty_add_cmds(). */ |
| 155 | NULL) /* same thing for helpstr. */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 156 | { |
Harald Welte | a62648b | 2011-02-18 21:03:27 +0100 | [diff] [blame] | 157 | int category = log_parse_category(argv[0]); |
| 158 | int level = log_parse_level(argv[1]); |
| 159 | struct log_target *tgt = osmo_log_vty2tgt(vty); |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 160 | |
Harald Welte | a62648b | 2011-02-18 21:03:27 +0100 | [diff] [blame] | 161 | if (!tgt) |
| 162 | return CMD_WARNING; |
| 163 | |
| 164 | if (level < 0) { |
| 165 | vty_out(vty, "Invalid level `%s'%s", argv[1], VTY_NEWLINE); |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 166 | return CMD_WARNING; |
| 167 | } |
| 168 | |
Harald Welte | a62648b | 2011-02-18 21:03:27 +0100 | [diff] [blame] | 169 | /* Check for special case where we want to set global log level */ |
| 170 | if (!strcmp(argv[0], "all")) { |
| 171 | log_set_log_level(tgt, level); |
| 172 | return CMD_SUCCESS; |
| 173 | } |
| 174 | |
| 175 | if (category < 0) { |
| 176 | vty_out(vty, "Invalid category `%s'%s", argv[0], VTY_NEWLINE); |
| 177 | return CMD_WARNING; |
| 178 | } |
| 179 | |
| 180 | tgt->categories[category].enabled = 1; |
| 181 | tgt->categories[category].loglevel = level; |
| 182 | |
| 183 | return CMD_SUCCESS; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | DEFUN(logging_set_category_mask, |
| 187 | logging_set_category_mask_cmd, |
Holger Hans Peter Freyther | 146d1d3 | 2011-10-03 23:15:41 +0200 | [diff] [blame] | 188 | "logging set-log-mask MASK", |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 189 | LOGGING_STR |
Holger Hans Peter Freyther | 146d1d3 | 2011-10-03 23:15:41 +0200 | [diff] [blame] | 190 | "Set the logmask of this logging target\n" |
| 191 | "The logmask to use\n") |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 192 | { |
Harald Welte | a62648b | 2011-02-18 21:03:27 +0100 | [diff] [blame] | 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) |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 196 | return CMD_WARNING; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 197 | |
Harald Welte | a62648b | 2011-02-18 21:03:27 +0100 | [diff] [blame] | 198 | log_parse_category_mask(tgt, argv[0]); |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 199 | return CMD_SUCCESS; |
| 200 | } |
| 201 | |
Holger Hans Peter Freyther | 146d1d3 | 2011-10-03 23:15:41 +0200 | [diff] [blame] | 202 | ALIAS_DEPRECATED(logging_set_category_mask, |
| 203 | logging_set_category_mask_old_cmd, |
| 204 | "logging set log mask MASK", |
| 205 | LOGGING_STR |
| 206 | "Decide which categories to output.\n" |
| 207 | "Log commands\n" "Mask commands\n" "The logmask to use\n"); |
| 208 | |
| 209 | |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 210 | DEFUN(diable_logging, |
| 211 | disable_logging_cmd, |
| 212 | "logging disable", |
| 213 | LOGGING_STR |
| 214 | "Disables logging to this vty\n") |
| 215 | { |
Harald Welte | a62648b | 2011-02-18 21:03:27 +0100 | [diff] [blame] | 216 | struct log_target *tgt = osmo_log_vty2tgt(vty); |
| 217 | struct telnet_connection *conn = (struct telnet_connection *) vty->priv; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 218 | |
Harald Welte | a62648b | 2011-02-18 21:03:27 +0100 | [diff] [blame] | 219 | if (!tgt) |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 220 | return CMD_WARNING; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 221 | |
Harald Welte | a62648b | 2011-02-18 21:03:27 +0100 | [diff] [blame] | 222 | log_del_target(tgt); |
| 223 | talloc_free(tgt); |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 224 | conn->dbg = NULL; |
Harald Welte | a62648b | 2011-02-18 21:03:27 +0100 | [diff] [blame] | 225 | |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 226 | return CMD_SUCCESS; |
| 227 | } |
| 228 | |
| 229 | static void vty_print_logtarget(struct vty *vty, const struct log_info *info, |
| 230 | const struct log_target *tgt) |
| 231 | { |
| 232 | unsigned int i; |
| 233 | |
| 234 | vty_out(vty, " Global Loglevel: %s%s", |
| 235 | log_level_str(tgt->loglevel), VTY_NEWLINE); |
| 236 | vty_out(vty, " Use color: %s, Print Timestamp: %s%s", |
| 237 | tgt->use_color ? "On" : "Off", |
| 238 | tgt->print_timestamp ? "On" : "Off", VTY_NEWLINE); |
| 239 | |
| 240 | vty_out(vty, " Log Level specific information:%s", VTY_NEWLINE); |
| 241 | |
| 242 | for (i = 0; i < info->num_cat; i++) { |
| 243 | const struct log_category *cat = &tgt->categories[i]; |
| 244 | vty_out(vty, " %-10s %-10s %-8s %s%s", |
| 245 | info->cat[i].name+1, log_level_str(cat->loglevel), |
| 246 | cat->enabled ? "Enabled" : "Disabled", |
| 247 | info->cat[i].description, |
| 248 | VTY_NEWLINE); |
| 249 | } |
Harald Welte | 6d2d4d6 | 2013-03-10 09:53:24 +0000 | [diff] [blame] | 250 | |
| 251 | vty_out(vty, " Log Filter 'ALL': %s%s", |
| 252 | tgt->filter_map & LOG_FILTER_ALL ? "Enabled" : "Disabled", |
| 253 | VTY_NEWLINE); |
| 254 | |
| 255 | /* FIXME: print application specific filters */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | #define SHOW_LOG_STR "Show current logging configuration\n" |
| 259 | |
| 260 | DEFUN(show_logging_vty, |
Katerina Barone-Adesi | 3309a43 | 2013-02-21 05:16:29 +0000 | [diff] [blame] | 261 | show_logging_vty_cmd, |
| 262 | "show logging vty", |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 263 | SHOW_STR SHOW_LOG_STR |
| 264 | "Show current logging configuration for this vty\n") |
| 265 | { |
Harald Welte | a62648b | 2011-02-18 21:03:27 +0100 | [diff] [blame] | 266 | struct log_target *tgt = osmo_log_vty2tgt(vty); |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 267 | |
Harald Welte | a62648b | 2011-02-18 21:03:27 +0100 | [diff] [blame] | 268 | if (!tgt) |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 269 | return CMD_WARNING; |
Harald Welte | a62648b | 2011-02-18 21:03:27 +0100 | [diff] [blame] | 270 | |
| 271 | vty_print_logtarget(vty, osmo_log_info, tgt); |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 272 | |
| 273 | return CMD_SUCCESS; |
| 274 | } |
| 275 | |
Katerina Barone-Adesi | 3309a43 | 2013-02-21 05:16:29 +0000 | [diff] [blame] | 276 | DEFUN(show_alarms, |
| 277 | show_alarms_cmd, |
| 278 | "show alarms", |
| 279 | SHOW_STR SHOW_LOG_STR |
| 280 | "Show the contents of the logging ringbuffer\n") |
| 281 | { |
| 282 | int i, num_alarms; |
| 283 | struct osmo_strrb *rb; |
| 284 | struct log_target *tgt = log_target_find(LOG_TGT_TYPE_STRRB, NULL); |
| 285 | if (!tgt) { |
| 286 | vty_out(vty, "%% No alarms, run 'log alarms <2-32700>'%s", |
| 287 | VTY_NEWLINE); |
| 288 | return CMD_WARNING; |
| 289 | } |
| 290 | |
| 291 | rb = tgt->tgt_rb.rb; |
| 292 | num_alarms = osmo_strrb_elements(rb); |
| 293 | |
| 294 | vty_out(vty, "%% Showing %i alarms%s", num_alarms, VTY_NEWLINE); |
| 295 | |
| 296 | for (i = 0; i < num_alarms; i++) |
| 297 | vty_out(vty, "%% %s%s", osmo_strrb_get_nth(rb, i), |
| 298 | VTY_NEWLINE); |
| 299 | |
| 300 | return CMD_SUCCESS; |
| 301 | } |
| 302 | |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 303 | gDEFUN(cfg_description, cfg_description_cmd, |
| 304 | "description .TEXT", |
Holger Hans Peter Freyther | c9b3e06 | 2012-07-25 13:02:49 +0200 | [diff] [blame] | 305 | "Save human-readable decription of the object\n" |
| 306 | "Text until the end of the line\n") |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 307 | { |
| 308 | char **dptr = vty->index_sub; |
| 309 | |
| 310 | if (!dptr) { |
| 311 | vty_out(vty, "vty->index_sub == NULL%s", VTY_NEWLINE); |
| 312 | return CMD_WARNING; |
| 313 | } |
| 314 | |
Holger Hans Peter Freyther | ff0670e | 2011-02-24 14:20:41 +0100 | [diff] [blame] | 315 | if (*dptr) |
| 316 | talloc_free(*dptr); |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 317 | *dptr = argv_concat(argv, argc, 0); |
Holger Hans Peter Freyther | 6a75d16 | 2013-07-14 09:07:11 +0200 | [diff] [blame] | 318 | if (!*dptr) |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 319 | return CMD_WARNING; |
| 320 | |
| 321 | return CMD_SUCCESS; |
| 322 | } |
| 323 | |
| 324 | gDEFUN(cfg_no_description, cfg_no_description_cmd, |
| 325 | "no description", |
| 326 | NO_STR |
| 327 | "Remove description of the object\n") |
| 328 | { |
| 329 | char **dptr = vty->index_sub; |
| 330 | |
| 331 | if (!dptr) { |
| 332 | vty_out(vty, "vty->index_sub == NULL%s", VTY_NEWLINE); |
| 333 | return CMD_WARNING; |
| 334 | } |
| 335 | |
| 336 | if (*dptr) { |
| 337 | talloc_free(*dptr); |
| 338 | *dptr = NULL; |
| 339 | } |
| 340 | |
| 341 | return CMD_SUCCESS; |
| 342 | } |
| 343 | |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 344 | /* Support for configuration of log targets != the current vty */ |
| 345 | |
| 346 | struct cmd_node cfg_log_node = { |
| 347 | CFG_LOG_NODE, |
| 348 | "%s(config-log)# ", |
| 349 | 1 |
| 350 | }; |
| 351 | |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 352 | #ifdef HAVE_SYSLOG_H |
| 353 | |
| 354 | #include <syslog.h> |
| 355 | |
| 356 | static const int local_sysl_map[] = { |
| 357 | [0] = LOG_LOCAL0, |
| 358 | [1] = LOG_LOCAL1, |
| 359 | [2] = LOG_LOCAL2, |
| 360 | [3] = LOG_LOCAL3, |
| 361 | [4] = LOG_LOCAL4, |
| 362 | [5] = LOG_LOCAL5, |
| 363 | [6] = LOG_LOCAL6, |
| 364 | [7] = LOG_LOCAL7 |
| 365 | }; |
| 366 | |
Harald Welte | de79cee | 2011-02-24 23:47:57 +0100 | [diff] [blame] | 367 | /* From VTY core code */ |
| 368 | extern struct host host; |
| 369 | |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 370 | static int _cfg_log_syslog(struct vty *vty, int facility) |
| 371 | { |
| 372 | struct log_target *tgt; |
| 373 | |
| 374 | /* First delete the old syslog target, if any */ |
| 375 | tgt = log_target_find(LOG_TGT_TYPE_SYSLOG, NULL); |
| 376 | if (tgt) |
| 377 | log_target_destroy(tgt); |
| 378 | |
Harald Welte | de79cee | 2011-02-24 23:47:57 +0100 | [diff] [blame] | 379 | tgt = log_target_create_syslog(host.app_info->name, 0, facility); |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 380 | if (!tgt) { |
| 381 | vty_out(vty, "%% Unable to open syslog%s", VTY_NEWLINE); |
| 382 | return CMD_WARNING; |
| 383 | } |
| 384 | log_add_target(tgt); |
| 385 | |
| 386 | vty->index = tgt; |
| 387 | vty->node = CFG_LOG_NODE; |
| 388 | |
| 389 | return CMD_SUCCESS; |
| 390 | } |
| 391 | |
| 392 | DEFUN(cfg_log_syslog_local, cfg_log_syslog_local_cmd, |
| 393 | "log syslog local <0-7>", |
| 394 | LOG_STR "Logging via syslog\n" "Syslog LOCAL facility\n" |
| 395 | "Local facility number\n") |
| 396 | { |
| 397 | int local = atoi(argv[0]); |
| 398 | int facility = local_sysl_map[local]; |
| 399 | |
| 400 | return _cfg_log_syslog(vty, facility); |
| 401 | } |
| 402 | |
| 403 | static struct value_string sysl_level_names[] = { |
| 404 | { LOG_AUTHPRIV, "authpriv" }, |
| 405 | { LOG_CRON, "cron" }, |
| 406 | { LOG_DAEMON, "daemon" }, |
| 407 | { LOG_FTP, "ftp" }, |
| 408 | { LOG_LPR, "lpr" }, |
| 409 | { LOG_MAIL, "mail" }, |
| 410 | { LOG_NEWS, "news" }, |
| 411 | { LOG_USER, "user" }, |
| 412 | { LOG_UUCP, "uucp" }, |
| 413 | /* only for value -> string conversion */ |
| 414 | { LOG_LOCAL0, "local 0" }, |
| 415 | { LOG_LOCAL1, "local 1" }, |
| 416 | { LOG_LOCAL2, "local 2" }, |
| 417 | { LOG_LOCAL3, "local 3" }, |
| 418 | { LOG_LOCAL4, "local 4" }, |
| 419 | { LOG_LOCAL5, "local 5" }, |
| 420 | { LOG_LOCAL6, "local 6" }, |
| 421 | { LOG_LOCAL7, "local 7" }, |
| 422 | { 0, NULL } |
| 423 | }; |
| 424 | |
| 425 | DEFUN(cfg_log_syslog, cfg_log_syslog_cmd, |
| 426 | "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] | 427 | LOG_STR "Logging via syslog\n" |
| 428 | "Security/authorization messages facility\n" |
| 429 | "Clock daemon (cron/at) facility\n" |
| 430 | "General system daemon facility\n" |
| 431 | "Ftp daemon facility\n" |
| 432 | "Line printer facility\n" |
| 433 | "Mail facility\n" |
| 434 | "News facility\n" |
| 435 | "Generic facility\n" |
| 436 | "UUCP facility\n") |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 437 | { |
| 438 | int facility = get_string_value(sysl_level_names, argv[0]); |
| 439 | |
| 440 | return _cfg_log_syslog(vty, facility); |
| 441 | } |
| 442 | |
| 443 | DEFUN(cfg_no_log_syslog, cfg_no_log_syslog_cmd, |
| 444 | "no log syslog", |
| 445 | NO_STR LOG_STR "Logging via syslog\n") |
| 446 | { |
| 447 | struct log_target *tgt; |
| 448 | |
| 449 | tgt = log_target_find(LOG_TGT_TYPE_SYSLOG, NULL); |
| 450 | if (!tgt) { |
| 451 | vty_out(vty, "%% No syslog target found%s", |
| 452 | VTY_NEWLINE); |
| 453 | return CMD_WARNING; |
| 454 | } |
| 455 | |
| 456 | log_target_destroy(tgt); |
| 457 | |
| 458 | return CMD_SUCCESS; |
| 459 | } |
| 460 | #endif /* HAVE_SYSLOG_H */ |
| 461 | |
| 462 | DEFUN(cfg_log_stderr, cfg_log_stderr_cmd, |
| 463 | "log stderr", |
| 464 | LOG_STR "Logging via STDERR of the process\n") |
| 465 | { |
| 466 | struct log_target *tgt; |
| 467 | |
| 468 | tgt = log_target_find(LOG_TGT_TYPE_STDERR, NULL); |
| 469 | if (!tgt) { |
| 470 | tgt = log_target_create_stderr(); |
| 471 | if (!tgt) { |
| 472 | vty_out(vty, "%% Unable to create stderr log%s", |
| 473 | VTY_NEWLINE); |
| 474 | return CMD_WARNING; |
| 475 | } |
| 476 | log_add_target(tgt); |
| 477 | } |
| 478 | |
| 479 | vty->index = tgt; |
| 480 | vty->node = CFG_LOG_NODE; |
| 481 | |
| 482 | return CMD_SUCCESS; |
| 483 | } |
| 484 | |
| 485 | DEFUN(cfg_no_log_stderr, cfg_no_log_stderr_cmd, |
| 486 | "no log stderr", |
| 487 | NO_STR LOG_STR "Logging via STDERR of the process\n") |
| 488 | { |
| 489 | struct log_target *tgt; |
| 490 | |
| 491 | tgt = log_target_find(LOG_TGT_TYPE_STDERR, NULL); |
| 492 | if (!tgt) { |
| 493 | vty_out(vty, "%% No stderr logging active%s", VTY_NEWLINE); |
| 494 | return CMD_WARNING; |
| 495 | } |
| 496 | |
| 497 | log_target_destroy(tgt); |
| 498 | |
| 499 | return CMD_SUCCESS; |
| 500 | } |
| 501 | |
| 502 | DEFUN(cfg_log_file, cfg_log_file_cmd, |
| 503 | "log file .FILENAME", |
| 504 | LOG_STR "Logging to text file\n" "Filename\n") |
| 505 | { |
| 506 | const char *fname = argv[0]; |
| 507 | struct log_target *tgt; |
| 508 | |
| 509 | tgt = log_target_find(LOG_TGT_TYPE_FILE, fname); |
| 510 | if (!tgt) { |
| 511 | tgt = log_target_create_file(fname); |
| 512 | if (!tgt) { |
| 513 | vty_out(vty, "%% Unable to create file `%s'%s", |
| 514 | fname, VTY_NEWLINE); |
| 515 | return CMD_WARNING; |
| 516 | } |
| 517 | log_add_target(tgt); |
| 518 | } |
| 519 | |
| 520 | vty->index = tgt; |
| 521 | vty->node = CFG_LOG_NODE; |
| 522 | |
| 523 | return CMD_SUCCESS; |
| 524 | } |
| 525 | |
| 526 | |
| 527 | DEFUN(cfg_no_log_file, cfg_no_log_file_cmd, |
| 528 | "no log file .FILENAME", |
| 529 | NO_STR LOG_STR "Logging to text file\n" "Filename\n") |
| 530 | { |
| 531 | const char *fname = argv[0]; |
| 532 | struct log_target *tgt; |
| 533 | |
| 534 | tgt = log_target_find(LOG_TGT_TYPE_FILE, fname); |
| 535 | if (!tgt) { |
| 536 | vty_out(vty, "%% No such log file `%s'%s", |
| 537 | fname, VTY_NEWLINE); |
| 538 | return CMD_WARNING; |
| 539 | } |
| 540 | |
| 541 | log_target_destroy(tgt); |
| 542 | |
| 543 | return CMD_SUCCESS; |
| 544 | } |
| 545 | |
Katerina Barone-Adesi | 3309a43 | 2013-02-21 05:16:29 +0000 | [diff] [blame] | 546 | DEFUN(cfg_log_alarms, cfg_log_alarms_cmd, |
| 547 | "log alarms <2-32700>", |
| 548 | LOG_STR "Logging alarms to osmo_strrb\n" |
| 549 | "Maximum number of messages to log\n") |
| 550 | { |
| 551 | struct log_target *tgt; |
| 552 | unsigned int rbsize = atoi(argv[0]); |
| 553 | |
| 554 | tgt = log_target_find(LOG_TGT_TYPE_STRRB, NULL); |
| 555 | if (tgt) |
| 556 | log_target_destroy(tgt); |
| 557 | |
| 558 | tgt = log_target_create_rb(rbsize); |
| 559 | if (!tgt) { |
| 560 | vty_out(vty, "%% Unable to create osmo_strrb (size %u)%s", |
| 561 | rbsize, VTY_NEWLINE); |
| 562 | return CMD_WARNING; |
| 563 | } |
| 564 | log_add_target(tgt); |
| 565 | |
| 566 | vty->index = tgt; |
| 567 | vty->node = CFG_LOG_NODE; |
| 568 | |
| 569 | return CMD_SUCCESS; |
| 570 | } |
| 571 | |
| 572 | DEFUN(cfg_no_log_alarms, cfg_no_log_alarms_cmd, |
| 573 | "no log alarms", |
| 574 | NO_STR LOG_STR "Logging alarms to osmo_strrb\n") |
| 575 | { |
| 576 | struct log_target *tgt; |
| 577 | |
| 578 | tgt = log_target_find(LOG_TGT_TYPE_STRRB, NULL); |
| 579 | if (!tgt) { |
| 580 | vty_out(vty, "%% No osmo_strrb target found%s", VTY_NEWLINE); |
| 581 | return CMD_WARNING; |
| 582 | } |
| 583 | |
| 584 | log_target_destroy(tgt); |
| 585 | |
| 586 | return CMD_SUCCESS; |
| 587 | } |
| 588 | |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 589 | static int config_write_log_single(struct vty *vty, struct log_target *tgt) |
| 590 | { |
| 591 | int i; |
| 592 | char level_lower[32]; |
| 593 | |
| 594 | switch (tgt->type) { |
| 595 | case LOG_TGT_TYPE_VTY: |
| 596 | return 1; |
| 597 | break; |
| 598 | case LOG_TGT_TYPE_STDERR: |
| 599 | vty_out(vty, "log stderr%s", VTY_NEWLINE); |
| 600 | break; |
| 601 | case LOG_TGT_TYPE_SYSLOG: |
| 602 | #ifdef HAVE_SYSLOG_H |
| 603 | vty_out(vty, "log syslog %s%s", |
| 604 | get_value_string(sysl_level_names, |
| 605 | tgt->tgt_syslog.facility), |
| 606 | VTY_NEWLINE); |
| 607 | #endif |
| 608 | break; |
| 609 | case LOG_TGT_TYPE_FILE: |
| 610 | vty_out(vty, "log file %s%s", tgt->tgt_file.fname, VTY_NEWLINE); |
| 611 | break; |
Katerina Barone-Adesi | 3309a43 | 2013-02-21 05:16:29 +0000 | [diff] [blame] | 612 | case LOG_TGT_TYPE_STRRB: |
| 613 | vty_out(vty, "log alarms %zu%s", |
| 614 | log_target_rb_avail_size(tgt), VTY_NEWLINE); |
| 615 | break; |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 616 | } |
| 617 | |
Harald Welte | 2da47f1 | 2012-10-22 19:31:54 +0200 | [diff] [blame] | 618 | vty_out(vty, " logging filter all %u%s", |
| 619 | tgt->filter_map & LOG_FILTER_ALL ? 1 : 0, VTY_NEWLINE); |
| 620 | /* FIXME: how to do this for filters outside of libosmocore? */ |
| 621 | |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 622 | vty_out(vty, " logging color %u%s", tgt->use_color ? 1 : 0, |
| 623 | VTY_NEWLINE); |
| 624 | vty_out(vty, " logging timestamp %u%s", tgt->print_timestamp ? 1 : 0, |
| 625 | VTY_NEWLINE); |
| 626 | |
| 627 | /* stupid old osmo logging API uses uppercase strings... */ |
| 628 | osmo_str2lower(level_lower, log_level_str(tgt->loglevel)); |
| 629 | vty_out(vty, " logging level all %s%s", level_lower, VTY_NEWLINE); |
| 630 | |
| 631 | for (i = 0; i < osmo_log_info->num_cat; i++) { |
| 632 | const struct log_category *cat = &tgt->categories[i]; |
| 633 | char cat_lower[32]; |
| 634 | |
Harald Welte | 1a02cfc | 2013-03-19 10:37:39 +0100 | [diff] [blame] | 635 | /* skip empty entries in the array */ |
| 636 | if (!osmo_log_info->cat[i].name) |
| 637 | continue; |
| 638 | |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 639 | /* stupid old osmo logging API uses uppercase strings... */ |
| 640 | osmo_str2lower(cat_lower, osmo_log_info->cat[i].name+1); |
| 641 | osmo_str2lower(level_lower, log_level_str(cat->loglevel)); |
| 642 | |
| 643 | vty_out(vty, " logging level %s %s%s", cat_lower, level_lower, |
| 644 | VTY_NEWLINE); |
| 645 | } |
| 646 | |
| 647 | /* FIXME: levels */ |
| 648 | |
| 649 | return 1; |
| 650 | } |
| 651 | |
| 652 | static int config_write_log(struct vty *vty) |
| 653 | { |
| 654 | struct log_target *dbg = vty->index; |
| 655 | |
| 656 | llist_for_each_entry(dbg, &osmo_log_target_list, entry) |
| 657 | config_write_log_single(vty, dbg); |
| 658 | |
| 659 | return 1; |
| 660 | } |
| 661 | |
Pablo Neira Ayuso | 04139f1 | 2011-03-09 13:05:08 +0100 | [diff] [blame] | 662 | void logging_vty_add_cmds(const struct log_info *cat) |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 663 | { |
| 664 | install_element_ve(&enable_logging_cmd); |
| 665 | install_element_ve(&disable_logging_cmd); |
| 666 | install_element_ve(&logging_fltr_all_cmd); |
| 667 | install_element_ve(&logging_use_clr_cmd); |
| 668 | install_element_ve(&logging_prnt_timestamp_cmd); |
| 669 | install_element_ve(&logging_set_category_mask_cmd); |
Holger Hans Peter Freyther | 146d1d3 | 2011-10-03 23:15:41 +0200 | [diff] [blame] | 670 | install_element_ve(&logging_set_category_mask_old_cmd); |
Pablo Neira Ayuso | 04139f1 | 2011-03-09 13:05:08 +0100 | [diff] [blame] | 671 | |
| 672 | /* Logging level strings are generated dynamically. */ |
| 673 | logging_level_cmd.string = log_vty_command_string(cat); |
| 674 | logging_level_cmd.doc = log_vty_command_description(cat); |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 675 | install_element_ve(&logging_level_cmd); |
| 676 | install_element_ve(&show_logging_vty_cmd); |
Katerina Barone-Adesi | 3309a43 | 2013-02-21 05:16:29 +0000 | [diff] [blame] | 677 | install_element_ve(&show_alarms_cmd); |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 678 | |
| 679 | install_node(&cfg_log_node, config_write_log); |
Jacob Erlbeck | 0c987bd | 2013-09-06 16:52:00 +0200 | [diff] [blame] | 680 | vty_install_default(CFG_LOG_NODE); |
Harald Welte | a62648b | 2011-02-18 21:03:27 +0100 | [diff] [blame] | 681 | install_element(CFG_LOG_NODE, &logging_fltr_all_cmd); |
| 682 | install_element(CFG_LOG_NODE, &logging_use_clr_cmd); |
| 683 | install_element(CFG_LOG_NODE, &logging_prnt_timestamp_cmd); |
| 684 | install_element(CFG_LOG_NODE, &logging_level_cmd); |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 685 | |
| 686 | install_element(CONFIG_NODE, &cfg_log_stderr_cmd); |
| 687 | install_element(CONFIG_NODE, &cfg_no_log_stderr_cmd); |
| 688 | install_element(CONFIG_NODE, &cfg_log_file_cmd); |
| 689 | install_element(CONFIG_NODE, &cfg_no_log_file_cmd); |
Katerina Barone-Adesi | 3309a43 | 2013-02-21 05:16:29 +0000 | [diff] [blame] | 690 | install_element(CONFIG_NODE, &cfg_log_alarms_cmd); |
| 691 | install_element(CONFIG_NODE, &cfg_no_log_alarms_cmd); |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 692 | #ifdef HAVE_SYSLOG_H |
| 693 | install_element(CONFIG_NODE, &cfg_log_syslog_cmd); |
| 694 | install_element(CONFIG_NODE, &cfg_log_syslog_local_cmd); |
| 695 | install_element(CONFIG_NODE, &cfg_no_log_syslog_cmd); |
| 696 | #endif |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 697 | } |