blob: 215394f0fa96822a57bb02e79e610298053cffa6 [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
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200206DEFUN(logging_level,
207 logging_level_cmd,
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100208 NULL, /* cmdstr is dynamically set in logging_vty_add_cmds(). */
209 NULL) /* same thing for helpstr. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200210{
Harald Weltea62648b2011-02-18 21:03:27 +0100211 int category = log_parse_category(argv[0]);
212 int level = log_parse_level(argv[1]);
213 struct log_target *tgt = osmo_log_vty2tgt(vty);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200214
Harald Weltea62648b2011-02-18 21:03:27 +0100215 if (!tgt)
216 return CMD_WARNING;
217
218 if (level < 0) {
219 vty_out(vty, "Invalid level `%s'%s", argv[1], VTY_NEWLINE);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200220 return CMD_WARNING;
221 }
222
Maxed0eda22017-07-06 17:09:01 +0200223 if (strcmp(argv[1], "everything") == 0) { /* FIXME: remove this check once 'everything' is phased out */
224 vty_out(vty, "%% Ignoring deprecated logging level %s%s", argv[1], VTY_NEWLINE);
225 return CMD_SUCCESS;
226 }
227
Harald Weltea62648b2011-02-18 21:03:27 +0100228 /* Check for special case where we want to set global log level */
229 if (!strcmp(argv[0], "all")) {
230 log_set_log_level(tgt, level);
231 return CMD_SUCCESS;
232 }
233
234 if (category < 0) {
235 vty_out(vty, "Invalid category `%s'%s", argv[0], VTY_NEWLINE);
236 return CMD_WARNING;
237 }
238
239 tgt->categories[category].enabled = 1;
240 tgt->categories[category].loglevel = level;
241
242 return CMD_SUCCESS;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200243}
244
245DEFUN(logging_set_category_mask,
246 logging_set_category_mask_cmd,
Holger Hans Peter Freyther146d1d32011-10-03 23:15:41 +0200247 "logging set-log-mask MASK",
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200248 LOGGING_STR
Holger Hans Peter Freyther146d1d32011-10-03 23:15:41 +0200249 "Set the logmask of this logging target\n"
250 "The logmask to use\n")
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200251{
Harald Weltea62648b2011-02-18 21:03:27 +0100252 struct log_target *tgt = osmo_log_vty2tgt(vty);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200253
Harald Weltea62648b2011-02-18 21:03:27 +0100254 if (!tgt)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200255 return CMD_WARNING;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200256
Harald Weltea62648b2011-02-18 21:03:27 +0100257 log_parse_category_mask(tgt, argv[0]);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200258 return CMD_SUCCESS;
259}
260
Holger Hans Peter Freyther146d1d32011-10-03 23:15:41 +0200261ALIAS_DEPRECATED(logging_set_category_mask,
262 logging_set_category_mask_old_cmd,
263 "logging set log mask MASK",
264 LOGGING_STR
265 "Decide which categories to output.\n"
266 "Log commands\n" "Mask commands\n" "The logmask to use\n");
267
268
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200269DEFUN(diable_logging,
270 disable_logging_cmd,
271 "logging disable",
272 LOGGING_STR
273 "Disables logging to this vty\n")
274{
Harald Weltea62648b2011-02-18 21:03:27 +0100275 struct log_target *tgt = osmo_log_vty2tgt(vty);
276 struct telnet_connection *conn = (struct telnet_connection *) vty->priv;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200277
Harald Weltea62648b2011-02-18 21:03:27 +0100278 if (!tgt)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200279 return CMD_WARNING;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200280
Harald Weltea62648b2011-02-18 21:03:27 +0100281 log_del_target(tgt);
282 talloc_free(tgt);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200283 conn->dbg = NULL;
Harald Weltea62648b2011-02-18 21:03:27 +0100284
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200285 return CMD_SUCCESS;
286}
287
288static void vty_print_logtarget(struct vty *vty, const struct log_info *info,
289 const struct log_target *tgt)
290{
291 unsigned int i;
292
293 vty_out(vty, " Global Loglevel: %s%s",
294 log_level_str(tgt->loglevel), VTY_NEWLINE);
295 vty_out(vty, " Use color: %s, Print Timestamp: %s%s",
296 tgt->use_color ? "On" : "Off",
297 tgt->print_timestamp ? "On" : "Off", VTY_NEWLINE);
298
299 vty_out(vty, " Log Level specific information:%s", VTY_NEWLINE);
300
301 for (i = 0; i < info->num_cat; i++) {
302 const struct log_category *cat = &tgt->categories[i];
Daniel Willmann55363a92016-11-15 10:05:51 +0100303 /* Skip categories that were not initialized */
304 if (!info->cat[i].name)
305 continue;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200306 vty_out(vty, " %-10s %-10s %-8s %s%s",
307 info->cat[i].name+1, log_level_str(cat->loglevel),
308 cat->enabled ? "Enabled" : "Disabled",
309 info->cat[i].description,
310 VTY_NEWLINE);
311 }
Harald Welte6d2d4d62013-03-10 09:53:24 +0000312
313 vty_out(vty, " Log Filter 'ALL': %s%s",
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100314 tgt->filter_map & (1 << LOG_FLT_ALL) ? "Enabled" : "Disabled",
Harald Welte6d2d4d62013-03-10 09:53:24 +0000315 VTY_NEWLINE);
316
Harald Weltefb84f322013-06-06 07:33:54 +0200317 /* print application specific filters */
318 if (info->print_fn)
319 info->print_fn(vty, info, tgt);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200320}
321
322#define SHOW_LOG_STR "Show current logging configuration\n"
323
324DEFUN(show_logging_vty,
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000325 show_logging_vty_cmd,
326 "show logging vty",
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200327 SHOW_STR SHOW_LOG_STR
328 "Show current logging configuration for this vty\n")
329{
Harald Weltea62648b2011-02-18 21:03:27 +0100330 struct log_target *tgt = osmo_log_vty2tgt(vty);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200331
Harald Weltea62648b2011-02-18 21:03:27 +0100332 if (!tgt)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200333 return CMD_WARNING;
Harald Weltea62648b2011-02-18 21:03:27 +0100334
335 vty_print_logtarget(vty, osmo_log_info, tgt);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200336
337 return CMD_SUCCESS;
338}
339
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000340DEFUN(show_alarms,
341 show_alarms_cmd,
342 "show alarms",
343 SHOW_STR SHOW_LOG_STR
344 "Show the contents of the logging ringbuffer\n")
345{
346 int i, num_alarms;
347 struct osmo_strrb *rb;
348 struct log_target *tgt = log_target_find(LOG_TGT_TYPE_STRRB, NULL);
349 if (!tgt) {
350 vty_out(vty, "%% No alarms, run 'log alarms <2-32700>'%s",
351 VTY_NEWLINE);
352 return CMD_WARNING;
353 }
354
355 rb = tgt->tgt_rb.rb;
356 num_alarms = osmo_strrb_elements(rb);
357
358 vty_out(vty, "%% Showing %i alarms%s", num_alarms, VTY_NEWLINE);
359
360 for (i = 0; i < num_alarms; i++)
361 vty_out(vty, "%% %s%s", osmo_strrb_get_nth(rb, i),
362 VTY_NEWLINE);
363
364 return CMD_SUCCESS;
365}
366
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200367gDEFUN(cfg_description, cfg_description_cmd,
368 "description .TEXT",
Thorsten Alteholza81055d2017-03-02 22:13:48 +0100369 "Save human-readable description of the object\n"
Holger Hans Peter Freytherc9b3e062012-07-25 13:02:49 +0200370 "Text until the end of the line\n")
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200371{
372 char **dptr = vty->index_sub;
373
374 if (!dptr) {
375 vty_out(vty, "vty->index_sub == NULL%s", VTY_NEWLINE);
376 return CMD_WARNING;
377 }
378
Holger Hans Peter Freytherff0670e2011-02-24 14:20:41 +0100379 if (*dptr)
380 talloc_free(*dptr);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200381 *dptr = argv_concat(argv, argc, 0);
Holger Hans Peter Freyther6a75d162013-07-14 09:07:11 +0200382 if (!*dptr)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200383 return CMD_WARNING;
384
385 return CMD_SUCCESS;
386}
387
388gDEFUN(cfg_no_description, cfg_no_description_cmd,
389 "no description",
390 NO_STR
391 "Remove description of the object\n")
392{
393 char **dptr = vty->index_sub;
394
395 if (!dptr) {
396 vty_out(vty, "vty->index_sub == NULL%s", VTY_NEWLINE);
397 return CMD_WARNING;
398 }
399
400 if (*dptr) {
401 talloc_free(*dptr);
402 *dptr = NULL;
403 }
404
405 return CMD_SUCCESS;
406}
407
Harald Welte28222962011-02-18 20:37:04 +0100408/* Support for configuration of log targets != the current vty */
409
410struct cmd_node cfg_log_node = {
411 CFG_LOG_NODE,
412 "%s(config-log)# ",
413 1
414};
415
Harald Welte28222962011-02-18 20:37:04 +0100416#ifdef HAVE_SYSLOG_H
417
418#include <syslog.h>
419
420static const int local_sysl_map[] = {
421 [0] = LOG_LOCAL0,
422 [1] = LOG_LOCAL1,
423 [2] = LOG_LOCAL2,
424 [3] = LOG_LOCAL3,
425 [4] = LOG_LOCAL4,
426 [5] = LOG_LOCAL5,
427 [6] = LOG_LOCAL6,
428 [7] = LOG_LOCAL7
429};
430
Harald Weltede79cee2011-02-24 23:47:57 +0100431/* From VTY core code */
432extern struct host host;
433
Harald Welte28222962011-02-18 20:37:04 +0100434static int _cfg_log_syslog(struct vty *vty, int facility)
435{
436 struct log_target *tgt;
437
438 /* First delete the old syslog target, if any */
439 tgt = log_target_find(LOG_TGT_TYPE_SYSLOG, NULL);
440 if (tgt)
441 log_target_destroy(tgt);
442
Harald Weltede79cee2011-02-24 23:47:57 +0100443 tgt = log_target_create_syslog(host.app_info->name, 0, facility);
Harald Welte28222962011-02-18 20:37:04 +0100444 if (!tgt) {
445 vty_out(vty, "%% Unable to open syslog%s", VTY_NEWLINE);
446 return CMD_WARNING;
447 }
448 log_add_target(tgt);
449
450 vty->index = tgt;
451 vty->node = CFG_LOG_NODE;
452
453 return CMD_SUCCESS;
454}
455
456DEFUN(cfg_log_syslog_local, cfg_log_syslog_local_cmd,
457 "log syslog local <0-7>",
458 LOG_STR "Logging via syslog\n" "Syslog LOCAL facility\n"
459 "Local facility number\n")
460{
461 int local = atoi(argv[0]);
462 int facility = local_sysl_map[local];
463
464 return _cfg_log_syslog(vty, facility);
465}
466
467static struct value_string sysl_level_names[] = {
468 { LOG_AUTHPRIV, "authpriv" },
469 { LOG_CRON, "cron" },
470 { LOG_DAEMON, "daemon" },
471 { LOG_FTP, "ftp" },
472 { LOG_LPR, "lpr" },
473 { LOG_MAIL, "mail" },
474 { LOG_NEWS, "news" },
475 { LOG_USER, "user" },
476 { LOG_UUCP, "uucp" },
477 /* only for value -> string conversion */
478 { LOG_LOCAL0, "local 0" },
479 { LOG_LOCAL1, "local 1" },
480 { LOG_LOCAL2, "local 2" },
481 { LOG_LOCAL3, "local 3" },
482 { LOG_LOCAL4, "local 4" },
483 { LOG_LOCAL5, "local 5" },
484 { LOG_LOCAL6, "local 6" },
485 { LOG_LOCAL7, "local 7" },
486 { 0, NULL }
487};
488
489DEFUN(cfg_log_syslog, cfg_log_syslog_cmd,
490 "log syslog (authpriv|cron|daemon|ftp|lpr|mail|news|user|uucp)",
Holger Hans Peter Freythera4463fd2011-10-03 23:17:36 +0200491 LOG_STR "Logging via syslog\n"
492 "Security/authorization messages facility\n"
493 "Clock daemon (cron/at) facility\n"
494 "General system daemon facility\n"
495 "Ftp daemon facility\n"
496 "Line printer facility\n"
497 "Mail facility\n"
498 "News facility\n"
499 "Generic facility\n"
500 "UUCP facility\n")
Harald Welte28222962011-02-18 20:37:04 +0100501{
502 int facility = get_string_value(sysl_level_names, argv[0]);
503
504 return _cfg_log_syslog(vty, facility);
505}
506
507DEFUN(cfg_no_log_syslog, cfg_no_log_syslog_cmd,
508 "no log syslog",
509 NO_STR LOG_STR "Logging via syslog\n")
510{
511 struct log_target *tgt;
512
513 tgt = log_target_find(LOG_TGT_TYPE_SYSLOG, NULL);
514 if (!tgt) {
515 vty_out(vty, "%% No syslog target found%s",
516 VTY_NEWLINE);
517 return CMD_WARNING;
518 }
519
520 log_target_destroy(tgt);
521
522 return CMD_SUCCESS;
523}
524#endif /* HAVE_SYSLOG_H */
525
Harald Welteaa00f992016-12-02 15:30:02 +0100526DEFUN(cfg_log_gsmtap, cfg_log_gsmtap_cmd,
527 "log gsmtap [HOSTNAME]",
Neels Hofmeyrfd9ec3b2016-12-11 01:48:26 +0100528 LOG_STR "Logging via GSMTAP\n"
529 "Host name to send the GSMTAP logging to (UDP port 4729)\n")
Harald Welteaa00f992016-12-02 15:30:02 +0100530{
531 const char *hostname = argv[0];
532 struct log_target *tgt;
533
534 tgt = log_target_find(LOG_TGT_TYPE_GSMTAP, hostname);
535 if (!tgt) {
536 tgt = log_target_create_gsmtap(hostname, GSMTAP_UDP_PORT,
537 host.app_info->name, false,
538 true);
539 if (!tgt) {
540 vty_out(vty, "%% Unable to create GSMTAP log%s",
541 VTY_NEWLINE);
542 return CMD_WARNING;
543 }
544 log_add_target(tgt);
545 }
546
547 vty->index = tgt;
548 vty->node = CFG_LOG_NODE;
549
550 return CMD_SUCCESS;
551}
552
Harald Welte28222962011-02-18 20:37:04 +0100553DEFUN(cfg_log_stderr, cfg_log_stderr_cmd,
554 "log stderr",
555 LOG_STR "Logging via STDERR of the process\n")
556{
557 struct log_target *tgt;
558
559 tgt = log_target_find(LOG_TGT_TYPE_STDERR, NULL);
560 if (!tgt) {
561 tgt = log_target_create_stderr();
562 if (!tgt) {
563 vty_out(vty, "%% Unable to create stderr log%s",
564 VTY_NEWLINE);
565 return CMD_WARNING;
566 }
567 log_add_target(tgt);
568 }
569
570 vty->index = tgt;
571 vty->node = CFG_LOG_NODE;
572
573 return CMD_SUCCESS;
574}
575
576DEFUN(cfg_no_log_stderr, cfg_no_log_stderr_cmd,
577 "no log stderr",
578 NO_STR LOG_STR "Logging via STDERR of the process\n")
579{
580 struct log_target *tgt;
581
582 tgt = log_target_find(LOG_TGT_TYPE_STDERR, NULL);
583 if (!tgt) {
584 vty_out(vty, "%% No stderr logging active%s", VTY_NEWLINE);
585 return CMD_WARNING;
586 }
587
588 log_target_destroy(tgt);
589
590 return CMD_SUCCESS;
591}
592
593DEFUN(cfg_log_file, cfg_log_file_cmd,
594 "log file .FILENAME",
595 LOG_STR "Logging to text file\n" "Filename\n")
596{
597 const char *fname = argv[0];
598 struct log_target *tgt;
599
600 tgt = log_target_find(LOG_TGT_TYPE_FILE, fname);
601 if (!tgt) {
602 tgt = log_target_create_file(fname);
603 if (!tgt) {
604 vty_out(vty, "%% Unable to create file `%s'%s",
605 fname, VTY_NEWLINE);
606 return CMD_WARNING;
607 }
608 log_add_target(tgt);
609 }
610
611 vty->index = tgt;
612 vty->node = CFG_LOG_NODE;
613
614 return CMD_SUCCESS;
615}
616
617
618DEFUN(cfg_no_log_file, cfg_no_log_file_cmd,
619 "no log file .FILENAME",
620 NO_STR LOG_STR "Logging to text file\n" "Filename\n")
621{
622 const char *fname = argv[0];
623 struct log_target *tgt;
624
625 tgt = log_target_find(LOG_TGT_TYPE_FILE, fname);
626 if (!tgt) {
627 vty_out(vty, "%% No such log file `%s'%s",
628 fname, VTY_NEWLINE);
629 return CMD_WARNING;
630 }
631
632 log_target_destroy(tgt);
633
634 return CMD_SUCCESS;
635}
636
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000637DEFUN(cfg_log_alarms, cfg_log_alarms_cmd,
638 "log alarms <2-32700>",
639 LOG_STR "Logging alarms to osmo_strrb\n"
640 "Maximum number of messages to log\n")
641{
642 struct log_target *tgt;
643 unsigned int rbsize = atoi(argv[0]);
644
645 tgt = log_target_find(LOG_TGT_TYPE_STRRB, NULL);
646 if (tgt)
647 log_target_destroy(tgt);
648
649 tgt = log_target_create_rb(rbsize);
650 if (!tgt) {
651 vty_out(vty, "%% Unable to create osmo_strrb (size %u)%s",
652 rbsize, VTY_NEWLINE);
653 return CMD_WARNING;
654 }
655 log_add_target(tgt);
656
657 vty->index = tgt;
658 vty->node = CFG_LOG_NODE;
659
660 return CMD_SUCCESS;
661}
662
663DEFUN(cfg_no_log_alarms, cfg_no_log_alarms_cmd,
664 "no log alarms",
665 NO_STR LOG_STR "Logging alarms to osmo_strrb\n")
666{
667 struct log_target *tgt;
668
669 tgt = log_target_find(LOG_TGT_TYPE_STRRB, NULL);
670 if (!tgt) {
671 vty_out(vty, "%% No osmo_strrb target found%s", VTY_NEWLINE);
672 return CMD_WARNING;
673 }
674
675 log_target_destroy(tgt);
676
677 return CMD_SUCCESS;
678}
679
Harald Welte28222962011-02-18 20:37:04 +0100680static int config_write_log_single(struct vty *vty, struct log_target *tgt)
681{
682 int i;
683 char level_lower[32];
684
685 switch (tgt->type) {
686 case LOG_TGT_TYPE_VTY:
687 return 1;
688 break;
689 case LOG_TGT_TYPE_STDERR:
690 vty_out(vty, "log stderr%s", VTY_NEWLINE);
691 break;
692 case LOG_TGT_TYPE_SYSLOG:
693#ifdef HAVE_SYSLOG_H
694 vty_out(vty, "log syslog %s%s",
695 get_value_string(sysl_level_names,
696 tgt->tgt_syslog.facility),
697 VTY_NEWLINE);
698#endif
699 break;
700 case LOG_TGT_TYPE_FILE:
701 vty_out(vty, "log file %s%s", tgt->tgt_file.fname, VTY_NEWLINE);
702 break;
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000703 case LOG_TGT_TYPE_STRRB:
704 vty_out(vty, "log alarms %zu%s",
705 log_target_rb_avail_size(tgt), VTY_NEWLINE);
706 break;
Harald Welteaa00f992016-12-02 15:30:02 +0100707 case LOG_TGT_TYPE_GSMTAP:
708 vty_out(vty, "log gsmtap %s%s",
709 tgt->tgt_gsmtap.hostname, VTY_NEWLINE);
710 break;
Harald Welte28222962011-02-18 20:37:04 +0100711 }
712
Harald Welte2da47f12012-10-22 19:31:54 +0200713 vty_out(vty, " logging filter all %u%s",
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100714 tgt->filter_map & (1 << LOG_FLT_ALL) ? 1 : 0, VTY_NEWLINE);
Harald Weltefb84f322013-06-06 07:33:54 +0200715 /* save filters outside of libosmocore, i.e. in app code */
716 if (osmo_log_info->save_fn)
717 osmo_log_info->save_fn(vty, osmo_log_info, tgt);
Harald Welte2da47f12012-10-22 19:31:54 +0200718
Harald Welte28222962011-02-18 20:37:04 +0100719 vty_out(vty, " logging color %u%s", tgt->use_color ? 1 : 0,
720 VTY_NEWLINE);
Holger Hans Peter Freyther879acef2015-01-27 11:06:51 +0100721 vty_out(vty, " logging print category %d%s",
Michael McTernan78933462015-03-20 15:29:25 +0100722 tgt->print_category ? 1 : 0, VTY_NEWLINE);
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100723 if (tgt->print_ext_timestamp)
724 vty_out(vty, " logging print extended-timestamp 1%s", VTY_NEWLINE);
725 else
726 vty_out(vty, " logging timestamp %u%s",
727 tgt->print_timestamp ? 1 : 0, VTY_NEWLINE);
Harald Welte28222962011-02-18 20:37:04 +0100728
729 /* stupid old osmo logging API uses uppercase strings... */
730 osmo_str2lower(level_lower, log_level_str(tgt->loglevel));
731 vty_out(vty, " logging level all %s%s", level_lower, VTY_NEWLINE);
732
733 for (i = 0; i < osmo_log_info->num_cat; i++) {
734 const struct log_category *cat = &tgt->categories[i];
735 char cat_lower[32];
736
Harald Welte1a02cfc2013-03-19 10:37:39 +0100737 /* skip empty entries in the array */
738 if (!osmo_log_info->cat[i].name)
739 continue;
740
Harald Welte28222962011-02-18 20:37:04 +0100741 /* stupid old osmo logging API uses uppercase strings... */
742 osmo_str2lower(cat_lower, osmo_log_info->cat[i].name+1);
743 osmo_str2lower(level_lower, log_level_str(cat->loglevel));
744
Maxed0eda22017-07-06 17:09:01 +0200745 if (strcmp(level_lower, "everything") != 0) /* FIXME: remove this check once 'everything' is phased out */
746 vty_out(vty, " logging level %s %s%s", cat_lower, level_lower, VTY_NEWLINE);
747 else
748 LOGP(DLSTATS, LOGL_ERROR, "logging level everything is deprecated and should not be used\n");
Harald Welte28222962011-02-18 20:37:04 +0100749 }
750
751 /* FIXME: levels */
752
753 return 1;
754}
755
756static int config_write_log(struct vty *vty)
757{
758 struct log_target *dbg = vty->index;
759
760 llist_for_each_entry(dbg, &osmo_log_target_list, entry)
761 config_write_log_single(vty, dbg);
762
763 return 1;
764}
765
Harald Welte8c648252017-10-16 15:17:03 +0200766/*! Register logging related commands to the VTY. Call this once from
767 * your application if you want to support those commands. */
Maxc65c5b42017-03-15 13:20:23 +0100768void logging_vty_add_cmds()
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200769{
770 install_element_ve(&enable_logging_cmd);
771 install_element_ve(&disable_logging_cmd);
772 install_element_ve(&logging_fltr_all_cmd);
773 install_element_ve(&logging_use_clr_cmd);
774 install_element_ve(&logging_prnt_timestamp_cmd);
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100775 install_element_ve(&logging_prnt_ext_timestamp_cmd);
776 install_element_ve(&logging_prnt_cat_cmd);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200777 install_element_ve(&logging_set_category_mask_cmd);
Holger Hans Peter Freyther146d1d32011-10-03 23:15:41 +0200778 install_element_ve(&logging_set_category_mask_old_cmd);
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100779
780 /* Logging level strings are generated dynamically. */
Maxc65c5b42017-03-15 13:20:23 +0100781 logging_level_cmd.string = log_vty_command_string();
782 logging_level_cmd.doc = log_vty_command_description();
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200783 install_element_ve(&logging_level_cmd);
784 install_element_ve(&show_logging_vty_cmd);
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000785 install_element_ve(&show_alarms_cmd);
Harald Welte28222962011-02-18 20:37:04 +0100786
787 install_node(&cfg_log_node, config_write_log);
Harald Weltea62648b2011-02-18 21:03:27 +0100788 install_element(CFG_LOG_NODE, &logging_fltr_all_cmd);
789 install_element(CFG_LOG_NODE, &logging_use_clr_cmd);
790 install_element(CFG_LOG_NODE, &logging_prnt_timestamp_cmd);
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100791 install_element(CFG_LOG_NODE, &logging_prnt_ext_timestamp_cmd);
792 install_element(CFG_LOG_NODE, &logging_prnt_cat_cmd);
Harald Weltea62648b2011-02-18 21:03:27 +0100793 install_element(CFG_LOG_NODE, &logging_level_cmd);
Harald Welte28222962011-02-18 20:37:04 +0100794
795 install_element(CONFIG_NODE, &cfg_log_stderr_cmd);
796 install_element(CONFIG_NODE, &cfg_no_log_stderr_cmd);
797 install_element(CONFIG_NODE, &cfg_log_file_cmd);
798 install_element(CONFIG_NODE, &cfg_no_log_file_cmd);
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000799 install_element(CONFIG_NODE, &cfg_log_alarms_cmd);
800 install_element(CONFIG_NODE, &cfg_no_log_alarms_cmd);
Harald Welte28222962011-02-18 20:37:04 +0100801#ifdef HAVE_SYSLOG_H
802 install_element(CONFIG_NODE, &cfg_log_syslog_cmd);
803 install_element(CONFIG_NODE, &cfg_log_syslog_local_cmd);
804 install_element(CONFIG_NODE, &cfg_no_log_syslog_cmd);
805#endif
Harald Welteaa00f992016-12-02 15:30:02 +0100806 install_element(CONFIG_NODE, &cfg_log_gsmtap_cmd);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200807}