blob: 7d97bb1d3311ae1a19c8a6d9c0335d3f527be4af [file] [log] [blame]
Neels Hofmeyr17518fe2017-06-20 04:35:06 +02001/*
2 * (C) 2009-2010 by Harald Welte <laforge@gnumonks.org>
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +01003 * (C) 2009-2014 by Holger Hans Peter Freyther
Harald Welte3fb0b6f2010-05-19 19:02:52 +02004 * All Rights Reserved
5 *
Harald Weltee08da972017-11-13 01:00:26 +09006 * SPDX-License-Identifier: GPL-2.0+
7 *
Harald Welte3fb0b6f2010-05-19 19:02:52 +02008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 *
22 */
23
24#include <stdlib.h>
25#include <string.h>
26
Harald Welte28222962011-02-18 20:37:04 +010027#include "../../config.h"
28
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010029#include <osmocom/core/talloc.h>
30#include <osmocom/core/logging.h>
31#include <osmocom/core/utils.h>
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +000032#include <osmocom/core/strrb.h>
33#include <osmocom/core/loggingrb.h>
Harald Welteaa00f992016-12-02 15:30:02 +010034#include <osmocom/core/gsmtap.h>
Harald Welte3fb0b6f2010-05-19 19:02:52 +020035
36#include <osmocom/vty/command.h>
37#include <osmocom/vty/buffer.h>
38#include <osmocom/vty/vty.h>
39#include <osmocom/vty/telnet_interface.h>
40#include <osmocom/vty/logging.h>
41
Harald Welte28222962011-02-18 20:37:04 +010042#define LOG_STR "Configure logging sub-system\n"
43
Harald Welte8c648252017-10-16 15:17:03 +020044/*! \file logging_vty.c
Neels Hofmeyr87e45502017-06-20 00:17:59 +020045 * Configuration of logging from VTY
Harald Welte96e2a002017-06-12 21:44:18 +020046 *
Harald Welte8c648252017-10-16 15:17:03 +020047 * This module implements
48 * - functions that permit configuration of the libosmocore logging
49 * framework from VTY commands in the configure -> logging node.
50 *
51 * - functions that permit logging *to* a VTY session. Basically each
52 * VTY session gets its own log target, with configurable
53 * per-subsystem log levels. This is performed internally via the
54 * \ref log_target_create_vty function.
55 *
56 * You have to call \ref logging_vty_add_cmds from your application
57 * once to enable both of the above.
58 *
Harald Welte96e2a002017-06-12 21:44:18 +020059 */
60
Harald Welte4ebdf742010-05-19 19:54:00 +020061extern const struct log_info *osmo_log_info;
Harald Welte3fb0b6f2010-05-19 19:02:52 +020062
Harald Welte76e72ab2011-02-17 15:52:39 +010063static void _vty_output(struct log_target *tgt,
64 unsigned int level, const char *line)
Harald Welte3fb0b6f2010-05-19 19:02:52 +020065{
66 struct vty *vty = tgt->tgt_vty.vty;
67 vty_out(vty, "%s", line);
68 /* This is an ugly hack, but there is no easy way... */
69 if (strchr(line, '\n'))
70 vty_out(vty, "\r");
71}
72
73struct log_target *log_target_create_vty(struct vty *vty)
74{
75 struct log_target *target;
76
77 target = log_target_create();
78 if (!target)
79 return NULL;
80
81 target->tgt_vty.vty = vty;
82 target->output = _vty_output;
83 return target;
84}
85
86DEFUN(enable_logging,
87 enable_logging_cmd,
88 "logging enable",
89 LOGGING_STR
90 "Enables logging to this vty\n")
91{
92 struct telnet_connection *conn;
93
94 conn = (struct telnet_connection *) vty->priv;
95 if (conn->dbg) {
96 vty_out(vty, "Logging already enabled.%s", VTY_NEWLINE);
97 return CMD_WARNING;
98 }
99
100 conn->dbg = log_target_create_vty(vty);
101 if (!conn->dbg)
102 return CMD_WARNING;
103
104 log_add_target(conn->dbg);
105 return CMD_SUCCESS;
106}
107
Harald Weltea62648b2011-02-18 21:03:27 +0100108struct log_target *osmo_log_vty2tgt(struct vty *vty)
109{
110 struct telnet_connection *conn;
111
112 if (vty->node == CFG_LOG_NODE)
113 return vty->index;
114
115
116 conn = (struct telnet_connection *) vty->priv;
117 if (!conn->dbg)
118 vty_out(vty, "Logging was not enabled.%s", VTY_NEWLINE);
119
120 return conn->dbg;
121}
122
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200123DEFUN(logging_fltr_all,
124 logging_fltr_all_cmd,
125 "logging filter all (0|1)",
126 LOGGING_STR FILTER_STR
127 "Do you want to log all messages?\n"
128 "Only print messages matched by other filters\n"
129 "Bypass filter and print all messages\n")
130{
Harald Weltea62648b2011-02-18 21:03:27 +0100131 struct log_target *tgt = osmo_log_vty2tgt(vty);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200132
Harald Weltea62648b2011-02-18 21:03:27 +0100133 if (!tgt)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200134 return CMD_WARNING;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200135
Harald Weltea62648b2011-02-18 21:03:27 +0100136 log_set_all_filter(tgt, atoi(argv[0]));
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200137 return CMD_SUCCESS;
138}
139
140DEFUN(logging_use_clr,
141 logging_use_clr_cmd,
142 "logging color (0|1)",
143 LOGGING_STR "Configure color-printing for log messages\n"
144 "Don't use color for printing messages\n"
145 "Use color for printing messages\n")
146{
Harald Weltea62648b2011-02-18 21:03:27 +0100147 struct log_target *tgt = osmo_log_vty2tgt(vty);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200148
Harald Weltea62648b2011-02-18 21:03:27 +0100149 if (!tgt)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200150 return CMD_WARNING;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200151
Harald Weltea62648b2011-02-18 21:03:27 +0100152 log_set_use_color(tgt, atoi(argv[0]));
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200153 return CMD_SUCCESS;
154}
155
156DEFUN(logging_prnt_timestamp,
157 logging_prnt_timestamp_cmd,
158 "logging timestamp (0|1)",
159 LOGGING_STR "Configure log message timestamping\n"
160 "Don't prefix each log message\n"
161 "Prefix each log message with current timestamp\n")
162{
Harald Weltea62648b2011-02-18 21:03:27 +0100163 struct log_target *tgt = osmo_log_vty2tgt(vty);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200164
Harald Weltea62648b2011-02-18 21:03:27 +0100165 if (!tgt)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200166 return CMD_WARNING;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200167
Harald Weltea62648b2011-02-18 21:03:27 +0100168 log_set_print_timestamp(tgt, atoi(argv[0]));
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200169 return CMD_SUCCESS;
170}
171
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100172DEFUN(logging_prnt_ext_timestamp,
173 logging_prnt_ext_timestamp_cmd,
174 "logging print extended-timestamp (0|1)",
175 LOGGING_STR "Log output settings\n"
176 "Configure log message timestamping\n"
177 "Don't prefix each log message\n"
178 "Prefix each log message with current timestamp with YYYYMMDDhhmmssnnn\n")
179{
180 struct log_target *tgt = osmo_log_vty2tgt(vty);
181
182 if (!tgt)
183 return CMD_WARNING;
184
185 log_set_print_extended_timestamp(tgt, atoi(argv[0]));
186 return CMD_SUCCESS;
187}
188
189DEFUN(logging_prnt_cat,
190 logging_prnt_cat_cmd,
191 "logging print category (0|1)",
192 LOGGING_STR "Log output settings\n"
193 "Configure log message\n"
194 "Don't prefix each log message\n"
195 "Prefix each log message with category/subsystem name\n")
196{
197 struct log_target *tgt = osmo_log_vty2tgt(vty);
198
199 if (!tgt)
200 return CMD_WARNING;
201
202 log_set_print_category(tgt, atoi(argv[0]));
203 return CMD_SUCCESS;
204}
205
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100206DEFUN(logging_prnt_cat_hex,
207 logging_prnt_cat_hex_cmd,
208 "logging print category-hex (0|1)",
209 LOGGING_STR "Log output settings\n"
210 "Configure log message\n"
211 "Don't prefix each log message\n"
212 "Prefix each log message with category/subsystem nr in hex ('<000b>')\n")
213{
214 struct log_target *tgt = osmo_log_vty2tgt(vty);
215
216 if (!tgt)
217 return CMD_WARNING;
218
219 log_set_print_category_hex(tgt, atoi(argv[0]));
220 return CMD_SUCCESS;
221}
222
Neels Hofmeyr886e5482018-01-16 01:49:37 +0100223DEFUN(logging_prnt_level,
224 logging_prnt_level_cmd,
225 "logging print level (0|1)",
226 LOGGING_STR "Log output settings\n"
227 "Configure log message\n"
228 "Don't prefix each log message\n"
229 "Prefix each log message with the log level name\n")
230{
231 struct log_target *tgt = osmo_log_vty2tgt(vty);
232
233 if (!tgt)
234 return CMD_WARNING;
235
236 log_set_print_level(tgt, atoi(argv[0]));
237 return CMD_SUCCESS;
238}
239
Neels Hofmeyr22772cc2018-02-06 00:52:08 +0100240static const struct value_string logging_print_file_args[] = {
241 { LOG_FILENAME_NONE, "0" },
242 { LOG_FILENAME_PATH, "1" },
243 { LOG_FILENAME_BASENAME, "basename" },
244 { 0, NULL }
245};
246
Neels Hofmeyrc6fd2452018-01-16 01:57:38 +0100247DEFUN(logging_prnt_file,
248 logging_prnt_file_cmd,
Neels Hofmeyr77ae45d2018-08-27 20:32:36 +0200249 "logging print file (0|1|basename) [last]",
Neels Hofmeyrc6fd2452018-01-16 01:57:38 +0100250 LOGGING_STR "Log output settings\n"
251 "Configure log message\n"
252 "Don't prefix each log message\n"
253 "Prefix each log message with the source file and line\n"
Neels Hofmeyr77ae45d2018-08-27 20:32:36 +0200254 "Prefix each log message with the source file's basename (strip leading paths) and line\n"
255 "Log source file info at the end of a log line. If omitted, log source file info just"
256 " before the log text.\n")
Neels Hofmeyrc6fd2452018-01-16 01:57:38 +0100257{
258 struct log_target *tgt = osmo_log_vty2tgt(vty);
Neels Hofmeyrc6fd2452018-01-16 01:57:38 +0100259
260 if (!tgt)
261 return CMD_WARNING;
262
Neels Hofmeyr22772cc2018-02-06 00:52:08 +0100263 log_set_print_filename2(tgt, get_string_value(logging_print_file_args, argv[0]));
Neels Hofmeyr77ae45d2018-08-27 20:32:36 +0200264 if (argc > 1)
265 log_set_print_filename_pos(tgt, LOG_FILENAME_POS_LINE_END);
266 else
267 log_set_print_filename_pos(tgt, LOG_FILENAME_POS_HEADER_END);
Neels Hofmeyrc6fd2452018-01-16 01:57:38 +0100268 return CMD_SUCCESS;
269}
270
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200271DEFUN(logging_level,
272 logging_level_cmd,
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100273 NULL, /* cmdstr is dynamically set in logging_vty_add_cmds(). */
274 NULL) /* same thing for helpstr. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200275{
Harald Weltea62648b2011-02-18 21:03:27 +0100276 int category = log_parse_category(argv[0]);
277 int level = log_parse_level(argv[1]);
278 struct log_target *tgt = osmo_log_vty2tgt(vty);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200279
Harald Weltea62648b2011-02-18 21:03:27 +0100280 if (!tgt)
281 return CMD_WARNING;
282
283 if (level < 0) {
284 vty_out(vty, "Invalid level `%s'%s", argv[1], VTY_NEWLINE);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200285 return CMD_WARNING;
286 }
287
Maxed0eda22017-07-06 17:09:01 +0200288 if (strcmp(argv[1], "everything") == 0) { /* FIXME: remove this check once 'everything' is phased out */
289 vty_out(vty, "%% Ignoring deprecated logging level %s%s", argv[1], VTY_NEWLINE);
290 return CMD_SUCCESS;
291 }
292
Harald Weltea62648b2011-02-18 21:03:27 +0100293 /* Check for special case where we want to set global log level */
294 if (!strcmp(argv[0], "all")) {
295 log_set_log_level(tgt, level);
296 return CMD_SUCCESS;
297 }
298
299 if (category < 0) {
300 vty_out(vty, "Invalid category `%s'%s", argv[0], VTY_NEWLINE);
301 return CMD_WARNING;
302 }
303
304 tgt->categories[category].enabled = 1;
305 tgt->categories[category].loglevel = level;
306
307 return CMD_SUCCESS;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200308}
309
310DEFUN(logging_set_category_mask,
311 logging_set_category_mask_cmd,
Holger Hans Peter Freyther146d1d32011-10-03 23:15:41 +0200312 "logging set-log-mask MASK",
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200313 LOGGING_STR
Holger Hans Peter Freyther146d1d32011-10-03 23:15:41 +0200314 "Set the logmask of this logging target\n"
Neels Hofmeyr84ea2e02017-12-09 05:53:18 +0100315 "List of logging categories to log, e.g. 'abc:mno:xyz'. Available log categories depend on the specific"
316 " application, refer to the 'logging level' command. Optionally add individual log levels like"
317 " 'abc,1:mno,3:xyz,5', where the level numbers are"
318 " " OSMO_STRINGIFY(LOGL_DEBUG) "=" OSMO_STRINGIFY_VAL(LOGL_DEBUG)
319 " " OSMO_STRINGIFY(LOGL_INFO) "=" OSMO_STRINGIFY_VAL(LOGL_INFO)
320 " " OSMO_STRINGIFY(LOGL_NOTICE) "=" OSMO_STRINGIFY_VAL(LOGL_NOTICE)
321 " " OSMO_STRINGIFY(LOGL_ERROR) "=" OSMO_STRINGIFY_VAL(LOGL_ERROR)
322 " " OSMO_STRINGIFY(LOGL_FATAL) "=" OSMO_STRINGIFY_VAL(LOGL_FATAL)
323 "\n")
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200324{
Harald Weltea62648b2011-02-18 21:03:27 +0100325 struct log_target *tgt = osmo_log_vty2tgt(vty);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200326
Harald Weltea62648b2011-02-18 21:03:27 +0100327 if (!tgt)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200328 return CMD_WARNING;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200329
Harald Weltea62648b2011-02-18 21:03:27 +0100330 log_parse_category_mask(tgt, argv[0]);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200331 return CMD_SUCCESS;
332}
333
Holger Hans Peter Freyther146d1d32011-10-03 23:15:41 +0200334ALIAS_DEPRECATED(logging_set_category_mask,
335 logging_set_category_mask_old_cmd,
336 "logging set log mask MASK",
337 LOGGING_STR
338 "Decide which categories to output.\n"
Neels Hofmeyr84ea2e02017-12-09 05:53:18 +0100339 "Log commands\n" "Mask commands\n"
340 "'set log mask' is deprecated, please refer to the docs of 'set-log-mask' instead\n");
Holger Hans Peter Freyther146d1d32011-10-03 23:15:41 +0200341
342
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200343DEFUN(diable_logging,
344 disable_logging_cmd,
345 "logging disable",
346 LOGGING_STR
347 "Disables logging to this vty\n")
348{
Harald Weltea62648b2011-02-18 21:03:27 +0100349 struct log_target *tgt = osmo_log_vty2tgt(vty);
350 struct telnet_connection *conn = (struct telnet_connection *) vty->priv;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200351
Harald Weltea62648b2011-02-18 21:03:27 +0100352 if (!tgt)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200353 return CMD_WARNING;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200354
Harald Weltea62648b2011-02-18 21:03:27 +0100355 log_del_target(tgt);
356 talloc_free(tgt);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200357 conn->dbg = NULL;
Harald Weltea62648b2011-02-18 21:03:27 +0100358
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200359 return CMD_SUCCESS;
360}
361
362static void vty_print_logtarget(struct vty *vty, const struct log_info *info,
363 const struct log_target *tgt)
364{
365 unsigned int i;
366
367 vty_out(vty, " Global Loglevel: %s%s",
368 log_level_str(tgt->loglevel), VTY_NEWLINE);
369 vty_out(vty, " Use color: %s, Print Timestamp: %s%s",
370 tgt->use_color ? "On" : "Off",
371 tgt->print_timestamp ? "On" : "Off", VTY_NEWLINE);
372
373 vty_out(vty, " Log Level specific information:%s", VTY_NEWLINE);
374
375 for (i = 0; i < info->num_cat; i++) {
376 const struct log_category *cat = &tgt->categories[i];
Daniel Willmann55363a92016-11-15 10:05:51 +0100377 /* Skip categories that were not initialized */
378 if (!info->cat[i].name)
379 continue;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200380 vty_out(vty, " %-10s %-10s %-8s %s%s",
381 info->cat[i].name+1, log_level_str(cat->loglevel),
382 cat->enabled ? "Enabled" : "Disabled",
383 info->cat[i].description,
384 VTY_NEWLINE);
385 }
Harald Welte6d2d4d62013-03-10 09:53:24 +0000386
387 vty_out(vty, " Log Filter 'ALL': %s%s",
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100388 tgt->filter_map & (1 << LOG_FLT_ALL) ? "Enabled" : "Disabled",
Harald Welte6d2d4d62013-03-10 09:53:24 +0000389 VTY_NEWLINE);
390
Harald Weltefb84f322013-06-06 07:33:54 +0200391 /* print application specific filters */
392 if (info->print_fn)
393 info->print_fn(vty, info, tgt);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200394}
395
396#define SHOW_LOG_STR "Show current logging configuration\n"
397
398DEFUN(show_logging_vty,
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000399 show_logging_vty_cmd,
400 "show logging vty",
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200401 SHOW_STR SHOW_LOG_STR
402 "Show current logging configuration for this vty\n")
403{
Harald Weltea62648b2011-02-18 21:03:27 +0100404 struct log_target *tgt = osmo_log_vty2tgt(vty);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200405
Harald Weltea62648b2011-02-18 21:03:27 +0100406 if (!tgt)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200407 return CMD_WARNING;
Harald Weltea62648b2011-02-18 21:03:27 +0100408
409 vty_print_logtarget(vty, osmo_log_info, tgt);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200410
411 return CMD_SUCCESS;
412}
413
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000414DEFUN(show_alarms,
415 show_alarms_cmd,
416 "show alarms",
417 SHOW_STR SHOW_LOG_STR
418 "Show the contents of the logging ringbuffer\n")
419{
420 int i, num_alarms;
421 struct osmo_strrb *rb;
422 struct log_target *tgt = log_target_find(LOG_TGT_TYPE_STRRB, NULL);
423 if (!tgt) {
424 vty_out(vty, "%% No alarms, run 'log alarms <2-32700>'%s",
425 VTY_NEWLINE);
426 return CMD_WARNING;
427 }
428
429 rb = tgt->tgt_rb.rb;
430 num_alarms = osmo_strrb_elements(rb);
431
432 vty_out(vty, "%% Showing %i alarms%s", num_alarms, VTY_NEWLINE);
433
434 for (i = 0; i < num_alarms; i++)
435 vty_out(vty, "%% %s%s", osmo_strrb_get_nth(rb, i),
436 VTY_NEWLINE);
437
438 return CMD_SUCCESS;
439}
440
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200441gDEFUN(cfg_description, cfg_description_cmd,
442 "description .TEXT",
Thorsten Alteholza81055d2017-03-02 22:13:48 +0100443 "Save human-readable description of the object\n"
Holger Hans Peter Freytherc9b3e062012-07-25 13:02:49 +0200444 "Text until the end of the line\n")
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200445{
446 char **dptr = vty->index_sub;
447
448 if (!dptr) {
449 vty_out(vty, "vty->index_sub == NULL%s", VTY_NEWLINE);
450 return CMD_WARNING;
451 }
452
Holger Hans Peter Freytherff0670e2011-02-24 14:20:41 +0100453 if (*dptr)
454 talloc_free(*dptr);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200455 *dptr = argv_concat(argv, argc, 0);
Holger Hans Peter Freyther6a75d162013-07-14 09:07:11 +0200456 if (!*dptr)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200457 return CMD_WARNING;
458
459 return CMD_SUCCESS;
460}
461
462gDEFUN(cfg_no_description, cfg_no_description_cmd,
463 "no description",
464 NO_STR
465 "Remove description of the object\n")
466{
467 char **dptr = vty->index_sub;
468
469 if (!dptr) {
470 vty_out(vty, "vty->index_sub == NULL%s", VTY_NEWLINE);
471 return CMD_WARNING;
472 }
473
474 if (*dptr) {
475 talloc_free(*dptr);
476 *dptr = NULL;
477 }
478
479 return CMD_SUCCESS;
480}
481
Harald Welte28222962011-02-18 20:37:04 +0100482/* Support for configuration of log targets != the current vty */
483
484struct cmd_node cfg_log_node = {
485 CFG_LOG_NODE,
486 "%s(config-log)# ",
487 1
488};
489
Harald Welte28222962011-02-18 20:37:04 +0100490#ifdef HAVE_SYSLOG_H
491
492#include <syslog.h>
493
494static const int local_sysl_map[] = {
495 [0] = LOG_LOCAL0,
496 [1] = LOG_LOCAL1,
497 [2] = LOG_LOCAL2,
498 [3] = LOG_LOCAL3,
499 [4] = LOG_LOCAL4,
500 [5] = LOG_LOCAL5,
501 [6] = LOG_LOCAL6,
502 [7] = LOG_LOCAL7
503};
504
Harald Weltede79cee2011-02-24 23:47:57 +0100505/* From VTY core code */
506extern struct host host;
507
Harald Welte28222962011-02-18 20:37:04 +0100508static int _cfg_log_syslog(struct vty *vty, int facility)
509{
510 struct log_target *tgt;
511
512 /* First delete the old syslog target, if any */
513 tgt = log_target_find(LOG_TGT_TYPE_SYSLOG, NULL);
514 if (tgt)
515 log_target_destroy(tgt);
516
Harald Weltede79cee2011-02-24 23:47:57 +0100517 tgt = log_target_create_syslog(host.app_info->name, 0, facility);
Harald Welte28222962011-02-18 20:37:04 +0100518 if (!tgt) {
519 vty_out(vty, "%% Unable to open syslog%s", VTY_NEWLINE);
520 return CMD_WARNING;
521 }
522 log_add_target(tgt);
523
524 vty->index = tgt;
525 vty->node = CFG_LOG_NODE;
526
527 return CMD_SUCCESS;
528}
529
530DEFUN(cfg_log_syslog_local, cfg_log_syslog_local_cmd,
531 "log syslog local <0-7>",
532 LOG_STR "Logging via syslog\n" "Syslog LOCAL facility\n"
533 "Local facility number\n")
534{
535 int local = atoi(argv[0]);
536 int facility = local_sysl_map[local];
537
538 return _cfg_log_syslog(vty, facility);
539}
540
541static struct value_string sysl_level_names[] = {
542 { LOG_AUTHPRIV, "authpriv" },
543 { LOG_CRON, "cron" },
544 { LOG_DAEMON, "daemon" },
545 { LOG_FTP, "ftp" },
546 { LOG_LPR, "lpr" },
547 { LOG_MAIL, "mail" },
548 { LOG_NEWS, "news" },
549 { LOG_USER, "user" },
550 { LOG_UUCP, "uucp" },
551 /* only for value -> string conversion */
552 { LOG_LOCAL0, "local 0" },
553 { LOG_LOCAL1, "local 1" },
554 { LOG_LOCAL2, "local 2" },
555 { LOG_LOCAL3, "local 3" },
556 { LOG_LOCAL4, "local 4" },
557 { LOG_LOCAL5, "local 5" },
558 { LOG_LOCAL6, "local 6" },
559 { LOG_LOCAL7, "local 7" },
560 { 0, NULL }
561};
562
563DEFUN(cfg_log_syslog, cfg_log_syslog_cmd,
564 "log syslog (authpriv|cron|daemon|ftp|lpr|mail|news|user|uucp)",
Holger Hans Peter Freythera4463fd2011-10-03 23:17:36 +0200565 LOG_STR "Logging via syslog\n"
566 "Security/authorization messages facility\n"
567 "Clock daemon (cron/at) facility\n"
568 "General system daemon facility\n"
569 "Ftp daemon facility\n"
570 "Line printer facility\n"
571 "Mail facility\n"
572 "News facility\n"
573 "Generic facility\n"
574 "UUCP facility\n")
Harald Welte28222962011-02-18 20:37:04 +0100575{
576 int facility = get_string_value(sysl_level_names, argv[0]);
577
578 return _cfg_log_syslog(vty, facility);
579}
580
581DEFUN(cfg_no_log_syslog, cfg_no_log_syslog_cmd,
582 "no log syslog",
583 NO_STR LOG_STR "Logging via syslog\n")
584{
585 struct log_target *tgt;
586
587 tgt = log_target_find(LOG_TGT_TYPE_SYSLOG, NULL);
588 if (!tgt) {
589 vty_out(vty, "%% No syslog target found%s",
590 VTY_NEWLINE);
591 return CMD_WARNING;
592 }
593
594 log_target_destroy(tgt);
595
596 return CMD_SUCCESS;
597}
598#endif /* HAVE_SYSLOG_H */
599
Harald Welteaa00f992016-12-02 15:30:02 +0100600DEFUN(cfg_log_gsmtap, cfg_log_gsmtap_cmd,
601 "log gsmtap [HOSTNAME]",
Neels Hofmeyrfd9ec3b2016-12-11 01:48:26 +0100602 LOG_STR "Logging via GSMTAP\n"
603 "Host name to send the GSMTAP logging to (UDP port 4729)\n")
Harald Welteaa00f992016-12-02 15:30:02 +0100604{
Max2f153b52018-01-04 12:25:57 +0100605 const char *hostname = argc ? argv[0] : "127.0.0.1";
Harald Welteaa00f992016-12-02 15:30:02 +0100606 struct log_target *tgt;
607
608 tgt = log_target_find(LOG_TGT_TYPE_GSMTAP, hostname);
609 if (!tgt) {
610 tgt = log_target_create_gsmtap(hostname, GSMTAP_UDP_PORT,
611 host.app_info->name, false,
612 true);
613 if (!tgt) {
Max2f153b52018-01-04 12:25:57 +0100614 vty_out(vty, "%% Unable to create GSMTAP log for %s%s",
615 hostname, VTY_NEWLINE);
Harald Welteaa00f992016-12-02 15:30:02 +0100616 return CMD_WARNING;
617 }
618 log_add_target(tgt);
619 }
620
621 vty->index = tgt;
622 vty->node = CFG_LOG_NODE;
623
624 return CMD_SUCCESS;
625}
626
Harald Welte28222962011-02-18 20:37:04 +0100627DEFUN(cfg_log_stderr, cfg_log_stderr_cmd,
628 "log stderr",
629 LOG_STR "Logging via STDERR of the process\n")
630{
631 struct log_target *tgt;
632
633 tgt = log_target_find(LOG_TGT_TYPE_STDERR, NULL);
634 if (!tgt) {
635 tgt = log_target_create_stderr();
636 if (!tgt) {
637 vty_out(vty, "%% Unable to create stderr log%s",
638 VTY_NEWLINE);
639 return CMD_WARNING;
640 }
641 log_add_target(tgt);
642 }
643
644 vty->index = tgt;
645 vty->node = CFG_LOG_NODE;
646
647 return CMD_SUCCESS;
648}
649
650DEFUN(cfg_no_log_stderr, cfg_no_log_stderr_cmd,
651 "no log stderr",
652 NO_STR LOG_STR "Logging via STDERR of the process\n")
653{
654 struct log_target *tgt;
655
656 tgt = log_target_find(LOG_TGT_TYPE_STDERR, NULL);
657 if (!tgt) {
658 vty_out(vty, "%% No stderr logging active%s", VTY_NEWLINE);
659 return CMD_WARNING;
660 }
661
662 log_target_destroy(tgt);
663
664 return CMD_SUCCESS;
665}
666
667DEFUN(cfg_log_file, cfg_log_file_cmd,
668 "log file .FILENAME",
669 LOG_STR "Logging to text file\n" "Filename\n")
670{
671 const char *fname = argv[0];
672 struct log_target *tgt;
673
674 tgt = log_target_find(LOG_TGT_TYPE_FILE, fname);
675 if (!tgt) {
676 tgt = log_target_create_file(fname);
677 if (!tgt) {
678 vty_out(vty, "%% Unable to create file `%s'%s",
679 fname, VTY_NEWLINE);
680 return CMD_WARNING;
681 }
682 log_add_target(tgt);
683 }
684
685 vty->index = tgt;
686 vty->node = CFG_LOG_NODE;
687
688 return CMD_SUCCESS;
689}
690
691
692DEFUN(cfg_no_log_file, cfg_no_log_file_cmd,
693 "no log file .FILENAME",
694 NO_STR LOG_STR "Logging to text file\n" "Filename\n")
695{
696 const char *fname = argv[0];
697 struct log_target *tgt;
698
699 tgt = log_target_find(LOG_TGT_TYPE_FILE, fname);
700 if (!tgt) {
701 vty_out(vty, "%% No such log file `%s'%s",
702 fname, VTY_NEWLINE);
703 return CMD_WARNING;
704 }
705
706 log_target_destroy(tgt);
707
708 return CMD_SUCCESS;
709}
710
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000711DEFUN(cfg_log_alarms, cfg_log_alarms_cmd,
712 "log alarms <2-32700>",
713 LOG_STR "Logging alarms to osmo_strrb\n"
714 "Maximum number of messages to log\n")
715{
716 struct log_target *tgt;
717 unsigned int rbsize = atoi(argv[0]);
718
719 tgt = log_target_find(LOG_TGT_TYPE_STRRB, NULL);
720 if (tgt)
721 log_target_destroy(tgt);
722
723 tgt = log_target_create_rb(rbsize);
724 if (!tgt) {
725 vty_out(vty, "%% Unable to create osmo_strrb (size %u)%s",
726 rbsize, VTY_NEWLINE);
727 return CMD_WARNING;
728 }
729 log_add_target(tgt);
730
731 vty->index = tgt;
732 vty->node = CFG_LOG_NODE;
733
734 return CMD_SUCCESS;
735}
736
737DEFUN(cfg_no_log_alarms, cfg_no_log_alarms_cmd,
738 "no log alarms",
739 NO_STR LOG_STR "Logging alarms to osmo_strrb\n")
740{
741 struct log_target *tgt;
742
743 tgt = log_target_find(LOG_TGT_TYPE_STRRB, NULL);
744 if (!tgt) {
745 vty_out(vty, "%% No osmo_strrb target found%s", VTY_NEWLINE);
746 return CMD_WARNING;
747 }
748
749 log_target_destroy(tgt);
750
751 return CMD_SUCCESS;
752}
753
Harald Welte28222962011-02-18 20:37:04 +0100754static int config_write_log_single(struct vty *vty, struct log_target *tgt)
755{
756 int i;
Harald Welte28222962011-02-18 20:37:04 +0100757
758 switch (tgt->type) {
759 case LOG_TGT_TYPE_VTY:
760 return 1;
761 break;
762 case LOG_TGT_TYPE_STDERR:
763 vty_out(vty, "log stderr%s", VTY_NEWLINE);
764 break;
765 case LOG_TGT_TYPE_SYSLOG:
766#ifdef HAVE_SYSLOG_H
767 vty_out(vty, "log syslog %s%s",
768 get_value_string(sysl_level_names,
769 tgt->tgt_syslog.facility),
770 VTY_NEWLINE);
771#endif
772 break;
773 case LOG_TGT_TYPE_FILE:
774 vty_out(vty, "log file %s%s", tgt->tgt_file.fname, VTY_NEWLINE);
775 break;
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000776 case LOG_TGT_TYPE_STRRB:
777 vty_out(vty, "log alarms %zu%s",
778 log_target_rb_avail_size(tgt), VTY_NEWLINE);
779 break;
Harald Welteaa00f992016-12-02 15:30:02 +0100780 case LOG_TGT_TYPE_GSMTAP:
781 vty_out(vty, "log gsmtap %s%s",
782 tgt->tgt_gsmtap.hostname, VTY_NEWLINE);
783 break;
Harald Welte28222962011-02-18 20:37:04 +0100784 }
785
Harald Welte2da47f12012-10-22 19:31:54 +0200786 vty_out(vty, " logging filter all %u%s",
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100787 tgt->filter_map & (1 << LOG_FLT_ALL) ? 1 : 0, VTY_NEWLINE);
Harald Weltefb84f322013-06-06 07:33:54 +0200788 /* save filters outside of libosmocore, i.e. in app code */
789 if (osmo_log_info->save_fn)
790 osmo_log_info->save_fn(vty, osmo_log_info, tgt);
Harald Welte2da47f12012-10-22 19:31:54 +0200791
Harald Welte28222962011-02-18 20:37:04 +0100792 vty_out(vty, " logging color %u%s", tgt->use_color ? 1 : 0,
793 VTY_NEWLINE);
Holger Hans Peter Freyther879acef2015-01-27 11:06:51 +0100794 vty_out(vty, " logging print category %d%s",
Michael McTernan78933462015-03-20 15:29:25 +0100795 tgt->print_category ? 1 : 0, VTY_NEWLINE);
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100796 if (tgt->print_ext_timestamp)
797 vty_out(vty, " logging print extended-timestamp 1%s", VTY_NEWLINE);
798 else
799 vty_out(vty, " logging timestamp %u%s",
800 tgt->print_timestamp ? 1 : 0, VTY_NEWLINE);
Neels Hofmeyr886e5482018-01-16 01:49:37 +0100801 if (tgt->print_level)
802 vty_out(vty, " logging print level 1%s", VTY_NEWLINE);
Neels Hofmeyr22772cc2018-02-06 00:52:08 +0100803 vty_out(vty, " logging print file %s%s",
804 get_value_string(logging_print_file_args, tgt->print_filename2),
805 VTY_NEWLINE);
Harald Welte28222962011-02-18 20:37:04 +0100806
807 /* stupid old osmo logging API uses uppercase strings... */
Neels Hofmeyr7c749892018-09-07 03:01:38 +0200808 vty_out(vty, " logging level all %s%s", osmo_str_tolower(log_level_str(tgt->loglevel)),
809 VTY_NEWLINE);
Harald Welte28222962011-02-18 20:37:04 +0100810
811 for (i = 0; i < osmo_log_info->num_cat; i++) {
812 const struct log_category *cat = &tgt->categories[i];
Harald Welte28222962011-02-18 20:37:04 +0100813
Harald Welte1a02cfc2013-03-19 10:37:39 +0100814 /* skip empty entries in the array */
815 if (!osmo_log_info->cat[i].name)
816 continue;
817
Harald Welte28222962011-02-18 20:37:04 +0100818 /* stupid old osmo logging API uses uppercase strings... */
Neels Hofmeyr7c749892018-09-07 03:01:38 +0200819 vty_out(vty, " logging level %s", osmo_str_tolower(osmo_log_info->cat[i].name+1));
820 vty_out(vty, " %s%s", osmo_str_tolower(log_level_str(cat->loglevel)), VTY_NEWLINE);
Harald Welte28222962011-02-18 20:37:04 +0100821 }
822
Harald Welte28222962011-02-18 20:37:04 +0100823 return 1;
824}
825
826static int config_write_log(struct vty *vty)
827{
828 struct log_target *dbg = vty->index;
829
830 llist_for_each_entry(dbg, &osmo_log_target_list, entry)
831 config_write_log_single(vty, dbg);
832
833 return 1;
834}
835
Harald Welte11eb4b52018-06-09 17:41:31 +0200836static int log_deprecated_func(struct cmd_element *cmd, struct vty *vty, int argc, const char *argv[])
837{
838 vty_out(vty, "%% Ignoring deprecated '%s'%s", cmd->string, VTY_NEWLINE);
839 return CMD_WARNING;
840}
841
842void logging_vty_add_deprecated_subsys(void *ctx, const char *name)
843{
844 struct cmd_element *cmd = talloc_zero(ctx, struct cmd_element);
845 OSMO_ASSERT(cmd);
846 cmd->string = talloc_asprintf(cmd, "logging level %s (everything|debug|info|notice|error|fatal)",
847 name);
848 printf("%s\n", cmd->string);
849 cmd->func = log_deprecated_func;
850 cmd->doc = "Set the log level for a specified category\n"
851 "Deprecated Category\n";
852 cmd->attr = CMD_ATTR_DEPRECATED;
853
854 install_element(CFG_LOG_NODE, cmd);
855}
856
Harald Welte8c648252017-10-16 15:17:03 +0200857/*! Register logging related commands to the VTY. Call this once from
858 * your application if you want to support those commands. */
Maxc65c5b42017-03-15 13:20:23 +0100859void logging_vty_add_cmds()
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200860{
861 install_element_ve(&enable_logging_cmd);
862 install_element_ve(&disable_logging_cmd);
863 install_element_ve(&logging_fltr_all_cmd);
864 install_element_ve(&logging_use_clr_cmd);
865 install_element_ve(&logging_prnt_timestamp_cmd);
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100866 install_element_ve(&logging_prnt_ext_timestamp_cmd);
867 install_element_ve(&logging_prnt_cat_cmd);
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100868 install_element_ve(&logging_prnt_cat_hex_cmd);
Neels Hofmeyr886e5482018-01-16 01:49:37 +0100869 install_element_ve(&logging_prnt_level_cmd);
Neels Hofmeyrc6fd2452018-01-16 01:57:38 +0100870 install_element_ve(&logging_prnt_file_cmd);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200871 install_element_ve(&logging_set_category_mask_cmd);
Holger Hans Peter Freyther146d1d32011-10-03 23:15:41 +0200872 install_element_ve(&logging_set_category_mask_old_cmd);
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100873
874 /* Logging level strings are generated dynamically. */
Maxc65c5b42017-03-15 13:20:23 +0100875 logging_level_cmd.string = log_vty_command_string();
876 logging_level_cmd.doc = log_vty_command_description();
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200877 install_element_ve(&logging_level_cmd);
878 install_element_ve(&show_logging_vty_cmd);
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000879 install_element_ve(&show_alarms_cmd);
Harald Welte28222962011-02-18 20:37:04 +0100880
881 install_node(&cfg_log_node, config_write_log);
Harald Weltea62648b2011-02-18 21:03:27 +0100882 install_element(CFG_LOG_NODE, &logging_fltr_all_cmd);
883 install_element(CFG_LOG_NODE, &logging_use_clr_cmd);
884 install_element(CFG_LOG_NODE, &logging_prnt_timestamp_cmd);
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100885 install_element(CFG_LOG_NODE, &logging_prnt_ext_timestamp_cmd);
886 install_element(CFG_LOG_NODE, &logging_prnt_cat_cmd);
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100887 install_element(CFG_LOG_NODE, &logging_prnt_cat_hex_cmd);
Neels Hofmeyr886e5482018-01-16 01:49:37 +0100888 install_element(CFG_LOG_NODE, &logging_prnt_level_cmd);
Neels Hofmeyrc6fd2452018-01-16 01:57:38 +0100889 install_element(CFG_LOG_NODE, &logging_prnt_file_cmd);
Harald Weltea62648b2011-02-18 21:03:27 +0100890 install_element(CFG_LOG_NODE, &logging_level_cmd);
Harald Welte28222962011-02-18 20:37:04 +0100891
892 install_element(CONFIG_NODE, &cfg_log_stderr_cmd);
893 install_element(CONFIG_NODE, &cfg_no_log_stderr_cmd);
894 install_element(CONFIG_NODE, &cfg_log_file_cmd);
895 install_element(CONFIG_NODE, &cfg_no_log_file_cmd);
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000896 install_element(CONFIG_NODE, &cfg_log_alarms_cmd);
897 install_element(CONFIG_NODE, &cfg_no_log_alarms_cmd);
Harald Welte28222962011-02-18 20:37:04 +0100898#ifdef HAVE_SYSLOG_H
899 install_element(CONFIG_NODE, &cfg_log_syslog_cmd);
900 install_element(CONFIG_NODE, &cfg_log_syslog_local_cmd);
901 install_element(CONFIG_NODE, &cfg_no_log_syslog_cmd);
902#endif
Harald Welteaa00f992016-12-02 15:30:02 +0100903 install_element(CONFIG_NODE, &cfg_log_gsmtap_cmd);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200904}