blob: 01480b10b88d28050812ea838177f8ce5697b939 [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
Maxed0eda22017-07-06 17:09:01 +0200216 if (strcmp(argv[1], "everything") == 0) { /* FIXME: remove this check once 'everything' is phased out */
217 vty_out(vty, "%% Ignoring deprecated logging level %s%s", argv[1], VTY_NEWLINE);
218 return CMD_SUCCESS;
219 }
220
Harald Weltea62648b2011-02-18 21:03:27 +0100221 /* Check for special case where we want to set global log level */
222 if (!strcmp(argv[0], "all")) {
223 log_set_log_level(tgt, level);
224 return CMD_SUCCESS;
225 }
226
227 if (category < 0) {
228 vty_out(vty, "Invalid category `%s'%s", argv[0], VTY_NEWLINE);
229 return CMD_WARNING;
230 }
231
232 tgt->categories[category].enabled = 1;
233 tgt->categories[category].loglevel = level;
234
235 return CMD_SUCCESS;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200236}
237
238DEFUN(logging_set_category_mask,
239 logging_set_category_mask_cmd,
Holger Hans Peter Freyther146d1d32011-10-03 23:15:41 +0200240 "logging set-log-mask MASK",
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200241 LOGGING_STR
Holger Hans Peter Freyther146d1d32011-10-03 23:15:41 +0200242 "Set the logmask of this logging target\n"
243 "The logmask to use\n")
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200244{
Harald Weltea62648b2011-02-18 21:03:27 +0100245 struct log_target *tgt = osmo_log_vty2tgt(vty);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200246
Harald Weltea62648b2011-02-18 21:03:27 +0100247 if (!tgt)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200248 return CMD_WARNING;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200249
Harald Weltea62648b2011-02-18 21:03:27 +0100250 log_parse_category_mask(tgt, argv[0]);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200251 return CMD_SUCCESS;
252}
253
Holger Hans Peter Freyther146d1d32011-10-03 23:15:41 +0200254ALIAS_DEPRECATED(logging_set_category_mask,
255 logging_set_category_mask_old_cmd,
256 "logging set log mask MASK",
257 LOGGING_STR
258 "Decide which categories to output.\n"
259 "Log commands\n" "Mask commands\n" "The logmask to use\n");
260
261
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200262DEFUN(diable_logging,
263 disable_logging_cmd,
264 "logging disable",
265 LOGGING_STR
266 "Disables logging to this vty\n")
267{
Harald Weltea62648b2011-02-18 21:03:27 +0100268 struct log_target *tgt = osmo_log_vty2tgt(vty);
269 struct telnet_connection *conn = (struct telnet_connection *) vty->priv;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200270
Harald Weltea62648b2011-02-18 21:03:27 +0100271 if (!tgt)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200272 return CMD_WARNING;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200273
Harald Weltea62648b2011-02-18 21:03:27 +0100274 log_del_target(tgt);
275 talloc_free(tgt);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200276 conn->dbg = NULL;
Harald Weltea62648b2011-02-18 21:03:27 +0100277
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200278 return CMD_SUCCESS;
279}
280
281static void vty_print_logtarget(struct vty *vty, const struct log_info *info,
282 const struct log_target *tgt)
283{
284 unsigned int i;
285
286 vty_out(vty, " Global Loglevel: %s%s",
287 log_level_str(tgt->loglevel), VTY_NEWLINE);
288 vty_out(vty, " Use color: %s, Print Timestamp: %s%s",
289 tgt->use_color ? "On" : "Off",
290 tgt->print_timestamp ? "On" : "Off", VTY_NEWLINE);
291
292 vty_out(vty, " Log Level specific information:%s", VTY_NEWLINE);
293
294 for (i = 0; i < info->num_cat; i++) {
295 const struct log_category *cat = &tgt->categories[i];
Daniel Willmann55363a92016-11-15 10:05:51 +0100296 /* Skip categories that were not initialized */
297 if (!info->cat[i].name)
298 continue;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200299 vty_out(vty, " %-10s %-10s %-8s %s%s",
300 info->cat[i].name+1, log_level_str(cat->loglevel),
301 cat->enabled ? "Enabled" : "Disabled",
302 info->cat[i].description,
303 VTY_NEWLINE);
304 }
Harald Welte6d2d4d62013-03-10 09:53:24 +0000305
306 vty_out(vty, " Log Filter 'ALL': %s%s",
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100307 tgt->filter_map & (1 << LOG_FLT_ALL) ? "Enabled" : "Disabled",
Harald Welte6d2d4d62013-03-10 09:53:24 +0000308 VTY_NEWLINE);
309
Harald Weltefb84f322013-06-06 07:33:54 +0200310 /* print application specific filters */
311 if (info->print_fn)
312 info->print_fn(vty, info, tgt);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200313}
314
315#define SHOW_LOG_STR "Show current logging configuration\n"
316
317DEFUN(show_logging_vty,
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000318 show_logging_vty_cmd,
319 "show logging vty",
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200320 SHOW_STR SHOW_LOG_STR
321 "Show current logging configuration for this vty\n")
322{
Harald Weltea62648b2011-02-18 21:03:27 +0100323 struct log_target *tgt = osmo_log_vty2tgt(vty);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200324
Harald Weltea62648b2011-02-18 21:03:27 +0100325 if (!tgt)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200326 return CMD_WARNING;
Harald Weltea62648b2011-02-18 21:03:27 +0100327
328 vty_print_logtarget(vty, osmo_log_info, tgt);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200329
330 return CMD_SUCCESS;
331}
332
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000333DEFUN(show_alarms,
334 show_alarms_cmd,
335 "show alarms",
336 SHOW_STR SHOW_LOG_STR
337 "Show the contents of the logging ringbuffer\n")
338{
339 int i, num_alarms;
340 struct osmo_strrb *rb;
341 struct log_target *tgt = log_target_find(LOG_TGT_TYPE_STRRB, NULL);
342 if (!tgt) {
343 vty_out(vty, "%% No alarms, run 'log alarms <2-32700>'%s",
344 VTY_NEWLINE);
345 return CMD_WARNING;
346 }
347
348 rb = tgt->tgt_rb.rb;
349 num_alarms = osmo_strrb_elements(rb);
350
351 vty_out(vty, "%% Showing %i alarms%s", num_alarms, VTY_NEWLINE);
352
353 for (i = 0; i < num_alarms; i++)
354 vty_out(vty, "%% %s%s", osmo_strrb_get_nth(rb, i),
355 VTY_NEWLINE);
356
357 return CMD_SUCCESS;
358}
359
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200360gDEFUN(cfg_description, cfg_description_cmd,
361 "description .TEXT",
Thorsten Alteholza81055d2017-03-02 22:13:48 +0100362 "Save human-readable description of the object\n"
Holger Hans Peter Freytherc9b3e062012-07-25 13:02:49 +0200363 "Text until the end of the line\n")
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200364{
365 char **dptr = vty->index_sub;
366
367 if (!dptr) {
368 vty_out(vty, "vty->index_sub == NULL%s", VTY_NEWLINE);
369 return CMD_WARNING;
370 }
371
Holger Hans Peter Freytherff0670e2011-02-24 14:20:41 +0100372 if (*dptr)
373 talloc_free(*dptr);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200374 *dptr = argv_concat(argv, argc, 0);
Holger Hans Peter Freyther6a75d162013-07-14 09:07:11 +0200375 if (!*dptr)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200376 return CMD_WARNING;
377
378 return CMD_SUCCESS;
379}
380
381gDEFUN(cfg_no_description, cfg_no_description_cmd,
382 "no description",
383 NO_STR
384 "Remove description of the object\n")
385{
386 char **dptr = vty->index_sub;
387
388 if (!dptr) {
389 vty_out(vty, "vty->index_sub == NULL%s", VTY_NEWLINE);
390 return CMD_WARNING;
391 }
392
393 if (*dptr) {
394 talloc_free(*dptr);
395 *dptr = NULL;
396 }
397
398 return CMD_SUCCESS;
399}
400
Harald Welte28222962011-02-18 20:37:04 +0100401/* Support for configuration of log targets != the current vty */
402
403struct cmd_node cfg_log_node = {
404 CFG_LOG_NODE,
405 "%s(config-log)# ",
406 1
407};
408
Harald Welte28222962011-02-18 20:37:04 +0100409#ifdef HAVE_SYSLOG_H
410
411#include <syslog.h>
412
413static const int local_sysl_map[] = {
414 [0] = LOG_LOCAL0,
415 [1] = LOG_LOCAL1,
416 [2] = LOG_LOCAL2,
417 [3] = LOG_LOCAL3,
418 [4] = LOG_LOCAL4,
419 [5] = LOG_LOCAL5,
420 [6] = LOG_LOCAL6,
421 [7] = LOG_LOCAL7
422};
423
Harald Weltede79cee2011-02-24 23:47:57 +0100424/* From VTY core code */
425extern struct host host;
426
Harald Welte28222962011-02-18 20:37:04 +0100427static int _cfg_log_syslog(struct vty *vty, int facility)
428{
429 struct log_target *tgt;
430
431 /* First delete the old syslog target, if any */
432 tgt = log_target_find(LOG_TGT_TYPE_SYSLOG, NULL);
433 if (tgt)
434 log_target_destroy(tgt);
435
Harald Weltede79cee2011-02-24 23:47:57 +0100436 tgt = log_target_create_syslog(host.app_info->name, 0, facility);
Harald Welte28222962011-02-18 20:37:04 +0100437 if (!tgt) {
438 vty_out(vty, "%% Unable to open syslog%s", VTY_NEWLINE);
439 return CMD_WARNING;
440 }
441 log_add_target(tgt);
442
443 vty->index = tgt;
444 vty->node = CFG_LOG_NODE;
445
446 return CMD_SUCCESS;
447}
448
449DEFUN(cfg_log_syslog_local, cfg_log_syslog_local_cmd,
450 "log syslog local <0-7>",
451 LOG_STR "Logging via syslog\n" "Syslog LOCAL facility\n"
452 "Local facility number\n")
453{
454 int local = atoi(argv[0]);
455 int facility = local_sysl_map[local];
456
457 return _cfg_log_syslog(vty, facility);
458}
459
460static struct value_string sysl_level_names[] = {
461 { LOG_AUTHPRIV, "authpriv" },
462 { LOG_CRON, "cron" },
463 { LOG_DAEMON, "daemon" },
464 { LOG_FTP, "ftp" },
465 { LOG_LPR, "lpr" },
466 { LOG_MAIL, "mail" },
467 { LOG_NEWS, "news" },
468 { LOG_USER, "user" },
469 { LOG_UUCP, "uucp" },
470 /* only for value -> string conversion */
471 { LOG_LOCAL0, "local 0" },
472 { LOG_LOCAL1, "local 1" },
473 { LOG_LOCAL2, "local 2" },
474 { LOG_LOCAL3, "local 3" },
475 { LOG_LOCAL4, "local 4" },
476 { LOG_LOCAL5, "local 5" },
477 { LOG_LOCAL6, "local 6" },
478 { LOG_LOCAL7, "local 7" },
479 { 0, NULL }
480};
481
482DEFUN(cfg_log_syslog, cfg_log_syslog_cmd,
483 "log syslog (authpriv|cron|daemon|ftp|lpr|mail|news|user|uucp)",
Holger Hans Peter Freythera4463fd2011-10-03 23:17:36 +0200484 LOG_STR "Logging via syslog\n"
485 "Security/authorization messages facility\n"
486 "Clock daemon (cron/at) facility\n"
487 "General system daemon facility\n"
488 "Ftp daemon facility\n"
489 "Line printer facility\n"
490 "Mail facility\n"
491 "News facility\n"
492 "Generic facility\n"
493 "UUCP facility\n")
Harald Welte28222962011-02-18 20:37:04 +0100494{
495 int facility = get_string_value(sysl_level_names, argv[0]);
496
497 return _cfg_log_syslog(vty, facility);
498}
499
500DEFUN(cfg_no_log_syslog, cfg_no_log_syslog_cmd,
501 "no log syslog",
502 NO_STR LOG_STR "Logging via syslog\n")
503{
504 struct log_target *tgt;
505
506 tgt = log_target_find(LOG_TGT_TYPE_SYSLOG, NULL);
507 if (!tgt) {
508 vty_out(vty, "%% No syslog target found%s",
509 VTY_NEWLINE);
510 return CMD_WARNING;
511 }
512
513 log_target_destroy(tgt);
514
515 return CMD_SUCCESS;
516}
517#endif /* HAVE_SYSLOG_H */
518
Harald Welteaa00f992016-12-02 15:30:02 +0100519DEFUN(cfg_log_gsmtap, cfg_log_gsmtap_cmd,
520 "log gsmtap [HOSTNAME]",
Neels Hofmeyrfd9ec3b2016-12-11 01:48:26 +0100521 LOG_STR "Logging via GSMTAP\n"
522 "Host name to send the GSMTAP logging to (UDP port 4729)\n")
Harald Welteaa00f992016-12-02 15:30:02 +0100523{
524 const char *hostname = argv[0];
525 struct log_target *tgt;
526
527 tgt = log_target_find(LOG_TGT_TYPE_GSMTAP, hostname);
528 if (!tgt) {
529 tgt = log_target_create_gsmtap(hostname, GSMTAP_UDP_PORT,
530 host.app_info->name, false,
531 true);
532 if (!tgt) {
533 vty_out(vty, "%% Unable to create GSMTAP log%s",
534 VTY_NEWLINE);
535 return CMD_WARNING;
536 }
537 log_add_target(tgt);
538 }
539
540 vty->index = tgt;
541 vty->node = CFG_LOG_NODE;
542
543 return CMD_SUCCESS;
544}
545
Harald Welte28222962011-02-18 20:37:04 +0100546DEFUN(cfg_log_stderr, cfg_log_stderr_cmd,
547 "log stderr",
548 LOG_STR "Logging via STDERR of the process\n")
549{
550 struct log_target *tgt;
551
552 tgt = log_target_find(LOG_TGT_TYPE_STDERR, NULL);
553 if (!tgt) {
554 tgt = log_target_create_stderr();
555 if (!tgt) {
556 vty_out(vty, "%% Unable to create stderr log%s",
557 VTY_NEWLINE);
558 return CMD_WARNING;
559 }
560 log_add_target(tgt);
561 }
562
563 vty->index = tgt;
564 vty->node = CFG_LOG_NODE;
565
566 return CMD_SUCCESS;
567}
568
569DEFUN(cfg_no_log_stderr, cfg_no_log_stderr_cmd,
570 "no log stderr",
571 NO_STR LOG_STR "Logging via STDERR of the process\n")
572{
573 struct log_target *tgt;
574
575 tgt = log_target_find(LOG_TGT_TYPE_STDERR, NULL);
576 if (!tgt) {
577 vty_out(vty, "%% No stderr logging active%s", VTY_NEWLINE);
578 return CMD_WARNING;
579 }
580
581 log_target_destroy(tgt);
582
583 return CMD_SUCCESS;
584}
585
586DEFUN(cfg_log_file, cfg_log_file_cmd,
587 "log file .FILENAME",
588 LOG_STR "Logging to text file\n" "Filename\n")
589{
590 const char *fname = argv[0];
591 struct log_target *tgt;
592
593 tgt = log_target_find(LOG_TGT_TYPE_FILE, fname);
594 if (!tgt) {
595 tgt = log_target_create_file(fname);
596 if (!tgt) {
597 vty_out(vty, "%% Unable to create file `%s'%s",
598 fname, VTY_NEWLINE);
599 return CMD_WARNING;
600 }
601 log_add_target(tgt);
602 }
603
604 vty->index = tgt;
605 vty->node = CFG_LOG_NODE;
606
607 return CMD_SUCCESS;
608}
609
610
611DEFUN(cfg_no_log_file, cfg_no_log_file_cmd,
612 "no log file .FILENAME",
613 NO_STR LOG_STR "Logging to text file\n" "Filename\n")
614{
615 const char *fname = argv[0];
616 struct log_target *tgt;
617
618 tgt = log_target_find(LOG_TGT_TYPE_FILE, fname);
619 if (!tgt) {
620 vty_out(vty, "%% No such log file `%s'%s",
621 fname, VTY_NEWLINE);
622 return CMD_WARNING;
623 }
624
625 log_target_destroy(tgt);
626
627 return CMD_SUCCESS;
628}
629
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000630DEFUN(cfg_log_alarms, cfg_log_alarms_cmd,
631 "log alarms <2-32700>",
632 LOG_STR "Logging alarms to osmo_strrb\n"
633 "Maximum number of messages to log\n")
634{
635 struct log_target *tgt;
636 unsigned int rbsize = atoi(argv[0]);
637
638 tgt = log_target_find(LOG_TGT_TYPE_STRRB, NULL);
639 if (tgt)
640 log_target_destroy(tgt);
641
642 tgt = log_target_create_rb(rbsize);
643 if (!tgt) {
644 vty_out(vty, "%% Unable to create osmo_strrb (size %u)%s",
645 rbsize, VTY_NEWLINE);
646 return CMD_WARNING;
647 }
648 log_add_target(tgt);
649
650 vty->index = tgt;
651 vty->node = CFG_LOG_NODE;
652
653 return CMD_SUCCESS;
654}
655
656DEFUN(cfg_no_log_alarms, cfg_no_log_alarms_cmd,
657 "no log alarms",
658 NO_STR LOG_STR "Logging alarms to osmo_strrb\n")
659{
660 struct log_target *tgt;
661
662 tgt = log_target_find(LOG_TGT_TYPE_STRRB, NULL);
663 if (!tgt) {
664 vty_out(vty, "%% No osmo_strrb target found%s", VTY_NEWLINE);
665 return CMD_WARNING;
666 }
667
668 log_target_destroy(tgt);
669
670 return CMD_SUCCESS;
671}
672
Harald Welte28222962011-02-18 20:37:04 +0100673static int config_write_log_single(struct vty *vty, struct log_target *tgt)
674{
675 int i;
676 char level_lower[32];
677
678 switch (tgt->type) {
679 case LOG_TGT_TYPE_VTY:
680 return 1;
681 break;
682 case LOG_TGT_TYPE_STDERR:
683 vty_out(vty, "log stderr%s", VTY_NEWLINE);
684 break;
685 case LOG_TGT_TYPE_SYSLOG:
686#ifdef HAVE_SYSLOG_H
687 vty_out(vty, "log syslog %s%s",
688 get_value_string(sysl_level_names,
689 tgt->tgt_syslog.facility),
690 VTY_NEWLINE);
691#endif
692 break;
693 case LOG_TGT_TYPE_FILE:
694 vty_out(vty, "log file %s%s", tgt->tgt_file.fname, VTY_NEWLINE);
695 break;
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000696 case LOG_TGT_TYPE_STRRB:
697 vty_out(vty, "log alarms %zu%s",
698 log_target_rb_avail_size(tgt), VTY_NEWLINE);
699 break;
Harald Welteaa00f992016-12-02 15:30:02 +0100700 case LOG_TGT_TYPE_GSMTAP:
701 vty_out(vty, "log gsmtap %s%s",
702 tgt->tgt_gsmtap.hostname, VTY_NEWLINE);
703 break;
Harald Welte28222962011-02-18 20:37:04 +0100704 }
705
Harald Welte2da47f12012-10-22 19:31:54 +0200706 vty_out(vty, " logging filter all %u%s",
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100707 tgt->filter_map & (1 << LOG_FLT_ALL) ? 1 : 0, VTY_NEWLINE);
Harald Weltefb84f322013-06-06 07:33:54 +0200708 /* save filters outside of libosmocore, i.e. in app code */
709 if (osmo_log_info->save_fn)
710 osmo_log_info->save_fn(vty, osmo_log_info, tgt);
Harald Welte2da47f12012-10-22 19:31:54 +0200711
Harald Welte28222962011-02-18 20:37:04 +0100712 vty_out(vty, " logging color %u%s", tgt->use_color ? 1 : 0,
713 VTY_NEWLINE);
Holger Hans Peter Freyther879acef2015-01-27 11:06:51 +0100714 vty_out(vty, " logging print category %d%s",
Michael McTernan78933462015-03-20 15:29:25 +0100715 tgt->print_category ? 1 : 0, VTY_NEWLINE);
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100716 if (tgt->print_ext_timestamp)
717 vty_out(vty, " logging print extended-timestamp 1%s", VTY_NEWLINE);
718 else
719 vty_out(vty, " logging timestamp %u%s",
720 tgt->print_timestamp ? 1 : 0, VTY_NEWLINE);
Harald Welte28222962011-02-18 20:37:04 +0100721
722 /* stupid old osmo logging API uses uppercase strings... */
723 osmo_str2lower(level_lower, log_level_str(tgt->loglevel));
724 vty_out(vty, " logging level all %s%s", level_lower, VTY_NEWLINE);
725
726 for (i = 0; i < osmo_log_info->num_cat; i++) {
727 const struct log_category *cat = &tgt->categories[i];
728 char cat_lower[32];
729
Harald Welte1a02cfc2013-03-19 10:37:39 +0100730 /* skip empty entries in the array */
731 if (!osmo_log_info->cat[i].name)
732 continue;
733
Harald Welte28222962011-02-18 20:37:04 +0100734 /* stupid old osmo logging API uses uppercase strings... */
735 osmo_str2lower(cat_lower, osmo_log_info->cat[i].name+1);
736 osmo_str2lower(level_lower, log_level_str(cat->loglevel));
737
Maxed0eda22017-07-06 17:09:01 +0200738 if (strcmp(level_lower, "everything") != 0) /* FIXME: remove this check once 'everything' is phased out */
739 vty_out(vty, " logging level %s %s%s", cat_lower, level_lower, VTY_NEWLINE);
740 else
741 LOGP(DLSTATS, LOGL_ERROR, "logging level everything is deprecated and should not be used\n");
Harald Welte28222962011-02-18 20:37:04 +0100742 }
743
744 /* FIXME: levels */
745
746 return 1;
747}
748
749static int config_write_log(struct vty *vty)
750{
751 struct log_target *dbg = vty->index;
752
753 llist_for_each_entry(dbg, &osmo_log_target_list, entry)
754 config_write_log_single(vty, dbg);
755
756 return 1;
757}
758
Maxc65c5b42017-03-15 13:20:23 +0100759void logging_vty_add_cmds()
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200760{
761 install_element_ve(&enable_logging_cmd);
762 install_element_ve(&disable_logging_cmd);
763 install_element_ve(&logging_fltr_all_cmd);
764 install_element_ve(&logging_use_clr_cmd);
765 install_element_ve(&logging_prnt_timestamp_cmd);
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100766 install_element_ve(&logging_prnt_ext_timestamp_cmd);
767 install_element_ve(&logging_prnt_cat_cmd);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200768 install_element_ve(&logging_set_category_mask_cmd);
Holger Hans Peter Freyther146d1d32011-10-03 23:15:41 +0200769 install_element_ve(&logging_set_category_mask_old_cmd);
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100770
771 /* Logging level strings are generated dynamically. */
Maxc65c5b42017-03-15 13:20:23 +0100772 logging_level_cmd.string = log_vty_command_string();
773 logging_level_cmd.doc = log_vty_command_description();
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200774 install_element_ve(&logging_level_cmd);
775 install_element_ve(&show_logging_vty_cmd);
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000776 install_element_ve(&show_alarms_cmd);
Harald Welte28222962011-02-18 20:37:04 +0100777
778 install_node(&cfg_log_node, config_write_log);
Jacob Erlbeck0c987bd2013-09-06 16:52:00 +0200779 vty_install_default(CFG_LOG_NODE);
Harald Weltea62648b2011-02-18 21:03:27 +0100780 install_element(CFG_LOG_NODE, &logging_fltr_all_cmd);
781 install_element(CFG_LOG_NODE, &logging_use_clr_cmd);
782 install_element(CFG_LOG_NODE, &logging_prnt_timestamp_cmd);
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100783 install_element(CFG_LOG_NODE, &logging_prnt_ext_timestamp_cmd);
784 install_element(CFG_LOG_NODE, &logging_prnt_cat_cmd);
Harald Weltea62648b2011-02-18 21:03:27 +0100785 install_element(CFG_LOG_NODE, &logging_level_cmd);
Harald Welte28222962011-02-18 20:37:04 +0100786
787 install_element(CONFIG_NODE, &cfg_log_stderr_cmd);
788 install_element(CONFIG_NODE, &cfg_no_log_stderr_cmd);
789 install_element(CONFIG_NODE, &cfg_log_file_cmd);
790 install_element(CONFIG_NODE, &cfg_no_log_file_cmd);
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000791 install_element(CONFIG_NODE, &cfg_log_alarms_cmd);
792 install_element(CONFIG_NODE, &cfg_no_log_alarms_cmd);
Harald Welte28222962011-02-18 20:37:04 +0100793#ifdef HAVE_SYSLOG_H
794 install_element(CONFIG_NODE, &cfg_log_syslog_cmd);
795 install_element(CONFIG_NODE, &cfg_log_syslog_local_cmd);
796 install_element(CONFIG_NODE, &cfg_no_log_syslog_cmd);
797#endif
Harald Welteaa00f992016-12-02 15:30:02 +0100798 install_element(CONFIG_NODE, &cfg_log_gsmtap_cmd);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200799}
Harald Welte96e2a002017-06-12 21:44:18 +0200800
801/* @} */