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
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.
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.
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.
25 #include "../../config.h"
27 #include <osmocore/talloc.h>
28 #include <osmocore/logging.h>
29 #include <osmocore/utils.h>
31 //#include <openbsc/vty.h>
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>
39 #define LOG_STR "Configure logging sub-system\n"
41 extern const struct log_info *osmo_log_info;
43 static void _vty_output(struct log_target *tgt,
44 unsigned int level, const char *line)
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'))
53 struct log_target *log_target_create_vty(struct vty *vty)
55 struct log_target *target;
57 target = log_target_create();
61 target->tgt_vty.vty = vty;
62 target->output = _vty_output;
70 "Enables logging to this vty\n")
72 struct telnet_connection *conn;
74 conn = (struct telnet_connection *) vty->priv;
76 vty_out(vty, "Logging already enabled.%s", VTY_NEWLINE);
80 conn->dbg = log_target_create_vty(vty);
84 log_add_target(conn->dbg);
88 DEFUN(logging_fltr_all,
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")
96 struct telnet_connection *conn;
98 conn = (struct telnet_connection *) vty->priv;
100 vty_out(vty, "Logging was not enabled.%s", VTY_NEWLINE);
104 log_set_all_filter(conn->dbg, atoi(argv[0]));
108 DEFUN(logging_use_clr,
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")
115 struct telnet_connection *conn;
117 conn = (struct telnet_connection *) vty->priv;
119 vty_out(vty, "Logging was not enabled.%s", VTY_NEWLINE);
123 log_set_use_color(conn->dbg, atoi(argv[0]));
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")
134 struct telnet_connection *conn;
136 conn = (struct telnet_connection *) vty->priv;
138 vty_out(vty, "Logging was not enabled.%s", VTY_NEWLINE);
142 log_set_print_timestamp(conn->dbg, atoi(argv[0]));
146 /* FIXME: those have to be kept in sync with the log levels and categories */
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|all)"
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" \
164 "Mobile Switching Center\n" \
165 "Media Gateway Control Protocol\n" \
168 "Reference Counting\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" \
174 "ISDN User Part (ISUP)\n" \
176 "Trace message IO\n" \
177 "Global setting for all subsytems\n"
179 #define VTY_DEBUG_LEVELS "(everything|debug|info|notice|error|fatal)"
180 #define LEVELS_HELP \
181 "Log simply everything\n" \
182 "Log debug messages and higher levels\n" \
183 "Log informational messages and higher levels\n" \
184 "Log noticable messages and higher levels\n" \
185 "Log error messages and higher levels\n" \
186 "Log only fatal messages\n"
188 static int _logging_level(struct vty *vty, struct log_target *dbg,
189 const char *cat_str, const char *lvl_str)
191 int category = log_parse_category(cat_str);
192 int level = log_parse_level(lvl_str);
195 vty_out(vty, "Invalid level `%s'%s", lvl_str, VTY_NEWLINE);
199 /* Check for special case where we want to set global log level */
200 if (!strcmp(cat_str, "all")) {
201 log_set_log_level(dbg, level);
206 vty_out(vty, "Invalid category `%s'%s", cat_str, VTY_NEWLINE);
210 dbg->categories[category].enabled = 1;
211 dbg->categories[category].loglevel = level;
218 "logging level " VTY_DEBUG_CATEGORIES " " VTY_DEBUG_LEVELS,
220 "Set the log level for a specified category\n"
224 struct telnet_connection *conn;
226 conn = (struct telnet_connection *) vty->priv;
228 vty_out(vty, "Logging was not enabled.%s", VTY_NEWLINE);
232 return _logging_level(vty, conn->dbg, argv[0], argv[1]);
235 DEFUN(logging_set_category_mask,
236 logging_set_category_mask_cmd,
237 "logging set log mask MASK",
239 "Decide which categories to output.\n")
241 struct telnet_connection *conn;
243 conn = (struct telnet_connection *) vty->priv;
245 vty_out(vty, "Logging was not enabled.%s", VTY_NEWLINE);
249 log_parse_category_mask(conn->dbg, argv[0]);
253 DEFUN(diable_logging,
257 "Disables logging to this vty\n")
259 struct telnet_connection *conn;
261 conn = (struct telnet_connection *) vty->priv;
263 vty_out(vty, "Logging was not enabled.%s", VTY_NEWLINE);
267 log_del_target(conn->dbg);
268 talloc_free(conn->dbg);
273 static void vty_print_logtarget(struct vty *vty, const struct log_info *info,
274 const struct log_target *tgt)
278 vty_out(vty, " Global Loglevel: %s%s",
279 log_level_str(tgt->loglevel), VTY_NEWLINE);
280 vty_out(vty, " Use color: %s, Print Timestamp: %s%s",
281 tgt->use_color ? "On" : "Off",
282 tgt->print_timestamp ? "On" : "Off", VTY_NEWLINE);
284 vty_out(vty, " Log Level specific information:%s", VTY_NEWLINE);
286 for (i = 0; i < info->num_cat; i++) {
287 const struct log_category *cat = &tgt->categories[i];
288 vty_out(vty, " %-10s %-10s %-8s %s%s",
289 info->cat[i].name+1, log_level_str(cat->loglevel),
290 cat->enabled ? "Enabled" : "Disabled",
291 info->cat[i].description,
296 #define SHOW_LOG_STR "Show current logging configuration\n"
298 DEFUN(show_logging_vty,
299 show_logging_vty_cmd,
301 SHOW_STR SHOW_LOG_STR
302 "Show current logging configuration for this vty\n")
304 struct telnet_connection *conn;
306 conn = (struct telnet_connection *) vty->priv;
308 vty_out(vty, "Logging was not enabled.%s", VTY_NEWLINE);
311 vty_print_logtarget(vty, osmo_log_info, conn->dbg);
316 gDEFUN(cfg_description, cfg_description_cmd,
318 "Save human-readable decription of the object\n")
320 char **dptr = vty->index_sub;
323 vty_out(vty, "vty->index_sub == NULL%s", VTY_NEWLINE);
327 *dptr = argv_concat(argv, argc, 0);
334 gDEFUN(cfg_no_description, cfg_no_description_cmd,
337 "Remove description of the object\n")
339 char **dptr = vty->index_sub;
342 vty_out(vty, "vty->index_sub == NULL%s", VTY_NEWLINE);
354 /* Support for configuration of log targets != the current vty */
356 struct cmd_node cfg_log_node = {
362 DEFUN(cfg_log_fltr_all,
363 cfg_log_fltr_all_cmd,
364 "logging filter all (0|1)",
365 LOGGING_STR FILTER_STR
366 "Do you want to log all messages?\n"
367 "Only print messages matched by other filters\n"
368 "Bypass filter and print all messages\n")
370 struct log_target *dbg = vty->index;
372 log_set_all_filter(dbg, atoi(argv[0]));
376 DEFUN(cfg_log_use_clr,
378 "logging color (0|1)",
379 LOGGING_STR "Configure color-printing for log messages\n"
380 "Don't use color for printing messages\n"
381 "Use color for printing messages\n")
383 struct log_target *dbg = vty->index;
385 log_set_use_color(dbg, atoi(argv[0]));
389 DEFUN(cfg_log_timestamp,
390 cfg_log_timestamp_cmd,
391 "logging timestamp (0|1)",
392 LOGGING_STR "Configure log message timestamping\n"
393 "Don't prefix each log message\n"
394 "Prefix each log message with current timestamp\n")
396 struct log_target *dbg = vty->index;
398 log_set_print_timestamp(dbg, atoi(argv[0]));
404 "logging level " VTY_DEBUG_CATEGORIES " " VTY_DEBUG_LEVELS,
406 "Set the log level for a specified category\n"
410 struct log_target *dbg = vty->index;
412 return _logging_level(vty, dbg, argv[0], argv[1]);
419 static const int local_sysl_map[] = {
430 static int _cfg_log_syslog(struct vty *vty, int facility)
432 struct log_target *tgt;
434 /* First delete the old syslog target, if any */
435 tgt = log_target_find(LOG_TGT_TYPE_SYSLOG, NULL);
437 log_target_destroy(tgt);
439 tgt = log_target_create_syslog("FIXME", 0, facility);
441 vty_out(vty, "%% Unable to open syslog%s", VTY_NEWLINE);
447 vty->node = CFG_LOG_NODE;
452 DEFUN(cfg_log_syslog_local, cfg_log_syslog_local_cmd,
453 "log syslog local <0-7>",
454 LOG_STR "Logging via syslog\n" "Syslog LOCAL facility\n"
455 "Local facility number\n")
457 int local = atoi(argv[0]);
458 int facility = local_sysl_map[local];
460 return _cfg_log_syslog(vty, facility);
463 static struct value_string sysl_level_names[] = {
464 { LOG_AUTHPRIV, "authpriv" },
465 { LOG_CRON, "cron" },
466 { LOG_DAEMON, "daemon" },
469 { LOG_MAIL, "mail" },
470 { LOG_NEWS, "news" },
471 { LOG_USER, "user" },
472 { LOG_UUCP, "uucp" },
473 /* only for value -> string conversion */
474 { LOG_LOCAL0, "local 0" },
475 { LOG_LOCAL1, "local 1" },
476 { LOG_LOCAL2, "local 2" },
477 { LOG_LOCAL3, "local 3" },
478 { LOG_LOCAL4, "local 4" },
479 { LOG_LOCAL5, "local 5" },
480 { LOG_LOCAL6, "local 6" },
481 { LOG_LOCAL7, "local 7" },
485 DEFUN(cfg_log_syslog, cfg_log_syslog_cmd,
486 "log syslog (authpriv|cron|daemon|ftp|lpr|mail|news|user|uucp)",
487 LOG_STR "Logging via syslog\n")
489 int facility = get_string_value(sysl_level_names, argv[0]);
491 return _cfg_log_syslog(vty, facility);
494 DEFUN(cfg_no_log_syslog, cfg_no_log_syslog_cmd,
496 NO_STR LOG_STR "Logging via syslog\n")
498 struct log_target *tgt;
500 tgt = log_target_find(LOG_TGT_TYPE_SYSLOG, NULL);
502 vty_out(vty, "%% No syslog target found%s",
507 log_target_destroy(tgt);
511 #endif /* HAVE_SYSLOG_H */
513 DEFUN(cfg_log_stderr, cfg_log_stderr_cmd,
515 LOG_STR "Logging via STDERR of the process\n")
517 struct log_target *tgt;
519 tgt = log_target_find(LOG_TGT_TYPE_STDERR, NULL);
521 tgt = log_target_create_stderr();
523 vty_out(vty, "%% Unable to create stderr log%s",
531 vty->node = CFG_LOG_NODE;
536 DEFUN(cfg_no_log_stderr, cfg_no_log_stderr_cmd,
538 NO_STR LOG_STR "Logging via STDERR of the process\n")
540 struct log_target *tgt;
542 tgt = log_target_find(LOG_TGT_TYPE_STDERR, NULL);
544 vty_out(vty, "%% No stderr logging active%s", VTY_NEWLINE);
548 log_target_destroy(tgt);
553 DEFUN(cfg_log_file, cfg_log_file_cmd,
554 "log file .FILENAME",
555 LOG_STR "Logging to text file\n" "Filename\n")
557 const char *fname = argv[0];
558 struct log_target *tgt;
560 tgt = log_target_find(LOG_TGT_TYPE_FILE, fname);
562 tgt = log_target_create_file(fname);
564 vty_out(vty, "%% Unable to create file `%s'%s",
572 vty->node = CFG_LOG_NODE;
578 DEFUN(cfg_no_log_file, cfg_no_log_file_cmd,
579 "no log file .FILENAME",
580 NO_STR LOG_STR "Logging to text file\n" "Filename\n")
582 const char *fname = argv[0];
583 struct log_target *tgt;
585 tgt = log_target_find(LOG_TGT_TYPE_FILE, fname);
587 vty_out(vty, "%% No such log file `%s'%s",
592 log_target_destroy(tgt);
597 static int config_write_log_single(struct vty *vty, struct log_target *tgt)
600 char level_lower[32];
603 case LOG_TGT_TYPE_VTY:
606 case LOG_TGT_TYPE_STDERR:
607 vty_out(vty, "log stderr%s", VTY_NEWLINE);
609 case LOG_TGT_TYPE_SYSLOG:
611 vty_out(vty, "log syslog %s%s",
612 get_value_string(sysl_level_names,
613 tgt->tgt_syslog.facility),
617 case LOG_TGT_TYPE_FILE:
618 vty_out(vty, "log file %s%s", tgt->tgt_file.fname, VTY_NEWLINE);
622 vty_out(vty, " logging color %u%s", tgt->use_color ? 1 : 0,
624 vty_out(vty, " logging timestamp %u%s", tgt->print_timestamp ? 1 : 0,
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);
631 for (i = 0; i < osmo_log_info->num_cat; i++) {
632 const struct log_category *cat = &tgt->categories[i];
635 /* stupid old osmo logging API uses uppercase strings... */
636 osmo_str2lower(cat_lower, osmo_log_info->cat[i].name+1);
637 osmo_str2lower(level_lower, log_level_str(cat->loglevel));
639 vty_out(vty, " logging level %s %s%s", cat_lower, level_lower,
648 static int config_write_log(struct vty *vty)
650 struct log_target *dbg = vty->index;
652 llist_for_each_entry(dbg, &osmo_log_target_list, entry)
653 config_write_log_single(vty, dbg);
658 void logging_vty_add_cmds()
660 install_element_ve(&enable_logging_cmd);
661 install_element_ve(&disable_logging_cmd);
662 install_element_ve(&logging_fltr_all_cmd);
663 install_element_ve(&logging_use_clr_cmd);
664 install_element_ve(&logging_prnt_timestamp_cmd);
665 install_element_ve(&logging_set_category_mask_cmd);
666 install_element_ve(&logging_level_cmd);
667 install_element_ve(&show_logging_vty_cmd);
669 install_node(&cfg_log_node, config_write_log);
670 install_element(CFG_LOG_NODE, &cfg_log_fltr_all_cmd);
671 install_element(CFG_LOG_NODE, &cfg_log_use_clr_cmd);
672 install_element(CFG_LOG_NODE, &cfg_log_timestamp_cmd);
673 install_element(CFG_LOG_NODE, &cfg_log_level_cmd);
675 install_element(CONFIG_NODE, &cfg_log_stderr_cmd);
676 install_element(CONFIG_NODE, &cfg_no_log_stderr_cmd);
677 install_element(CONFIG_NODE, &cfg_log_file_cmd);
678 install_element(CONFIG_NODE, &cfg_no_log_file_cmd);
680 install_element(CONFIG_NODE, &cfg_log_syslog_cmd);
681 install_element(CONFIG_NODE, &cfg_log_syslog_local_cmd);
682 install_element(CONFIG_NODE, &cfg_no_log_syslog_cmd);