blob: 0af8995d0699151ec7858407e13ac046ed3b5aff [file] [log] [blame]
Holger Hans Peter Freyther3c712322010-04-06 11:55:37 +02001/* 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 <openbsc/vty.h>
23#include <openbsc/telnet_interface.h>
24
25#include <osmocore/talloc.h>
26
27#include <vty/command.h>
28#include <vty/buffer.h>
29#include <vty/vty.h>
30
31#include <stdlib.h>
32
33static void _vty_output(struct log_target *tgt, const char *line)
34{
35 struct vty *vty = tgt->tgt_vty.vty;
36 vty_out(vty, "%s", line);
37 /* This is an ugly hack, but there is no easy way... */
38 if (strchr(line, '\n'))
39 vty_out(vty, "\r");
40}
41
42struct log_target *log_target_create_vty(struct vty *vty)
43{
44 struct log_target *target;
45
46 target = log_target_create();
47 if (!target)
48 return NULL;
49
50 target->tgt_vty.vty = vty;
51 target->output = _vty_output;
52 return target;
53}
54
55DEFUN(enable_logging,
56 enable_logging_cmd,
57 "logging enable",
58 "Enables logging to this vty\n")
59{
60 struct telnet_connection *conn;
61
62 conn = (struct telnet_connection *) vty->priv;
63 if (conn->dbg) {
64 vty_out(vty, "Logging already enabled.%s", VTY_NEWLINE);
65 return CMD_WARNING;
66 }
67
68 conn->dbg = log_target_create_vty(vty);
69 if (!conn->dbg)
70 return CMD_WARNING;
71
72 log_add_target(conn->dbg);
73 return CMD_SUCCESS;
74}
75
76DEFUN(logging_fltr_imsi,
77 logging_fltr_imsi_cmd,
78 "logging filter imsi IMSI",
79 "Print all messages related to a IMSI\n")
80{
81 struct telnet_connection *conn;
82
83 conn = (struct telnet_connection *) vty->priv;
84 if (!conn->dbg) {
85 vty_out(vty, "Logging was not enabled.%s", VTY_NEWLINE);
86 return CMD_WARNING;
87 }
88
89 log_set_imsi_filter(conn->dbg, argv[0]);
90 return CMD_SUCCESS;
91}
92
93DEFUN(logging_fltr_all,
94 logging_fltr_all_cmd,
95 "logging filter all <0-1>",
96 "Print all messages to the console\n")
97{
98 struct telnet_connection *conn;
99
100 conn = (struct telnet_connection *) vty->priv;
101 if (!conn->dbg) {
102 vty_out(vty, "Logging was not enabled.%s", VTY_NEWLINE);
103 return CMD_WARNING;
104 }
105
106 log_set_all_filter(conn->dbg, atoi(argv[0]));
107 return CMD_SUCCESS;
108}
109
110DEFUN(logging_use_clr,
111 logging_use_clr_cmd,
112 "logging color <0-1>",
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
127DEFUN(logging_prnt_timestamp,
128 logging_prnt_timestamp_cmd,
129 "logging timestamp <0-1>",
130 "Print the timestamp of each message\n")
131{
132 struct telnet_connection *conn;
133
134 conn = (struct telnet_connection *) vty->priv;
135 if (!conn->dbg) {
136 vty_out(vty, "Logging was not enabled.%s", VTY_NEWLINE);
137 return CMD_WARNING;
138 }
139
140 log_set_print_timestamp(conn->dbg, atoi(argv[0]));
141 return CMD_SUCCESS;
142}
143
144/* FIXME: those have to be kept in sync with the log levels and categories */
Harald Welte6fab2362010-05-11 10:21:45 +0200145#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|all)"
Holger Hans Peter Freyther3c712322010-04-06 11:55:37 +0200146#define VTY_DEBUG_LEVELS "(everything|debug|info|notice|error|fatal)"
147DEFUN(logging_level,
148 logging_level_cmd,
149 "logging level " VTY_DEBUG_CATEGORIES " " VTY_DEBUG_LEVELS,
150 "Set the log level for a specified category\n")
151{
152 struct telnet_connection *conn;
153 int category = log_parse_category(argv[0]);
154 int level = log_parse_level(argv[1]);
155
156 conn = (struct telnet_connection *) vty->priv;
157 if (!conn->dbg) {
158 vty_out(vty, "Logging was not enabled.%s", VTY_NEWLINE);
159 return CMD_WARNING;
160 }
161
Harald Welte6fab2362010-05-11 10:21:45 +0200162 if (level < 0) {
163 vty_out(vty, "Invalid level `%s'%s", argv[1], VTY_NEWLINE);
Holger Hans Peter Freyther3c712322010-04-06 11:55:37 +0200164 return CMD_WARNING;
165 }
166
Harald Welte6fab2362010-05-11 10:21:45 +0200167 /* Check for special case where we want to set global log level */
168 if (!strcmp(argv[0], "all")) {
169 log_set_log_level(conn->dbg, level);
170 return CMD_SUCCESS;
171 }
172
173 if (category < 0) {
174 vty_out(vty, "Invalid category `%s'%s", argv[0], VTY_NEWLINE);
Holger Hans Peter Freyther3c712322010-04-06 11:55:37 +0200175 return CMD_WARNING;
176 }
177
178 conn->dbg->categories[category].enabled = 1;
179 conn->dbg->categories[category].loglevel = level;
180
181 return CMD_SUCCESS;
182}
183
184DEFUN(logging_set_category_mask,
185 logging_set_category_mask_cmd,
186 "logging set log mask MASK",
187 "Decide which categories to output.\n")
188{
189 struct telnet_connection *conn;
190
191 conn = (struct telnet_connection *) vty->priv;
192 if (!conn->dbg) {
193 vty_out(vty, "Logging was not enabled.%s", VTY_NEWLINE);
194 return CMD_WARNING;
195 }
196
197 log_parse_category_mask(conn->dbg, argv[0]);
198 return CMD_SUCCESS;
199}
200
Holger Hans Peter Freyther3c712322010-04-06 11:55:37 +0200201DEFUN(diable_logging,
202 disable_logging_cmd,
203 "logging disable",
204 "Disables logging to this vty\n")
205{
206 struct telnet_connection *conn;
207
208 conn = (struct telnet_connection *) vty->priv;
209 if (!conn->dbg) {
210 vty_out(vty, "Logging was not enabled.%s", VTY_NEWLINE);
211 return CMD_WARNING;
212 }
213
214 log_del_target(conn->dbg);
215 talloc_free(conn->dbg);
216 conn->dbg = NULL;
217 return CMD_SUCCESS;
218}
219
Holger Hans Peter Freythere0ec3262010-04-15 11:28:14 +0200220void openbsc_vty_print_statistics(struct vty *vty, struct gsm_network *net)
221{
222 vty_out(vty, "Channel Requests : %lu total, %lu no channel%s",
223 counter_get(net->stats.chreq.total),
224 counter_get(net->stats.chreq.no_channel), VTY_NEWLINE);
Holger Hans Peter Freyther3ba36d52010-04-17 06:48:29 +0200225 vty_out(vty, "Channel Failures : %lu rf_failures, %lu rll failures%s",
226 counter_get(net->stats.chan.rf_fail),
227 counter_get(net->stats.chan.rll_err), VTY_NEWLINE);
Holger Hans Peter Freythere0ec3262010-04-15 11:28:14 +0200228 vty_out(vty, "Paging : %lu attempted, %lu complete, %lu expired%s",
229 counter_get(net->stats.paging.attempted),
230 counter_get(net->stats.paging.completed),
231 counter_get(net->stats.paging.expired), VTY_NEWLINE);
Holger Hans Peter Freytherbb110f92010-04-12 10:45:52 +0200232 vty_out(vty, "BTS failures : %lu OML, %lu RSL%s",
233 counter_get(net->stats.bts.oml_fail),
234 counter_get(net->stats.bts.rsl_fail), VTY_NEWLINE);
Holger Hans Peter Freythere0ec3262010-04-15 11:28:14 +0200235}
236
Holger Hans Peter Freyther3c712322010-04-06 11:55:37 +0200237void openbsc_vty_add_cmds()
238{
239 install_element(VIEW_NODE, &enable_logging_cmd);
240 install_element(VIEW_NODE, &disable_logging_cmd);
241 install_element(VIEW_NODE, &logging_fltr_imsi_cmd);
242 install_element(VIEW_NODE, &logging_fltr_all_cmd);
243 install_element(VIEW_NODE, &logging_use_clr_cmd);
244 install_element(VIEW_NODE, &logging_prnt_timestamp_cmd);
245 install_element(VIEW_NODE, &logging_set_category_mask_cmd);
246 install_element(VIEW_NODE, &logging_level_cmd);
Holger Hans Peter Freyther3c712322010-04-06 11:55:37 +0200247
248}