blob: 2b001bc9aa69d323c95d2231a309c29d8d592a59 [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>
Neels Hofmeyrba0762d2018-09-10 13:56:03 +020031#include <osmocom/core/logging_internal.h>
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010032#include <osmocom/core/utils.h>
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +000033#include <osmocom/core/strrb.h>
34#include <osmocom/core/loggingrb.h>
Harald Welteaa00f992016-12-02 15:30:02 +010035#include <osmocom/core/gsmtap.h>
Harald Welte3fb0b6f2010-05-19 19:02:52 +020036
37#include <osmocom/vty/command.h>
38#include <osmocom/vty/buffer.h>
39#include <osmocom/vty/vty.h>
40#include <osmocom/vty/telnet_interface.h>
41#include <osmocom/vty/logging.h>
42
Harald Welte28222962011-02-18 20:37:04 +010043#define LOG_STR "Configure logging sub-system\n"
Neels Hofmeyrba0762d2018-09-10 13:56:03 +020044#define LEVEL_STR "Set the log level for a specified category\n"
45
46#define CATEGORY_ALL_STR "Global setting for all subsystems\n"
47
48#define LOG_LEVEL_ARGS "debug|info|notice|error|fatal"
49#define LOG_LEVEL_STRS \
50 "Log debug messages and higher levels\n" \
51 "Log informational messages and higher levels\n" \
52 "Log noticeable messages and higher levels\n" \
53 "Log error messages and higher levels\n" \
54 "Log only fatal messages\n"
55
56#define EVERYTHING_STR "Don't use. It doesn't log anything\n"
Harald Welte28222962011-02-18 20:37:04 +010057
Harald Welte8c648252017-10-16 15:17:03 +020058/*! \file logging_vty.c
Neels Hofmeyr87e45502017-06-20 00:17:59 +020059 * Configuration of logging from VTY
Harald Welte96e2a002017-06-12 21:44:18 +020060 *
Harald Welte8c648252017-10-16 15:17:03 +020061 * This module implements
62 * - functions that permit configuration of the libosmocore logging
63 * framework from VTY commands in the configure -> logging node.
64 *
65 * - functions that permit logging *to* a VTY session. Basically each
66 * VTY session gets its own log target, with configurable
67 * per-subsystem log levels. This is performed internally via the
68 * \ref log_target_create_vty function.
69 *
70 * You have to call \ref logging_vty_add_cmds from your application
71 * once to enable both of the above.
72 *
Harald Welte96e2a002017-06-12 21:44:18 +020073 */
74
Harald Welte76e72ab2011-02-17 15:52:39 +010075static void _vty_output(struct log_target *tgt,
76 unsigned int level, const char *line)
Harald Welte3fb0b6f2010-05-19 19:02:52 +020077{
78 struct vty *vty = tgt->tgt_vty.vty;
79 vty_out(vty, "%s", line);
80 /* This is an ugly hack, but there is no easy way... */
81 if (strchr(line, '\n'))
82 vty_out(vty, "\r");
83}
84
85struct log_target *log_target_create_vty(struct vty *vty)
86{
87 struct log_target *target;
88
89 target = log_target_create();
90 if (!target)
91 return NULL;
92
93 target->tgt_vty.vty = vty;
94 target->output = _vty_output;
95 return target;
96}
97
98DEFUN(enable_logging,
99 enable_logging_cmd,
100 "logging enable",
101 LOGGING_STR
102 "Enables logging to this vty\n")
103{
104 struct telnet_connection *conn;
105
106 conn = (struct telnet_connection *) vty->priv;
107 if (conn->dbg) {
108 vty_out(vty, "Logging already enabled.%s", VTY_NEWLINE);
109 return CMD_WARNING;
110 }
111
112 conn->dbg = log_target_create_vty(vty);
113 if (!conn->dbg)
114 return CMD_WARNING;
115
116 log_add_target(conn->dbg);
117 return CMD_SUCCESS;
118}
119
Harald Weltea62648b2011-02-18 21:03:27 +0100120struct log_target *osmo_log_vty2tgt(struct vty *vty)
121{
122 struct telnet_connection *conn;
123
124 if (vty->node == CFG_LOG_NODE)
125 return vty->index;
126
127
128 conn = (struct telnet_connection *) vty->priv;
129 if (!conn->dbg)
130 vty_out(vty, "Logging was not enabled.%s", VTY_NEWLINE);
131
132 return conn->dbg;
133}
134
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200135DEFUN(logging_fltr_all,
136 logging_fltr_all_cmd,
137 "logging filter all (0|1)",
138 LOGGING_STR FILTER_STR
139 "Do you want to log all messages?\n"
140 "Only print messages matched by other filters\n"
141 "Bypass filter and print all messages\n")
142{
Harald Weltea62648b2011-02-18 21:03:27 +0100143 struct log_target *tgt = osmo_log_vty2tgt(vty);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200144
Harald Weltea62648b2011-02-18 21:03:27 +0100145 if (!tgt)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200146 return CMD_WARNING;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200147
Harald Weltea62648b2011-02-18 21:03:27 +0100148 log_set_all_filter(tgt, atoi(argv[0]));
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200149 return CMD_SUCCESS;
150}
151
152DEFUN(logging_use_clr,
153 logging_use_clr_cmd,
154 "logging color (0|1)",
155 LOGGING_STR "Configure color-printing for log messages\n"
156 "Don't use color for printing messages\n"
157 "Use color for printing messages\n")
158{
Harald Weltea62648b2011-02-18 21:03:27 +0100159 struct log_target *tgt = osmo_log_vty2tgt(vty);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200160
Harald Weltea62648b2011-02-18 21:03:27 +0100161 if (!tgt)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200162 return CMD_WARNING;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200163
Harald Weltea62648b2011-02-18 21:03:27 +0100164 log_set_use_color(tgt, atoi(argv[0]));
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200165 return CMD_SUCCESS;
166}
167
168DEFUN(logging_prnt_timestamp,
169 logging_prnt_timestamp_cmd,
170 "logging timestamp (0|1)",
171 LOGGING_STR "Configure log message timestamping\n"
172 "Don't prefix each log message\n"
173 "Prefix each log message with current timestamp\n")
174{
Harald Weltea62648b2011-02-18 21:03:27 +0100175 struct log_target *tgt = osmo_log_vty2tgt(vty);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200176
Harald Weltea62648b2011-02-18 21:03:27 +0100177 if (!tgt)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200178 return CMD_WARNING;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200179
Harald Weltea62648b2011-02-18 21:03:27 +0100180 log_set_print_timestamp(tgt, atoi(argv[0]));
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200181 return CMD_SUCCESS;
182}
183
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100184DEFUN(logging_prnt_ext_timestamp,
185 logging_prnt_ext_timestamp_cmd,
186 "logging print extended-timestamp (0|1)",
187 LOGGING_STR "Log output settings\n"
188 "Configure log message timestamping\n"
189 "Don't prefix each log message\n"
190 "Prefix each log message with current timestamp with YYYYMMDDhhmmssnnn\n")
191{
192 struct log_target *tgt = osmo_log_vty2tgt(vty);
193
194 if (!tgt)
195 return CMD_WARNING;
196
197 log_set_print_extended_timestamp(tgt, atoi(argv[0]));
198 return CMD_SUCCESS;
199}
200
201DEFUN(logging_prnt_cat,
202 logging_prnt_cat_cmd,
203 "logging print category (0|1)",
204 LOGGING_STR "Log output settings\n"
205 "Configure log message\n"
206 "Don't prefix each log message\n"
207 "Prefix each log message with category/subsystem name\n")
208{
209 struct log_target *tgt = osmo_log_vty2tgt(vty);
210
211 if (!tgt)
212 return CMD_WARNING;
213
214 log_set_print_category(tgt, atoi(argv[0]));
215 return CMD_SUCCESS;
216}
217
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100218DEFUN(logging_prnt_cat_hex,
219 logging_prnt_cat_hex_cmd,
220 "logging print category-hex (0|1)",
221 LOGGING_STR "Log output settings\n"
222 "Configure log message\n"
223 "Don't prefix each log message\n"
224 "Prefix each log message with category/subsystem nr in hex ('<000b>')\n")
225{
226 struct log_target *tgt = osmo_log_vty2tgt(vty);
227
228 if (!tgt)
229 return CMD_WARNING;
230
231 log_set_print_category_hex(tgt, atoi(argv[0]));
232 return CMD_SUCCESS;
233}
234
Neels Hofmeyr886e5482018-01-16 01:49:37 +0100235DEFUN(logging_prnt_level,
236 logging_prnt_level_cmd,
237 "logging print level (0|1)",
238 LOGGING_STR "Log output settings\n"
239 "Configure log message\n"
240 "Don't prefix each log message\n"
241 "Prefix each log message with the log level name\n")
242{
243 struct log_target *tgt = osmo_log_vty2tgt(vty);
244
245 if (!tgt)
246 return CMD_WARNING;
247
248 log_set_print_level(tgt, atoi(argv[0]));
249 return CMD_SUCCESS;
250}
251
Neels Hofmeyr22772cc2018-02-06 00:52:08 +0100252static const struct value_string logging_print_file_args[] = {
253 { LOG_FILENAME_NONE, "0" },
254 { LOG_FILENAME_PATH, "1" },
255 { LOG_FILENAME_BASENAME, "basename" },
256 { 0, NULL }
257};
258
Neels Hofmeyrc6fd2452018-01-16 01:57:38 +0100259DEFUN(logging_prnt_file,
260 logging_prnt_file_cmd,
Neels Hofmeyr77ae45d2018-08-27 20:32:36 +0200261 "logging print file (0|1|basename) [last]",
Neels Hofmeyrc6fd2452018-01-16 01:57:38 +0100262 LOGGING_STR "Log output settings\n"
263 "Configure log message\n"
264 "Don't prefix each log message\n"
265 "Prefix each log message with the source file and line\n"
Neels Hofmeyr77ae45d2018-08-27 20:32:36 +0200266 "Prefix each log message with the source file's basename (strip leading paths) and line\n"
267 "Log source file info at the end of a log line. If omitted, log source file info just"
268 " before the log text.\n")
Neels Hofmeyrc6fd2452018-01-16 01:57:38 +0100269{
270 struct log_target *tgt = osmo_log_vty2tgt(vty);
Neels Hofmeyrc6fd2452018-01-16 01:57:38 +0100271
272 if (!tgt)
273 return CMD_WARNING;
274
Neels Hofmeyr22772cc2018-02-06 00:52:08 +0100275 log_set_print_filename2(tgt, get_string_value(logging_print_file_args, argv[0]));
Neels Hofmeyr77ae45d2018-08-27 20:32:36 +0200276 if (argc > 1)
277 log_set_print_filename_pos(tgt, LOG_FILENAME_POS_LINE_END);
278 else
279 log_set_print_filename_pos(tgt, LOG_FILENAME_POS_HEADER_END);
Neels Hofmeyrc6fd2452018-01-16 01:57:38 +0100280 return CMD_SUCCESS;
281}
282
Neels Hofmeyrba0762d2018-09-10 13:56:03 +0200283static void add_category_strings(char **cmd_str_p, char **doc_str_p,
284 const struct log_info *categories)
285{
286 int i;
287 for (i = 0; i < categories->num_cat; i++) {
288 if (categories->cat[i].name == NULL)
289 continue;
290 /* skip the leading 'D' in each category name, hence '+ 1' */
291 osmo_talloc_asprintf(tall_log_ctx, *cmd_str_p, "%s%s",
292 i ? "|" : "",
293 osmo_str_tolower(categories->cat[i].name + 1));
294 osmo_talloc_asprintf(tall_log_ctx, *doc_str_p, "%s\n",
295 categories->cat[i].description);
296 }
297}
298
299static void gen_logging_level_cmd_strs(struct cmd_element *cmd,
300 const char *level_args, const char *level_strs)
301{
302 char *cmd_str = NULL;
303 char *doc_str = NULL;
304
305 assert_loginfo(__func__);
306
307 OSMO_ASSERT(cmd->string == NULL);
308 OSMO_ASSERT(cmd->doc == NULL);
309
310 osmo_talloc_asprintf(tall_log_ctx, cmd_str, "logging level (all|");
311 osmo_talloc_asprintf(tall_log_ctx, doc_str,
312 LOGGING_STR
313 LEVEL_STR
314 CATEGORY_ALL_STR);
315 add_category_strings(&cmd_str, &doc_str, osmo_log_info);
316 osmo_talloc_asprintf(tall_log_ctx, cmd_str, ") %s", level_args);
317 osmo_talloc_asprintf(tall_log_ctx, doc_str, "%s", level_strs);
318
319 cmd->string = cmd_str;
320 cmd->doc = doc_str;
321}
322
323/* logging level (all|<categories>) (everything|debug|...|fatal) */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200324DEFUN(logging_level,
325 logging_level_cmd,
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100326 NULL, /* cmdstr is dynamically set in logging_vty_add_cmds(). */
327 NULL) /* same thing for helpstr. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200328{
Harald Weltea62648b2011-02-18 21:03:27 +0100329 int category = log_parse_category(argv[0]);
330 int level = log_parse_level(argv[1]);
331 struct log_target *tgt = osmo_log_vty2tgt(vty);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200332
Harald Weltea62648b2011-02-18 21:03:27 +0100333 if (!tgt)
334 return CMD_WARNING;
335
336 if (level < 0) {
337 vty_out(vty, "Invalid level `%s'%s", argv[1], VTY_NEWLINE);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200338 return CMD_WARNING;
339 }
340
Maxed0eda22017-07-06 17:09:01 +0200341 if (strcmp(argv[1], "everything") == 0) { /* FIXME: remove this check once 'everything' is phased out */
342 vty_out(vty, "%% Ignoring deprecated logging level %s%s", argv[1], VTY_NEWLINE);
343 return CMD_SUCCESS;
344 }
345
Harald Weltea62648b2011-02-18 21:03:27 +0100346 /* Check for special case where we want to set global log level */
347 if (!strcmp(argv[0], "all")) {
348 log_set_log_level(tgt, level);
349 return CMD_SUCCESS;
350 }
351
352 if (category < 0) {
353 vty_out(vty, "Invalid category `%s'%s", argv[0], VTY_NEWLINE);
354 return CMD_WARNING;
355 }
356
357 tgt->categories[category].enabled = 1;
358 tgt->categories[category].loglevel = level;
359
360 return CMD_SUCCESS;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200361}
362
363DEFUN(logging_set_category_mask,
364 logging_set_category_mask_cmd,
Holger Hans Peter Freyther146d1d32011-10-03 23:15:41 +0200365 "logging set-log-mask MASK",
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200366 LOGGING_STR
Holger Hans Peter Freyther146d1d32011-10-03 23:15:41 +0200367 "Set the logmask of this logging target\n"
Neels Hofmeyr84ea2e02017-12-09 05:53:18 +0100368 "List of logging categories to log, e.g. 'abc:mno:xyz'. Available log categories depend on the specific"
369 " application, refer to the 'logging level' command. Optionally add individual log levels like"
370 " 'abc,1:mno,3:xyz,5', where the level numbers are"
371 " " OSMO_STRINGIFY(LOGL_DEBUG) "=" OSMO_STRINGIFY_VAL(LOGL_DEBUG)
372 " " OSMO_STRINGIFY(LOGL_INFO) "=" OSMO_STRINGIFY_VAL(LOGL_INFO)
373 " " OSMO_STRINGIFY(LOGL_NOTICE) "=" OSMO_STRINGIFY_VAL(LOGL_NOTICE)
374 " " OSMO_STRINGIFY(LOGL_ERROR) "=" OSMO_STRINGIFY_VAL(LOGL_ERROR)
375 " " OSMO_STRINGIFY(LOGL_FATAL) "=" OSMO_STRINGIFY_VAL(LOGL_FATAL)
376 "\n")
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200377{
Harald Weltea62648b2011-02-18 21:03:27 +0100378 struct log_target *tgt = osmo_log_vty2tgt(vty);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200379
Harald Weltea62648b2011-02-18 21:03:27 +0100380 if (!tgt)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200381 return CMD_WARNING;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200382
Harald Weltea62648b2011-02-18 21:03:27 +0100383 log_parse_category_mask(tgt, argv[0]);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200384 return CMD_SUCCESS;
385}
386
Holger Hans Peter Freyther146d1d32011-10-03 23:15:41 +0200387ALIAS_DEPRECATED(logging_set_category_mask,
388 logging_set_category_mask_old_cmd,
389 "logging set log mask MASK",
390 LOGGING_STR
391 "Decide which categories to output.\n"
Neels Hofmeyr84ea2e02017-12-09 05:53:18 +0100392 "Log commands\n" "Mask commands\n"
393 "'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 +0200394
395
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200396DEFUN(diable_logging,
397 disable_logging_cmd,
398 "logging disable",
399 LOGGING_STR
400 "Disables logging to this vty\n")
401{
Harald Weltea62648b2011-02-18 21:03:27 +0100402 struct log_target *tgt = osmo_log_vty2tgt(vty);
403 struct telnet_connection *conn = (struct telnet_connection *) vty->priv;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200404
Harald Weltea62648b2011-02-18 21:03:27 +0100405 if (!tgt)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200406 return CMD_WARNING;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200407
Harald Weltea62648b2011-02-18 21:03:27 +0100408 log_del_target(tgt);
409 talloc_free(tgt);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200410 conn->dbg = NULL;
Harald Weltea62648b2011-02-18 21:03:27 +0100411
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200412 return CMD_SUCCESS;
413}
414
415static void vty_print_logtarget(struct vty *vty, const struct log_info *info,
416 const struct log_target *tgt)
417{
418 unsigned int i;
419
420 vty_out(vty, " Global Loglevel: %s%s",
421 log_level_str(tgt->loglevel), VTY_NEWLINE);
422 vty_out(vty, " Use color: %s, Print Timestamp: %s%s",
423 tgt->use_color ? "On" : "Off",
424 tgt->print_timestamp ? "On" : "Off", VTY_NEWLINE);
425
426 vty_out(vty, " Log Level specific information:%s", VTY_NEWLINE);
427
428 for (i = 0; i < info->num_cat; i++) {
429 const struct log_category *cat = &tgt->categories[i];
Daniel Willmann55363a92016-11-15 10:05:51 +0100430 /* Skip categories that were not initialized */
431 if (!info->cat[i].name)
432 continue;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200433 vty_out(vty, " %-10s %-10s %-8s %s%s",
434 info->cat[i].name+1, log_level_str(cat->loglevel),
435 cat->enabled ? "Enabled" : "Disabled",
436 info->cat[i].description,
437 VTY_NEWLINE);
438 }
Harald Welte6d2d4d62013-03-10 09:53:24 +0000439
440 vty_out(vty, " Log Filter 'ALL': %s%s",
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100441 tgt->filter_map & (1 << LOG_FLT_ALL) ? "Enabled" : "Disabled",
Harald Welte6d2d4d62013-03-10 09:53:24 +0000442 VTY_NEWLINE);
443
Harald Weltefb84f322013-06-06 07:33:54 +0200444 /* print application specific filters */
445 if (info->print_fn)
446 info->print_fn(vty, info, tgt);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200447}
448
449#define SHOW_LOG_STR "Show current logging configuration\n"
450
451DEFUN(show_logging_vty,
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000452 show_logging_vty_cmd,
453 "show logging vty",
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200454 SHOW_STR SHOW_LOG_STR
455 "Show current logging configuration for this vty\n")
456{
Harald Weltea62648b2011-02-18 21:03:27 +0100457 struct log_target *tgt = osmo_log_vty2tgt(vty);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200458
Harald Weltea62648b2011-02-18 21:03:27 +0100459 if (!tgt)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200460 return CMD_WARNING;
Harald Weltea62648b2011-02-18 21:03:27 +0100461
462 vty_print_logtarget(vty, osmo_log_info, tgt);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200463
464 return CMD_SUCCESS;
465}
466
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000467DEFUN(show_alarms,
468 show_alarms_cmd,
469 "show alarms",
470 SHOW_STR SHOW_LOG_STR
471 "Show the contents of the logging ringbuffer\n")
472{
473 int i, num_alarms;
474 struct osmo_strrb *rb;
475 struct log_target *tgt = log_target_find(LOG_TGT_TYPE_STRRB, NULL);
476 if (!tgt) {
477 vty_out(vty, "%% No alarms, run 'log alarms <2-32700>'%s",
478 VTY_NEWLINE);
479 return CMD_WARNING;
480 }
481
482 rb = tgt->tgt_rb.rb;
483 num_alarms = osmo_strrb_elements(rb);
484
485 vty_out(vty, "%% Showing %i alarms%s", num_alarms, VTY_NEWLINE);
486
487 for (i = 0; i < num_alarms; i++)
488 vty_out(vty, "%% %s%s", osmo_strrb_get_nth(rb, i),
489 VTY_NEWLINE);
490
491 return CMD_SUCCESS;
492}
493
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200494gDEFUN(cfg_description, cfg_description_cmd,
495 "description .TEXT",
Thorsten Alteholza81055d2017-03-02 22:13:48 +0100496 "Save human-readable description of the object\n"
Holger Hans Peter Freytherc9b3e062012-07-25 13:02:49 +0200497 "Text until the end of the line\n")
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200498{
499 char **dptr = vty->index_sub;
500
501 if (!dptr) {
502 vty_out(vty, "vty->index_sub == NULL%s", VTY_NEWLINE);
503 return CMD_WARNING;
504 }
505
Holger Hans Peter Freytherff0670e2011-02-24 14:20:41 +0100506 if (*dptr)
507 talloc_free(*dptr);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200508 *dptr = argv_concat(argv, argc, 0);
Holger Hans Peter Freyther6a75d162013-07-14 09:07:11 +0200509 if (!*dptr)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200510 return CMD_WARNING;
511
512 return CMD_SUCCESS;
513}
514
515gDEFUN(cfg_no_description, cfg_no_description_cmd,
516 "no description",
517 NO_STR
518 "Remove description of the object\n")
519{
520 char **dptr = vty->index_sub;
521
522 if (!dptr) {
523 vty_out(vty, "vty->index_sub == NULL%s", VTY_NEWLINE);
524 return CMD_WARNING;
525 }
526
527 if (*dptr) {
528 talloc_free(*dptr);
529 *dptr = NULL;
530 }
531
532 return CMD_SUCCESS;
533}
534
Harald Welte28222962011-02-18 20:37:04 +0100535/* Support for configuration of log targets != the current vty */
536
537struct cmd_node cfg_log_node = {
538 CFG_LOG_NODE,
539 "%s(config-log)# ",
540 1
541};
542
Harald Welte28222962011-02-18 20:37:04 +0100543#ifdef HAVE_SYSLOG_H
544
545#include <syslog.h>
546
547static const int local_sysl_map[] = {
548 [0] = LOG_LOCAL0,
549 [1] = LOG_LOCAL1,
550 [2] = LOG_LOCAL2,
551 [3] = LOG_LOCAL3,
552 [4] = LOG_LOCAL4,
553 [5] = LOG_LOCAL5,
554 [6] = LOG_LOCAL6,
555 [7] = LOG_LOCAL7
556};
557
Harald Weltede79cee2011-02-24 23:47:57 +0100558/* From VTY core code */
559extern struct host host;
560
Harald Welte28222962011-02-18 20:37:04 +0100561static int _cfg_log_syslog(struct vty *vty, int facility)
562{
563 struct log_target *tgt;
564
565 /* First delete the old syslog target, if any */
566 tgt = log_target_find(LOG_TGT_TYPE_SYSLOG, NULL);
567 if (tgt)
568 log_target_destroy(tgt);
569
Harald Weltede79cee2011-02-24 23:47:57 +0100570 tgt = log_target_create_syslog(host.app_info->name, 0, facility);
Harald Welte28222962011-02-18 20:37:04 +0100571 if (!tgt) {
572 vty_out(vty, "%% Unable to open syslog%s", VTY_NEWLINE);
573 return CMD_WARNING;
574 }
575 log_add_target(tgt);
576
577 vty->index = tgt;
578 vty->node = CFG_LOG_NODE;
579
580 return CMD_SUCCESS;
581}
582
583DEFUN(cfg_log_syslog_local, cfg_log_syslog_local_cmd,
584 "log syslog local <0-7>",
585 LOG_STR "Logging via syslog\n" "Syslog LOCAL facility\n"
586 "Local facility number\n")
587{
588 int local = atoi(argv[0]);
589 int facility = local_sysl_map[local];
590
591 return _cfg_log_syslog(vty, facility);
592}
593
594static struct value_string sysl_level_names[] = {
595 { LOG_AUTHPRIV, "authpriv" },
596 { LOG_CRON, "cron" },
597 { LOG_DAEMON, "daemon" },
598 { LOG_FTP, "ftp" },
599 { LOG_LPR, "lpr" },
600 { LOG_MAIL, "mail" },
601 { LOG_NEWS, "news" },
602 { LOG_USER, "user" },
603 { LOG_UUCP, "uucp" },
604 /* only for value -> string conversion */
605 { LOG_LOCAL0, "local 0" },
606 { LOG_LOCAL1, "local 1" },
607 { LOG_LOCAL2, "local 2" },
608 { LOG_LOCAL3, "local 3" },
609 { LOG_LOCAL4, "local 4" },
610 { LOG_LOCAL5, "local 5" },
611 { LOG_LOCAL6, "local 6" },
612 { LOG_LOCAL7, "local 7" },
613 { 0, NULL }
614};
615
616DEFUN(cfg_log_syslog, cfg_log_syslog_cmd,
617 "log syslog (authpriv|cron|daemon|ftp|lpr|mail|news|user|uucp)",
Holger Hans Peter Freythera4463fd2011-10-03 23:17:36 +0200618 LOG_STR "Logging via syslog\n"
619 "Security/authorization messages facility\n"
620 "Clock daemon (cron/at) facility\n"
621 "General system daemon facility\n"
622 "Ftp daemon facility\n"
623 "Line printer facility\n"
624 "Mail facility\n"
625 "News facility\n"
626 "Generic facility\n"
627 "UUCP facility\n")
Harald Welte28222962011-02-18 20:37:04 +0100628{
629 int facility = get_string_value(sysl_level_names, argv[0]);
630
631 return _cfg_log_syslog(vty, facility);
632}
633
634DEFUN(cfg_no_log_syslog, cfg_no_log_syslog_cmd,
635 "no log syslog",
636 NO_STR LOG_STR "Logging via syslog\n")
637{
638 struct log_target *tgt;
639
640 tgt = log_target_find(LOG_TGT_TYPE_SYSLOG, NULL);
641 if (!tgt) {
642 vty_out(vty, "%% No syslog target found%s",
643 VTY_NEWLINE);
644 return CMD_WARNING;
645 }
646
647 log_target_destroy(tgt);
648
649 return CMD_SUCCESS;
650}
651#endif /* HAVE_SYSLOG_H */
652
Harald Welteaa00f992016-12-02 15:30:02 +0100653DEFUN(cfg_log_gsmtap, cfg_log_gsmtap_cmd,
654 "log gsmtap [HOSTNAME]",
Neels Hofmeyrfd9ec3b2016-12-11 01:48:26 +0100655 LOG_STR "Logging via GSMTAP\n"
656 "Host name to send the GSMTAP logging to (UDP port 4729)\n")
Harald Welteaa00f992016-12-02 15:30:02 +0100657{
Max2f153b52018-01-04 12:25:57 +0100658 const char *hostname = argc ? argv[0] : "127.0.0.1";
Harald Welteaa00f992016-12-02 15:30:02 +0100659 struct log_target *tgt;
660
661 tgt = log_target_find(LOG_TGT_TYPE_GSMTAP, hostname);
662 if (!tgt) {
663 tgt = log_target_create_gsmtap(hostname, GSMTAP_UDP_PORT,
664 host.app_info->name, false,
665 true);
666 if (!tgt) {
Max2f153b52018-01-04 12:25:57 +0100667 vty_out(vty, "%% Unable to create GSMTAP log for %s%s",
668 hostname, VTY_NEWLINE);
Harald Welteaa00f992016-12-02 15:30:02 +0100669 return CMD_WARNING;
670 }
671 log_add_target(tgt);
672 }
673
674 vty->index = tgt;
675 vty->node = CFG_LOG_NODE;
676
677 return CMD_SUCCESS;
678}
679
Harald Welte28222962011-02-18 20:37:04 +0100680DEFUN(cfg_log_stderr, cfg_log_stderr_cmd,
681 "log stderr",
682 LOG_STR "Logging via STDERR of the process\n")
683{
684 struct log_target *tgt;
685
686 tgt = log_target_find(LOG_TGT_TYPE_STDERR, NULL);
687 if (!tgt) {
688 tgt = log_target_create_stderr();
689 if (!tgt) {
690 vty_out(vty, "%% Unable to create stderr log%s",
691 VTY_NEWLINE);
692 return CMD_WARNING;
693 }
694 log_add_target(tgt);
695 }
696
697 vty->index = tgt;
698 vty->node = CFG_LOG_NODE;
699
700 return CMD_SUCCESS;
701}
702
703DEFUN(cfg_no_log_stderr, cfg_no_log_stderr_cmd,
704 "no log stderr",
705 NO_STR LOG_STR "Logging via STDERR of the process\n")
706{
707 struct log_target *tgt;
708
709 tgt = log_target_find(LOG_TGT_TYPE_STDERR, NULL);
710 if (!tgt) {
711 vty_out(vty, "%% No stderr logging active%s", VTY_NEWLINE);
712 return CMD_WARNING;
713 }
714
715 log_target_destroy(tgt);
716
717 return CMD_SUCCESS;
718}
719
720DEFUN(cfg_log_file, cfg_log_file_cmd,
721 "log file .FILENAME",
722 LOG_STR "Logging to text file\n" "Filename\n")
723{
724 const char *fname = argv[0];
725 struct log_target *tgt;
726
727 tgt = log_target_find(LOG_TGT_TYPE_FILE, fname);
728 if (!tgt) {
729 tgt = log_target_create_file(fname);
730 if (!tgt) {
731 vty_out(vty, "%% Unable to create file `%s'%s",
732 fname, VTY_NEWLINE);
733 return CMD_WARNING;
734 }
735 log_add_target(tgt);
736 }
737
738 vty->index = tgt;
739 vty->node = CFG_LOG_NODE;
740
741 return CMD_SUCCESS;
742}
743
744
745DEFUN(cfg_no_log_file, cfg_no_log_file_cmd,
746 "no log file .FILENAME",
747 NO_STR LOG_STR "Logging to text file\n" "Filename\n")
748{
749 const char *fname = argv[0];
750 struct log_target *tgt;
751
752 tgt = log_target_find(LOG_TGT_TYPE_FILE, fname);
753 if (!tgt) {
754 vty_out(vty, "%% No such log file `%s'%s",
755 fname, VTY_NEWLINE);
756 return CMD_WARNING;
757 }
758
759 log_target_destroy(tgt);
760
761 return CMD_SUCCESS;
762}
763
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000764DEFUN(cfg_log_alarms, cfg_log_alarms_cmd,
765 "log alarms <2-32700>",
766 LOG_STR "Logging alarms to osmo_strrb\n"
767 "Maximum number of messages to log\n")
768{
769 struct log_target *tgt;
770 unsigned int rbsize = atoi(argv[0]);
771
772 tgt = log_target_find(LOG_TGT_TYPE_STRRB, NULL);
773 if (tgt)
774 log_target_destroy(tgt);
775
776 tgt = log_target_create_rb(rbsize);
777 if (!tgt) {
778 vty_out(vty, "%% Unable to create osmo_strrb (size %u)%s",
779 rbsize, VTY_NEWLINE);
780 return CMD_WARNING;
781 }
782 log_add_target(tgt);
783
784 vty->index = tgt;
785 vty->node = CFG_LOG_NODE;
786
787 return CMD_SUCCESS;
788}
789
790DEFUN(cfg_no_log_alarms, cfg_no_log_alarms_cmd,
791 "no log alarms",
792 NO_STR LOG_STR "Logging alarms to osmo_strrb\n")
793{
794 struct log_target *tgt;
795
796 tgt = log_target_find(LOG_TGT_TYPE_STRRB, NULL);
797 if (!tgt) {
798 vty_out(vty, "%% No osmo_strrb target found%s", VTY_NEWLINE);
799 return CMD_WARNING;
800 }
801
802 log_target_destroy(tgt);
803
804 return CMD_SUCCESS;
805}
806
Harald Welte28222962011-02-18 20:37:04 +0100807static int config_write_log_single(struct vty *vty, struct log_target *tgt)
808{
809 int i;
Harald Welte28222962011-02-18 20:37:04 +0100810
811 switch (tgt->type) {
812 case LOG_TGT_TYPE_VTY:
813 return 1;
814 break;
815 case LOG_TGT_TYPE_STDERR:
816 vty_out(vty, "log stderr%s", VTY_NEWLINE);
817 break;
818 case LOG_TGT_TYPE_SYSLOG:
819#ifdef HAVE_SYSLOG_H
820 vty_out(vty, "log syslog %s%s",
821 get_value_string(sysl_level_names,
822 tgt->tgt_syslog.facility),
823 VTY_NEWLINE);
824#endif
825 break;
826 case LOG_TGT_TYPE_FILE:
827 vty_out(vty, "log file %s%s", tgt->tgt_file.fname, VTY_NEWLINE);
828 break;
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000829 case LOG_TGT_TYPE_STRRB:
830 vty_out(vty, "log alarms %zu%s",
831 log_target_rb_avail_size(tgt), VTY_NEWLINE);
832 break;
Harald Welteaa00f992016-12-02 15:30:02 +0100833 case LOG_TGT_TYPE_GSMTAP:
834 vty_out(vty, "log gsmtap %s%s",
835 tgt->tgt_gsmtap.hostname, VTY_NEWLINE);
836 break;
Harald Welte28222962011-02-18 20:37:04 +0100837 }
838
Harald Welte2da47f12012-10-22 19:31:54 +0200839 vty_out(vty, " logging filter all %u%s",
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100840 tgt->filter_map & (1 << LOG_FLT_ALL) ? 1 : 0, VTY_NEWLINE);
Harald Weltefb84f322013-06-06 07:33:54 +0200841 /* save filters outside of libosmocore, i.e. in app code */
842 if (osmo_log_info->save_fn)
843 osmo_log_info->save_fn(vty, osmo_log_info, tgt);
Harald Welte2da47f12012-10-22 19:31:54 +0200844
Harald Welte28222962011-02-18 20:37:04 +0100845 vty_out(vty, " logging color %u%s", tgt->use_color ? 1 : 0,
846 VTY_NEWLINE);
Holger Hans Peter Freyther879acef2015-01-27 11:06:51 +0100847 vty_out(vty, " logging print category %d%s",
Michael McTernan78933462015-03-20 15:29:25 +0100848 tgt->print_category ? 1 : 0, VTY_NEWLINE);
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100849 if (tgt->print_ext_timestamp)
850 vty_out(vty, " logging print extended-timestamp 1%s", VTY_NEWLINE);
851 else
852 vty_out(vty, " logging timestamp %u%s",
853 tgt->print_timestamp ? 1 : 0, VTY_NEWLINE);
Neels Hofmeyr886e5482018-01-16 01:49:37 +0100854 if (tgt->print_level)
855 vty_out(vty, " logging print level 1%s", VTY_NEWLINE);
Neels Hofmeyr22772cc2018-02-06 00:52:08 +0100856 vty_out(vty, " logging print file %s%s",
857 get_value_string(logging_print_file_args, tgt->print_filename2),
858 VTY_NEWLINE);
Harald Welte28222962011-02-18 20:37:04 +0100859
860 /* stupid old osmo logging API uses uppercase strings... */
Neels Hofmeyr7c749892018-09-07 03:01:38 +0200861 vty_out(vty, " logging level all %s%s", osmo_str_tolower(log_level_str(tgt->loglevel)),
862 VTY_NEWLINE);
Harald Welte28222962011-02-18 20:37:04 +0100863
864 for (i = 0; i < osmo_log_info->num_cat; i++) {
865 const struct log_category *cat = &tgt->categories[i];
Harald Welte28222962011-02-18 20:37:04 +0100866
Harald Welte1a02cfc2013-03-19 10:37:39 +0100867 /* skip empty entries in the array */
868 if (!osmo_log_info->cat[i].name)
869 continue;
870
Harald Welte28222962011-02-18 20:37:04 +0100871 /* stupid old osmo logging API uses uppercase strings... */
Neels Hofmeyr7c749892018-09-07 03:01:38 +0200872 vty_out(vty, " logging level %s", osmo_str_tolower(osmo_log_info->cat[i].name+1));
873 vty_out(vty, " %s%s", osmo_str_tolower(log_level_str(cat->loglevel)), VTY_NEWLINE);
Harald Welte28222962011-02-18 20:37:04 +0100874 }
875
Harald Welte28222962011-02-18 20:37:04 +0100876 return 1;
877}
878
879static int config_write_log(struct vty *vty)
880{
881 struct log_target *dbg = vty->index;
882
883 llist_for_each_entry(dbg, &osmo_log_target_list, entry)
884 config_write_log_single(vty, dbg);
885
886 return 1;
887}
888
Harald Welte11eb4b52018-06-09 17:41:31 +0200889static int log_deprecated_func(struct cmd_element *cmd, struct vty *vty, int argc, const char *argv[])
890{
891 vty_out(vty, "%% Ignoring deprecated '%s'%s", cmd->string, VTY_NEWLINE);
892 return CMD_WARNING;
893}
894
895void logging_vty_add_deprecated_subsys(void *ctx, const char *name)
896{
897 struct cmd_element *cmd = talloc_zero(ctx, struct cmd_element);
898 OSMO_ASSERT(cmd);
899 cmd->string = talloc_asprintf(cmd, "logging level %s (everything|debug|info|notice|error|fatal)",
900 name);
901 printf("%s\n", cmd->string);
902 cmd->func = log_deprecated_func;
Neels Hofmeyrba0762d2018-09-10 13:56:03 +0200903 cmd->doc = LEVEL_STR
Harald Welte11eb4b52018-06-09 17:41:31 +0200904 "Deprecated Category\n";
905 cmd->attr = CMD_ATTR_DEPRECATED;
906
907 install_element(CFG_LOG_NODE, cmd);
908}
909
Harald Welte8c648252017-10-16 15:17:03 +0200910/*! Register logging related commands to the VTY. Call this once from
911 * your application if you want to support those commands. */
Maxc65c5b42017-03-15 13:20:23 +0100912void logging_vty_add_cmds()
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200913{
914 install_element_ve(&enable_logging_cmd);
915 install_element_ve(&disable_logging_cmd);
916 install_element_ve(&logging_fltr_all_cmd);
917 install_element_ve(&logging_use_clr_cmd);
918 install_element_ve(&logging_prnt_timestamp_cmd);
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100919 install_element_ve(&logging_prnt_ext_timestamp_cmd);
920 install_element_ve(&logging_prnt_cat_cmd);
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100921 install_element_ve(&logging_prnt_cat_hex_cmd);
Neels Hofmeyr886e5482018-01-16 01:49:37 +0100922 install_element_ve(&logging_prnt_level_cmd);
Neels Hofmeyrc6fd2452018-01-16 01:57:38 +0100923 install_element_ve(&logging_prnt_file_cmd);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200924 install_element_ve(&logging_set_category_mask_cmd);
Holger Hans Peter Freyther146d1d32011-10-03 23:15:41 +0200925 install_element_ve(&logging_set_category_mask_old_cmd);
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100926
Neels Hofmeyrba0762d2018-09-10 13:56:03 +0200927 /* logging level (all|<categories>) (everything|debug|...|fatal) */
928 gen_logging_level_cmd_strs(&logging_level_cmd,
929 "(everything|" LOG_LEVEL_ARGS ")",
930 EVERYTHING_STR LOG_LEVEL_STRS);
931
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200932 install_element_ve(&logging_level_cmd);
933 install_element_ve(&show_logging_vty_cmd);
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000934 install_element_ve(&show_alarms_cmd);
Harald Welte28222962011-02-18 20:37:04 +0100935
936 install_node(&cfg_log_node, config_write_log);
Harald Weltea62648b2011-02-18 21:03:27 +0100937 install_element(CFG_LOG_NODE, &logging_fltr_all_cmd);
938 install_element(CFG_LOG_NODE, &logging_use_clr_cmd);
939 install_element(CFG_LOG_NODE, &logging_prnt_timestamp_cmd);
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100940 install_element(CFG_LOG_NODE, &logging_prnt_ext_timestamp_cmd);
941 install_element(CFG_LOG_NODE, &logging_prnt_cat_cmd);
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100942 install_element(CFG_LOG_NODE, &logging_prnt_cat_hex_cmd);
Neels Hofmeyr886e5482018-01-16 01:49:37 +0100943 install_element(CFG_LOG_NODE, &logging_prnt_level_cmd);
Neels Hofmeyrc6fd2452018-01-16 01:57:38 +0100944 install_element(CFG_LOG_NODE, &logging_prnt_file_cmd);
Harald Weltea62648b2011-02-18 21:03:27 +0100945 install_element(CFG_LOG_NODE, &logging_level_cmd);
Harald Welte28222962011-02-18 20:37:04 +0100946
947 install_element(CONFIG_NODE, &cfg_log_stderr_cmd);
948 install_element(CONFIG_NODE, &cfg_no_log_stderr_cmd);
949 install_element(CONFIG_NODE, &cfg_log_file_cmd);
950 install_element(CONFIG_NODE, &cfg_no_log_file_cmd);
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000951 install_element(CONFIG_NODE, &cfg_log_alarms_cmd);
952 install_element(CONFIG_NODE, &cfg_no_log_alarms_cmd);
Harald Welte28222962011-02-18 20:37:04 +0100953#ifdef HAVE_SYSLOG_H
954 install_element(CONFIG_NODE, &cfg_log_syslog_cmd);
955 install_element(CONFIG_NODE, &cfg_log_syslog_local_cmd);
956 install_element(CONFIG_NODE, &cfg_no_log_syslog_cmd);
957#endif
Harald Welteaa00f992016-12-02 15:30:02 +0100958 install_element(CONFIG_NODE, &cfg_log_gsmtap_cmd);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200959}