blob: dc457ffc51b5f4ad329ddc68ee77b2c45fdb6a2c [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"
Neels Hofmeyr84ea2e02017-12-09 05:53:18 +0100250 "List of logging categories to log, e.g. 'abc:mno:xyz'. Available log categories depend on the specific"
251 " application, refer to the 'logging level' command. Optionally add individual log levels like"
252 " 'abc,1:mno,3:xyz,5', where the level numbers are"
253 " " OSMO_STRINGIFY(LOGL_DEBUG) "=" OSMO_STRINGIFY_VAL(LOGL_DEBUG)
254 " " OSMO_STRINGIFY(LOGL_INFO) "=" OSMO_STRINGIFY_VAL(LOGL_INFO)
255 " " OSMO_STRINGIFY(LOGL_NOTICE) "=" OSMO_STRINGIFY_VAL(LOGL_NOTICE)
256 " " OSMO_STRINGIFY(LOGL_ERROR) "=" OSMO_STRINGIFY_VAL(LOGL_ERROR)
257 " " OSMO_STRINGIFY(LOGL_FATAL) "=" OSMO_STRINGIFY_VAL(LOGL_FATAL)
258 "\n")
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200259{
Harald Weltea62648b2011-02-18 21:03:27 +0100260 struct log_target *tgt = osmo_log_vty2tgt(vty);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200261
Harald Weltea62648b2011-02-18 21:03:27 +0100262 if (!tgt)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200263 return CMD_WARNING;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200264
Harald Weltea62648b2011-02-18 21:03:27 +0100265 log_parse_category_mask(tgt, argv[0]);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200266 return CMD_SUCCESS;
267}
268
Holger Hans Peter Freyther146d1d32011-10-03 23:15:41 +0200269ALIAS_DEPRECATED(logging_set_category_mask,
270 logging_set_category_mask_old_cmd,
271 "logging set log mask MASK",
272 LOGGING_STR
273 "Decide which categories to output.\n"
Neels Hofmeyr84ea2e02017-12-09 05:53:18 +0100274 "Log commands\n" "Mask commands\n"
275 "'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 +0200276
277
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200278DEFUN(diable_logging,
279 disable_logging_cmd,
280 "logging disable",
281 LOGGING_STR
282 "Disables logging to this vty\n")
283{
Harald Weltea62648b2011-02-18 21:03:27 +0100284 struct log_target *tgt = osmo_log_vty2tgt(vty);
285 struct telnet_connection *conn = (struct telnet_connection *) vty->priv;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200286
Harald Weltea62648b2011-02-18 21:03:27 +0100287 if (!tgt)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200288 return CMD_WARNING;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200289
Harald Weltea62648b2011-02-18 21:03:27 +0100290 log_del_target(tgt);
291 talloc_free(tgt);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200292 conn->dbg = NULL;
Harald Weltea62648b2011-02-18 21:03:27 +0100293
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200294 return CMD_SUCCESS;
295}
296
297static void vty_print_logtarget(struct vty *vty, const struct log_info *info,
298 const struct log_target *tgt)
299{
300 unsigned int i;
301
302 vty_out(vty, " Global Loglevel: %s%s",
303 log_level_str(tgt->loglevel), VTY_NEWLINE);
304 vty_out(vty, " Use color: %s, Print Timestamp: %s%s",
305 tgt->use_color ? "On" : "Off",
306 tgt->print_timestamp ? "On" : "Off", VTY_NEWLINE);
307
308 vty_out(vty, " Log Level specific information:%s", VTY_NEWLINE);
309
310 for (i = 0; i < info->num_cat; i++) {
311 const struct log_category *cat = &tgt->categories[i];
Daniel Willmann55363a92016-11-15 10:05:51 +0100312 /* Skip categories that were not initialized */
313 if (!info->cat[i].name)
314 continue;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200315 vty_out(vty, " %-10s %-10s %-8s %s%s",
316 info->cat[i].name+1, log_level_str(cat->loglevel),
317 cat->enabled ? "Enabled" : "Disabled",
318 info->cat[i].description,
319 VTY_NEWLINE);
320 }
Harald Welte6d2d4d62013-03-10 09:53:24 +0000321
322 vty_out(vty, " Log Filter 'ALL': %s%s",
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100323 tgt->filter_map & (1 << LOG_FLT_ALL) ? "Enabled" : "Disabled",
Harald Welte6d2d4d62013-03-10 09:53:24 +0000324 VTY_NEWLINE);
325
Harald Weltefb84f322013-06-06 07:33:54 +0200326 /* print application specific filters */
327 if (info->print_fn)
328 info->print_fn(vty, info, tgt);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200329}
330
331#define SHOW_LOG_STR "Show current logging configuration\n"
332
333DEFUN(show_logging_vty,
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000334 show_logging_vty_cmd,
335 "show logging vty",
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200336 SHOW_STR SHOW_LOG_STR
337 "Show current logging configuration for this vty\n")
338{
Harald Weltea62648b2011-02-18 21:03:27 +0100339 struct log_target *tgt = osmo_log_vty2tgt(vty);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200340
Harald Weltea62648b2011-02-18 21:03:27 +0100341 if (!tgt)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200342 return CMD_WARNING;
Harald Weltea62648b2011-02-18 21:03:27 +0100343
344 vty_print_logtarget(vty, osmo_log_info, tgt);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200345
346 return CMD_SUCCESS;
347}
348
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000349DEFUN(show_alarms,
350 show_alarms_cmd,
351 "show alarms",
352 SHOW_STR SHOW_LOG_STR
353 "Show the contents of the logging ringbuffer\n")
354{
355 int i, num_alarms;
356 struct osmo_strrb *rb;
357 struct log_target *tgt = log_target_find(LOG_TGT_TYPE_STRRB, NULL);
358 if (!tgt) {
359 vty_out(vty, "%% No alarms, run 'log alarms <2-32700>'%s",
360 VTY_NEWLINE);
361 return CMD_WARNING;
362 }
363
364 rb = tgt->tgt_rb.rb;
365 num_alarms = osmo_strrb_elements(rb);
366
367 vty_out(vty, "%% Showing %i alarms%s", num_alarms, VTY_NEWLINE);
368
369 for (i = 0; i < num_alarms; i++)
370 vty_out(vty, "%% %s%s", osmo_strrb_get_nth(rb, i),
371 VTY_NEWLINE);
372
373 return CMD_SUCCESS;
374}
375
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200376gDEFUN(cfg_description, cfg_description_cmd,
377 "description .TEXT",
Thorsten Alteholza81055d2017-03-02 22:13:48 +0100378 "Save human-readable description of the object\n"
Holger Hans Peter Freytherc9b3e062012-07-25 13:02:49 +0200379 "Text until the end of the line\n")
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200380{
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
Holger Hans Peter Freytherff0670e2011-02-24 14:20:41 +0100388 if (*dptr)
389 talloc_free(*dptr);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200390 *dptr = argv_concat(argv, argc, 0);
Holger Hans Peter Freyther6a75d162013-07-14 09:07:11 +0200391 if (!*dptr)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200392 return CMD_WARNING;
393
394 return CMD_SUCCESS;
395}
396
397gDEFUN(cfg_no_description, cfg_no_description_cmd,
398 "no description",
399 NO_STR
400 "Remove description of the object\n")
401{
402 char **dptr = vty->index_sub;
403
404 if (!dptr) {
405 vty_out(vty, "vty->index_sub == NULL%s", VTY_NEWLINE);
406 return CMD_WARNING;
407 }
408
409 if (*dptr) {
410 talloc_free(*dptr);
411 *dptr = NULL;
412 }
413
414 return CMD_SUCCESS;
415}
416
Harald Welte28222962011-02-18 20:37:04 +0100417/* Support for configuration of log targets != the current vty */
418
419struct cmd_node cfg_log_node = {
420 CFG_LOG_NODE,
421 "%s(config-log)# ",
422 1
423};
424
Harald Welte28222962011-02-18 20:37:04 +0100425#ifdef HAVE_SYSLOG_H
426
427#include <syslog.h>
428
429static const int local_sysl_map[] = {
430 [0] = LOG_LOCAL0,
431 [1] = LOG_LOCAL1,
432 [2] = LOG_LOCAL2,
433 [3] = LOG_LOCAL3,
434 [4] = LOG_LOCAL4,
435 [5] = LOG_LOCAL5,
436 [6] = LOG_LOCAL6,
437 [7] = LOG_LOCAL7
438};
439
Harald Weltede79cee2011-02-24 23:47:57 +0100440/* From VTY core code */
441extern struct host host;
442
Harald Welte28222962011-02-18 20:37:04 +0100443static int _cfg_log_syslog(struct vty *vty, int facility)
444{
445 struct log_target *tgt;
446
447 /* First delete the old syslog target, if any */
448 tgt = log_target_find(LOG_TGT_TYPE_SYSLOG, NULL);
449 if (tgt)
450 log_target_destroy(tgt);
451
Harald Weltede79cee2011-02-24 23:47:57 +0100452 tgt = log_target_create_syslog(host.app_info->name, 0, facility);
Harald Welte28222962011-02-18 20:37:04 +0100453 if (!tgt) {
454 vty_out(vty, "%% Unable to open syslog%s", VTY_NEWLINE);
455 return CMD_WARNING;
456 }
457 log_add_target(tgt);
458
459 vty->index = tgt;
460 vty->node = CFG_LOG_NODE;
461
462 return CMD_SUCCESS;
463}
464
465DEFUN(cfg_log_syslog_local, cfg_log_syslog_local_cmd,
466 "log syslog local <0-7>",
467 LOG_STR "Logging via syslog\n" "Syslog LOCAL facility\n"
468 "Local facility number\n")
469{
470 int local = atoi(argv[0]);
471 int facility = local_sysl_map[local];
472
473 return _cfg_log_syslog(vty, facility);
474}
475
476static struct value_string sysl_level_names[] = {
477 { LOG_AUTHPRIV, "authpriv" },
478 { LOG_CRON, "cron" },
479 { LOG_DAEMON, "daemon" },
480 { LOG_FTP, "ftp" },
481 { LOG_LPR, "lpr" },
482 { LOG_MAIL, "mail" },
483 { LOG_NEWS, "news" },
484 { LOG_USER, "user" },
485 { LOG_UUCP, "uucp" },
486 /* only for value -> string conversion */
487 { LOG_LOCAL0, "local 0" },
488 { LOG_LOCAL1, "local 1" },
489 { LOG_LOCAL2, "local 2" },
490 { LOG_LOCAL3, "local 3" },
491 { LOG_LOCAL4, "local 4" },
492 { LOG_LOCAL5, "local 5" },
493 { LOG_LOCAL6, "local 6" },
494 { LOG_LOCAL7, "local 7" },
495 { 0, NULL }
496};
497
498DEFUN(cfg_log_syslog, cfg_log_syslog_cmd,
499 "log syslog (authpriv|cron|daemon|ftp|lpr|mail|news|user|uucp)",
Holger Hans Peter Freythera4463fd2011-10-03 23:17:36 +0200500 LOG_STR "Logging via syslog\n"
501 "Security/authorization messages facility\n"
502 "Clock daemon (cron/at) facility\n"
503 "General system daemon facility\n"
504 "Ftp daemon facility\n"
505 "Line printer facility\n"
506 "Mail facility\n"
507 "News facility\n"
508 "Generic facility\n"
509 "UUCP facility\n")
Harald Welte28222962011-02-18 20:37:04 +0100510{
511 int facility = get_string_value(sysl_level_names, argv[0]);
512
513 return _cfg_log_syslog(vty, facility);
514}
515
516DEFUN(cfg_no_log_syslog, cfg_no_log_syslog_cmd,
517 "no log syslog",
518 NO_STR LOG_STR "Logging via syslog\n")
519{
520 struct log_target *tgt;
521
522 tgt = log_target_find(LOG_TGT_TYPE_SYSLOG, NULL);
523 if (!tgt) {
524 vty_out(vty, "%% No syslog target found%s",
525 VTY_NEWLINE);
526 return CMD_WARNING;
527 }
528
529 log_target_destroy(tgt);
530
531 return CMD_SUCCESS;
532}
533#endif /* HAVE_SYSLOG_H */
534
Harald Welteaa00f992016-12-02 15:30:02 +0100535DEFUN(cfg_log_gsmtap, cfg_log_gsmtap_cmd,
536 "log gsmtap [HOSTNAME]",
Neels Hofmeyrfd9ec3b2016-12-11 01:48:26 +0100537 LOG_STR "Logging via GSMTAP\n"
538 "Host name to send the GSMTAP logging to (UDP port 4729)\n")
Harald Welteaa00f992016-12-02 15:30:02 +0100539{
Max2f153b52018-01-04 12:25:57 +0100540 const char *hostname = argc ? argv[0] : "127.0.0.1";
Harald Welteaa00f992016-12-02 15:30:02 +0100541 struct log_target *tgt;
542
543 tgt = log_target_find(LOG_TGT_TYPE_GSMTAP, hostname);
544 if (!tgt) {
545 tgt = log_target_create_gsmtap(hostname, GSMTAP_UDP_PORT,
546 host.app_info->name, false,
547 true);
548 if (!tgt) {
Max2f153b52018-01-04 12:25:57 +0100549 vty_out(vty, "%% Unable to create GSMTAP log for %s%s",
550 hostname, VTY_NEWLINE);
Harald Welteaa00f992016-12-02 15:30:02 +0100551 return CMD_WARNING;
552 }
553 log_add_target(tgt);
554 }
555
556 vty->index = tgt;
557 vty->node = CFG_LOG_NODE;
558
559 return CMD_SUCCESS;
560}
561
Harald Welte28222962011-02-18 20:37:04 +0100562DEFUN(cfg_log_stderr, cfg_log_stderr_cmd,
563 "log stderr",
564 LOG_STR "Logging via STDERR of the process\n")
565{
566 struct log_target *tgt;
567
568 tgt = log_target_find(LOG_TGT_TYPE_STDERR, NULL);
569 if (!tgt) {
570 tgt = log_target_create_stderr();
571 if (!tgt) {
572 vty_out(vty, "%% Unable to create stderr log%s",
573 VTY_NEWLINE);
574 return CMD_WARNING;
575 }
576 log_add_target(tgt);
577 }
578
579 vty->index = tgt;
580 vty->node = CFG_LOG_NODE;
581
582 return CMD_SUCCESS;
583}
584
585DEFUN(cfg_no_log_stderr, cfg_no_log_stderr_cmd,
586 "no log stderr",
587 NO_STR LOG_STR "Logging via STDERR of the process\n")
588{
589 struct log_target *tgt;
590
591 tgt = log_target_find(LOG_TGT_TYPE_STDERR, NULL);
592 if (!tgt) {
593 vty_out(vty, "%% No stderr logging active%s", VTY_NEWLINE);
594 return CMD_WARNING;
595 }
596
597 log_target_destroy(tgt);
598
599 return CMD_SUCCESS;
600}
601
602DEFUN(cfg_log_file, cfg_log_file_cmd,
603 "log file .FILENAME",
604 LOG_STR "Logging to text file\n" "Filename\n")
605{
606 const char *fname = argv[0];
607 struct log_target *tgt;
608
609 tgt = log_target_find(LOG_TGT_TYPE_FILE, fname);
610 if (!tgt) {
611 tgt = log_target_create_file(fname);
612 if (!tgt) {
613 vty_out(vty, "%% Unable to create file `%s'%s",
614 fname, VTY_NEWLINE);
615 return CMD_WARNING;
616 }
617 log_add_target(tgt);
618 }
619
620 vty->index = tgt;
621 vty->node = CFG_LOG_NODE;
622
623 return CMD_SUCCESS;
624}
625
626
627DEFUN(cfg_no_log_file, cfg_no_log_file_cmd,
628 "no log file .FILENAME",
629 NO_STR LOG_STR "Logging to text file\n" "Filename\n")
630{
631 const char *fname = argv[0];
632 struct log_target *tgt;
633
634 tgt = log_target_find(LOG_TGT_TYPE_FILE, fname);
635 if (!tgt) {
636 vty_out(vty, "%% No such log file `%s'%s",
637 fname, VTY_NEWLINE);
638 return CMD_WARNING;
639 }
640
641 log_target_destroy(tgt);
642
643 return CMD_SUCCESS;
644}
645
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000646DEFUN(cfg_log_alarms, cfg_log_alarms_cmd,
647 "log alarms <2-32700>",
648 LOG_STR "Logging alarms to osmo_strrb\n"
649 "Maximum number of messages to log\n")
650{
651 struct log_target *tgt;
652 unsigned int rbsize = atoi(argv[0]);
653
654 tgt = log_target_find(LOG_TGT_TYPE_STRRB, NULL);
655 if (tgt)
656 log_target_destroy(tgt);
657
658 tgt = log_target_create_rb(rbsize);
659 if (!tgt) {
660 vty_out(vty, "%% Unable to create osmo_strrb (size %u)%s",
661 rbsize, VTY_NEWLINE);
662 return CMD_WARNING;
663 }
664 log_add_target(tgt);
665
666 vty->index = tgt;
667 vty->node = CFG_LOG_NODE;
668
669 return CMD_SUCCESS;
670}
671
672DEFUN(cfg_no_log_alarms, cfg_no_log_alarms_cmd,
673 "no log alarms",
674 NO_STR LOG_STR "Logging alarms to osmo_strrb\n")
675{
676 struct log_target *tgt;
677
678 tgt = log_target_find(LOG_TGT_TYPE_STRRB, NULL);
679 if (!tgt) {
680 vty_out(vty, "%% No osmo_strrb target found%s", VTY_NEWLINE);
681 return CMD_WARNING;
682 }
683
684 log_target_destroy(tgt);
685
686 return CMD_SUCCESS;
687}
688
Harald Welte28222962011-02-18 20:37:04 +0100689static int config_write_log_single(struct vty *vty, struct log_target *tgt)
690{
691 int i;
692 char level_lower[32];
693
694 switch (tgt->type) {
695 case LOG_TGT_TYPE_VTY:
696 return 1;
697 break;
698 case LOG_TGT_TYPE_STDERR:
699 vty_out(vty, "log stderr%s", VTY_NEWLINE);
700 break;
701 case LOG_TGT_TYPE_SYSLOG:
702#ifdef HAVE_SYSLOG_H
703 vty_out(vty, "log syslog %s%s",
704 get_value_string(sysl_level_names,
705 tgt->tgt_syslog.facility),
706 VTY_NEWLINE);
707#endif
708 break;
709 case LOG_TGT_TYPE_FILE:
710 vty_out(vty, "log file %s%s", tgt->tgt_file.fname, VTY_NEWLINE);
711 break;
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000712 case LOG_TGT_TYPE_STRRB:
713 vty_out(vty, "log alarms %zu%s",
714 log_target_rb_avail_size(tgt), VTY_NEWLINE);
715 break;
Harald Welteaa00f992016-12-02 15:30:02 +0100716 case LOG_TGT_TYPE_GSMTAP:
717 vty_out(vty, "log gsmtap %s%s",
718 tgt->tgt_gsmtap.hostname, VTY_NEWLINE);
719 break;
Harald Welte28222962011-02-18 20:37:04 +0100720 }
721
Harald Welte2da47f12012-10-22 19:31:54 +0200722 vty_out(vty, " logging filter all %u%s",
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100723 tgt->filter_map & (1 << LOG_FLT_ALL) ? 1 : 0, VTY_NEWLINE);
Harald Weltefb84f322013-06-06 07:33:54 +0200724 /* save filters outside of libosmocore, i.e. in app code */
725 if (osmo_log_info->save_fn)
726 osmo_log_info->save_fn(vty, osmo_log_info, tgt);
Harald Welte2da47f12012-10-22 19:31:54 +0200727
Harald Welte28222962011-02-18 20:37:04 +0100728 vty_out(vty, " logging color %u%s", tgt->use_color ? 1 : 0,
729 VTY_NEWLINE);
Holger Hans Peter Freyther879acef2015-01-27 11:06:51 +0100730 vty_out(vty, " logging print category %d%s",
Michael McTernan78933462015-03-20 15:29:25 +0100731 tgt->print_category ? 1 : 0, VTY_NEWLINE);
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100732 if (tgt->print_ext_timestamp)
733 vty_out(vty, " logging print extended-timestamp 1%s", VTY_NEWLINE);
734 else
735 vty_out(vty, " logging timestamp %u%s",
736 tgt->print_timestamp ? 1 : 0, VTY_NEWLINE);
Harald Welte28222962011-02-18 20:37:04 +0100737
738 /* stupid old osmo logging API uses uppercase strings... */
739 osmo_str2lower(level_lower, log_level_str(tgt->loglevel));
740 vty_out(vty, " logging level all %s%s", level_lower, VTY_NEWLINE);
741
742 for (i = 0; i < osmo_log_info->num_cat; i++) {
743 const struct log_category *cat = &tgt->categories[i];
744 char cat_lower[32];
745
Harald Welte1a02cfc2013-03-19 10:37:39 +0100746 /* skip empty entries in the array */
747 if (!osmo_log_info->cat[i].name)
748 continue;
749
Harald Welte28222962011-02-18 20:37:04 +0100750 /* stupid old osmo logging API uses uppercase strings... */
751 osmo_str2lower(cat_lower, osmo_log_info->cat[i].name+1);
752 osmo_str2lower(level_lower, log_level_str(cat->loglevel));
753
Maxed0eda22017-07-06 17:09:01 +0200754 if (strcmp(level_lower, "everything") != 0) /* FIXME: remove this check once 'everything' is phased out */
755 vty_out(vty, " logging level %s %s%s", cat_lower, level_lower, VTY_NEWLINE);
756 else
757 LOGP(DLSTATS, LOGL_ERROR, "logging level everything is deprecated and should not be used\n");
Harald Welte28222962011-02-18 20:37:04 +0100758 }
759
760 /* FIXME: levels */
761
762 return 1;
763}
764
765static int config_write_log(struct vty *vty)
766{
767 struct log_target *dbg = vty->index;
768
769 llist_for_each_entry(dbg, &osmo_log_target_list, entry)
770 config_write_log_single(vty, dbg);
771
772 return 1;
773}
774
Harald Welte8c648252017-10-16 15:17:03 +0200775/*! Register logging related commands to the VTY. Call this once from
776 * your application if you want to support those commands. */
Maxc65c5b42017-03-15 13:20:23 +0100777void logging_vty_add_cmds()
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200778{
779 install_element_ve(&enable_logging_cmd);
780 install_element_ve(&disable_logging_cmd);
781 install_element_ve(&logging_fltr_all_cmd);
782 install_element_ve(&logging_use_clr_cmd);
783 install_element_ve(&logging_prnt_timestamp_cmd);
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100784 install_element_ve(&logging_prnt_ext_timestamp_cmd);
785 install_element_ve(&logging_prnt_cat_cmd);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200786 install_element_ve(&logging_set_category_mask_cmd);
Holger Hans Peter Freyther146d1d32011-10-03 23:15:41 +0200787 install_element_ve(&logging_set_category_mask_old_cmd);
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100788
789 /* Logging level strings are generated dynamically. */
Maxc65c5b42017-03-15 13:20:23 +0100790 logging_level_cmd.string = log_vty_command_string();
791 logging_level_cmd.doc = log_vty_command_description();
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200792 install_element_ve(&logging_level_cmd);
793 install_element_ve(&show_logging_vty_cmd);
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000794 install_element_ve(&show_alarms_cmd);
Harald Welte28222962011-02-18 20:37:04 +0100795
796 install_node(&cfg_log_node, config_write_log);
Harald Weltea62648b2011-02-18 21:03:27 +0100797 install_element(CFG_LOG_NODE, &logging_fltr_all_cmd);
798 install_element(CFG_LOG_NODE, &logging_use_clr_cmd);
799 install_element(CFG_LOG_NODE, &logging_prnt_timestamp_cmd);
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100800 install_element(CFG_LOG_NODE, &logging_prnt_ext_timestamp_cmd);
801 install_element(CFG_LOG_NODE, &logging_prnt_cat_cmd);
Harald Weltea62648b2011-02-18 21:03:27 +0100802 install_element(CFG_LOG_NODE, &logging_level_cmd);
Harald Welte28222962011-02-18 20:37:04 +0100803
804 install_element(CONFIG_NODE, &cfg_log_stderr_cmd);
805 install_element(CONFIG_NODE, &cfg_no_log_stderr_cmd);
806 install_element(CONFIG_NODE, &cfg_log_file_cmd);
807 install_element(CONFIG_NODE, &cfg_no_log_file_cmd);
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000808 install_element(CONFIG_NODE, &cfg_log_alarms_cmd);
809 install_element(CONFIG_NODE, &cfg_no_log_alarms_cmd);
Harald Welte28222962011-02-18 20:37:04 +0100810#ifdef HAVE_SYSLOG_H
811 install_element(CONFIG_NODE, &cfg_log_syslog_cmd);
812 install_element(CONFIG_NODE, &cfg_log_syslog_local_cmd);
813 install_element(CONFIG_NODE, &cfg_no_log_syslog_cmd);
814#endif
Harald Welteaa00f992016-12-02 15:30:02 +0100815 install_element(CONFIG_NODE, &cfg_log_gsmtap_cmd);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200816}