blob: 5914822571759590d37c8b6816c451ded3b1f6ff [file] [log] [blame]
Neels Hofmeyr17518fe2017-06-20 04:35:06 +02001/*
2 * (C) 2009-2010 by Harald Welte <laforge@gnumonks.org>
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +01003 * (C) 2009-2014 by Holger Hans Peter Freyther
Harald Welte3fb0b6f2010-05-19 19:02:52 +02004 * All Rights Reserved
5 *
Harald Weltee08da972017-11-13 01:00:26 +09006 * SPDX-License-Identifier: GPL-2.0+
7 *
Harald Welte3fb0b6f2010-05-19 19:02:52 +02008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 *
22 */
23
24#include <stdlib.h>
25#include <string.h>
26
Harald Welte28222962011-02-18 20:37:04 +010027#include "../../config.h"
28
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010029#include <osmocom/core/talloc.h>
30#include <osmocom/core/logging.h>
31#include <osmocom/core/utils.h>
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +000032#include <osmocom/core/strrb.h>
33#include <osmocom/core/loggingrb.h>
Harald Welteaa00f992016-12-02 15:30:02 +010034#include <osmocom/core/gsmtap.h>
Harald Welte3fb0b6f2010-05-19 19:02:52 +020035
36#include <osmocom/vty/command.h>
37#include <osmocom/vty/buffer.h>
38#include <osmocom/vty/vty.h>
39#include <osmocom/vty/telnet_interface.h>
40#include <osmocom/vty/logging.h>
41
Harald Welte28222962011-02-18 20:37:04 +010042#define LOG_STR "Configure logging sub-system\n"
43
Harald Welte8c648252017-10-16 15:17:03 +020044/*! \file logging_vty.c
Neels Hofmeyr87e45502017-06-20 00:17:59 +020045 * Configuration of logging from VTY
Harald Welte96e2a002017-06-12 21:44:18 +020046 *
Harald Welte8c648252017-10-16 15:17:03 +020047 * This module implements
48 * - functions that permit configuration of the libosmocore logging
49 * framework from VTY commands in the configure -> logging node.
50 *
51 * - functions that permit logging *to* a VTY session. Basically each
52 * VTY session gets its own log target, with configurable
53 * per-subsystem log levels. This is performed internally via the
54 * \ref log_target_create_vty function.
55 *
56 * You have to call \ref logging_vty_add_cmds from your application
57 * once to enable both of the above.
58 *
Harald Welte96e2a002017-06-12 21:44:18 +020059 */
60
Harald Welte4ebdf742010-05-19 19:54:00 +020061extern const struct log_info *osmo_log_info;
Harald Welte3fb0b6f2010-05-19 19:02:52 +020062
Harald Welte76e72ab2011-02-17 15:52:39 +010063static void _vty_output(struct log_target *tgt,
64 unsigned int level, const char *line)
Harald Welte3fb0b6f2010-05-19 19:02:52 +020065{
66 struct vty *vty = tgt->tgt_vty.vty;
67 vty_out(vty, "%s", line);
68 /* This is an ugly hack, but there is no easy way... */
69 if (strchr(line, '\n'))
70 vty_out(vty, "\r");
71}
72
73struct log_target *log_target_create_vty(struct vty *vty)
74{
75 struct log_target *target;
76
77 target = log_target_create();
78 if (!target)
79 return NULL;
80
81 target->tgt_vty.vty = vty;
82 target->output = _vty_output;
83 return target;
84}
85
86DEFUN(enable_logging,
87 enable_logging_cmd,
88 "logging enable",
89 LOGGING_STR
90 "Enables logging to this vty\n")
91{
92 struct telnet_connection *conn;
93
94 conn = (struct telnet_connection *) vty->priv;
95 if (conn->dbg) {
96 vty_out(vty, "Logging already enabled.%s", VTY_NEWLINE);
97 return CMD_WARNING;
98 }
99
100 conn->dbg = log_target_create_vty(vty);
101 if (!conn->dbg)
102 return CMD_WARNING;
103
104 log_add_target(conn->dbg);
105 return CMD_SUCCESS;
106}
107
Harald Weltea62648b2011-02-18 21:03:27 +0100108struct log_target *osmo_log_vty2tgt(struct vty *vty)
109{
110 struct telnet_connection *conn;
111
112 if (vty->node == CFG_LOG_NODE)
113 return vty->index;
114
115
116 conn = (struct telnet_connection *) vty->priv;
117 if (!conn->dbg)
118 vty_out(vty, "Logging was not enabled.%s", VTY_NEWLINE);
119
120 return conn->dbg;
121}
122
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200123DEFUN(logging_fltr_all,
124 logging_fltr_all_cmd,
125 "logging filter all (0|1)",
126 LOGGING_STR FILTER_STR
127 "Do you want to log all messages?\n"
128 "Only print messages matched by other filters\n"
129 "Bypass filter and print all messages\n")
130{
Harald Weltea62648b2011-02-18 21:03:27 +0100131 struct log_target *tgt = osmo_log_vty2tgt(vty);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200132
Harald Weltea62648b2011-02-18 21:03:27 +0100133 if (!tgt)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200134 return CMD_WARNING;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200135
Harald Weltea62648b2011-02-18 21:03:27 +0100136 log_set_all_filter(tgt, atoi(argv[0]));
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200137 return CMD_SUCCESS;
138}
139
140DEFUN(logging_use_clr,
141 logging_use_clr_cmd,
142 "logging color (0|1)",
143 LOGGING_STR "Configure color-printing for log messages\n"
144 "Don't use color for printing messages\n"
145 "Use color for printing messages\n")
146{
Harald Weltea62648b2011-02-18 21:03:27 +0100147 struct log_target *tgt = osmo_log_vty2tgt(vty);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200148
Harald Weltea62648b2011-02-18 21:03:27 +0100149 if (!tgt)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200150 return CMD_WARNING;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200151
Harald Weltea62648b2011-02-18 21:03:27 +0100152 log_set_use_color(tgt, atoi(argv[0]));
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200153 return CMD_SUCCESS;
154}
155
156DEFUN(logging_prnt_timestamp,
157 logging_prnt_timestamp_cmd,
158 "logging timestamp (0|1)",
159 LOGGING_STR "Configure log message timestamping\n"
160 "Don't prefix each log message\n"
161 "Prefix each log message with current timestamp\n")
162{
Harald Weltea62648b2011-02-18 21:03:27 +0100163 struct log_target *tgt = osmo_log_vty2tgt(vty);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200164
Harald Weltea62648b2011-02-18 21:03:27 +0100165 if (!tgt)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200166 return CMD_WARNING;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200167
Harald Weltea62648b2011-02-18 21:03:27 +0100168 log_set_print_timestamp(tgt, atoi(argv[0]));
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200169 return CMD_SUCCESS;
170}
171
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100172DEFUN(logging_prnt_ext_timestamp,
173 logging_prnt_ext_timestamp_cmd,
174 "logging print extended-timestamp (0|1)",
175 LOGGING_STR "Log output settings\n"
176 "Configure log message timestamping\n"
177 "Don't prefix each log message\n"
178 "Prefix each log message with current timestamp with YYYYMMDDhhmmssnnn\n")
179{
180 struct log_target *tgt = osmo_log_vty2tgt(vty);
181
182 if (!tgt)
183 return CMD_WARNING;
184
185 log_set_print_extended_timestamp(tgt, atoi(argv[0]));
186 return CMD_SUCCESS;
187}
188
189DEFUN(logging_prnt_cat,
190 logging_prnt_cat_cmd,
191 "logging print category (0|1)",
192 LOGGING_STR "Log output settings\n"
193 "Configure log message\n"
194 "Don't prefix each log message\n"
195 "Prefix each log message with category/subsystem name\n")
196{
197 struct log_target *tgt = osmo_log_vty2tgt(vty);
198
199 if (!tgt)
200 return CMD_WARNING;
201
202 log_set_print_category(tgt, atoi(argv[0]));
203 return CMD_SUCCESS;
204}
205
Neels Hofmeyr886e5482018-01-16 01:49:37 +0100206DEFUN(logging_prnt_level,
207 logging_prnt_level_cmd,
208 "logging print level (0|1)",
209 LOGGING_STR "Log output settings\n"
210 "Configure log message\n"
211 "Don't prefix each log message\n"
212 "Prefix each log message with the log level name\n")
213{
214 struct log_target *tgt = osmo_log_vty2tgt(vty);
215
216 if (!tgt)
217 return CMD_WARNING;
218
219 log_set_print_level(tgt, atoi(argv[0]));
220 return CMD_SUCCESS;
221}
222
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200223DEFUN(logging_level,
224 logging_level_cmd,
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100225 NULL, /* cmdstr is dynamically set in logging_vty_add_cmds(). */
226 NULL) /* same thing for helpstr. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200227{
Harald Weltea62648b2011-02-18 21:03:27 +0100228 int category = log_parse_category(argv[0]);
229 int level = log_parse_level(argv[1]);
230 struct log_target *tgt = osmo_log_vty2tgt(vty);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200231
Harald Weltea62648b2011-02-18 21:03:27 +0100232 if (!tgt)
233 return CMD_WARNING;
234
235 if (level < 0) {
236 vty_out(vty, "Invalid level `%s'%s", argv[1], VTY_NEWLINE);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200237 return CMD_WARNING;
238 }
239
Maxed0eda22017-07-06 17:09:01 +0200240 if (strcmp(argv[1], "everything") == 0) { /* FIXME: remove this check once 'everything' is phased out */
241 vty_out(vty, "%% Ignoring deprecated logging level %s%s", argv[1], VTY_NEWLINE);
242 return CMD_SUCCESS;
243 }
244
Harald Weltea62648b2011-02-18 21:03:27 +0100245 /* Check for special case where we want to set global log level */
246 if (!strcmp(argv[0], "all")) {
247 log_set_log_level(tgt, level);
248 return CMD_SUCCESS;
249 }
250
251 if (category < 0) {
252 vty_out(vty, "Invalid category `%s'%s", argv[0], VTY_NEWLINE);
253 return CMD_WARNING;
254 }
255
256 tgt->categories[category].enabled = 1;
257 tgt->categories[category].loglevel = level;
258
259 return CMD_SUCCESS;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200260}
261
262DEFUN(logging_set_category_mask,
263 logging_set_category_mask_cmd,
Holger Hans Peter Freyther146d1d32011-10-03 23:15:41 +0200264 "logging set-log-mask MASK",
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200265 LOGGING_STR
Holger Hans Peter Freyther146d1d32011-10-03 23:15:41 +0200266 "Set the logmask of this logging target\n"
Neels Hofmeyr84ea2e02017-12-09 05:53:18 +0100267 "List of logging categories to log, e.g. 'abc:mno:xyz'. Available log categories depend on the specific"
268 " application, refer to the 'logging level' command. Optionally add individual log levels like"
269 " 'abc,1:mno,3:xyz,5', where the level numbers are"
270 " " OSMO_STRINGIFY(LOGL_DEBUG) "=" OSMO_STRINGIFY_VAL(LOGL_DEBUG)
271 " " OSMO_STRINGIFY(LOGL_INFO) "=" OSMO_STRINGIFY_VAL(LOGL_INFO)
272 " " OSMO_STRINGIFY(LOGL_NOTICE) "=" OSMO_STRINGIFY_VAL(LOGL_NOTICE)
273 " " OSMO_STRINGIFY(LOGL_ERROR) "=" OSMO_STRINGIFY_VAL(LOGL_ERROR)
274 " " OSMO_STRINGIFY(LOGL_FATAL) "=" OSMO_STRINGIFY_VAL(LOGL_FATAL)
275 "\n")
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200276{
Harald Weltea62648b2011-02-18 21:03:27 +0100277 struct log_target *tgt = osmo_log_vty2tgt(vty);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200278
Harald Weltea62648b2011-02-18 21:03:27 +0100279 if (!tgt)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200280 return CMD_WARNING;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200281
Harald Weltea62648b2011-02-18 21:03:27 +0100282 log_parse_category_mask(tgt, argv[0]);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200283 return CMD_SUCCESS;
284}
285
Holger Hans Peter Freyther146d1d32011-10-03 23:15:41 +0200286ALIAS_DEPRECATED(logging_set_category_mask,
287 logging_set_category_mask_old_cmd,
288 "logging set log mask MASK",
289 LOGGING_STR
290 "Decide which categories to output.\n"
Neels Hofmeyr84ea2e02017-12-09 05:53:18 +0100291 "Log commands\n" "Mask commands\n"
292 "'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 +0200293
294
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200295DEFUN(diable_logging,
296 disable_logging_cmd,
297 "logging disable",
298 LOGGING_STR
299 "Disables logging to this vty\n")
300{
Harald Weltea62648b2011-02-18 21:03:27 +0100301 struct log_target *tgt = osmo_log_vty2tgt(vty);
302 struct telnet_connection *conn = (struct telnet_connection *) vty->priv;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200303
Harald Weltea62648b2011-02-18 21:03:27 +0100304 if (!tgt)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200305 return CMD_WARNING;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200306
Harald Weltea62648b2011-02-18 21:03:27 +0100307 log_del_target(tgt);
308 talloc_free(tgt);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200309 conn->dbg = NULL;
Harald Weltea62648b2011-02-18 21:03:27 +0100310
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200311 return CMD_SUCCESS;
312}
313
314static void vty_print_logtarget(struct vty *vty, const struct log_info *info,
315 const struct log_target *tgt)
316{
317 unsigned int i;
318
319 vty_out(vty, " Global Loglevel: %s%s",
320 log_level_str(tgt->loglevel), VTY_NEWLINE);
321 vty_out(vty, " Use color: %s, Print Timestamp: %s%s",
322 tgt->use_color ? "On" : "Off",
323 tgt->print_timestamp ? "On" : "Off", VTY_NEWLINE);
324
325 vty_out(vty, " Log Level specific information:%s", VTY_NEWLINE);
326
327 for (i = 0; i < info->num_cat; i++) {
328 const struct log_category *cat = &tgt->categories[i];
Daniel Willmann55363a92016-11-15 10:05:51 +0100329 /* Skip categories that were not initialized */
330 if (!info->cat[i].name)
331 continue;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200332 vty_out(vty, " %-10s %-10s %-8s %s%s",
333 info->cat[i].name+1, log_level_str(cat->loglevel),
334 cat->enabled ? "Enabled" : "Disabled",
335 info->cat[i].description,
336 VTY_NEWLINE);
337 }
Harald Welte6d2d4d62013-03-10 09:53:24 +0000338
339 vty_out(vty, " Log Filter 'ALL': %s%s",
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100340 tgt->filter_map & (1 << LOG_FLT_ALL) ? "Enabled" : "Disabled",
Harald Welte6d2d4d62013-03-10 09:53:24 +0000341 VTY_NEWLINE);
342
Harald Weltefb84f322013-06-06 07:33:54 +0200343 /* print application specific filters */
344 if (info->print_fn)
345 info->print_fn(vty, info, tgt);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200346}
347
348#define SHOW_LOG_STR "Show current logging configuration\n"
349
350DEFUN(show_logging_vty,
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000351 show_logging_vty_cmd,
352 "show logging vty",
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200353 SHOW_STR SHOW_LOG_STR
354 "Show current logging configuration for this vty\n")
355{
Harald Weltea62648b2011-02-18 21:03:27 +0100356 struct log_target *tgt = osmo_log_vty2tgt(vty);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200357
Harald Weltea62648b2011-02-18 21:03:27 +0100358 if (!tgt)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200359 return CMD_WARNING;
Harald Weltea62648b2011-02-18 21:03:27 +0100360
361 vty_print_logtarget(vty, osmo_log_info, tgt);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200362
363 return CMD_SUCCESS;
364}
365
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000366DEFUN(show_alarms,
367 show_alarms_cmd,
368 "show alarms",
369 SHOW_STR SHOW_LOG_STR
370 "Show the contents of the logging ringbuffer\n")
371{
372 int i, num_alarms;
373 struct osmo_strrb *rb;
374 struct log_target *tgt = log_target_find(LOG_TGT_TYPE_STRRB, NULL);
375 if (!tgt) {
376 vty_out(vty, "%% No alarms, run 'log alarms <2-32700>'%s",
377 VTY_NEWLINE);
378 return CMD_WARNING;
379 }
380
381 rb = tgt->tgt_rb.rb;
382 num_alarms = osmo_strrb_elements(rb);
383
384 vty_out(vty, "%% Showing %i alarms%s", num_alarms, VTY_NEWLINE);
385
386 for (i = 0; i < num_alarms; i++)
387 vty_out(vty, "%% %s%s", osmo_strrb_get_nth(rb, i),
388 VTY_NEWLINE);
389
390 return CMD_SUCCESS;
391}
392
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200393gDEFUN(cfg_description, cfg_description_cmd,
394 "description .TEXT",
Thorsten Alteholza81055d2017-03-02 22:13:48 +0100395 "Save human-readable description of the object\n"
Holger Hans Peter Freytherc9b3e062012-07-25 13:02:49 +0200396 "Text until the end of the line\n")
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200397{
398 char **dptr = vty->index_sub;
399
400 if (!dptr) {
401 vty_out(vty, "vty->index_sub == NULL%s", VTY_NEWLINE);
402 return CMD_WARNING;
403 }
404
Holger Hans Peter Freytherff0670e2011-02-24 14:20:41 +0100405 if (*dptr)
406 talloc_free(*dptr);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200407 *dptr = argv_concat(argv, argc, 0);
Holger Hans Peter Freyther6a75d162013-07-14 09:07:11 +0200408 if (!*dptr)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200409 return CMD_WARNING;
410
411 return CMD_SUCCESS;
412}
413
414gDEFUN(cfg_no_description, cfg_no_description_cmd,
415 "no description",
416 NO_STR
417 "Remove description of the object\n")
418{
419 char **dptr = vty->index_sub;
420
421 if (!dptr) {
422 vty_out(vty, "vty->index_sub == NULL%s", VTY_NEWLINE);
423 return CMD_WARNING;
424 }
425
426 if (*dptr) {
427 talloc_free(*dptr);
428 *dptr = NULL;
429 }
430
431 return CMD_SUCCESS;
432}
433
Harald Welte28222962011-02-18 20:37:04 +0100434/* Support for configuration of log targets != the current vty */
435
436struct cmd_node cfg_log_node = {
437 CFG_LOG_NODE,
438 "%s(config-log)# ",
439 1
440};
441
Harald Welte28222962011-02-18 20:37:04 +0100442#ifdef HAVE_SYSLOG_H
443
444#include <syslog.h>
445
446static const int local_sysl_map[] = {
447 [0] = LOG_LOCAL0,
448 [1] = LOG_LOCAL1,
449 [2] = LOG_LOCAL2,
450 [3] = LOG_LOCAL3,
451 [4] = LOG_LOCAL4,
452 [5] = LOG_LOCAL5,
453 [6] = LOG_LOCAL6,
454 [7] = LOG_LOCAL7
455};
456
Harald Weltede79cee2011-02-24 23:47:57 +0100457/* From VTY core code */
458extern struct host host;
459
Harald Welte28222962011-02-18 20:37:04 +0100460static int _cfg_log_syslog(struct vty *vty, int facility)
461{
462 struct log_target *tgt;
463
464 /* First delete the old syslog target, if any */
465 tgt = log_target_find(LOG_TGT_TYPE_SYSLOG, NULL);
466 if (tgt)
467 log_target_destroy(tgt);
468
Harald Weltede79cee2011-02-24 23:47:57 +0100469 tgt = log_target_create_syslog(host.app_info->name, 0, facility);
Harald Welte28222962011-02-18 20:37:04 +0100470 if (!tgt) {
471 vty_out(vty, "%% Unable to open syslog%s", VTY_NEWLINE);
472 return CMD_WARNING;
473 }
474 log_add_target(tgt);
475
476 vty->index = tgt;
477 vty->node = CFG_LOG_NODE;
478
479 return CMD_SUCCESS;
480}
481
482DEFUN(cfg_log_syslog_local, cfg_log_syslog_local_cmd,
483 "log syslog local <0-7>",
484 LOG_STR "Logging via syslog\n" "Syslog LOCAL facility\n"
485 "Local facility number\n")
486{
487 int local = atoi(argv[0]);
488 int facility = local_sysl_map[local];
489
490 return _cfg_log_syslog(vty, facility);
491}
492
493static struct value_string sysl_level_names[] = {
494 { LOG_AUTHPRIV, "authpriv" },
495 { LOG_CRON, "cron" },
496 { LOG_DAEMON, "daemon" },
497 { LOG_FTP, "ftp" },
498 { LOG_LPR, "lpr" },
499 { LOG_MAIL, "mail" },
500 { LOG_NEWS, "news" },
501 { LOG_USER, "user" },
502 { LOG_UUCP, "uucp" },
503 /* only for value -> string conversion */
504 { LOG_LOCAL0, "local 0" },
505 { LOG_LOCAL1, "local 1" },
506 { LOG_LOCAL2, "local 2" },
507 { LOG_LOCAL3, "local 3" },
508 { LOG_LOCAL4, "local 4" },
509 { LOG_LOCAL5, "local 5" },
510 { LOG_LOCAL6, "local 6" },
511 { LOG_LOCAL7, "local 7" },
512 { 0, NULL }
513};
514
515DEFUN(cfg_log_syslog, cfg_log_syslog_cmd,
516 "log syslog (authpriv|cron|daemon|ftp|lpr|mail|news|user|uucp)",
Holger Hans Peter Freythera4463fd2011-10-03 23:17:36 +0200517 LOG_STR "Logging via syslog\n"
518 "Security/authorization messages facility\n"
519 "Clock daemon (cron/at) facility\n"
520 "General system daemon facility\n"
521 "Ftp daemon facility\n"
522 "Line printer facility\n"
523 "Mail facility\n"
524 "News facility\n"
525 "Generic facility\n"
526 "UUCP facility\n")
Harald Welte28222962011-02-18 20:37:04 +0100527{
528 int facility = get_string_value(sysl_level_names, argv[0]);
529
530 return _cfg_log_syslog(vty, facility);
531}
532
533DEFUN(cfg_no_log_syslog, cfg_no_log_syslog_cmd,
534 "no log syslog",
535 NO_STR LOG_STR "Logging via syslog\n")
536{
537 struct log_target *tgt;
538
539 tgt = log_target_find(LOG_TGT_TYPE_SYSLOG, NULL);
540 if (!tgt) {
541 vty_out(vty, "%% No syslog target found%s",
542 VTY_NEWLINE);
543 return CMD_WARNING;
544 }
545
546 log_target_destroy(tgt);
547
548 return CMD_SUCCESS;
549}
550#endif /* HAVE_SYSLOG_H */
551
Harald Welteaa00f992016-12-02 15:30:02 +0100552DEFUN(cfg_log_gsmtap, cfg_log_gsmtap_cmd,
553 "log gsmtap [HOSTNAME]",
Neels Hofmeyrfd9ec3b2016-12-11 01:48:26 +0100554 LOG_STR "Logging via GSMTAP\n"
555 "Host name to send the GSMTAP logging to (UDP port 4729)\n")
Harald Welteaa00f992016-12-02 15:30:02 +0100556{
Max2f153b52018-01-04 12:25:57 +0100557 const char *hostname = argc ? argv[0] : "127.0.0.1";
Harald Welteaa00f992016-12-02 15:30:02 +0100558 struct log_target *tgt;
559
560 tgt = log_target_find(LOG_TGT_TYPE_GSMTAP, hostname);
561 if (!tgt) {
562 tgt = log_target_create_gsmtap(hostname, GSMTAP_UDP_PORT,
563 host.app_info->name, false,
564 true);
565 if (!tgt) {
Max2f153b52018-01-04 12:25:57 +0100566 vty_out(vty, "%% Unable to create GSMTAP log for %s%s",
567 hostname, VTY_NEWLINE);
Harald Welteaa00f992016-12-02 15:30:02 +0100568 return CMD_WARNING;
569 }
570 log_add_target(tgt);
571 }
572
573 vty->index = tgt;
574 vty->node = CFG_LOG_NODE;
575
576 return CMD_SUCCESS;
577}
578
Harald Welte28222962011-02-18 20:37:04 +0100579DEFUN(cfg_log_stderr, cfg_log_stderr_cmd,
580 "log stderr",
581 LOG_STR "Logging via STDERR of the process\n")
582{
583 struct log_target *tgt;
584
585 tgt = log_target_find(LOG_TGT_TYPE_STDERR, NULL);
586 if (!tgt) {
587 tgt = log_target_create_stderr();
588 if (!tgt) {
589 vty_out(vty, "%% Unable to create stderr log%s",
590 VTY_NEWLINE);
591 return CMD_WARNING;
592 }
593 log_add_target(tgt);
594 }
595
596 vty->index = tgt;
597 vty->node = CFG_LOG_NODE;
598
599 return CMD_SUCCESS;
600}
601
602DEFUN(cfg_no_log_stderr, cfg_no_log_stderr_cmd,
603 "no log stderr",
604 NO_STR LOG_STR "Logging via STDERR of the process\n")
605{
606 struct log_target *tgt;
607
608 tgt = log_target_find(LOG_TGT_TYPE_STDERR, NULL);
609 if (!tgt) {
610 vty_out(vty, "%% No stderr logging active%s", VTY_NEWLINE);
611 return CMD_WARNING;
612 }
613
614 log_target_destroy(tgt);
615
616 return CMD_SUCCESS;
617}
618
619DEFUN(cfg_log_file, cfg_log_file_cmd,
620 "log file .FILENAME",
621 LOG_STR "Logging to text file\n" "Filename\n")
622{
623 const char *fname = argv[0];
624 struct log_target *tgt;
625
626 tgt = log_target_find(LOG_TGT_TYPE_FILE, fname);
627 if (!tgt) {
628 tgt = log_target_create_file(fname);
629 if (!tgt) {
630 vty_out(vty, "%% Unable to create file `%s'%s",
631 fname, VTY_NEWLINE);
632 return CMD_WARNING;
633 }
634 log_add_target(tgt);
635 }
636
637 vty->index = tgt;
638 vty->node = CFG_LOG_NODE;
639
640 return CMD_SUCCESS;
641}
642
643
644DEFUN(cfg_no_log_file, cfg_no_log_file_cmd,
645 "no log file .FILENAME",
646 NO_STR LOG_STR "Logging to text file\n" "Filename\n")
647{
648 const char *fname = argv[0];
649 struct log_target *tgt;
650
651 tgt = log_target_find(LOG_TGT_TYPE_FILE, fname);
652 if (!tgt) {
653 vty_out(vty, "%% No such log file `%s'%s",
654 fname, VTY_NEWLINE);
655 return CMD_WARNING;
656 }
657
658 log_target_destroy(tgt);
659
660 return CMD_SUCCESS;
661}
662
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000663DEFUN(cfg_log_alarms, cfg_log_alarms_cmd,
664 "log alarms <2-32700>",
665 LOG_STR "Logging alarms to osmo_strrb\n"
666 "Maximum number of messages to log\n")
667{
668 struct log_target *tgt;
669 unsigned int rbsize = atoi(argv[0]);
670
671 tgt = log_target_find(LOG_TGT_TYPE_STRRB, NULL);
672 if (tgt)
673 log_target_destroy(tgt);
674
675 tgt = log_target_create_rb(rbsize);
676 if (!tgt) {
677 vty_out(vty, "%% Unable to create osmo_strrb (size %u)%s",
678 rbsize, VTY_NEWLINE);
679 return CMD_WARNING;
680 }
681 log_add_target(tgt);
682
683 vty->index = tgt;
684 vty->node = CFG_LOG_NODE;
685
686 return CMD_SUCCESS;
687}
688
689DEFUN(cfg_no_log_alarms, cfg_no_log_alarms_cmd,
690 "no log alarms",
691 NO_STR LOG_STR "Logging alarms to osmo_strrb\n")
692{
693 struct log_target *tgt;
694
695 tgt = log_target_find(LOG_TGT_TYPE_STRRB, NULL);
696 if (!tgt) {
697 vty_out(vty, "%% No osmo_strrb target found%s", VTY_NEWLINE);
698 return CMD_WARNING;
699 }
700
701 log_target_destroy(tgt);
702
703 return CMD_SUCCESS;
704}
705
Harald Welte28222962011-02-18 20:37:04 +0100706static int config_write_log_single(struct vty *vty, struct log_target *tgt)
707{
708 int i;
709 char level_lower[32];
710
711 switch (tgt->type) {
712 case LOG_TGT_TYPE_VTY:
713 return 1;
714 break;
715 case LOG_TGT_TYPE_STDERR:
716 vty_out(vty, "log stderr%s", VTY_NEWLINE);
717 break;
718 case LOG_TGT_TYPE_SYSLOG:
719#ifdef HAVE_SYSLOG_H
720 vty_out(vty, "log syslog %s%s",
721 get_value_string(sysl_level_names,
722 tgt->tgt_syslog.facility),
723 VTY_NEWLINE);
724#endif
725 break;
726 case LOG_TGT_TYPE_FILE:
727 vty_out(vty, "log file %s%s", tgt->tgt_file.fname, VTY_NEWLINE);
728 break;
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000729 case LOG_TGT_TYPE_STRRB:
730 vty_out(vty, "log alarms %zu%s",
731 log_target_rb_avail_size(tgt), VTY_NEWLINE);
732 break;
Harald Welteaa00f992016-12-02 15:30:02 +0100733 case LOG_TGT_TYPE_GSMTAP:
734 vty_out(vty, "log gsmtap %s%s",
735 tgt->tgt_gsmtap.hostname, VTY_NEWLINE);
736 break;
Harald Welte28222962011-02-18 20:37:04 +0100737 }
738
Harald Welte2da47f12012-10-22 19:31:54 +0200739 vty_out(vty, " logging filter all %u%s",
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100740 tgt->filter_map & (1 << LOG_FLT_ALL) ? 1 : 0, VTY_NEWLINE);
Harald Weltefb84f322013-06-06 07:33:54 +0200741 /* save filters outside of libosmocore, i.e. in app code */
742 if (osmo_log_info->save_fn)
743 osmo_log_info->save_fn(vty, osmo_log_info, tgt);
Harald Welte2da47f12012-10-22 19:31:54 +0200744
Harald Welte28222962011-02-18 20:37:04 +0100745 vty_out(vty, " logging color %u%s", tgt->use_color ? 1 : 0,
746 VTY_NEWLINE);
Holger Hans Peter Freyther879acef2015-01-27 11:06:51 +0100747 vty_out(vty, " logging print category %d%s",
Michael McTernan78933462015-03-20 15:29:25 +0100748 tgt->print_category ? 1 : 0, VTY_NEWLINE);
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100749 if (tgt->print_ext_timestamp)
750 vty_out(vty, " logging print extended-timestamp 1%s", VTY_NEWLINE);
751 else
752 vty_out(vty, " logging timestamp %u%s",
753 tgt->print_timestamp ? 1 : 0, VTY_NEWLINE);
Neels Hofmeyr886e5482018-01-16 01:49:37 +0100754 if (tgt->print_level)
755 vty_out(vty, " logging print level 1%s", VTY_NEWLINE);
Harald Welte28222962011-02-18 20:37:04 +0100756
757 /* stupid old osmo logging API uses uppercase strings... */
758 osmo_str2lower(level_lower, log_level_str(tgt->loglevel));
759 vty_out(vty, " logging level all %s%s", level_lower, VTY_NEWLINE);
760
761 for (i = 0; i < osmo_log_info->num_cat; i++) {
762 const struct log_category *cat = &tgt->categories[i];
763 char cat_lower[32];
764
Harald Welte1a02cfc2013-03-19 10:37:39 +0100765 /* skip empty entries in the array */
766 if (!osmo_log_info->cat[i].name)
767 continue;
768
Harald Welte28222962011-02-18 20:37:04 +0100769 /* stupid old osmo logging API uses uppercase strings... */
770 osmo_str2lower(cat_lower, osmo_log_info->cat[i].name+1);
771 osmo_str2lower(level_lower, log_level_str(cat->loglevel));
772
Maxed0eda22017-07-06 17:09:01 +0200773 if (strcmp(level_lower, "everything") != 0) /* FIXME: remove this check once 'everything' is phased out */
774 vty_out(vty, " logging level %s %s%s", cat_lower, level_lower, VTY_NEWLINE);
775 else
776 LOGP(DLSTATS, LOGL_ERROR, "logging level everything is deprecated and should not be used\n");
Harald Welte28222962011-02-18 20:37:04 +0100777 }
778
779 /* FIXME: levels */
780
781 return 1;
782}
783
784static int config_write_log(struct vty *vty)
785{
786 struct log_target *dbg = vty->index;
787
788 llist_for_each_entry(dbg, &osmo_log_target_list, entry)
789 config_write_log_single(vty, dbg);
790
791 return 1;
792}
793
Harald Welte8c648252017-10-16 15:17:03 +0200794/*! Register logging related commands to the VTY. Call this once from
795 * your application if you want to support those commands. */
Maxc65c5b42017-03-15 13:20:23 +0100796void logging_vty_add_cmds()
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200797{
798 install_element_ve(&enable_logging_cmd);
799 install_element_ve(&disable_logging_cmd);
800 install_element_ve(&logging_fltr_all_cmd);
801 install_element_ve(&logging_use_clr_cmd);
802 install_element_ve(&logging_prnt_timestamp_cmd);
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100803 install_element_ve(&logging_prnt_ext_timestamp_cmd);
804 install_element_ve(&logging_prnt_cat_cmd);
Neels Hofmeyr886e5482018-01-16 01:49:37 +0100805 install_element_ve(&logging_prnt_level_cmd);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200806 install_element_ve(&logging_set_category_mask_cmd);
Holger Hans Peter Freyther146d1d32011-10-03 23:15:41 +0200807 install_element_ve(&logging_set_category_mask_old_cmd);
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100808
809 /* Logging level strings are generated dynamically. */
Maxc65c5b42017-03-15 13:20:23 +0100810 logging_level_cmd.string = log_vty_command_string();
811 logging_level_cmd.doc = log_vty_command_description();
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200812 install_element_ve(&logging_level_cmd);
813 install_element_ve(&show_logging_vty_cmd);
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000814 install_element_ve(&show_alarms_cmd);
Harald Welte28222962011-02-18 20:37:04 +0100815
816 install_node(&cfg_log_node, config_write_log);
Harald Weltea62648b2011-02-18 21:03:27 +0100817 install_element(CFG_LOG_NODE, &logging_fltr_all_cmd);
818 install_element(CFG_LOG_NODE, &logging_use_clr_cmd);
819 install_element(CFG_LOG_NODE, &logging_prnt_timestamp_cmd);
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100820 install_element(CFG_LOG_NODE, &logging_prnt_ext_timestamp_cmd);
821 install_element(CFG_LOG_NODE, &logging_prnt_cat_cmd);
Neels Hofmeyr886e5482018-01-16 01:49:37 +0100822 install_element(CFG_LOG_NODE, &logging_prnt_level_cmd);
Harald Weltea62648b2011-02-18 21:03:27 +0100823 install_element(CFG_LOG_NODE, &logging_level_cmd);
Harald Welte28222962011-02-18 20:37:04 +0100824
825 install_element(CONFIG_NODE, &cfg_log_stderr_cmd);
826 install_element(CONFIG_NODE, &cfg_no_log_stderr_cmd);
827 install_element(CONFIG_NODE, &cfg_log_file_cmd);
828 install_element(CONFIG_NODE, &cfg_no_log_file_cmd);
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000829 install_element(CONFIG_NODE, &cfg_log_alarms_cmd);
830 install_element(CONFIG_NODE, &cfg_no_log_alarms_cmd);
Harald Welte28222962011-02-18 20:37:04 +0100831#ifdef HAVE_SYSLOG_H
832 install_element(CONFIG_NODE, &cfg_log_syslog_cmd);
833 install_element(CONFIG_NODE, &cfg_log_syslog_local_cmd);
834 install_element(CONFIG_NODE, &cfg_no_log_syslog_cmd);
835#endif
Harald Welteaa00f992016-12-02 15:30:02 +0100836 install_element(CONFIG_NODE, &cfg_log_gsmtap_cmd);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200837}