blob: 6758dd220235d1fe01ceefd204cbdcc38416b685 [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
Neels Hofmeyr7e0686c2018-09-10 20:58:52 +0200323/* logging level (all|<categories>) (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
Harald Weltea62648b2011-02-18 21:03:27 +0100341 /* Check for special case where we want to set global log level */
342 if (!strcmp(argv[0], "all")) {
343 log_set_log_level(tgt, level);
344 return CMD_SUCCESS;
345 }
346
347 if (category < 0) {
348 vty_out(vty, "Invalid category `%s'%s", argv[0], VTY_NEWLINE);
349 return CMD_WARNING;
350 }
351
352 tgt->categories[category].enabled = 1;
353 tgt->categories[category].loglevel = level;
354
355 return CMD_SUCCESS;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200356}
357
Neels Hofmeyr7e0686c2018-09-10 20:58:52 +0200358/* logging level (all|<categories>) everything */
359DEFUN_DEPRECATED(deprecated_logging_level_everything, deprecated_logging_level_everything_cmd,
360 NULL, /* cmdstr is dynamically set in logging_vty_add_cmds(). */
361 NULL) /* same thing for helpstr. */
362{
363 vty_out(vty, "%% Ignoring deprecated logging level 'everything' keyword%s", VTY_NEWLINE);
364 return CMD_SUCCESS;
365}
366
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200367DEFUN(logging_set_category_mask,
368 logging_set_category_mask_cmd,
Holger Hans Peter Freyther146d1d32011-10-03 23:15:41 +0200369 "logging set-log-mask MASK",
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200370 LOGGING_STR
Holger Hans Peter Freyther146d1d32011-10-03 23:15:41 +0200371 "Set the logmask of this logging target\n"
Neels Hofmeyr84ea2e02017-12-09 05:53:18 +0100372 "List of logging categories to log, e.g. 'abc:mno:xyz'. Available log categories depend on the specific"
373 " application, refer to the 'logging level' command. Optionally add individual log levels like"
374 " 'abc,1:mno,3:xyz,5', where the level numbers are"
375 " " OSMO_STRINGIFY(LOGL_DEBUG) "=" OSMO_STRINGIFY_VAL(LOGL_DEBUG)
376 " " OSMO_STRINGIFY(LOGL_INFO) "=" OSMO_STRINGIFY_VAL(LOGL_INFO)
377 " " OSMO_STRINGIFY(LOGL_NOTICE) "=" OSMO_STRINGIFY_VAL(LOGL_NOTICE)
378 " " OSMO_STRINGIFY(LOGL_ERROR) "=" OSMO_STRINGIFY_VAL(LOGL_ERROR)
379 " " OSMO_STRINGIFY(LOGL_FATAL) "=" OSMO_STRINGIFY_VAL(LOGL_FATAL)
380 "\n")
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200381{
Harald Weltea62648b2011-02-18 21:03:27 +0100382 struct log_target *tgt = osmo_log_vty2tgt(vty);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200383
Harald Weltea62648b2011-02-18 21:03:27 +0100384 if (!tgt)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200385 return CMD_WARNING;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200386
Harald Weltea62648b2011-02-18 21:03:27 +0100387 log_parse_category_mask(tgt, argv[0]);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200388 return CMD_SUCCESS;
389}
390
Holger Hans Peter Freyther146d1d32011-10-03 23:15:41 +0200391ALIAS_DEPRECATED(logging_set_category_mask,
392 logging_set_category_mask_old_cmd,
393 "logging set log mask MASK",
394 LOGGING_STR
395 "Decide which categories to output.\n"
Neels Hofmeyr84ea2e02017-12-09 05:53:18 +0100396 "Log commands\n" "Mask commands\n"
397 "'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 +0200398
399
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200400DEFUN(diable_logging,
401 disable_logging_cmd,
402 "logging disable",
403 LOGGING_STR
404 "Disables logging to this vty\n")
405{
Harald Weltea62648b2011-02-18 21:03:27 +0100406 struct log_target *tgt = osmo_log_vty2tgt(vty);
407 struct telnet_connection *conn = (struct telnet_connection *) vty->priv;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200408
Harald Weltea62648b2011-02-18 21:03:27 +0100409 if (!tgt)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200410 return CMD_WARNING;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200411
Harald Weltea62648b2011-02-18 21:03:27 +0100412 log_del_target(tgt);
413 talloc_free(tgt);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200414 conn->dbg = NULL;
Harald Weltea62648b2011-02-18 21:03:27 +0100415
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200416 return CMD_SUCCESS;
417}
418
419static void vty_print_logtarget(struct vty *vty, const struct log_info *info,
420 const struct log_target *tgt)
421{
422 unsigned int i;
423
424 vty_out(vty, " Global Loglevel: %s%s",
425 log_level_str(tgt->loglevel), VTY_NEWLINE);
426 vty_out(vty, " Use color: %s, Print Timestamp: %s%s",
427 tgt->use_color ? "On" : "Off",
428 tgt->print_timestamp ? "On" : "Off", VTY_NEWLINE);
429
430 vty_out(vty, " Log Level specific information:%s", VTY_NEWLINE);
431
432 for (i = 0; i < info->num_cat; i++) {
433 const struct log_category *cat = &tgt->categories[i];
Daniel Willmann55363a92016-11-15 10:05:51 +0100434 /* Skip categories that were not initialized */
435 if (!info->cat[i].name)
436 continue;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200437 vty_out(vty, " %-10s %-10s %-8s %s%s",
438 info->cat[i].name+1, log_level_str(cat->loglevel),
439 cat->enabled ? "Enabled" : "Disabled",
440 info->cat[i].description,
441 VTY_NEWLINE);
442 }
Harald Welte6d2d4d62013-03-10 09:53:24 +0000443
444 vty_out(vty, " Log Filter 'ALL': %s%s",
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100445 tgt->filter_map & (1 << LOG_FLT_ALL) ? "Enabled" : "Disabled",
Harald Welte6d2d4d62013-03-10 09:53:24 +0000446 VTY_NEWLINE);
447
Harald Weltefb84f322013-06-06 07:33:54 +0200448 /* print application specific filters */
449 if (info->print_fn)
450 info->print_fn(vty, info, tgt);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200451}
452
453#define SHOW_LOG_STR "Show current logging configuration\n"
454
455DEFUN(show_logging_vty,
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000456 show_logging_vty_cmd,
457 "show logging vty",
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200458 SHOW_STR SHOW_LOG_STR
459 "Show current logging configuration for this vty\n")
460{
Harald Weltea62648b2011-02-18 21:03:27 +0100461 struct log_target *tgt = osmo_log_vty2tgt(vty);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200462
Harald Weltea62648b2011-02-18 21:03:27 +0100463 if (!tgt)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200464 return CMD_WARNING;
Harald Weltea62648b2011-02-18 21:03:27 +0100465
466 vty_print_logtarget(vty, osmo_log_info, tgt);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200467
468 return CMD_SUCCESS;
469}
470
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000471DEFUN(show_alarms,
472 show_alarms_cmd,
473 "show alarms",
474 SHOW_STR SHOW_LOG_STR
475 "Show the contents of the logging ringbuffer\n")
476{
477 int i, num_alarms;
478 struct osmo_strrb *rb;
479 struct log_target *tgt = log_target_find(LOG_TGT_TYPE_STRRB, NULL);
480 if (!tgt) {
481 vty_out(vty, "%% No alarms, run 'log alarms <2-32700>'%s",
482 VTY_NEWLINE);
483 return CMD_WARNING;
484 }
485
486 rb = tgt->tgt_rb.rb;
487 num_alarms = osmo_strrb_elements(rb);
488
489 vty_out(vty, "%% Showing %i alarms%s", num_alarms, VTY_NEWLINE);
490
491 for (i = 0; i < num_alarms; i++)
492 vty_out(vty, "%% %s%s", osmo_strrb_get_nth(rb, i),
493 VTY_NEWLINE);
494
495 return CMD_SUCCESS;
496}
497
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200498gDEFUN(cfg_description, cfg_description_cmd,
499 "description .TEXT",
Thorsten Alteholza81055d2017-03-02 22:13:48 +0100500 "Save human-readable description of the object\n"
Holger Hans Peter Freytherc9b3e062012-07-25 13:02:49 +0200501 "Text until the end of the line\n")
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200502{
503 char **dptr = vty->index_sub;
504
505 if (!dptr) {
506 vty_out(vty, "vty->index_sub == NULL%s", VTY_NEWLINE);
507 return CMD_WARNING;
508 }
509
Holger Hans Peter Freytherff0670e2011-02-24 14:20:41 +0100510 if (*dptr)
511 talloc_free(*dptr);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200512 *dptr = argv_concat(argv, argc, 0);
Holger Hans Peter Freyther6a75d162013-07-14 09:07:11 +0200513 if (!*dptr)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200514 return CMD_WARNING;
515
516 return CMD_SUCCESS;
517}
518
519gDEFUN(cfg_no_description, cfg_no_description_cmd,
520 "no description",
521 NO_STR
522 "Remove description of the object\n")
523{
524 char **dptr = vty->index_sub;
525
526 if (!dptr) {
527 vty_out(vty, "vty->index_sub == NULL%s", VTY_NEWLINE);
528 return CMD_WARNING;
529 }
530
531 if (*dptr) {
532 talloc_free(*dptr);
533 *dptr = NULL;
534 }
535
536 return CMD_SUCCESS;
537}
538
Harald Welte28222962011-02-18 20:37:04 +0100539/* Support for configuration of log targets != the current vty */
540
541struct cmd_node cfg_log_node = {
542 CFG_LOG_NODE,
543 "%s(config-log)# ",
544 1
545};
546
Harald Welte28222962011-02-18 20:37:04 +0100547#ifdef HAVE_SYSLOG_H
548
549#include <syslog.h>
550
551static const int local_sysl_map[] = {
552 [0] = LOG_LOCAL0,
553 [1] = LOG_LOCAL1,
554 [2] = LOG_LOCAL2,
555 [3] = LOG_LOCAL3,
556 [4] = LOG_LOCAL4,
557 [5] = LOG_LOCAL5,
558 [6] = LOG_LOCAL6,
559 [7] = LOG_LOCAL7
560};
561
Harald Weltede79cee2011-02-24 23:47:57 +0100562/* From VTY core code */
563extern struct host host;
564
Harald Welte28222962011-02-18 20:37:04 +0100565static int _cfg_log_syslog(struct vty *vty, int facility)
566{
567 struct log_target *tgt;
568
569 /* First delete the old syslog target, if any */
570 tgt = log_target_find(LOG_TGT_TYPE_SYSLOG, NULL);
571 if (tgt)
572 log_target_destroy(tgt);
573
Harald Weltede79cee2011-02-24 23:47:57 +0100574 tgt = log_target_create_syslog(host.app_info->name, 0, facility);
Harald Welte28222962011-02-18 20:37:04 +0100575 if (!tgt) {
576 vty_out(vty, "%% Unable to open syslog%s", VTY_NEWLINE);
577 return CMD_WARNING;
578 }
579 log_add_target(tgt);
580
581 vty->index = tgt;
582 vty->node = CFG_LOG_NODE;
583
584 return CMD_SUCCESS;
585}
586
587DEFUN(cfg_log_syslog_local, cfg_log_syslog_local_cmd,
588 "log syslog local <0-7>",
589 LOG_STR "Logging via syslog\n" "Syslog LOCAL facility\n"
590 "Local facility number\n")
591{
592 int local = atoi(argv[0]);
593 int facility = local_sysl_map[local];
594
595 return _cfg_log_syslog(vty, facility);
596}
597
598static struct value_string sysl_level_names[] = {
599 { LOG_AUTHPRIV, "authpriv" },
600 { LOG_CRON, "cron" },
601 { LOG_DAEMON, "daemon" },
602 { LOG_FTP, "ftp" },
603 { LOG_LPR, "lpr" },
604 { LOG_MAIL, "mail" },
605 { LOG_NEWS, "news" },
606 { LOG_USER, "user" },
607 { LOG_UUCP, "uucp" },
608 /* only for value -> string conversion */
609 { LOG_LOCAL0, "local 0" },
610 { LOG_LOCAL1, "local 1" },
611 { LOG_LOCAL2, "local 2" },
612 { LOG_LOCAL3, "local 3" },
613 { LOG_LOCAL4, "local 4" },
614 { LOG_LOCAL5, "local 5" },
615 { LOG_LOCAL6, "local 6" },
616 { LOG_LOCAL7, "local 7" },
617 { 0, NULL }
618};
619
620DEFUN(cfg_log_syslog, cfg_log_syslog_cmd,
621 "log syslog (authpriv|cron|daemon|ftp|lpr|mail|news|user|uucp)",
Holger Hans Peter Freythera4463fd2011-10-03 23:17:36 +0200622 LOG_STR "Logging via syslog\n"
623 "Security/authorization messages facility\n"
624 "Clock daemon (cron/at) facility\n"
625 "General system daemon facility\n"
626 "Ftp daemon facility\n"
627 "Line printer facility\n"
628 "Mail facility\n"
629 "News facility\n"
630 "Generic facility\n"
631 "UUCP facility\n")
Harald Welte28222962011-02-18 20:37:04 +0100632{
633 int facility = get_string_value(sysl_level_names, argv[0]);
634
635 return _cfg_log_syslog(vty, facility);
636}
637
638DEFUN(cfg_no_log_syslog, cfg_no_log_syslog_cmd,
639 "no log syslog",
640 NO_STR LOG_STR "Logging via syslog\n")
641{
642 struct log_target *tgt;
643
644 tgt = log_target_find(LOG_TGT_TYPE_SYSLOG, NULL);
645 if (!tgt) {
646 vty_out(vty, "%% No syslog target found%s",
647 VTY_NEWLINE);
648 return CMD_WARNING;
649 }
650
651 log_target_destroy(tgt);
652
653 return CMD_SUCCESS;
654}
655#endif /* HAVE_SYSLOG_H */
656
Harald Welteaa00f992016-12-02 15:30:02 +0100657DEFUN(cfg_log_gsmtap, cfg_log_gsmtap_cmd,
658 "log gsmtap [HOSTNAME]",
Neels Hofmeyrfd9ec3b2016-12-11 01:48:26 +0100659 LOG_STR "Logging via GSMTAP\n"
660 "Host name to send the GSMTAP logging to (UDP port 4729)\n")
Harald Welteaa00f992016-12-02 15:30:02 +0100661{
Max2f153b52018-01-04 12:25:57 +0100662 const char *hostname = argc ? argv[0] : "127.0.0.1";
Harald Welteaa00f992016-12-02 15:30:02 +0100663 struct log_target *tgt;
664
665 tgt = log_target_find(LOG_TGT_TYPE_GSMTAP, hostname);
666 if (!tgt) {
667 tgt = log_target_create_gsmtap(hostname, GSMTAP_UDP_PORT,
668 host.app_info->name, false,
669 true);
670 if (!tgt) {
Max2f153b52018-01-04 12:25:57 +0100671 vty_out(vty, "%% Unable to create GSMTAP log for %s%s",
672 hostname, VTY_NEWLINE);
Harald Welteaa00f992016-12-02 15:30:02 +0100673 return CMD_WARNING;
674 }
675 log_add_target(tgt);
676 }
677
678 vty->index = tgt;
679 vty->node = CFG_LOG_NODE;
680
681 return CMD_SUCCESS;
682}
683
Harald Welte28222962011-02-18 20:37:04 +0100684DEFUN(cfg_log_stderr, cfg_log_stderr_cmd,
685 "log stderr",
686 LOG_STR "Logging via STDERR of the process\n")
687{
688 struct log_target *tgt;
689
690 tgt = log_target_find(LOG_TGT_TYPE_STDERR, NULL);
691 if (!tgt) {
692 tgt = log_target_create_stderr();
693 if (!tgt) {
694 vty_out(vty, "%% Unable to create stderr log%s",
695 VTY_NEWLINE);
696 return CMD_WARNING;
697 }
698 log_add_target(tgt);
699 }
700
701 vty->index = tgt;
702 vty->node = CFG_LOG_NODE;
703
704 return CMD_SUCCESS;
705}
706
707DEFUN(cfg_no_log_stderr, cfg_no_log_stderr_cmd,
708 "no log stderr",
709 NO_STR LOG_STR "Logging via STDERR of the process\n")
710{
711 struct log_target *tgt;
712
713 tgt = log_target_find(LOG_TGT_TYPE_STDERR, NULL);
714 if (!tgt) {
715 vty_out(vty, "%% No stderr logging active%s", VTY_NEWLINE);
716 return CMD_WARNING;
717 }
718
719 log_target_destroy(tgt);
720
721 return CMD_SUCCESS;
722}
723
724DEFUN(cfg_log_file, cfg_log_file_cmd,
725 "log file .FILENAME",
726 LOG_STR "Logging to text file\n" "Filename\n")
727{
728 const char *fname = argv[0];
729 struct log_target *tgt;
730
731 tgt = log_target_find(LOG_TGT_TYPE_FILE, fname);
732 if (!tgt) {
733 tgt = log_target_create_file(fname);
734 if (!tgt) {
735 vty_out(vty, "%% Unable to create file `%s'%s",
736 fname, VTY_NEWLINE);
737 return CMD_WARNING;
738 }
739 log_add_target(tgt);
740 }
741
742 vty->index = tgt;
743 vty->node = CFG_LOG_NODE;
744
745 return CMD_SUCCESS;
746}
747
748
749DEFUN(cfg_no_log_file, cfg_no_log_file_cmd,
750 "no log file .FILENAME",
751 NO_STR LOG_STR "Logging to text file\n" "Filename\n")
752{
753 const char *fname = argv[0];
754 struct log_target *tgt;
755
756 tgt = log_target_find(LOG_TGT_TYPE_FILE, fname);
757 if (!tgt) {
758 vty_out(vty, "%% No such log file `%s'%s",
759 fname, VTY_NEWLINE);
760 return CMD_WARNING;
761 }
762
763 log_target_destroy(tgt);
764
765 return CMD_SUCCESS;
766}
767
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000768DEFUN(cfg_log_alarms, cfg_log_alarms_cmd,
769 "log alarms <2-32700>",
770 LOG_STR "Logging alarms to osmo_strrb\n"
771 "Maximum number of messages to log\n")
772{
773 struct log_target *tgt;
774 unsigned int rbsize = atoi(argv[0]);
775
776 tgt = log_target_find(LOG_TGT_TYPE_STRRB, NULL);
777 if (tgt)
778 log_target_destroy(tgt);
779
780 tgt = log_target_create_rb(rbsize);
781 if (!tgt) {
782 vty_out(vty, "%% Unable to create osmo_strrb (size %u)%s",
783 rbsize, VTY_NEWLINE);
784 return CMD_WARNING;
785 }
786 log_add_target(tgt);
787
788 vty->index = tgt;
789 vty->node = CFG_LOG_NODE;
790
791 return CMD_SUCCESS;
792}
793
794DEFUN(cfg_no_log_alarms, cfg_no_log_alarms_cmd,
795 "no log alarms",
796 NO_STR LOG_STR "Logging alarms to osmo_strrb\n")
797{
798 struct log_target *tgt;
799
800 tgt = log_target_find(LOG_TGT_TYPE_STRRB, NULL);
801 if (!tgt) {
802 vty_out(vty, "%% No osmo_strrb target found%s", VTY_NEWLINE);
803 return CMD_WARNING;
804 }
805
806 log_target_destroy(tgt);
807
808 return CMD_SUCCESS;
809}
810
Harald Welte28222962011-02-18 20:37:04 +0100811static int config_write_log_single(struct vty *vty, struct log_target *tgt)
812{
813 int i;
Harald Welte28222962011-02-18 20:37:04 +0100814
815 switch (tgt->type) {
816 case LOG_TGT_TYPE_VTY:
817 return 1;
818 break;
819 case LOG_TGT_TYPE_STDERR:
820 vty_out(vty, "log stderr%s", VTY_NEWLINE);
821 break;
822 case LOG_TGT_TYPE_SYSLOG:
823#ifdef HAVE_SYSLOG_H
824 vty_out(vty, "log syslog %s%s",
825 get_value_string(sysl_level_names,
826 tgt->tgt_syslog.facility),
827 VTY_NEWLINE);
828#endif
829 break;
830 case LOG_TGT_TYPE_FILE:
831 vty_out(vty, "log file %s%s", tgt->tgt_file.fname, VTY_NEWLINE);
832 break;
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000833 case LOG_TGT_TYPE_STRRB:
834 vty_out(vty, "log alarms %zu%s",
835 log_target_rb_avail_size(tgt), VTY_NEWLINE);
836 break;
Harald Welteaa00f992016-12-02 15:30:02 +0100837 case LOG_TGT_TYPE_GSMTAP:
838 vty_out(vty, "log gsmtap %s%s",
839 tgt->tgt_gsmtap.hostname, VTY_NEWLINE);
840 break;
Harald Welte28222962011-02-18 20:37:04 +0100841 }
842
Harald Welte2da47f12012-10-22 19:31:54 +0200843 vty_out(vty, " logging filter all %u%s",
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100844 tgt->filter_map & (1 << LOG_FLT_ALL) ? 1 : 0, VTY_NEWLINE);
Harald Weltefb84f322013-06-06 07:33:54 +0200845 /* save filters outside of libosmocore, i.e. in app code */
846 if (osmo_log_info->save_fn)
847 osmo_log_info->save_fn(vty, osmo_log_info, tgt);
Harald Welte2da47f12012-10-22 19:31:54 +0200848
Harald Welte28222962011-02-18 20:37:04 +0100849 vty_out(vty, " logging color %u%s", tgt->use_color ? 1 : 0,
850 VTY_NEWLINE);
Holger Hans Peter Freyther879acef2015-01-27 11:06:51 +0100851 vty_out(vty, " logging print category %d%s",
Michael McTernan78933462015-03-20 15:29:25 +0100852 tgt->print_category ? 1 : 0, VTY_NEWLINE);
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100853 if (tgt->print_ext_timestamp)
854 vty_out(vty, " logging print extended-timestamp 1%s", VTY_NEWLINE);
855 else
856 vty_out(vty, " logging timestamp %u%s",
857 tgt->print_timestamp ? 1 : 0, VTY_NEWLINE);
Neels Hofmeyr886e5482018-01-16 01:49:37 +0100858 if (tgt->print_level)
859 vty_out(vty, " logging print level 1%s", VTY_NEWLINE);
Neels Hofmeyr22772cc2018-02-06 00:52:08 +0100860 vty_out(vty, " logging print file %s%s",
861 get_value_string(logging_print_file_args, tgt->print_filename2),
862 VTY_NEWLINE);
Harald Welte28222962011-02-18 20:37:04 +0100863
864 /* stupid old osmo logging API uses uppercase strings... */
Neels Hofmeyr7e0686c2018-09-10 20:58:52 +0200865 if (tgt->loglevel)
866 vty_out(vty, " logging level all %s%s", osmo_str_tolower(log_level_str(tgt->loglevel)),
867 VTY_NEWLINE);
Harald Welte28222962011-02-18 20:37:04 +0100868
869 for (i = 0; i < osmo_log_info->num_cat; i++) {
870 const struct log_category *cat = &tgt->categories[i];
Harald Welte28222962011-02-18 20:37:04 +0100871
Harald Welte1a02cfc2013-03-19 10:37:39 +0100872 /* skip empty entries in the array */
873 if (!osmo_log_info->cat[i].name)
874 continue;
875
Harald Welte28222962011-02-18 20:37:04 +0100876 /* stupid old osmo logging API uses uppercase strings... */
Neels Hofmeyr7c749892018-09-07 03:01:38 +0200877 vty_out(vty, " logging level %s", osmo_str_tolower(osmo_log_info->cat[i].name+1));
878 vty_out(vty, " %s%s", osmo_str_tolower(log_level_str(cat->loglevel)), VTY_NEWLINE);
Harald Welte28222962011-02-18 20:37:04 +0100879 }
880
Harald Welte28222962011-02-18 20:37:04 +0100881 return 1;
882}
883
884static int config_write_log(struct vty *vty)
885{
886 struct log_target *dbg = vty->index;
887
888 llist_for_each_entry(dbg, &osmo_log_target_list, entry)
889 config_write_log_single(vty, dbg);
890
891 return 1;
892}
893
Harald Welte11eb4b52018-06-09 17:41:31 +0200894static int log_deprecated_func(struct cmd_element *cmd, struct vty *vty, int argc, const char *argv[])
895{
896 vty_out(vty, "%% Ignoring deprecated '%s'%s", cmd->string, VTY_NEWLINE);
897 return CMD_WARNING;
898}
899
900void logging_vty_add_deprecated_subsys(void *ctx, const char *name)
901{
902 struct cmd_element *cmd = talloc_zero(ctx, struct cmd_element);
903 OSMO_ASSERT(cmd);
Neels Hofmeyr7e0686c2018-09-10 20:58:52 +0200904 cmd->string = talloc_asprintf(cmd, "logging level %s (debug|info|notice|error|fatal)",
Harald Welte11eb4b52018-06-09 17:41:31 +0200905 name);
906 printf("%s\n", cmd->string);
907 cmd->func = log_deprecated_func;
Neels Hofmeyrba0762d2018-09-10 13:56:03 +0200908 cmd->doc = LEVEL_STR
Harald Welte11eb4b52018-06-09 17:41:31 +0200909 "Deprecated Category\n";
910 cmd->attr = CMD_ATTR_DEPRECATED;
911
912 install_element(CFG_LOG_NODE, cmd);
913}
914
Harald Welte8c648252017-10-16 15:17:03 +0200915/*! Register logging related commands to the VTY. Call this once from
916 * your application if you want to support those commands. */
Maxc65c5b42017-03-15 13:20:23 +0100917void logging_vty_add_cmds()
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200918{
919 install_element_ve(&enable_logging_cmd);
920 install_element_ve(&disable_logging_cmd);
921 install_element_ve(&logging_fltr_all_cmd);
922 install_element_ve(&logging_use_clr_cmd);
923 install_element_ve(&logging_prnt_timestamp_cmd);
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100924 install_element_ve(&logging_prnt_ext_timestamp_cmd);
925 install_element_ve(&logging_prnt_cat_cmd);
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100926 install_element_ve(&logging_prnt_cat_hex_cmd);
Neels Hofmeyr886e5482018-01-16 01:49:37 +0100927 install_element_ve(&logging_prnt_level_cmd);
Neels Hofmeyrc6fd2452018-01-16 01:57:38 +0100928 install_element_ve(&logging_prnt_file_cmd);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200929 install_element_ve(&logging_set_category_mask_cmd);
Holger Hans Peter Freyther146d1d32011-10-03 23:15:41 +0200930 install_element_ve(&logging_set_category_mask_old_cmd);
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100931
Neels Hofmeyr7e0686c2018-09-10 20:58:52 +0200932 /* logging level (all|<categories>) (debug|...|fatal) */
Neels Hofmeyrba0762d2018-09-10 13:56:03 +0200933 gen_logging_level_cmd_strs(&logging_level_cmd,
Neels Hofmeyr7e0686c2018-09-10 20:58:52 +0200934 "(" LOG_LEVEL_ARGS ")",
935 LOG_LEVEL_STRS);
936 /* logging level (all|<categories>) everything */
937 gen_logging_level_cmd_strs(&deprecated_logging_level_everything_cmd,
938 "everything", EVERYTHING_STR);
Neels Hofmeyrba0762d2018-09-10 13:56:03 +0200939
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200940 install_element_ve(&logging_level_cmd);
Neels Hofmeyr7e0686c2018-09-10 20:58:52 +0200941 install_element_ve(&deprecated_logging_level_everything_cmd);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200942 install_element_ve(&show_logging_vty_cmd);
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000943 install_element_ve(&show_alarms_cmd);
Harald Welte28222962011-02-18 20:37:04 +0100944
945 install_node(&cfg_log_node, config_write_log);
Harald Weltea62648b2011-02-18 21:03:27 +0100946 install_element(CFG_LOG_NODE, &logging_fltr_all_cmd);
947 install_element(CFG_LOG_NODE, &logging_use_clr_cmd);
948 install_element(CFG_LOG_NODE, &logging_prnt_timestamp_cmd);
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100949 install_element(CFG_LOG_NODE, &logging_prnt_ext_timestamp_cmd);
950 install_element(CFG_LOG_NODE, &logging_prnt_cat_cmd);
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100951 install_element(CFG_LOG_NODE, &logging_prnt_cat_hex_cmd);
Neels Hofmeyr886e5482018-01-16 01:49:37 +0100952 install_element(CFG_LOG_NODE, &logging_prnt_level_cmd);
Neels Hofmeyrc6fd2452018-01-16 01:57:38 +0100953 install_element(CFG_LOG_NODE, &logging_prnt_file_cmd);
Harald Weltea62648b2011-02-18 21:03:27 +0100954 install_element(CFG_LOG_NODE, &logging_level_cmd);
Neels Hofmeyr7e0686c2018-09-10 20:58:52 +0200955 install_element(CFG_LOG_NODE, &deprecated_logging_level_everything_cmd);
Harald Welte28222962011-02-18 20:37:04 +0100956
957 install_element(CONFIG_NODE, &cfg_log_stderr_cmd);
958 install_element(CONFIG_NODE, &cfg_no_log_stderr_cmd);
959 install_element(CONFIG_NODE, &cfg_log_file_cmd);
960 install_element(CONFIG_NODE, &cfg_no_log_file_cmd);
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000961 install_element(CONFIG_NODE, &cfg_log_alarms_cmd);
962 install_element(CONFIG_NODE, &cfg_no_log_alarms_cmd);
Harald Welte28222962011-02-18 20:37:04 +0100963#ifdef HAVE_SYSLOG_H
964 install_element(CONFIG_NODE, &cfg_log_syslog_cmd);
965 install_element(CONFIG_NODE, &cfg_log_syslog_local_cmd);
966 install_element(CONFIG_NODE, &cfg_no_log_syslog_cmd);
967#endif
Harald Welteaa00f992016-12-02 15:30:02 +0100968 install_element(CONFIG_NODE, &cfg_log_gsmtap_cmd);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200969}