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 <osmocore/talloc.h>
26 #include <osmocore/logging.h>
28 //#include <openbsc/vty.h>
30 #include <osmocom/vty/command.h>
31 #include <osmocom/vty/buffer.h>
32 #include <osmocom/vty/vty.h>
33 #include <osmocom/vty/telnet_interface.h>
34 #include <osmocom/vty/logging.h>
36 extern const struct log_info *osmo_log_info;
38 static void _vty_output(struct log_target *tgt,
39 unsigned int level, const char *line)
41 struct vty *vty = tgt->tgt_vty.vty;
42 vty_out(vty, "%s", line);
43 /* This is an ugly hack, but there is no easy way... */
44 if (strchr(line, '\n'))
48 struct log_target *log_target_create_vty(struct vty *vty)
50 struct log_target *target;
52 target = log_target_create();
56 target->tgt_vty.vty = vty;
57 target->output = _vty_output;
65 "Enables logging to this vty\n")
67 struct telnet_connection *conn;
69 conn = (struct telnet_connection *) vty->priv;
71 vty_out(vty, "Logging already enabled.%s", VTY_NEWLINE);
75 conn->dbg = log_target_create_vty(vty);
79 log_add_target(conn->dbg);
83 DEFUN(logging_fltr_all,
85 "logging filter all (0|1)",
86 LOGGING_STR FILTER_STR
87 "Do you want to log all messages?\n"
88 "Only print messages matched by other filters\n"
89 "Bypass filter and print all messages\n")
91 struct telnet_connection *conn;
93 conn = (struct telnet_connection *) vty->priv;
95 vty_out(vty, "Logging was not enabled.%s", VTY_NEWLINE);
99 log_set_all_filter(conn->dbg, atoi(argv[0]));
103 DEFUN(logging_use_clr,
105 "logging color (0|1)",
106 LOGGING_STR "Configure color-printing for log messages\n"
107 "Don't use color for printing messages\n"
108 "Use color for printing messages\n")
110 struct telnet_connection *conn;
112 conn = (struct telnet_connection *) vty->priv;
114 vty_out(vty, "Logging was not enabled.%s", VTY_NEWLINE);
118 log_set_use_color(conn->dbg, atoi(argv[0]));
122 DEFUN(logging_prnt_timestamp,
123 logging_prnt_timestamp_cmd,
124 "logging timestamp (0|1)",
125 LOGGING_STR "Configure log message timestamping\n"
126 "Don't prefix each log message\n"
127 "Prefix each log message with current timestamp\n")
129 struct telnet_connection *conn;
131 conn = (struct telnet_connection *) vty->priv;
133 vty_out(vty, "Logging was not enabled.%s", VTY_NEWLINE);
137 log_set_print_timestamp(conn->dbg, atoi(argv[0]));
141 /* FIXME: those have to be kept in sync with the log levels and categories */
142 #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)"
143 #define CATEGORIES_HELP \
144 "A-bis Radio Link Layer (RLL)\n" \
145 "Layer3 Call Control (CC)\n" \
146 "Layer3 Mobility Management (MM)\n" \
147 "Layer3 Radio Resource (RR)\n" \
148 "A-bis Radio Signalling Link (RSL)\n" \
149 "A-bis Network Management / O&M (NM/OML)\n" \
150 "Layer3 Short Messagaging Service (SMS)\n" \
151 "Paging Subsystem\n" \
152 "MNCC API for Call Control application\n" \
153 "A-bis Input Subsystem\n" \
154 "A-bis Input Driver for Signalling\n" \
155 "A-bis Input Driver for B-Channel (voice data)\n" \
156 "A-bis B-Channel / Sub-channel Multiplexer\n" \
157 "Radio Measurements\n" \
159 "Mobile Switching Center\n" \
160 "Media Gateway Control Protocol\n" \
163 "Reference Counting\n" \
165 "GPRS Network Service (NS)\n" \
166 "GPRS BSS Gateway Protocol (BSSGP)\n" \
167 "GPRS Logical Link Control Protocol (LLC)\n" \
168 "GPRS Sub-Network Dependent Control Protocol (SNDCP)\n" \
169 "ISDN User Part (ISUP)\n" \
171 "Trace message IO\n" \
172 "Global setting for all subsytems\n"
174 #define VTY_DEBUG_LEVELS "(everything|debug|info|notice|error|fatal)"
175 #define LEVELS_HELP \
176 "Log simply everything\n" \
177 "Log debug messages and higher levels\n" \
178 "Log informational messages and higher levels\n" \
179 "Log noticable messages and higher levels\n" \
180 "Log error messages and higher levels\n" \
181 "Log only fatal messages\n"
184 "logging level " VTY_DEBUG_CATEGORIES " " VTY_DEBUG_LEVELS,
186 "Set the log level for a specified category\n"
190 struct telnet_connection *conn;
191 int category = log_parse_category(argv[0]);
192 int level = log_parse_level(argv[1]);
194 conn = (struct telnet_connection *) vty->priv;
196 vty_out(vty, "Logging was not enabled.%s", VTY_NEWLINE);
201 vty_out(vty, "Invalid level `%s'%s", argv[1], VTY_NEWLINE);
205 /* Check for special case where we want to set global log level */
206 if (!strcmp(argv[0], "all")) {
207 log_set_log_level(conn->dbg, level);
212 vty_out(vty, "Invalid category `%s'%s", argv[0], VTY_NEWLINE);
216 conn->dbg->categories[category].enabled = 1;
217 conn->dbg->categories[category].loglevel = level;
222 DEFUN(logging_set_category_mask,
223 logging_set_category_mask_cmd,
224 "logging set log mask MASK",
226 "Decide which categories to output.\n")
228 struct telnet_connection *conn;
230 conn = (struct telnet_connection *) vty->priv;
232 vty_out(vty, "Logging was not enabled.%s", VTY_NEWLINE);
236 log_parse_category_mask(conn->dbg, argv[0]);
240 DEFUN(diable_logging,
244 "Disables logging to this vty\n")
246 struct telnet_connection *conn;
248 conn = (struct telnet_connection *) vty->priv;
250 vty_out(vty, "Logging was not enabled.%s", VTY_NEWLINE);
254 log_del_target(conn->dbg);
255 talloc_free(conn->dbg);
260 static void vty_print_logtarget(struct vty *vty, const struct log_info *info,
261 const struct log_target *tgt)
265 vty_out(vty, " Global Loglevel: %s%s",
266 log_level_str(tgt->loglevel), VTY_NEWLINE);
267 vty_out(vty, " Use color: %s, Print Timestamp: %s%s",
268 tgt->use_color ? "On" : "Off",
269 tgt->print_timestamp ? "On" : "Off", VTY_NEWLINE);
271 vty_out(vty, " Log Level specific information:%s", VTY_NEWLINE);
273 for (i = 0; i < info->num_cat; i++) {
274 const struct log_category *cat = &tgt->categories[i];
275 vty_out(vty, " %-10s %-10s %-8s %s%s",
276 info->cat[i].name+1, log_level_str(cat->loglevel),
277 cat->enabled ? "Enabled" : "Disabled",
278 info->cat[i].description,
283 #define SHOW_LOG_STR "Show current logging configuration\n"
285 DEFUN(show_logging_vty,
286 show_logging_vty_cmd,
288 SHOW_STR SHOW_LOG_STR
289 "Show current logging configuration for this vty\n")
291 struct telnet_connection *conn;
293 conn = (struct telnet_connection *) vty->priv;
295 vty_out(vty, "Logging was not enabled.%s", VTY_NEWLINE);
298 vty_print_logtarget(vty, osmo_log_info, conn->dbg);
303 gDEFUN(cfg_description, cfg_description_cmd,
305 "Save human-readable decription of the object\n")
307 char **dptr = vty->index_sub;
310 vty_out(vty, "vty->index_sub == NULL%s", VTY_NEWLINE);
314 *dptr = argv_concat(argv, argc, 0);
321 gDEFUN(cfg_no_description, cfg_no_description_cmd,
324 "Remove description of the object\n")
326 char **dptr = vty->index_sub;
329 vty_out(vty, "vty->index_sub == NULL%s", VTY_NEWLINE);
341 void logging_vty_add_cmds()
343 install_element_ve(&enable_logging_cmd);
344 install_element_ve(&disable_logging_cmd);
345 install_element_ve(&logging_fltr_all_cmd);
346 install_element_ve(&logging_use_clr_cmd);
347 install_element_ve(&logging_prnt_timestamp_cmd);
348 install_element_ve(&logging_set_category_mask_cmd);
349 install_element_ve(&logging_level_cmd);
350 install_element_ve(&show_logging_vty_cmd);