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 | |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 27 | #include <osmocore/talloc.h> |
| 28 | #include <osmocore/logging.h> |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 29 | #include <osmocore/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 | |
| 88 | DEFUN(logging_fltr_all, |
| 89 | logging_fltr_all_cmd, |
| 90 | "logging filter all (0|1)", |
| 91 | LOGGING_STR FILTER_STR |
| 92 | "Do you want to log all messages?\n" |
| 93 | "Only print messages matched by other filters\n" |
| 94 | "Bypass filter and print all messages\n") |
| 95 | { |
| 96 | struct telnet_connection *conn; |
| 97 | |
| 98 | conn = (struct telnet_connection *) vty->priv; |
| 99 | if (!conn->dbg) { |
| 100 | vty_out(vty, "Logging was not enabled.%s", VTY_NEWLINE); |
| 101 | return CMD_WARNING; |
| 102 | } |
| 103 | |
| 104 | log_set_all_filter(conn->dbg, atoi(argv[0])); |
| 105 | return CMD_SUCCESS; |
| 106 | } |
| 107 | |
| 108 | DEFUN(logging_use_clr, |
| 109 | logging_use_clr_cmd, |
| 110 | "logging color (0|1)", |
| 111 | LOGGING_STR "Configure color-printing for log messages\n" |
| 112 | "Don't use color for printing messages\n" |
| 113 | "Use color for printing messages\n") |
| 114 | { |
| 115 | struct telnet_connection *conn; |
| 116 | |
| 117 | conn = (struct telnet_connection *) vty->priv; |
| 118 | if (!conn->dbg) { |
| 119 | vty_out(vty, "Logging was not enabled.%s", VTY_NEWLINE); |
| 120 | return CMD_WARNING; |
| 121 | } |
| 122 | |
| 123 | log_set_use_color(conn->dbg, atoi(argv[0])); |
| 124 | return CMD_SUCCESS; |
| 125 | } |
| 126 | |
| 127 | DEFUN(logging_prnt_timestamp, |
| 128 | logging_prnt_timestamp_cmd, |
| 129 | "logging timestamp (0|1)", |
| 130 | LOGGING_STR "Configure log message timestamping\n" |
| 131 | "Don't prefix each log message\n" |
| 132 | "Prefix each log message with current timestamp\n") |
| 133 | { |
| 134 | struct telnet_connection *conn; |
| 135 | |
| 136 | conn = (struct telnet_connection *) vty->priv; |
| 137 | if (!conn->dbg) { |
| 138 | vty_out(vty, "Logging was not enabled.%s", VTY_NEWLINE); |
| 139 | return CMD_WARNING; |
| 140 | } |
| 141 | |
| 142 | log_set_print_timestamp(conn->dbg, atoi(argv[0])); |
| 143 | return CMD_SUCCESS; |
| 144 | } |
| 145 | |
| 146 | /* FIXME: those have to be kept in sync with the log levels and categories */ |
Harald Welte | 6b43103 | 2011-02-18 20:39:36 +0100 | [diff] [blame^] | 147 | #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|isup|m2ua|pcap|nat|all)" |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 148 | #define CATEGORIES_HELP \ |
| 149 | "A-bis Radio Link Layer (RLL)\n" \ |
| 150 | "Layer3 Call Control (CC)\n" \ |
| 151 | "Layer3 Mobility Management (MM)\n" \ |
| 152 | "Layer3 Radio Resource (RR)\n" \ |
| 153 | "A-bis Radio Signalling Link (RSL)\n" \ |
| 154 | "A-bis Network Management / O&M (NM/OML)\n" \ |
| 155 | "Layer3 Short Messagaging Service (SMS)\n" \ |
| 156 | "Paging Subsystem\n" \ |
| 157 | "MNCC API for Call Control application\n" \ |
| 158 | "A-bis Input Subsystem\n" \ |
| 159 | "A-bis Input Driver for Signalling\n" \ |
| 160 | "A-bis Input Driver for B-Channel (voice data)\n" \ |
| 161 | "A-bis B-Channel / Sub-channel Multiplexer\n" \ |
| 162 | "Radio Measurements\n" \ |
| 163 | "SCCP\n" \ |
| 164 | "Mobile Switching Center\n" \ |
| 165 | "Media Gateway Control Protocol\n" \ |
| 166 | "Hand-over\n" \ |
| 167 | "Database Layer\n" \ |
| 168 | "Reference Counting\n" \ |
| 169 | "GPRS Core\n" \ |
| 170 | "GPRS Network Service (NS)\n" \ |
| 171 | "GPRS BSS Gateway Protocol (BSSGP)\n" \ |
| 172 | "GPRS Logical Link Control Protocol (LLC)\n" \ |
| 173 | "GPRS Sub-Network Dependent Control Protocol (SNDCP)\n" \ |
Holger Hans Peter Freyther | 549fbb8 | 2011-01-22 23:17:21 +0100 | [diff] [blame] | 174 | "ISDN User Part (ISUP)\n" \ |
| 175 | "SCTP M2UA\n" \ |
| 176 | "Trace message IO\n" \ |
Harald Welte | 6b43103 | 2011-02-18 20:39:36 +0100 | [diff] [blame^] | 177 | "BSC NAT\n" \ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 178 | "Global setting for all subsytems\n" |
| 179 | |
| 180 | #define VTY_DEBUG_LEVELS "(everything|debug|info|notice|error|fatal)" |
| 181 | #define LEVELS_HELP \ |
| 182 | "Log simply everything\n" \ |
| 183 | "Log debug messages and higher levels\n" \ |
| 184 | "Log informational messages and higher levels\n" \ |
| 185 | "Log noticable messages and higher levels\n" \ |
| 186 | "Log error messages and higher levels\n" \ |
| 187 | "Log only fatal messages\n" |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 188 | |
| 189 | static int _logging_level(struct vty *vty, struct log_target *dbg, |
| 190 | const char *cat_str, const char *lvl_str) |
| 191 | { |
| 192 | int category = log_parse_category(cat_str); |
| 193 | int level = log_parse_level(lvl_str); |
| 194 | |
| 195 | if (level < 0) { |
| 196 | vty_out(vty, "Invalid level `%s'%s", lvl_str, VTY_NEWLINE); |
| 197 | return CMD_WARNING; |
| 198 | } |
| 199 | |
| 200 | /* Check for special case where we want to set global log level */ |
| 201 | if (!strcmp(cat_str, "all")) { |
| 202 | log_set_log_level(dbg, level); |
| 203 | return CMD_SUCCESS; |
| 204 | } |
| 205 | |
| 206 | if (category < 0) { |
| 207 | vty_out(vty, "Invalid category `%s'%s", cat_str, VTY_NEWLINE); |
| 208 | return CMD_WARNING; |
| 209 | } |
| 210 | |
| 211 | dbg->categories[category].enabled = 1; |
| 212 | dbg->categories[category].loglevel = level; |
| 213 | |
| 214 | return CMD_SUCCESS; |
| 215 | } |
| 216 | |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 217 | DEFUN(logging_level, |
| 218 | logging_level_cmd, |
| 219 | "logging level " VTY_DEBUG_CATEGORIES " " VTY_DEBUG_LEVELS, |
| 220 | LOGGING_STR |
| 221 | "Set the log level for a specified category\n" |
| 222 | CATEGORIES_HELP |
| 223 | LEVELS_HELP) |
| 224 | { |
| 225 | struct telnet_connection *conn; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 226 | |
| 227 | conn = (struct telnet_connection *) vty->priv; |
| 228 | if (!conn->dbg) { |
| 229 | vty_out(vty, "Logging was not enabled.%s", VTY_NEWLINE); |
| 230 | return CMD_WARNING; |
| 231 | } |
| 232 | |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 233 | return _logging_level(vty, conn->dbg, argv[0], argv[1]); |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | DEFUN(logging_set_category_mask, |
| 237 | logging_set_category_mask_cmd, |
| 238 | "logging set log mask MASK", |
| 239 | LOGGING_STR |
| 240 | "Decide which categories to output.\n") |
| 241 | { |
| 242 | struct telnet_connection *conn; |
| 243 | |
| 244 | conn = (struct telnet_connection *) vty->priv; |
| 245 | if (!conn->dbg) { |
| 246 | vty_out(vty, "Logging was not enabled.%s", VTY_NEWLINE); |
| 247 | return CMD_WARNING; |
| 248 | } |
| 249 | |
| 250 | log_parse_category_mask(conn->dbg, argv[0]); |
| 251 | return CMD_SUCCESS; |
| 252 | } |
| 253 | |
| 254 | DEFUN(diable_logging, |
| 255 | disable_logging_cmd, |
| 256 | "logging disable", |
| 257 | LOGGING_STR |
| 258 | "Disables logging to this vty\n") |
| 259 | { |
| 260 | struct telnet_connection *conn; |
| 261 | |
| 262 | conn = (struct telnet_connection *) vty->priv; |
| 263 | if (!conn->dbg) { |
| 264 | vty_out(vty, "Logging was not enabled.%s", VTY_NEWLINE); |
| 265 | return CMD_WARNING; |
| 266 | } |
| 267 | |
| 268 | log_del_target(conn->dbg); |
| 269 | talloc_free(conn->dbg); |
| 270 | conn->dbg = NULL; |
| 271 | return CMD_SUCCESS; |
| 272 | } |
| 273 | |
| 274 | static void vty_print_logtarget(struct vty *vty, const struct log_info *info, |
| 275 | const struct log_target *tgt) |
| 276 | { |
| 277 | unsigned int i; |
| 278 | |
| 279 | vty_out(vty, " Global Loglevel: %s%s", |
| 280 | log_level_str(tgt->loglevel), VTY_NEWLINE); |
| 281 | vty_out(vty, " Use color: %s, Print Timestamp: %s%s", |
| 282 | tgt->use_color ? "On" : "Off", |
| 283 | tgt->print_timestamp ? "On" : "Off", VTY_NEWLINE); |
| 284 | |
| 285 | vty_out(vty, " Log Level specific information:%s", VTY_NEWLINE); |
| 286 | |
| 287 | for (i = 0; i < info->num_cat; i++) { |
| 288 | const struct log_category *cat = &tgt->categories[i]; |
| 289 | vty_out(vty, " %-10s %-10s %-8s %s%s", |
| 290 | info->cat[i].name+1, log_level_str(cat->loglevel), |
| 291 | cat->enabled ? "Enabled" : "Disabled", |
| 292 | info->cat[i].description, |
| 293 | VTY_NEWLINE); |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | #define SHOW_LOG_STR "Show current logging configuration\n" |
| 298 | |
| 299 | DEFUN(show_logging_vty, |
| 300 | show_logging_vty_cmd, |
| 301 | "show logging vty", |
| 302 | SHOW_STR SHOW_LOG_STR |
| 303 | "Show current logging configuration for this vty\n") |
| 304 | { |
| 305 | struct telnet_connection *conn; |
| 306 | |
| 307 | conn = (struct telnet_connection *) vty->priv; |
| 308 | if (!conn->dbg) { |
| 309 | vty_out(vty, "Logging was not enabled.%s", VTY_NEWLINE); |
| 310 | return CMD_WARNING; |
| 311 | } |
Harald Welte | 4ebdf74 | 2010-05-19 19:54:00 +0200 | [diff] [blame] | 312 | vty_print_logtarget(vty, osmo_log_info, conn->dbg); |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 313 | |
| 314 | return CMD_SUCCESS; |
| 315 | } |
| 316 | |
| 317 | gDEFUN(cfg_description, cfg_description_cmd, |
| 318 | "description .TEXT", |
| 319 | "Save human-readable decription of the object\n") |
| 320 | { |
| 321 | char **dptr = vty->index_sub; |
| 322 | |
| 323 | if (!dptr) { |
| 324 | vty_out(vty, "vty->index_sub == NULL%s", VTY_NEWLINE); |
| 325 | return CMD_WARNING; |
| 326 | } |
| 327 | |
| 328 | *dptr = argv_concat(argv, argc, 0); |
| 329 | if (!dptr) |
| 330 | return CMD_WARNING; |
| 331 | |
| 332 | return CMD_SUCCESS; |
| 333 | } |
| 334 | |
| 335 | gDEFUN(cfg_no_description, cfg_no_description_cmd, |
| 336 | "no description", |
| 337 | NO_STR |
| 338 | "Remove description of the object\n") |
| 339 | { |
| 340 | char **dptr = vty->index_sub; |
| 341 | |
| 342 | if (!dptr) { |
| 343 | vty_out(vty, "vty->index_sub == NULL%s", VTY_NEWLINE); |
| 344 | return CMD_WARNING; |
| 345 | } |
| 346 | |
| 347 | if (*dptr) { |
| 348 | talloc_free(*dptr); |
| 349 | *dptr = NULL; |
| 350 | } |
| 351 | |
| 352 | return CMD_SUCCESS; |
| 353 | } |
| 354 | |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 355 | /* Support for configuration of log targets != the current vty */ |
| 356 | |
| 357 | struct cmd_node cfg_log_node = { |
| 358 | CFG_LOG_NODE, |
| 359 | "%s(config-log)# ", |
| 360 | 1 |
| 361 | }; |
| 362 | |
| 363 | DEFUN(cfg_log_fltr_all, |
| 364 | cfg_log_fltr_all_cmd, |
| 365 | "logging filter all (0|1)", |
| 366 | LOGGING_STR FILTER_STR |
| 367 | "Do you want to log all messages?\n" |
| 368 | "Only print messages matched by other filters\n" |
| 369 | "Bypass filter and print all messages\n") |
| 370 | { |
| 371 | struct log_target *dbg = vty->index; |
| 372 | |
| 373 | log_set_all_filter(dbg, atoi(argv[0])); |
| 374 | return CMD_SUCCESS; |
| 375 | } |
| 376 | |
| 377 | DEFUN(cfg_log_use_clr, |
| 378 | cfg_log_use_clr_cmd, |
| 379 | "logging color (0|1)", |
| 380 | LOGGING_STR "Configure color-printing for log messages\n" |
| 381 | "Don't use color for printing messages\n" |
| 382 | "Use color for printing messages\n") |
| 383 | { |
| 384 | struct log_target *dbg = vty->index; |
| 385 | |
| 386 | log_set_use_color(dbg, atoi(argv[0])); |
| 387 | return CMD_SUCCESS; |
| 388 | } |
| 389 | |
| 390 | DEFUN(cfg_log_timestamp, |
| 391 | cfg_log_timestamp_cmd, |
| 392 | "logging timestamp (0|1)", |
| 393 | LOGGING_STR "Configure log message timestamping\n" |
| 394 | "Don't prefix each log message\n" |
| 395 | "Prefix each log message with current timestamp\n") |
| 396 | { |
| 397 | struct log_target *dbg = vty->index; |
| 398 | |
| 399 | log_set_print_timestamp(dbg, atoi(argv[0])); |
| 400 | return CMD_SUCCESS; |
| 401 | } |
| 402 | |
| 403 | DEFUN(cfg_log_level, |
| 404 | cfg_log_level_cmd, |
| 405 | "logging level " VTY_DEBUG_CATEGORIES " " VTY_DEBUG_LEVELS, |
| 406 | LOGGING_STR |
| 407 | "Set the log level for a specified category\n" |
| 408 | CATEGORIES_HELP |
| 409 | LEVELS_HELP) |
| 410 | { |
| 411 | struct log_target *dbg = vty->index; |
| 412 | |
| 413 | return _logging_level(vty, dbg, argv[0], argv[1]); |
| 414 | } |
| 415 | |
| 416 | #ifdef HAVE_SYSLOG_H |
| 417 | |
| 418 | #include <syslog.h> |
| 419 | |
| 420 | static const int local_sysl_map[] = { |
| 421 | [0] = LOG_LOCAL0, |
| 422 | [1] = LOG_LOCAL1, |
| 423 | [2] = LOG_LOCAL2, |
| 424 | [3] = LOG_LOCAL3, |
| 425 | [4] = LOG_LOCAL4, |
| 426 | [5] = LOG_LOCAL5, |
| 427 | [6] = LOG_LOCAL6, |
| 428 | [7] = LOG_LOCAL7 |
| 429 | }; |
| 430 | |
| 431 | static int _cfg_log_syslog(struct vty *vty, int facility) |
| 432 | { |
| 433 | struct log_target *tgt; |
| 434 | |
| 435 | /* First delete the old syslog target, if any */ |
| 436 | tgt = log_target_find(LOG_TGT_TYPE_SYSLOG, NULL); |
| 437 | if (tgt) |
| 438 | log_target_destroy(tgt); |
| 439 | |
| 440 | tgt = log_target_create_syslog("FIXME", 0, facility); |
| 441 | if (!tgt) { |
| 442 | vty_out(vty, "%% Unable to open syslog%s", VTY_NEWLINE); |
| 443 | return CMD_WARNING; |
| 444 | } |
| 445 | log_add_target(tgt); |
| 446 | |
| 447 | vty->index = tgt; |
| 448 | vty->node = CFG_LOG_NODE; |
| 449 | |
| 450 | return CMD_SUCCESS; |
| 451 | } |
| 452 | |
| 453 | DEFUN(cfg_log_syslog_local, cfg_log_syslog_local_cmd, |
| 454 | "log syslog local <0-7>", |
| 455 | LOG_STR "Logging via syslog\n" "Syslog LOCAL facility\n" |
| 456 | "Local facility number\n") |
| 457 | { |
| 458 | int local = atoi(argv[0]); |
| 459 | int facility = local_sysl_map[local]; |
| 460 | |
| 461 | return _cfg_log_syslog(vty, facility); |
| 462 | } |
| 463 | |
| 464 | static struct value_string sysl_level_names[] = { |
| 465 | { LOG_AUTHPRIV, "authpriv" }, |
| 466 | { LOG_CRON, "cron" }, |
| 467 | { LOG_DAEMON, "daemon" }, |
| 468 | { LOG_FTP, "ftp" }, |
| 469 | { LOG_LPR, "lpr" }, |
| 470 | { LOG_MAIL, "mail" }, |
| 471 | { LOG_NEWS, "news" }, |
| 472 | { LOG_USER, "user" }, |
| 473 | { LOG_UUCP, "uucp" }, |
| 474 | /* only for value -> string conversion */ |
| 475 | { LOG_LOCAL0, "local 0" }, |
| 476 | { LOG_LOCAL1, "local 1" }, |
| 477 | { LOG_LOCAL2, "local 2" }, |
| 478 | { LOG_LOCAL3, "local 3" }, |
| 479 | { LOG_LOCAL4, "local 4" }, |
| 480 | { LOG_LOCAL5, "local 5" }, |
| 481 | { LOG_LOCAL6, "local 6" }, |
| 482 | { LOG_LOCAL7, "local 7" }, |
| 483 | { 0, NULL } |
| 484 | }; |
| 485 | |
| 486 | DEFUN(cfg_log_syslog, cfg_log_syslog_cmd, |
| 487 | "log syslog (authpriv|cron|daemon|ftp|lpr|mail|news|user|uucp)", |
| 488 | LOG_STR "Logging via syslog\n") |
| 489 | { |
| 490 | int facility = get_string_value(sysl_level_names, argv[0]); |
| 491 | |
| 492 | return _cfg_log_syslog(vty, facility); |
| 493 | } |
| 494 | |
| 495 | DEFUN(cfg_no_log_syslog, cfg_no_log_syslog_cmd, |
| 496 | "no log syslog", |
| 497 | NO_STR LOG_STR "Logging via syslog\n") |
| 498 | { |
| 499 | struct log_target *tgt; |
| 500 | |
| 501 | tgt = log_target_find(LOG_TGT_TYPE_SYSLOG, NULL); |
| 502 | if (!tgt) { |
| 503 | vty_out(vty, "%% No syslog target found%s", |
| 504 | VTY_NEWLINE); |
| 505 | return CMD_WARNING; |
| 506 | } |
| 507 | |
| 508 | log_target_destroy(tgt); |
| 509 | |
| 510 | return CMD_SUCCESS; |
| 511 | } |
| 512 | #endif /* HAVE_SYSLOG_H */ |
| 513 | |
| 514 | DEFUN(cfg_log_stderr, cfg_log_stderr_cmd, |
| 515 | "log stderr", |
| 516 | LOG_STR "Logging via STDERR of the process\n") |
| 517 | { |
| 518 | struct log_target *tgt; |
| 519 | |
| 520 | tgt = log_target_find(LOG_TGT_TYPE_STDERR, NULL); |
| 521 | if (!tgt) { |
| 522 | tgt = log_target_create_stderr(); |
| 523 | if (!tgt) { |
| 524 | vty_out(vty, "%% Unable to create stderr log%s", |
| 525 | VTY_NEWLINE); |
| 526 | return CMD_WARNING; |
| 527 | } |
| 528 | log_add_target(tgt); |
| 529 | } |
| 530 | |
| 531 | vty->index = tgt; |
| 532 | vty->node = CFG_LOG_NODE; |
| 533 | |
| 534 | return CMD_SUCCESS; |
| 535 | } |
| 536 | |
| 537 | DEFUN(cfg_no_log_stderr, cfg_no_log_stderr_cmd, |
| 538 | "no log stderr", |
| 539 | NO_STR LOG_STR "Logging via STDERR of the process\n") |
| 540 | { |
| 541 | struct log_target *tgt; |
| 542 | |
| 543 | tgt = log_target_find(LOG_TGT_TYPE_STDERR, NULL); |
| 544 | if (!tgt) { |
| 545 | vty_out(vty, "%% No stderr logging active%s", VTY_NEWLINE); |
| 546 | return CMD_WARNING; |
| 547 | } |
| 548 | |
| 549 | log_target_destroy(tgt); |
| 550 | |
| 551 | return CMD_SUCCESS; |
| 552 | } |
| 553 | |
| 554 | DEFUN(cfg_log_file, cfg_log_file_cmd, |
| 555 | "log file .FILENAME", |
| 556 | LOG_STR "Logging to text file\n" "Filename\n") |
| 557 | { |
| 558 | const char *fname = argv[0]; |
| 559 | struct log_target *tgt; |
| 560 | |
| 561 | tgt = log_target_find(LOG_TGT_TYPE_FILE, fname); |
| 562 | if (!tgt) { |
| 563 | tgt = log_target_create_file(fname); |
| 564 | if (!tgt) { |
| 565 | vty_out(vty, "%% Unable to create file `%s'%s", |
| 566 | fname, VTY_NEWLINE); |
| 567 | return CMD_WARNING; |
| 568 | } |
| 569 | log_add_target(tgt); |
| 570 | } |
| 571 | |
| 572 | vty->index = tgt; |
| 573 | vty->node = CFG_LOG_NODE; |
| 574 | |
| 575 | return CMD_SUCCESS; |
| 576 | } |
| 577 | |
| 578 | |
| 579 | DEFUN(cfg_no_log_file, cfg_no_log_file_cmd, |
| 580 | "no log file .FILENAME", |
| 581 | NO_STR LOG_STR "Logging to text file\n" "Filename\n") |
| 582 | { |
| 583 | const char *fname = argv[0]; |
| 584 | struct log_target *tgt; |
| 585 | |
| 586 | tgt = log_target_find(LOG_TGT_TYPE_FILE, fname); |
| 587 | if (!tgt) { |
| 588 | vty_out(vty, "%% No such log file `%s'%s", |
| 589 | fname, VTY_NEWLINE); |
| 590 | return CMD_WARNING; |
| 591 | } |
| 592 | |
| 593 | log_target_destroy(tgt); |
| 594 | |
| 595 | return CMD_SUCCESS; |
| 596 | } |
| 597 | |
| 598 | static int config_write_log_single(struct vty *vty, struct log_target *tgt) |
| 599 | { |
| 600 | int i; |
| 601 | char level_lower[32]; |
| 602 | |
| 603 | switch (tgt->type) { |
| 604 | case LOG_TGT_TYPE_VTY: |
| 605 | return 1; |
| 606 | break; |
| 607 | case LOG_TGT_TYPE_STDERR: |
| 608 | vty_out(vty, "log stderr%s", VTY_NEWLINE); |
| 609 | break; |
| 610 | case LOG_TGT_TYPE_SYSLOG: |
| 611 | #ifdef HAVE_SYSLOG_H |
| 612 | vty_out(vty, "log syslog %s%s", |
| 613 | get_value_string(sysl_level_names, |
| 614 | tgt->tgt_syslog.facility), |
| 615 | VTY_NEWLINE); |
| 616 | #endif |
| 617 | break; |
| 618 | case LOG_TGT_TYPE_FILE: |
| 619 | vty_out(vty, "log file %s%s", tgt->tgt_file.fname, VTY_NEWLINE); |
| 620 | break; |
| 621 | } |
| 622 | |
| 623 | vty_out(vty, " logging color %u%s", tgt->use_color ? 1 : 0, |
| 624 | VTY_NEWLINE); |
| 625 | vty_out(vty, " logging timestamp %u%s", tgt->print_timestamp ? 1 : 0, |
| 626 | VTY_NEWLINE); |
| 627 | |
| 628 | /* stupid old osmo logging API uses uppercase strings... */ |
| 629 | osmo_str2lower(level_lower, log_level_str(tgt->loglevel)); |
| 630 | vty_out(vty, " logging level all %s%s", level_lower, VTY_NEWLINE); |
| 631 | |
| 632 | for (i = 0; i < osmo_log_info->num_cat; i++) { |
| 633 | const struct log_category *cat = &tgt->categories[i]; |
| 634 | char cat_lower[32]; |
| 635 | |
| 636 | /* stupid old osmo logging API uses uppercase strings... */ |
| 637 | osmo_str2lower(cat_lower, osmo_log_info->cat[i].name+1); |
| 638 | osmo_str2lower(level_lower, log_level_str(cat->loglevel)); |
| 639 | |
| 640 | vty_out(vty, " logging level %s %s%s", cat_lower, level_lower, |
| 641 | VTY_NEWLINE); |
| 642 | } |
| 643 | |
| 644 | /* FIXME: levels */ |
| 645 | |
| 646 | return 1; |
| 647 | } |
| 648 | |
| 649 | static int config_write_log(struct vty *vty) |
| 650 | { |
| 651 | struct log_target *dbg = vty->index; |
| 652 | |
| 653 | llist_for_each_entry(dbg, &osmo_log_target_list, entry) |
| 654 | config_write_log_single(vty, dbg); |
| 655 | |
| 656 | return 1; |
| 657 | } |
| 658 | |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 659 | void logging_vty_add_cmds() |
| 660 | { |
| 661 | install_element_ve(&enable_logging_cmd); |
| 662 | install_element_ve(&disable_logging_cmd); |
| 663 | install_element_ve(&logging_fltr_all_cmd); |
| 664 | install_element_ve(&logging_use_clr_cmd); |
| 665 | install_element_ve(&logging_prnt_timestamp_cmd); |
| 666 | install_element_ve(&logging_set_category_mask_cmd); |
| 667 | install_element_ve(&logging_level_cmd); |
| 668 | install_element_ve(&show_logging_vty_cmd); |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 669 | |
| 670 | install_node(&cfg_log_node, config_write_log); |
| 671 | install_element(CFG_LOG_NODE, &cfg_log_fltr_all_cmd); |
| 672 | install_element(CFG_LOG_NODE, &cfg_log_use_clr_cmd); |
| 673 | install_element(CFG_LOG_NODE, &cfg_log_timestamp_cmd); |
| 674 | install_element(CFG_LOG_NODE, &cfg_log_level_cmd); |
| 675 | |
| 676 | install_element(CONFIG_NODE, &cfg_log_stderr_cmd); |
| 677 | install_element(CONFIG_NODE, &cfg_no_log_stderr_cmd); |
| 678 | install_element(CONFIG_NODE, &cfg_log_file_cmd); |
| 679 | install_element(CONFIG_NODE, &cfg_no_log_file_cmd); |
| 680 | #ifdef HAVE_SYSLOG_H |
| 681 | install_element(CONFIG_NODE, &cfg_log_syslog_cmd); |
| 682 | install_element(CONFIG_NODE, &cfg_log_syslog_local_cmd); |
| 683 | install_element(CONFIG_NODE, &cfg_no_log_syslog_cmd); |
| 684 | #endif |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 685 | } |