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> |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 30 | |
| 31 | //#include <openbsc/vty.h> |
| 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 | } |
| 250 | } |
| 251 | |
| 252 | #define SHOW_LOG_STR "Show current logging configuration\n" |
| 253 | |
| 254 | DEFUN(show_logging_vty, |
| 255 | show_logging_vty_cmd, |
| 256 | "show logging vty", |
| 257 | SHOW_STR SHOW_LOG_STR |
| 258 | "Show current logging configuration for this vty\n") |
| 259 | { |
Harald Welte | a62648b | 2011-02-18 21:03:27 +0100 | [diff] [blame] | 260 | struct log_target *tgt = osmo_log_vty2tgt(vty); |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 261 | |
Harald Welte | a62648b | 2011-02-18 21:03:27 +0100 | [diff] [blame] | 262 | if (!tgt) |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 263 | return CMD_WARNING; |
Harald Welte | a62648b | 2011-02-18 21:03:27 +0100 | [diff] [blame] | 264 | |
| 265 | vty_print_logtarget(vty, osmo_log_info, tgt); |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 266 | |
| 267 | return CMD_SUCCESS; |
| 268 | } |
| 269 | |
| 270 | gDEFUN(cfg_description, cfg_description_cmd, |
| 271 | "description .TEXT", |
| 272 | "Save human-readable decription of the object\n") |
| 273 | { |
| 274 | char **dptr = vty->index_sub; |
| 275 | |
| 276 | if (!dptr) { |
| 277 | vty_out(vty, "vty->index_sub == NULL%s", VTY_NEWLINE); |
| 278 | return CMD_WARNING; |
| 279 | } |
| 280 | |
Holger Hans Peter Freyther | ff0670e | 2011-02-24 14:20:41 +0100 | [diff] [blame] | 281 | if (*dptr) |
| 282 | talloc_free(*dptr); |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 283 | *dptr = argv_concat(argv, argc, 0); |
| 284 | if (!dptr) |
| 285 | return CMD_WARNING; |
| 286 | |
| 287 | return CMD_SUCCESS; |
| 288 | } |
| 289 | |
| 290 | gDEFUN(cfg_no_description, cfg_no_description_cmd, |
| 291 | "no description", |
| 292 | NO_STR |
| 293 | "Remove description of the object\n") |
| 294 | { |
| 295 | char **dptr = vty->index_sub; |
| 296 | |
| 297 | if (!dptr) { |
| 298 | vty_out(vty, "vty->index_sub == NULL%s", VTY_NEWLINE); |
| 299 | return CMD_WARNING; |
| 300 | } |
| 301 | |
| 302 | if (*dptr) { |
| 303 | talloc_free(*dptr); |
| 304 | *dptr = NULL; |
| 305 | } |
| 306 | |
| 307 | return CMD_SUCCESS; |
| 308 | } |
| 309 | |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 310 | /* Support for configuration of log targets != the current vty */ |
| 311 | |
| 312 | struct cmd_node cfg_log_node = { |
| 313 | CFG_LOG_NODE, |
| 314 | "%s(config-log)# ", |
| 315 | 1 |
| 316 | }; |
| 317 | |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 318 | #ifdef HAVE_SYSLOG_H |
| 319 | |
| 320 | #include <syslog.h> |
| 321 | |
| 322 | static const int local_sysl_map[] = { |
| 323 | [0] = LOG_LOCAL0, |
| 324 | [1] = LOG_LOCAL1, |
| 325 | [2] = LOG_LOCAL2, |
| 326 | [3] = LOG_LOCAL3, |
| 327 | [4] = LOG_LOCAL4, |
| 328 | [5] = LOG_LOCAL5, |
| 329 | [6] = LOG_LOCAL6, |
| 330 | [7] = LOG_LOCAL7 |
| 331 | }; |
| 332 | |
Harald Welte | de79cee | 2011-02-24 23:47:57 +0100 | [diff] [blame] | 333 | /* From VTY core code */ |
| 334 | extern struct host host; |
| 335 | |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 336 | static int _cfg_log_syslog(struct vty *vty, int facility) |
| 337 | { |
| 338 | struct log_target *tgt; |
| 339 | |
| 340 | /* First delete the old syslog target, if any */ |
| 341 | tgt = log_target_find(LOG_TGT_TYPE_SYSLOG, NULL); |
| 342 | if (tgt) |
| 343 | log_target_destroy(tgt); |
| 344 | |
Harald Welte | de79cee | 2011-02-24 23:47:57 +0100 | [diff] [blame] | 345 | tgt = log_target_create_syslog(host.app_info->name, 0, facility); |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 346 | if (!tgt) { |
| 347 | vty_out(vty, "%% Unable to open syslog%s", VTY_NEWLINE); |
| 348 | return CMD_WARNING; |
| 349 | } |
| 350 | log_add_target(tgt); |
| 351 | |
| 352 | vty->index = tgt; |
| 353 | vty->node = CFG_LOG_NODE; |
| 354 | |
| 355 | return CMD_SUCCESS; |
| 356 | } |
| 357 | |
| 358 | DEFUN(cfg_log_syslog_local, cfg_log_syslog_local_cmd, |
| 359 | "log syslog local <0-7>", |
| 360 | LOG_STR "Logging via syslog\n" "Syslog LOCAL facility\n" |
| 361 | "Local facility number\n") |
| 362 | { |
| 363 | int local = atoi(argv[0]); |
| 364 | int facility = local_sysl_map[local]; |
| 365 | |
| 366 | return _cfg_log_syslog(vty, facility); |
| 367 | } |
| 368 | |
| 369 | static struct value_string sysl_level_names[] = { |
| 370 | { LOG_AUTHPRIV, "authpriv" }, |
| 371 | { LOG_CRON, "cron" }, |
| 372 | { LOG_DAEMON, "daemon" }, |
| 373 | { LOG_FTP, "ftp" }, |
| 374 | { LOG_LPR, "lpr" }, |
| 375 | { LOG_MAIL, "mail" }, |
| 376 | { LOG_NEWS, "news" }, |
| 377 | { LOG_USER, "user" }, |
| 378 | { LOG_UUCP, "uucp" }, |
| 379 | /* only for value -> string conversion */ |
| 380 | { LOG_LOCAL0, "local 0" }, |
| 381 | { LOG_LOCAL1, "local 1" }, |
| 382 | { LOG_LOCAL2, "local 2" }, |
| 383 | { LOG_LOCAL3, "local 3" }, |
| 384 | { LOG_LOCAL4, "local 4" }, |
| 385 | { LOG_LOCAL5, "local 5" }, |
| 386 | { LOG_LOCAL6, "local 6" }, |
| 387 | { LOG_LOCAL7, "local 7" }, |
| 388 | { 0, NULL } |
| 389 | }; |
| 390 | |
| 391 | DEFUN(cfg_log_syslog, cfg_log_syslog_cmd, |
| 392 | "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] | 393 | LOG_STR "Logging via syslog\n" |
| 394 | "Security/authorization messages facility\n" |
| 395 | "Clock daemon (cron/at) facility\n" |
| 396 | "General system daemon facility\n" |
| 397 | "Ftp daemon facility\n" |
| 398 | "Line printer facility\n" |
| 399 | "Mail facility\n" |
| 400 | "News facility\n" |
| 401 | "Generic facility\n" |
| 402 | "UUCP facility\n") |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 403 | { |
| 404 | int facility = get_string_value(sysl_level_names, argv[0]); |
| 405 | |
| 406 | return _cfg_log_syslog(vty, facility); |
| 407 | } |
| 408 | |
| 409 | DEFUN(cfg_no_log_syslog, cfg_no_log_syslog_cmd, |
| 410 | "no log syslog", |
| 411 | NO_STR LOG_STR "Logging via syslog\n") |
| 412 | { |
| 413 | struct log_target *tgt; |
| 414 | |
| 415 | tgt = log_target_find(LOG_TGT_TYPE_SYSLOG, NULL); |
| 416 | if (!tgt) { |
| 417 | vty_out(vty, "%% No syslog target found%s", |
| 418 | VTY_NEWLINE); |
| 419 | return CMD_WARNING; |
| 420 | } |
| 421 | |
| 422 | log_target_destroy(tgt); |
| 423 | |
| 424 | return CMD_SUCCESS; |
| 425 | } |
| 426 | #endif /* HAVE_SYSLOG_H */ |
| 427 | |
| 428 | DEFUN(cfg_log_stderr, cfg_log_stderr_cmd, |
| 429 | "log stderr", |
| 430 | LOG_STR "Logging via STDERR of the process\n") |
| 431 | { |
| 432 | struct log_target *tgt; |
| 433 | |
| 434 | tgt = log_target_find(LOG_TGT_TYPE_STDERR, NULL); |
| 435 | if (!tgt) { |
| 436 | tgt = log_target_create_stderr(); |
| 437 | if (!tgt) { |
| 438 | vty_out(vty, "%% Unable to create stderr log%s", |
| 439 | VTY_NEWLINE); |
| 440 | return CMD_WARNING; |
| 441 | } |
| 442 | log_add_target(tgt); |
| 443 | } |
| 444 | |
| 445 | vty->index = tgt; |
| 446 | vty->node = CFG_LOG_NODE; |
| 447 | |
| 448 | return CMD_SUCCESS; |
| 449 | } |
| 450 | |
| 451 | DEFUN(cfg_no_log_stderr, cfg_no_log_stderr_cmd, |
| 452 | "no log stderr", |
| 453 | NO_STR LOG_STR "Logging via STDERR of the process\n") |
| 454 | { |
| 455 | struct log_target *tgt; |
| 456 | |
| 457 | tgt = log_target_find(LOG_TGT_TYPE_STDERR, NULL); |
| 458 | if (!tgt) { |
| 459 | vty_out(vty, "%% No stderr logging active%s", VTY_NEWLINE); |
| 460 | return CMD_WARNING; |
| 461 | } |
| 462 | |
| 463 | log_target_destroy(tgt); |
| 464 | |
| 465 | return CMD_SUCCESS; |
| 466 | } |
| 467 | |
| 468 | DEFUN(cfg_log_file, cfg_log_file_cmd, |
| 469 | "log file .FILENAME", |
| 470 | LOG_STR "Logging to text file\n" "Filename\n") |
| 471 | { |
| 472 | const char *fname = argv[0]; |
| 473 | struct log_target *tgt; |
| 474 | |
| 475 | tgt = log_target_find(LOG_TGT_TYPE_FILE, fname); |
| 476 | if (!tgt) { |
| 477 | tgt = log_target_create_file(fname); |
| 478 | if (!tgt) { |
| 479 | vty_out(vty, "%% Unable to create file `%s'%s", |
| 480 | fname, VTY_NEWLINE); |
| 481 | return CMD_WARNING; |
| 482 | } |
| 483 | log_add_target(tgt); |
| 484 | } |
| 485 | |
| 486 | vty->index = tgt; |
| 487 | vty->node = CFG_LOG_NODE; |
| 488 | |
| 489 | return CMD_SUCCESS; |
| 490 | } |
| 491 | |
| 492 | |
| 493 | DEFUN(cfg_no_log_file, cfg_no_log_file_cmd, |
| 494 | "no log file .FILENAME", |
| 495 | NO_STR LOG_STR "Logging to text file\n" "Filename\n") |
| 496 | { |
| 497 | const char *fname = argv[0]; |
| 498 | struct log_target *tgt; |
| 499 | |
| 500 | tgt = log_target_find(LOG_TGT_TYPE_FILE, fname); |
| 501 | if (!tgt) { |
| 502 | vty_out(vty, "%% No such log file `%s'%s", |
| 503 | fname, VTY_NEWLINE); |
| 504 | return CMD_WARNING; |
| 505 | } |
| 506 | |
| 507 | log_target_destroy(tgt); |
| 508 | |
| 509 | return CMD_SUCCESS; |
| 510 | } |
| 511 | |
| 512 | static int config_write_log_single(struct vty *vty, struct log_target *tgt) |
| 513 | { |
| 514 | int i; |
| 515 | char level_lower[32]; |
| 516 | |
| 517 | switch (tgt->type) { |
| 518 | case LOG_TGT_TYPE_VTY: |
| 519 | return 1; |
| 520 | break; |
| 521 | case LOG_TGT_TYPE_STDERR: |
| 522 | vty_out(vty, "log stderr%s", VTY_NEWLINE); |
| 523 | break; |
| 524 | case LOG_TGT_TYPE_SYSLOG: |
| 525 | #ifdef HAVE_SYSLOG_H |
| 526 | vty_out(vty, "log syslog %s%s", |
| 527 | get_value_string(sysl_level_names, |
| 528 | tgt->tgt_syslog.facility), |
| 529 | VTY_NEWLINE); |
| 530 | #endif |
| 531 | break; |
| 532 | case LOG_TGT_TYPE_FILE: |
| 533 | vty_out(vty, "log file %s%s", tgt->tgt_file.fname, VTY_NEWLINE); |
| 534 | break; |
| 535 | } |
| 536 | |
| 537 | vty_out(vty, " logging color %u%s", tgt->use_color ? 1 : 0, |
| 538 | VTY_NEWLINE); |
| 539 | vty_out(vty, " logging timestamp %u%s", tgt->print_timestamp ? 1 : 0, |
| 540 | VTY_NEWLINE); |
| 541 | |
| 542 | /* stupid old osmo logging API uses uppercase strings... */ |
| 543 | osmo_str2lower(level_lower, log_level_str(tgt->loglevel)); |
| 544 | vty_out(vty, " logging level all %s%s", level_lower, VTY_NEWLINE); |
| 545 | |
| 546 | for (i = 0; i < osmo_log_info->num_cat; i++) { |
| 547 | const struct log_category *cat = &tgt->categories[i]; |
| 548 | char cat_lower[32]; |
| 549 | |
| 550 | /* stupid old osmo logging API uses uppercase strings... */ |
| 551 | osmo_str2lower(cat_lower, osmo_log_info->cat[i].name+1); |
| 552 | osmo_str2lower(level_lower, log_level_str(cat->loglevel)); |
| 553 | |
| 554 | vty_out(vty, " logging level %s %s%s", cat_lower, level_lower, |
| 555 | VTY_NEWLINE); |
| 556 | } |
| 557 | |
| 558 | /* FIXME: levels */ |
| 559 | |
| 560 | return 1; |
| 561 | } |
| 562 | |
| 563 | static int config_write_log(struct vty *vty) |
| 564 | { |
| 565 | struct log_target *dbg = vty->index; |
| 566 | |
| 567 | llist_for_each_entry(dbg, &osmo_log_target_list, entry) |
| 568 | config_write_log_single(vty, dbg); |
| 569 | |
| 570 | return 1; |
| 571 | } |
| 572 | |
Pablo Neira Ayuso | 04139f1 | 2011-03-09 13:05:08 +0100 | [diff] [blame] | 573 | void logging_vty_add_cmds(const struct log_info *cat) |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 574 | { |
| 575 | install_element_ve(&enable_logging_cmd); |
| 576 | install_element_ve(&disable_logging_cmd); |
| 577 | install_element_ve(&logging_fltr_all_cmd); |
| 578 | install_element_ve(&logging_use_clr_cmd); |
| 579 | install_element_ve(&logging_prnt_timestamp_cmd); |
| 580 | install_element_ve(&logging_set_category_mask_cmd); |
Holger Hans Peter Freyther | 146d1d3 | 2011-10-03 23:15:41 +0200 | [diff] [blame] | 581 | install_element_ve(&logging_set_category_mask_old_cmd); |
Pablo Neira Ayuso | 04139f1 | 2011-03-09 13:05:08 +0100 | [diff] [blame] | 582 | |
| 583 | /* Logging level strings are generated dynamically. */ |
| 584 | logging_level_cmd.string = log_vty_command_string(cat); |
| 585 | logging_level_cmd.doc = log_vty_command_description(cat); |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 586 | install_element_ve(&logging_level_cmd); |
| 587 | install_element_ve(&show_logging_vty_cmd); |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 588 | |
| 589 | install_node(&cfg_log_node, config_write_log); |
Harald Welte | a62648b | 2011-02-18 21:03:27 +0100 | [diff] [blame] | 590 | install_element(CFG_LOG_NODE, &logging_fltr_all_cmd); |
| 591 | install_element(CFG_LOG_NODE, &logging_use_clr_cmd); |
| 592 | install_element(CFG_LOG_NODE, &logging_prnt_timestamp_cmd); |
| 593 | install_element(CFG_LOG_NODE, &logging_level_cmd); |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 594 | |
| 595 | install_element(CONFIG_NODE, &cfg_log_stderr_cmd); |
| 596 | install_element(CONFIG_NODE, &cfg_no_log_stderr_cmd); |
| 597 | install_element(CONFIG_NODE, &cfg_log_file_cmd); |
| 598 | install_element(CONFIG_NODE, &cfg_no_log_file_cmd); |
| 599 | #ifdef HAVE_SYSLOG_H |
| 600 | install_element(CONFIG_NODE, &cfg_log_syslog_cmd); |
| 601 | install_element(CONFIG_NODE, &cfg_log_syslog_local_cmd); |
| 602 | install_element(CONFIG_NODE, &cfg_no_log_syslog_cmd); |
| 603 | #endif |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 604 | } |