blob: 758f0b96a4cb3205fab7f363b8db31b2760b31d5 [file] [log] [blame]
Neels Hofmeyr17518fe2017-06-20 04:35:06 +02001/*! \file logging_vty.c
2 * OpenBSC logging helper for the VTY. */
3/*
4 * (C) 2009-2010 by Harald Welte <laforge@gnumonks.org>
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +01005 * (C) 2009-2014 by Holger Hans Peter Freyther
Harald Welte3fb0b6f2010-05-19 19:02:52 +02006 * All Rights Reserved
7 *
8 * 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 Welte96e2a002017-06-12 21:44:18 +020044/*! \addtogroup logging
45 * @{
Neels Hofmeyr87e45502017-06-20 00:17:59 +020046 * Configuration of logging from VTY
Harald Welte96e2a002017-06-12 21:44:18 +020047 *
48 * This module implements functions that permit configuration of
49 * the libosmocore logging framework from VTY commands. This applies
50 * both to logging to the VTY (telnet sessions), as well as logging to
51 * other targets, such as sysslog, file, gsmtap, ...
52 */
53
Harald Welte4ebdf742010-05-19 19:54:00 +020054extern const struct log_info *osmo_log_info;
Harald Welte3fb0b6f2010-05-19 19:02:52 +020055
Harald Welte76e72ab2011-02-17 15:52:39 +010056static void _vty_output(struct log_target *tgt,
57 unsigned int level, const char *line)
Harald Welte3fb0b6f2010-05-19 19:02:52 +020058{
59 struct vty *vty = tgt->tgt_vty.vty;
60 vty_out(vty, "%s", line);
61 /* This is an ugly hack, but there is no easy way... */
62 if (strchr(line, '\n'))
63 vty_out(vty, "\r");
64}
65
66struct log_target *log_target_create_vty(struct vty *vty)
67{
68 struct log_target *target;
69
70 target = log_target_create();
71 if (!target)
72 return NULL;
73
74 target->tgt_vty.vty = vty;
75 target->output = _vty_output;
76 return target;
77}
78
79DEFUN(enable_logging,
80 enable_logging_cmd,
81 "logging enable",
82 LOGGING_STR
83 "Enables logging to this vty\n")
84{
85 struct telnet_connection *conn;
86
87 conn = (struct telnet_connection *) vty->priv;
88 if (conn->dbg) {
89 vty_out(vty, "Logging already enabled.%s", VTY_NEWLINE);
90 return CMD_WARNING;
91 }
92
93 conn->dbg = log_target_create_vty(vty);
94 if (!conn->dbg)
95 return CMD_WARNING;
96
97 log_add_target(conn->dbg);
98 return CMD_SUCCESS;
99}
100
Harald Weltea62648b2011-02-18 21:03:27 +0100101struct log_target *osmo_log_vty2tgt(struct vty *vty)
102{
103 struct telnet_connection *conn;
104
105 if (vty->node == CFG_LOG_NODE)
106 return vty->index;
107
108
109 conn = (struct telnet_connection *) vty->priv;
110 if (!conn->dbg)
111 vty_out(vty, "Logging was not enabled.%s", VTY_NEWLINE);
112
113 return conn->dbg;
114}
115
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200116DEFUN(logging_fltr_all,
117 logging_fltr_all_cmd,
118 "logging filter all (0|1)",
119 LOGGING_STR FILTER_STR
120 "Do you want to log all messages?\n"
121 "Only print messages matched by other filters\n"
122 "Bypass filter and print all messages\n")
123{
Harald Weltea62648b2011-02-18 21:03:27 +0100124 struct log_target *tgt = osmo_log_vty2tgt(vty);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200125
Harald Weltea62648b2011-02-18 21:03:27 +0100126 if (!tgt)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200127 return CMD_WARNING;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200128
Harald Weltea62648b2011-02-18 21:03:27 +0100129 log_set_all_filter(tgt, atoi(argv[0]));
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200130 return CMD_SUCCESS;
131}
132
133DEFUN(logging_use_clr,
134 logging_use_clr_cmd,
135 "logging color (0|1)",
136 LOGGING_STR "Configure color-printing for log messages\n"
137 "Don't use color for printing messages\n"
138 "Use color for printing messages\n")
139{
Harald Weltea62648b2011-02-18 21:03:27 +0100140 struct log_target *tgt = osmo_log_vty2tgt(vty);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200141
Harald Weltea62648b2011-02-18 21:03:27 +0100142 if (!tgt)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200143 return CMD_WARNING;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200144
Harald Weltea62648b2011-02-18 21:03:27 +0100145 log_set_use_color(tgt, atoi(argv[0]));
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200146 return CMD_SUCCESS;
147}
148
149DEFUN(logging_prnt_timestamp,
150 logging_prnt_timestamp_cmd,
151 "logging timestamp (0|1)",
152 LOGGING_STR "Configure log message timestamping\n"
153 "Don't prefix each log message\n"
154 "Prefix each log message with current timestamp\n")
155{
Harald Weltea62648b2011-02-18 21:03:27 +0100156 struct log_target *tgt = osmo_log_vty2tgt(vty);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200157
Harald Weltea62648b2011-02-18 21:03:27 +0100158 if (!tgt)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200159 return CMD_WARNING;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200160
Harald Weltea62648b2011-02-18 21:03:27 +0100161 log_set_print_timestamp(tgt, atoi(argv[0]));
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200162 return CMD_SUCCESS;
163}
164
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100165DEFUN(logging_prnt_ext_timestamp,
166 logging_prnt_ext_timestamp_cmd,
167 "logging print extended-timestamp (0|1)",
168 LOGGING_STR "Log output settings\n"
169 "Configure log message timestamping\n"
170 "Don't prefix each log message\n"
171 "Prefix each log message with current timestamp with YYYYMMDDhhmmssnnn\n")
172{
173 struct log_target *tgt = osmo_log_vty2tgt(vty);
174
175 if (!tgt)
176 return CMD_WARNING;
177
178 log_set_print_extended_timestamp(tgt, atoi(argv[0]));
179 return CMD_SUCCESS;
180}
181
182DEFUN(logging_prnt_cat,
183 logging_prnt_cat_cmd,
184 "logging print category (0|1)",
185 LOGGING_STR "Log output settings\n"
186 "Configure log message\n"
187 "Don't prefix each log message\n"
188 "Prefix each log message with category/subsystem name\n")
189{
190 struct log_target *tgt = osmo_log_vty2tgt(vty);
191
192 if (!tgt)
193 return CMD_WARNING;
194
195 log_set_print_category(tgt, atoi(argv[0]));
196 return CMD_SUCCESS;
197}
198
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200199DEFUN(logging_level,
200 logging_level_cmd,
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100201 NULL, /* cmdstr is dynamically set in logging_vty_add_cmds(). */
202 NULL) /* same thing for helpstr. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200203{
Harald Weltea62648b2011-02-18 21:03:27 +0100204 int category = log_parse_category(argv[0]);
205 int level = log_parse_level(argv[1]);
206 struct log_target *tgt = osmo_log_vty2tgt(vty);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200207
Harald Weltea62648b2011-02-18 21:03:27 +0100208 if (!tgt)
209 return CMD_WARNING;
210
211 if (level < 0) {
212 vty_out(vty, "Invalid level `%s'%s", argv[1], VTY_NEWLINE);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200213 return CMD_WARNING;
214 }
215
Harald Weltea62648b2011-02-18 21:03:27 +0100216 /* Check for special case where we want to set global log level */
217 if (!strcmp(argv[0], "all")) {
218 log_set_log_level(tgt, level);
219 return CMD_SUCCESS;
220 }
221
222 if (category < 0) {
223 vty_out(vty, "Invalid category `%s'%s", argv[0], VTY_NEWLINE);
224 return CMD_WARNING;
225 }
226
227 tgt->categories[category].enabled = 1;
228 tgt->categories[category].loglevel = level;
229
230 return CMD_SUCCESS;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200231}
232
233DEFUN(logging_set_category_mask,
234 logging_set_category_mask_cmd,
Holger Hans Peter Freyther146d1d32011-10-03 23:15:41 +0200235 "logging set-log-mask MASK",
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200236 LOGGING_STR
Holger Hans Peter Freyther146d1d32011-10-03 23:15:41 +0200237 "Set the logmask of this logging target\n"
238 "The logmask to use\n")
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200239{
Harald Weltea62648b2011-02-18 21:03:27 +0100240 struct log_target *tgt = osmo_log_vty2tgt(vty);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200241
Harald Weltea62648b2011-02-18 21:03:27 +0100242 if (!tgt)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200243 return CMD_WARNING;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200244
Harald Weltea62648b2011-02-18 21:03:27 +0100245 log_parse_category_mask(tgt, argv[0]);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200246 return CMD_SUCCESS;
247}
248
Holger Hans Peter Freyther146d1d32011-10-03 23:15:41 +0200249ALIAS_DEPRECATED(logging_set_category_mask,
250 logging_set_category_mask_old_cmd,
251 "logging set log mask MASK",
252 LOGGING_STR
253 "Decide which categories to output.\n"
254 "Log commands\n" "Mask commands\n" "The logmask to use\n");
255
256
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200257DEFUN(diable_logging,
258 disable_logging_cmd,
259 "logging disable",
260 LOGGING_STR
261 "Disables logging to this vty\n")
262{
Harald Weltea62648b2011-02-18 21:03:27 +0100263 struct log_target *tgt = osmo_log_vty2tgt(vty);
264 struct telnet_connection *conn = (struct telnet_connection *) vty->priv;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200265
Harald Weltea62648b2011-02-18 21:03:27 +0100266 if (!tgt)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200267 return CMD_WARNING;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200268
Harald Weltea62648b2011-02-18 21:03:27 +0100269 log_del_target(tgt);
270 talloc_free(tgt);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200271 conn->dbg = NULL;
Harald Weltea62648b2011-02-18 21:03:27 +0100272
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200273 return CMD_SUCCESS;
274}
275
276static void vty_print_logtarget(struct vty *vty, const struct log_info *info,
277 const struct log_target *tgt)
278{
279 unsigned int i;
280
281 vty_out(vty, " Global Loglevel: %s%s",
282 log_level_str(tgt->loglevel), VTY_NEWLINE);
283 vty_out(vty, " Use color: %s, Print Timestamp: %s%s",
284 tgt->use_color ? "On" : "Off",
285 tgt->print_timestamp ? "On" : "Off", VTY_NEWLINE);
286
287 vty_out(vty, " Log Level specific information:%s", VTY_NEWLINE);
288
289 for (i = 0; i < info->num_cat; i++) {
290 const struct log_category *cat = &tgt->categories[i];
Daniel Willmann55363a92016-11-15 10:05:51 +0100291 /* Skip categories that were not initialized */
292 if (!info->cat[i].name)
293 continue;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200294 vty_out(vty, " %-10s %-10s %-8s %s%s",
295 info->cat[i].name+1, log_level_str(cat->loglevel),
296 cat->enabled ? "Enabled" : "Disabled",
297 info->cat[i].description,
298 VTY_NEWLINE);
299 }
Harald Welte6d2d4d62013-03-10 09:53:24 +0000300
301 vty_out(vty, " Log Filter 'ALL': %s%s",
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100302 tgt->filter_map & (1 << LOG_FLT_ALL) ? "Enabled" : "Disabled",
Harald Welte6d2d4d62013-03-10 09:53:24 +0000303 VTY_NEWLINE);
304
Harald Weltefb84f322013-06-06 07:33:54 +0200305 /* print application specific filters */
306 if (info->print_fn)
307 info->print_fn(vty, info, tgt);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200308}
309
310#define SHOW_LOG_STR "Show current logging configuration\n"
311
312DEFUN(show_logging_vty,
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000313 show_logging_vty_cmd,
314 "show logging vty",
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200315 SHOW_STR SHOW_LOG_STR
316 "Show current logging configuration for this vty\n")
317{
Harald Weltea62648b2011-02-18 21:03:27 +0100318 struct log_target *tgt = osmo_log_vty2tgt(vty);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200319
Harald Weltea62648b2011-02-18 21:03:27 +0100320 if (!tgt)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200321 return CMD_WARNING;
Harald Weltea62648b2011-02-18 21:03:27 +0100322
323 vty_print_logtarget(vty, osmo_log_info, tgt);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200324
325 return CMD_SUCCESS;
326}
327
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000328DEFUN(show_alarms,
329 show_alarms_cmd,
330 "show alarms",
331 SHOW_STR SHOW_LOG_STR
332 "Show the contents of the logging ringbuffer\n")
333{
334 int i, num_alarms;
335 struct osmo_strrb *rb;
336 struct log_target *tgt = log_target_find(LOG_TGT_TYPE_STRRB, NULL);
337 if (!tgt) {
338 vty_out(vty, "%% No alarms, run 'log alarms <2-32700>'%s",
339 VTY_NEWLINE);
340 return CMD_WARNING;
341 }
342
343 rb = tgt->tgt_rb.rb;
344 num_alarms = osmo_strrb_elements(rb);
345
346 vty_out(vty, "%% Showing %i alarms%s", num_alarms, VTY_NEWLINE);
347
348 for (i = 0; i < num_alarms; i++)
349 vty_out(vty, "%% %s%s", osmo_strrb_get_nth(rb, i),
350 VTY_NEWLINE);
351
352 return CMD_SUCCESS;
353}
354
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200355gDEFUN(cfg_description, cfg_description_cmd,
356 "description .TEXT",
Thorsten Alteholza81055d2017-03-02 22:13:48 +0100357 "Save human-readable description of the object\n"
Holger Hans Peter Freytherc9b3e062012-07-25 13:02:49 +0200358 "Text until the end of the line\n")
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200359{
360 char **dptr = vty->index_sub;
361
362 if (!dptr) {
363 vty_out(vty, "vty->index_sub == NULL%s", VTY_NEWLINE);
364 return CMD_WARNING;
365 }
366
Holger Hans Peter Freytherff0670e2011-02-24 14:20:41 +0100367 if (*dptr)
368 talloc_free(*dptr);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200369 *dptr = argv_concat(argv, argc, 0);
Holger Hans Peter Freyther6a75d162013-07-14 09:07:11 +0200370 if (!*dptr)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200371 return CMD_WARNING;
372
373 return CMD_SUCCESS;
374}
375
376gDEFUN(cfg_no_description, cfg_no_description_cmd,
377 "no description",
378 NO_STR
379 "Remove description of the object\n")
380{
381 char **dptr = vty->index_sub;
382
383 if (!dptr) {
384 vty_out(vty, "vty->index_sub == NULL%s", VTY_NEWLINE);
385 return CMD_WARNING;
386 }
387
388 if (*dptr) {
389 talloc_free(*dptr);
390 *dptr = NULL;
391 }
392
393 return CMD_SUCCESS;
394}
395
Harald Welte28222962011-02-18 20:37:04 +0100396/* Support for configuration of log targets != the current vty */
397
398struct cmd_node cfg_log_node = {
399 CFG_LOG_NODE,
400 "%s(config-log)# ",
401 1
402};
403
Harald Welte28222962011-02-18 20:37:04 +0100404#ifdef HAVE_SYSLOG_H
405
406#include <syslog.h>
407
408static const int local_sysl_map[] = {
409 [0] = LOG_LOCAL0,
410 [1] = LOG_LOCAL1,
411 [2] = LOG_LOCAL2,
412 [3] = LOG_LOCAL3,
413 [4] = LOG_LOCAL4,
414 [5] = LOG_LOCAL5,
415 [6] = LOG_LOCAL6,
416 [7] = LOG_LOCAL7
417};
418
Harald Weltede79cee2011-02-24 23:47:57 +0100419/* From VTY core code */
420extern struct host host;
421
Harald Welte28222962011-02-18 20:37:04 +0100422static int _cfg_log_syslog(struct vty *vty, int facility)
423{
424 struct log_target *tgt;
425
426 /* First delete the old syslog target, if any */
427 tgt = log_target_find(LOG_TGT_TYPE_SYSLOG, NULL);
428 if (tgt)
429 log_target_destroy(tgt);
430
Harald Weltede79cee2011-02-24 23:47:57 +0100431 tgt = log_target_create_syslog(host.app_info->name, 0, facility);
Harald Welte28222962011-02-18 20:37:04 +0100432 if (!tgt) {
433 vty_out(vty, "%% Unable to open syslog%s", VTY_NEWLINE);
434 return CMD_WARNING;
435 }
436 log_add_target(tgt);
437
438 vty->index = tgt;
439 vty->node = CFG_LOG_NODE;
440
441 return CMD_SUCCESS;
442}
443
444DEFUN(cfg_log_syslog_local, cfg_log_syslog_local_cmd,
445 "log syslog local <0-7>",
446 LOG_STR "Logging via syslog\n" "Syslog LOCAL facility\n"
447 "Local facility number\n")
448{
449 int local = atoi(argv[0]);
450 int facility = local_sysl_map[local];
451
452 return _cfg_log_syslog(vty, facility);
453}
454
455static struct value_string sysl_level_names[] = {
456 { LOG_AUTHPRIV, "authpriv" },
457 { LOG_CRON, "cron" },
458 { LOG_DAEMON, "daemon" },
459 { LOG_FTP, "ftp" },
460 { LOG_LPR, "lpr" },
461 { LOG_MAIL, "mail" },
462 { LOG_NEWS, "news" },
463 { LOG_USER, "user" },
464 { LOG_UUCP, "uucp" },
465 /* only for value -> string conversion */
466 { LOG_LOCAL0, "local 0" },
467 { LOG_LOCAL1, "local 1" },
468 { LOG_LOCAL2, "local 2" },
469 { LOG_LOCAL3, "local 3" },
470 { LOG_LOCAL4, "local 4" },
471 { LOG_LOCAL5, "local 5" },
472 { LOG_LOCAL6, "local 6" },
473 { LOG_LOCAL7, "local 7" },
474 { 0, NULL }
475};
476
477DEFUN(cfg_log_syslog, cfg_log_syslog_cmd,
478 "log syslog (authpriv|cron|daemon|ftp|lpr|mail|news|user|uucp)",
Holger Hans Peter Freythera4463fd2011-10-03 23:17:36 +0200479 LOG_STR "Logging via syslog\n"
480 "Security/authorization messages facility\n"
481 "Clock daemon (cron/at) facility\n"
482 "General system daemon facility\n"
483 "Ftp daemon facility\n"
484 "Line printer facility\n"
485 "Mail facility\n"
486 "News facility\n"
487 "Generic facility\n"
488 "UUCP facility\n")
Harald Welte28222962011-02-18 20:37:04 +0100489{
490 int facility = get_string_value(sysl_level_names, argv[0]);
491
492 return _cfg_log_syslog(vty, facility);
493}
494
495DEFUN(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
Harald Welteaa00f992016-12-02 15:30:02 +0100514DEFUN(cfg_log_gsmtap, cfg_log_gsmtap_cmd,
515 "log gsmtap [HOSTNAME]",
Neels Hofmeyrfd9ec3b2016-12-11 01:48:26 +0100516 LOG_STR "Logging via GSMTAP\n"
517 "Host name to send the GSMTAP logging to (UDP port 4729)\n")
Harald Welteaa00f992016-12-02 15:30:02 +0100518{
519 const char *hostname = argv[0];
520 struct log_target *tgt;
521
522 tgt = log_target_find(LOG_TGT_TYPE_GSMTAP, hostname);
523 if (!tgt) {
524 tgt = log_target_create_gsmtap(hostname, GSMTAP_UDP_PORT,
525 host.app_info->name, false,
526 true);
527 if (!tgt) {
528 vty_out(vty, "%% Unable to create GSMTAP log%s",
529 VTY_NEWLINE);
530 return CMD_WARNING;
531 }
532 log_add_target(tgt);
533 }
534
535 vty->index = tgt;
536 vty->node = CFG_LOG_NODE;
537
538 return CMD_SUCCESS;
539}
540
Harald Welte28222962011-02-18 20:37:04 +0100541DEFUN(cfg_log_stderr, cfg_log_stderr_cmd,
542 "log stderr",
543 LOG_STR "Logging via STDERR of the process\n")
544{
545 struct log_target *tgt;
546
547 tgt = log_target_find(LOG_TGT_TYPE_STDERR, NULL);
548 if (!tgt) {
549 tgt = log_target_create_stderr();
550 if (!tgt) {
551 vty_out(vty, "%% Unable to create stderr log%s",
552 VTY_NEWLINE);
553 return CMD_WARNING;
554 }
555 log_add_target(tgt);
556 }
557
558 vty->index = tgt;
559 vty->node = CFG_LOG_NODE;
560
561 return CMD_SUCCESS;
562}
563
564DEFUN(cfg_no_log_stderr, cfg_no_log_stderr_cmd,
565 "no log stderr",
566 NO_STR LOG_STR "Logging via STDERR of the process\n")
567{
568 struct log_target *tgt;
569
570 tgt = log_target_find(LOG_TGT_TYPE_STDERR, NULL);
571 if (!tgt) {
572 vty_out(vty, "%% No stderr logging active%s", VTY_NEWLINE);
573 return CMD_WARNING;
574 }
575
576 log_target_destroy(tgt);
577
578 return CMD_SUCCESS;
579}
580
581DEFUN(cfg_log_file, cfg_log_file_cmd,
582 "log file .FILENAME",
583 LOG_STR "Logging to text file\n" "Filename\n")
584{
585 const char *fname = argv[0];
586 struct log_target *tgt;
587
588 tgt = log_target_find(LOG_TGT_TYPE_FILE, fname);
589 if (!tgt) {
590 tgt = log_target_create_file(fname);
591 if (!tgt) {
592 vty_out(vty, "%% Unable to create file `%s'%s",
593 fname, VTY_NEWLINE);
594 return CMD_WARNING;
595 }
596 log_add_target(tgt);
597 }
598
599 vty->index = tgt;
600 vty->node = CFG_LOG_NODE;
601
602 return CMD_SUCCESS;
603}
604
605
606DEFUN(cfg_no_log_file, cfg_no_log_file_cmd,
607 "no log file .FILENAME",
608 NO_STR LOG_STR "Logging to text file\n" "Filename\n")
609{
610 const char *fname = argv[0];
611 struct log_target *tgt;
612
613 tgt = log_target_find(LOG_TGT_TYPE_FILE, fname);
614 if (!tgt) {
615 vty_out(vty, "%% No such log file `%s'%s",
616 fname, VTY_NEWLINE);
617 return CMD_WARNING;
618 }
619
620 log_target_destroy(tgt);
621
622 return CMD_SUCCESS;
623}
624
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000625DEFUN(cfg_log_alarms, cfg_log_alarms_cmd,
626 "log alarms <2-32700>",
627 LOG_STR "Logging alarms to osmo_strrb\n"
628 "Maximum number of messages to log\n")
629{
630 struct log_target *tgt;
631 unsigned int rbsize = atoi(argv[0]);
632
633 tgt = log_target_find(LOG_TGT_TYPE_STRRB, NULL);
634 if (tgt)
635 log_target_destroy(tgt);
636
637 tgt = log_target_create_rb(rbsize);
638 if (!tgt) {
639 vty_out(vty, "%% Unable to create osmo_strrb (size %u)%s",
640 rbsize, VTY_NEWLINE);
641 return CMD_WARNING;
642 }
643 log_add_target(tgt);
644
645 vty->index = tgt;
646 vty->node = CFG_LOG_NODE;
647
648 return CMD_SUCCESS;
649}
650
651DEFUN(cfg_no_log_alarms, cfg_no_log_alarms_cmd,
652 "no log alarms",
653 NO_STR LOG_STR "Logging alarms to osmo_strrb\n")
654{
655 struct log_target *tgt;
656
657 tgt = log_target_find(LOG_TGT_TYPE_STRRB, NULL);
658 if (!tgt) {
659 vty_out(vty, "%% No osmo_strrb target found%s", VTY_NEWLINE);
660 return CMD_WARNING;
661 }
662
663 log_target_destroy(tgt);
664
665 return CMD_SUCCESS;
666}
667
Harald Welte28222962011-02-18 20:37:04 +0100668static int config_write_log_single(struct vty *vty, struct log_target *tgt)
669{
670 int i;
671 char level_lower[32];
672
673 switch (tgt->type) {
674 case LOG_TGT_TYPE_VTY:
675 return 1;
676 break;
677 case LOG_TGT_TYPE_STDERR:
678 vty_out(vty, "log stderr%s", VTY_NEWLINE);
679 break;
680 case LOG_TGT_TYPE_SYSLOG:
681#ifdef HAVE_SYSLOG_H
682 vty_out(vty, "log syslog %s%s",
683 get_value_string(sysl_level_names,
684 tgt->tgt_syslog.facility),
685 VTY_NEWLINE);
686#endif
687 break;
688 case LOG_TGT_TYPE_FILE:
689 vty_out(vty, "log file %s%s", tgt->tgt_file.fname, VTY_NEWLINE);
690 break;
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000691 case LOG_TGT_TYPE_STRRB:
692 vty_out(vty, "log alarms %zu%s",
693 log_target_rb_avail_size(tgt), VTY_NEWLINE);
694 break;
Harald Welteaa00f992016-12-02 15:30:02 +0100695 case LOG_TGT_TYPE_GSMTAP:
696 vty_out(vty, "log gsmtap %s%s",
697 tgt->tgt_gsmtap.hostname, VTY_NEWLINE);
698 break;
Harald Welte28222962011-02-18 20:37:04 +0100699 }
700
Harald Welte2da47f12012-10-22 19:31:54 +0200701 vty_out(vty, " logging filter all %u%s",
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100702 tgt->filter_map & (1 << LOG_FLT_ALL) ? 1 : 0, VTY_NEWLINE);
Harald Weltefb84f322013-06-06 07:33:54 +0200703 /* save filters outside of libosmocore, i.e. in app code */
704 if (osmo_log_info->save_fn)
705 osmo_log_info->save_fn(vty, osmo_log_info, tgt);
Harald Welte2da47f12012-10-22 19:31:54 +0200706
Harald Welte28222962011-02-18 20:37:04 +0100707 vty_out(vty, " logging color %u%s", tgt->use_color ? 1 : 0,
708 VTY_NEWLINE);
Holger Hans Peter Freyther879acef2015-01-27 11:06:51 +0100709 vty_out(vty, " logging print category %d%s",
Michael McTernan78933462015-03-20 15:29:25 +0100710 tgt->print_category ? 1 : 0, VTY_NEWLINE);
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100711 if (tgt->print_ext_timestamp)
712 vty_out(vty, " logging print extended-timestamp 1%s", VTY_NEWLINE);
713 else
714 vty_out(vty, " logging timestamp %u%s",
715 tgt->print_timestamp ? 1 : 0, VTY_NEWLINE);
Harald Welte28222962011-02-18 20:37:04 +0100716
717 /* stupid old osmo logging API uses uppercase strings... */
718 osmo_str2lower(level_lower, log_level_str(tgt->loglevel));
719 vty_out(vty, " logging level all %s%s", level_lower, VTY_NEWLINE);
720
721 for (i = 0; i < osmo_log_info->num_cat; i++) {
722 const struct log_category *cat = &tgt->categories[i];
723 char cat_lower[32];
724
Harald Welte1a02cfc2013-03-19 10:37:39 +0100725 /* skip empty entries in the array */
726 if (!osmo_log_info->cat[i].name)
727 continue;
728
Harald Welte28222962011-02-18 20:37:04 +0100729 /* stupid old osmo logging API uses uppercase strings... */
730 osmo_str2lower(cat_lower, osmo_log_info->cat[i].name+1);
731 osmo_str2lower(level_lower, log_level_str(cat->loglevel));
732
733 vty_out(vty, " logging level %s %s%s", cat_lower, level_lower,
734 VTY_NEWLINE);
735 }
736
737 /* FIXME: levels */
738
739 return 1;
740}
741
742static int config_write_log(struct vty *vty)
743{
744 struct log_target *dbg = vty->index;
745
746 llist_for_each_entry(dbg, &osmo_log_target_list, entry)
747 config_write_log_single(vty, dbg);
748
749 return 1;
750}
751
Maxc65c5b42017-03-15 13:20:23 +0100752void logging_vty_add_cmds()
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200753{
754 install_element_ve(&enable_logging_cmd);
755 install_element_ve(&disable_logging_cmd);
756 install_element_ve(&logging_fltr_all_cmd);
757 install_element_ve(&logging_use_clr_cmd);
758 install_element_ve(&logging_prnt_timestamp_cmd);
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100759 install_element_ve(&logging_prnt_ext_timestamp_cmd);
760 install_element_ve(&logging_prnt_cat_cmd);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200761 install_element_ve(&logging_set_category_mask_cmd);
Holger Hans Peter Freyther146d1d32011-10-03 23:15:41 +0200762 install_element_ve(&logging_set_category_mask_old_cmd);
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100763
764 /* Logging level strings are generated dynamically. */
Maxc65c5b42017-03-15 13:20:23 +0100765 logging_level_cmd.string = log_vty_command_string();
766 logging_level_cmd.doc = log_vty_command_description();
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200767 install_element_ve(&logging_level_cmd);
768 install_element_ve(&show_logging_vty_cmd);
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000769 install_element_ve(&show_alarms_cmd);
Harald Welte28222962011-02-18 20:37:04 +0100770
771 install_node(&cfg_log_node, config_write_log);
Jacob Erlbeck0c987bd2013-09-06 16:52:00 +0200772 vty_install_default(CFG_LOG_NODE);
Harald Weltea62648b2011-02-18 21:03:27 +0100773 install_element(CFG_LOG_NODE, &logging_fltr_all_cmd);
774 install_element(CFG_LOG_NODE, &logging_use_clr_cmd);
775 install_element(CFG_LOG_NODE, &logging_prnt_timestamp_cmd);
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100776 install_element(CFG_LOG_NODE, &logging_prnt_ext_timestamp_cmd);
777 install_element(CFG_LOG_NODE, &logging_prnt_cat_cmd);
Harald Weltea62648b2011-02-18 21:03:27 +0100778 install_element(CFG_LOG_NODE, &logging_level_cmd);
Harald Welte28222962011-02-18 20:37:04 +0100779
780 install_element(CONFIG_NODE, &cfg_log_stderr_cmd);
781 install_element(CONFIG_NODE, &cfg_no_log_stderr_cmd);
782 install_element(CONFIG_NODE, &cfg_log_file_cmd);
783 install_element(CONFIG_NODE, &cfg_no_log_file_cmd);
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000784 install_element(CONFIG_NODE, &cfg_log_alarms_cmd);
785 install_element(CONFIG_NODE, &cfg_no_log_alarms_cmd);
Harald Welte28222962011-02-18 20:37:04 +0100786#ifdef HAVE_SYSLOG_H
787 install_element(CONFIG_NODE, &cfg_log_syslog_cmd);
788 install_element(CONFIG_NODE, &cfg_log_syslog_local_cmd);
789 install_element(CONFIG_NODE, &cfg_no_log_syslog_cmd);
790#endif
Harald Welteaa00f992016-12-02 15:30:02 +0100791 install_element(CONFIG_NODE, &cfg_log_gsmtap_cmd);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200792}
Harald Welte96e2a002017-06-12 21:44:18 +0200793
794/* @} */