blob: 0e1782a45c34872af2d53160935da0ab0e5080a9 [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
Neels Hofmeyr9540c242018-09-12 00:20:50 +020046#define CATEGORY_ALL_STR "Deprecated alias for 'force-all'\n"
47#define FORCE_ALL_STR \
48 "Globally force all logging categories to a specific level. This is released by the" \
49 " 'no logging level force-all' command. Note: any 'logging level <category> <level>'" \
50 " commands will have no visible effect after this, until the forced level is released.\n"
51#define NO_FORCE_ALL_STR \
52 "Release any globally forced log level set with 'logging level force-all <level>'\n"
Neels Hofmeyrba0762d2018-09-10 13:56:03 +020053
Neels Hofmeyr9540c242018-09-12 00:20:50 +020054#define LOG_LEVEL_ARGS "(debug|info|notice|error|fatal)"
Neels Hofmeyrba0762d2018-09-10 13:56:03 +020055#define LOG_LEVEL_STRS \
56 "Log debug messages and higher levels\n" \
57 "Log informational messages and higher levels\n" \
58 "Log noticeable messages and higher levels\n" \
59 "Log error messages and higher levels\n" \
60 "Log only fatal messages\n"
61
Neels Hofmeyr9540c242018-09-12 00:20:50 +020062#define EVERYTHING_STR "Deprecated alias for 'no logging level force-all'\n"
Harald Welte28222962011-02-18 20:37:04 +010063
Harald Welte8c648252017-10-16 15:17:03 +020064/*! \file logging_vty.c
Neels Hofmeyr87e45502017-06-20 00:17:59 +020065 * Configuration of logging from VTY
Harald Welte96e2a002017-06-12 21:44:18 +020066 *
Harald Welte8c648252017-10-16 15:17:03 +020067 * This module implements
68 * - functions that permit configuration of the libosmocore logging
69 * framework from VTY commands in the configure -> logging node.
70 *
71 * - functions that permit logging *to* a VTY session. Basically each
72 * VTY session gets its own log target, with configurable
73 * per-subsystem log levels. This is performed internally via the
74 * \ref log_target_create_vty function.
75 *
76 * You have to call \ref logging_vty_add_cmds from your application
77 * once to enable both of the above.
78 *
Harald Welte96e2a002017-06-12 21:44:18 +020079 */
80
Harald Welte76e72ab2011-02-17 15:52:39 +010081static void _vty_output(struct log_target *tgt,
82 unsigned int level, const char *line)
Harald Welte3fb0b6f2010-05-19 19:02:52 +020083{
84 struct vty *vty = tgt->tgt_vty.vty;
85 vty_out(vty, "%s", line);
86 /* This is an ugly hack, but there is no easy way... */
87 if (strchr(line, '\n'))
88 vty_out(vty, "\r");
89}
90
91struct log_target *log_target_create_vty(struct vty *vty)
92{
93 struct log_target *target;
94
95 target = log_target_create();
96 if (!target)
97 return NULL;
98
99 target->tgt_vty.vty = vty;
100 target->output = _vty_output;
101 return target;
102}
103
Pau Espin Pedrold12f6982019-09-17 18:38:58 +0200104/*! Get tgt with log lock acquired, return and release lock with warning if tgt
105 * is not found. Lock must be released later with log_tgt_mutex_unlock().
106 */
107#define ACQUIRE_VTY_LOG_TGT_WITH_LOCK(vty, tgt) \
108 do { \
109 log_tgt_mutex_lock(); \
110 tgt = osmo_log_vty2tgt(vty); \
111 if (!(tgt)) { \
112 log_tgt_mutex_unlock(); \
113 return CMD_WARNING; \
114 } \
115 } while (0)
116
117#define RET_WITH_UNLOCK(ret) \
118 do { \
119 log_tgt_mutex_unlock(); \
120 return (ret); \
121 } while (0)
122
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200123DEFUN(enable_logging,
124 enable_logging_cmd,
125 "logging enable",
126 LOGGING_STR
127 "Enables logging to this vty\n")
128{
129 struct telnet_connection *conn;
130
131 conn = (struct telnet_connection *) vty->priv;
132 if (conn->dbg) {
133 vty_out(vty, "Logging already enabled.%s", VTY_NEWLINE);
134 return CMD_WARNING;
135 }
136
137 conn->dbg = log_target_create_vty(vty);
138 if (!conn->dbg)
139 return CMD_WARNING;
Pau Espin Pedrold12f6982019-09-17 18:38:58 +0200140 log_tgt_mutex_lock();
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200141 log_add_target(conn->dbg);
Pau Espin Pedrold12f6982019-09-17 18:38:58 +0200142 RET_WITH_UNLOCK(CMD_SUCCESS);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200143}
144
Pau Espin Pedrold12f6982019-09-17 18:38:58 +0200145/*! Get log target associated to VTY console.
146 * \param[in] vty Log target type
147 * \returns Log target (if logging enabled), NULL otherwise
148 * Must be called with mutex osmo_log_tgt_mutex held, see log_tgt_mutex_lock.
149 */
Harald Weltea62648b2011-02-18 21:03:27 +0100150struct log_target *osmo_log_vty2tgt(struct vty *vty)
151{
152 struct telnet_connection *conn;
153
154 if (vty->node == CFG_LOG_NODE)
155 return vty->index;
156
157
158 conn = (struct telnet_connection *) vty->priv;
159 if (!conn->dbg)
160 vty_out(vty, "Logging was not enabled.%s", VTY_NEWLINE);
161
162 return conn->dbg;
163}
164
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200165DEFUN(logging_fltr_all,
166 logging_fltr_all_cmd,
167 "logging filter all (0|1)",
168 LOGGING_STR FILTER_STR
169 "Do you want to log all messages?\n"
170 "Only print messages matched by other filters\n"
171 "Bypass filter and print all messages\n")
172{
Pau Espin Pedrold12f6982019-09-17 18:38:58 +0200173 struct log_target *tgt;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200174
Pau Espin Pedrold12f6982019-09-17 18:38:58 +0200175 ACQUIRE_VTY_LOG_TGT_WITH_LOCK(vty, tgt);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200176
Harald Weltea62648b2011-02-18 21:03:27 +0100177 log_set_all_filter(tgt, atoi(argv[0]));
Pau Espin Pedrold12f6982019-09-17 18:38:58 +0200178 RET_WITH_UNLOCK(CMD_SUCCESS);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200179}
180
181DEFUN(logging_use_clr,
182 logging_use_clr_cmd,
183 "logging color (0|1)",
184 LOGGING_STR "Configure color-printing for log messages\n"
185 "Don't use color for printing messages\n"
186 "Use color for printing messages\n")
187{
Pau Espin Pedrold12f6982019-09-17 18:38:58 +0200188 struct log_target *tgt;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200189
Pau Espin Pedrold12f6982019-09-17 18:38:58 +0200190 ACQUIRE_VTY_LOG_TGT_WITH_LOCK(vty, tgt);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200191
Harald Weltea62648b2011-02-18 21:03:27 +0100192 log_set_use_color(tgt, atoi(argv[0]));
Pau Espin Pedrold12f6982019-09-17 18:38:58 +0200193 RET_WITH_UNLOCK(CMD_SUCCESS);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200194}
195
196DEFUN(logging_prnt_timestamp,
197 logging_prnt_timestamp_cmd,
198 "logging timestamp (0|1)",
199 LOGGING_STR "Configure log message timestamping\n"
200 "Don't prefix each log message\n"
201 "Prefix each log message with current timestamp\n")
202{
Pau Espin Pedrold12f6982019-09-17 18:38:58 +0200203 struct log_target *tgt;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200204
Pau Espin Pedrold12f6982019-09-17 18:38:58 +0200205 ACQUIRE_VTY_LOG_TGT_WITH_LOCK(vty, tgt);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200206
Harald Weltea62648b2011-02-18 21:03:27 +0100207 log_set_print_timestamp(tgt, atoi(argv[0]));
Pau Espin Pedrold12f6982019-09-17 18:38:58 +0200208 RET_WITH_UNLOCK(CMD_SUCCESS);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200209}
210
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100211DEFUN(logging_prnt_ext_timestamp,
212 logging_prnt_ext_timestamp_cmd,
213 "logging print extended-timestamp (0|1)",
214 LOGGING_STR "Log output settings\n"
215 "Configure log message timestamping\n"
216 "Don't prefix each log message\n"
217 "Prefix each log message with current timestamp with YYYYMMDDhhmmssnnn\n")
218{
Pau Espin Pedrold12f6982019-09-17 18:38:58 +0200219 struct log_target *tgt;
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100220
Pau Espin Pedrold12f6982019-09-17 18:38:58 +0200221 ACQUIRE_VTY_LOG_TGT_WITH_LOCK(vty, tgt);
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100222
223 log_set_print_extended_timestamp(tgt, atoi(argv[0]));
Pau Espin Pedrold12f6982019-09-17 18:38:58 +0200224 RET_WITH_UNLOCK(CMD_SUCCESS);
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100225}
226
227DEFUN(logging_prnt_cat,
228 logging_prnt_cat_cmd,
229 "logging print category (0|1)",
230 LOGGING_STR "Log output settings\n"
231 "Configure log message\n"
232 "Don't prefix each log message\n"
233 "Prefix each log message with category/subsystem name\n")
234{
Pau Espin Pedrold12f6982019-09-17 18:38:58 +0200235 struct log_target *tgt;
236 ACQUIRE_VTY_LOG_TGT_WITH_LOCK(vty, tgt);
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100237
238 log_set_print_category(tgt, atoi(argv[0]));
Pau Espin Pedrold12f6982019-09-17 18:38:58 +0200239 RET_WITH_UNLOCK(CMD_SUCCESS);
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100240}
241
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100242DEFUN(logging_prnt_cat_hex,
243 logging_prnt_cat_hex_cmd,
244 "logging print category-hex (0|1)",
245 LOGGING_STR "Log output settings\n"
246 "Configure log message\n"
247 "Don't prefix each log message\n"
248 "Prefix each log message with category/subsystem nr in hex ('<000b>')\n")
249{
Pau Espin Pedrold12f6982019-09-17 18:38:58 +0200250 struct log_target *tgt;
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100251
Pau Espin Pedrold12f6982019-09-17 18:38:58 +0200252 ACQUIRE_VTY_LOG_TGT_WITH_LOCK(vty, tgt);
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100253
254 log_set_print_category_hex(tgt, atoi(argv[0]));
Pau Espin Pedrold12f6982019-09-17 18:38:58 +0200255 RET_WITH_UNLOCK(CMD_SUCCESS);
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100256}
257
Neels Hofmeyr886e5482018-01-16 01:49:37 +0100258DEFUN(logging_prnt_level,
259 logging_prnt_level_cmd,
260 "logging print level (0|1)",
261 LOGGING_STR "Log output settings\n"
262 "Configure log message\n"
263 "Don't prefix each log message\n"
264 "Prefix each log message with the log level name\n")
265{
Pau Espin Pedrold12f6982019-09-17 18:38:58 +0200266 struct log_target *tgt;
Neels Hofmeyr886e5482018-01-16 01:49:37 +0100267
Pau Espin Pedrold12f6982019-09-17 18:38:58 +0200268 ACQUIRE_VTY_LOG_TGT_WITH_LOCK(vty, tgt);
Neels Hofmeyr886e5482018-01-16 01:49:37 +0100269
270 log_set_print_level(tgt, atoi(argv[0]));
Pau Espin Pedrold12f6982019-09-17 18:38:58 +0200271 RET_WITH_UNLOCK(CMD_SUCCESS);
Neels Hofmeyr886e5482018-01-16 01:49:37 +0100272}
273
Neels Hofmeyr22772cc2018-02-06 00:52:08 +0100274static const struct value_string logging_print_file_args[] = {
275 { LOG_FILENAME_NONE, "0" },
276 { LOG_FILENAME_PATH, "1" },
277 { LOG_FILENAME_BASENAME, "basename" },
278 { 0, NULL }
279};
280
Neels Hofmeyrc6fd2452018-01-16 01:57:38 +0100281DEFUN(logging_prnt_file,
282 logging_prnt_file_cmd,
Neels Hofmeyr77ae45d2018-08-27 20:32:36 +0200283 "logging print file (0|1|basename) [last]",
Neels Hofmeyrc6fd2452018-01-16 01:57:38 +0100284 LOGGING_STR "Log output settings\n"
285 "Configure log message\n"
286 "Don't prefix each log message\n"
287 "Prefix each log message with the source file and line\n"
Neels Hofmeyr77ae45d2018-08-27 20:32:36 +0200288 "Prefix each log message with the source file's basename (strip leading paths) and line\n"
289 "Log source file info at the end of a log line. If omitted, log source file info just"
290 " before the log text.\n")
Neels Hofmeyrc6fd2452018-01-16 01:57:38 +0100291{
Pau Espin Pedrold12f6982019-09-17 18:38:58 +0200292 struct log_target *tgt;
Neels Hofmeyrc6fd2452018-01-16 01:57:38 +0100293
Pau Espin Pedrold12f6982019-09-17 18:38:58 +0200294 ACQUIRE_VTY_LOG_TGT_WITH_LOCK(vty, tgt);
Neels Hofmeyrc6fd2452018-01-16 01:57:38 +0100295
Neels Hofmeyr22772cc2018-02-06 00:52:08 +0100296 log_set_print_filename2(tgt, get_string_value(logging_print_file_args, argv[0]));
Neels Hofmeyr77ae45d2018-08-27 20:32:36 +0200297 if (argc > 1)
298 log_set_print_filename_pos(tgt, LOG_FILENAME_POS_LINE_END);
299 else
300 log_set_print_filename_pos(tgt, LOG_FILENAME_POS_HEADER_END);
Pau Espin Pedrold12f6982019-09-17 18:38:58 +0200301 RET_WITH_UNLOCK(CMD_SUCCESS);
Neels Hofmeyrc6fd2452018-01-16 01:57:38 +0100302}
303
Neels Hofmeyrba0762d2018-09-10 13:56:03 +0200304static void add_category_strings(char **cmd_str_p, char **doc_str_p,
305 const struct log_info *categories)
306{
Pau Espin Pedrolc21ba152019-08-02 20:32:34 +0200307 char buf[128];
Neels Hofmeyrba0762d2018-09-10 13:56:03 +0200308 int i;
309 for (i = 0; i < categories->num_cat; i++) {
310 if (categories->cat[i].name == NULL)
311 continue;
312 /* skip the leading 'D' in each category name, hence '+ 1' */
Pau Espin Pedrolc21ba152019-08-02 20:32:34 +0200313 osmo_str_tolower_buf(buf, sizeof(buf), categories->cat[i].name + 1);
Neels Hofmeyrba0762d2018-09-10 13:56:03 +0200314 osmo_talloc_asprintf(tall_log_ctx, *cmd_str_p, "%s%s",
Pau Espin Pedrolc21ba152019-08-02 20:32:34 +0200315 i ? "|" : "", buf);
Neels Hofmeyrba0762d2018-09-10 13:56:03 +0200316 osmo_talloc_asprintf(tall_log_ctx, *doc_str_p, "%s\n",
317 categories->cat[i].description);
318 }
319}
320
321static void gen_logging_level_cmd_strs(struct cmd_element *cmd,
322 const char *level_args, const char *level_strs)
323{
324 char *cmd_str = NULL;
325 char *doc_str = NULL;
326
327 assert_loginfo(__func__);
328
329 OSMO_ASSERT(cmd->string == NULL);
330 OSMO_ASSERT(cmd->doc == NULL);
331
Neels Hofmeyr9540c242018-09-12 00:20:50 +0200332 osmo_talloc_asprintf(tall_log_ctx, cmd_str, "logging level (");
Neels Hofmeyrba0762d2018-09-10 13:56:03 +0200333 osmo_talloc_asprintf(tall_log_ctx, doc_str,
334 LOGGING_STR
Neels Hofmeyr9540c242018-09-12 00:20:50 +0200335 LEVEL_STR);
Neels Hofmeyrba0762d2018-09-10 13:56:03 +0200336 add_category_strings(&cmd_str, &doc_str, osmo_log_info);
337 osmo_talloc_asprintf(tall_log_ctx, cmd_str, ") %s", level_args);
338 osmo_talloc_asprintf(tall_log_ctx, doc_str, "%s", level_strs);
339
Harald Weltebebec212020-07-15 12:21:29 +0200340 talloc_set_name_const(cmd_str, "vty_log_level_cmd_str");
341 talloc_set_name_const(doc_str, "vty_log_level_doc_str");
342
Neels Hofmeyrba0762d2018-09-10 13:56:03 +0200343 cmd->string = cmd_str;
344 cmd->doc = doc_str;
345}
346
Neels Hofmeyr9540c242018-09-12 00:20:50 +0200347/* logging level (<categories>) (debug|...|fatal) */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200348DEFUN(logging_level,
349 logging_level_cmd,
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100350 NULL, /* cmdstr is dynamically set in logging_vty_add_cmds(). */
351 NULL) /* same thing for helpstr. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200352{
Pau Espin Pedrold12f6982019-09-17 18:38:58 +0200353 struct log_target *tgt;
Harald Weltea62648b2011-02-18 21:03:27 +0100354 int category = log_parse_category(argv[0]);
355 int level = log_parse_level(argv[1]);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200356
Harald Weltea62648b2011-02-18 21:03:27 +0100357 if (level < 0) {
358 vty_out(vty, "Invalid level `%s'%s", argv[1], VTY_NEWLINE);
Pau Espin Pedrol57d11182020-01-02 16:21:14 +0100359 return CMD_WARNING;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200360 }
361
Harald Weltea62648b2011-02-18 21:03:27 +0100362 if (category < 0) {
363 vty_out(vty, "Invalid category `%s'%s", argv[0], VTY_NEWLINE);
Pau Espin Pedrol57d11182020-01-02 16:21:14 +0100364 return CMD_WARNING;
Harald Weltea62648b2011-02-18 21:03:27 +0100365 }
366
Pau Espin Pedrol57d11182020-01-02 16:21:14 +0100367 ACQUIRE_VTY_LOG_TGT_WITH_LOCK(vty, tgt);
368
Harald Weltea62648b2011-02-18 21:03:27 +0100369 tgt->categories[category].enabled = 1;
370 tgt->categories[category].loglevel = level;
371
Pau Espin Pedrold12f6982019-09-17 18:38:58 +0200372 RET_WITH_UNLOCK(CMD_SUCCESS);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200373}
374
Neels Hofmeyr28fc0782018-09-12 02:32:02 +0200375DEFUN(logging_level_set_all, logging_level_set_all_cmd,
376 "logging level set-all " LOG_LEVEL_ARGS,
377 LOGGING_STR LEVEL_STR
378 "Once-off set all categories to the given log level. There is no single command"
379 " to take back these changes -- each category is set to the given level, period.\n"
380 LOG_LEVEL_STRS)
381{
Pau Espin Pedrold12f6982019-09-17 18:38:58 +0200382 struct log_target *tgt;
Neels Hofmeyr28fc0782018-09-12 02:32:02 +0200383 int level = log_parse_level(argv[0]);
384 int i;
Neels Hofmeyrea6f5192018-10-01 15:51:18 +0200385
Pau Espin Pedrold12f6982019-09-17 18:38:58 +0200386 ACQUIRE_VTY_LOG_TGT_WITH_LOCK(vty, tgt);
Neels Hofmeyrea6f5192018-10-01 15:51:18 +0200387
Neels Hofmeyr28fc0782018-09-12 02:32:02 +0200388 for (i = 0; i < osmo_log_info->num_cat; i++) {
389 struct log_category *cat = &tgt->categories[i];
390 /* skip empty entries in the array */
391 if (!osmo_log_info->cat[i].name)
392 continue;
393
394 cat->enabled = 1;
395 cat->loglevel = level;
396 }
Pau Espin Pedrold12f6982019-09-17 18:38:58 +0200397 RET_WITH_UNLOCK(CMD_SUCCESS);
Neels Hofmeyr28fc0782018-09-12 02:32:02 +0200398}
399
Neels Hofmeyr9540c242018-09-12 00:20:50 +0200400/* logging level (<categories>) everything */
Neels Hofmeyr7e0686c2018-09-10 20:58:52 +0200401DEFUN_DEPRECATED(deprecated_logging_level_everything, deprecated_logging_level_everything_cmd,
402 NULL, /* cmdstr is dynamically set in logging_vty_add_cmds(). */
403 NULL) /* same thing for helpstr. */
404{
405 vty_out(vty, "%% Ignoring deprecated logging level 'everything' keyword%s", VTY_NEWLINE);
406 return CMD_SUCCESS;
407}
408
Neels Hofmeyr9540c242018-09-12 00:20:50 +0200409DEFUN(logging_level_force_all, logging_level_force_all_cmd,
410 "logging level force-all " LOG_LEVEL_ARGS,
411 LOGGING_STR LEVEL_STR FORCE_ALL_STR LOG_LEVEL_STRS)
412{
Pau Espin Pedrold12f6982019-09-17 18:38:58 +0200413 struct log_target *tgt;
Neels Hofmeyr9540c242018-09-12 00:20:50 +0200414 int level = log_parse_level(argv[0]);
Pau Espin Pedrold12f6982019-09-17 18:38:58 +0200415
416 ACQUIRE_VTY_LOG_TGT_WITH_LOCK(vty, tgt);
417
Neels Hofmeyr9540c242018-09-12 00:20:50 +0200418 log_set_log_level(tgt, level);
Pau Espin Pedrold12f6982019-09-17 18:38:58 +0200419 RET_WITH_UNLOCK(CMD_SUCCESS);
Neels Hofmeyr9540c242018-09-12 00:20:50 +0200420}
421
422DEFUN(no_logging_level_force_all, no_logging_level_force_all_cmd,
423 "no logging level force-all",
424 NO_STR LOGGING_STR LEVEL_STR NO_FORCE_ALL_STR)
425{
Pau Espin Pedrold12f6982019-09-17 18:38:58 +0200426 struct log_target *tgt;
427
428 ACQUIRE_VTY_LOG_TGT_WITH_LOCK(vty, tgt);
429
Neels Hofmeyr9540c242018-09-12 00:20:50 +0200430 log_set_log_level(tgt, 0);
Pau Espin Pedrold12f6982019-09-17 18:38:58 +0200431 RET_WITH_UNLOCK(CMD_SUCCESS);
Neels Hofmeyr9540c242018-09-12 00:20:50 +0200432}
433
434/* 'logging level all (debug|...|fatal)' */
435ALIAS_DEPRECATED(logging_level_force_all, deprecated_logging_level_all_cmd,
436 "logging level all " LOG_LEVEL_ARGS,
437 LOGGING_STR LEVEL_STR CATEGORY_ALL_STR LOG_LEVEL_STRS);
438
439/* 'logging level all everything' */
440ALIAS_DEPRECATED(no_logging_level_force_all, deprecated_logging_level_all_everything_cmd,
441 "logging level all everything",
442 LOGGING_STR LEVEL_STR CATEGORY_ALL_STR EVERYTHING_STR);
443
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200444DEFUN(logging_set_category_mask,
445 logging_set_category_mask_cmd,
Holger Hans Peter Freyther146d1d32011-10-03 23:15:41 +0200446 "logging set-log-mask MASK",
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200447 LOGGING_STR
Holger Hans Peter Freyther146d1d32011-10-03 23:15:41 +0200448 "Set the logmask of this logging target\n"
Neels Hofmeyr84ea2e02017-12-09 05:53:18 +0100449 "List of logging categories to log, e.g. 'abc:mno:xyz'. Available log categories depend on the specific"
450 " application, refer to the 'logging level' command. Optionally add individual log levels like"
451 " 'abc,1:mno,3:xyz,5', where the level numbers are"
452 " " OSMO_STRINGIFY(LOGL_DEBUG) "=" OSMO_STRINGIFY_VAL(LOGL_DEBUG)
453 " " OSMO_STRINGIFY(LOGL_INFO) "=" OSMO_STRINGIFY_VAL(LOGL_INFO)
454 " " OSMO_STRINGIFY(LOGL_NOTICE) "=" OSMO_STRINGIFY_VAL(LOGL_NOTICE)
455 " " OSMO_STRINGIFY(LOGL_ERROR) "=" OSMO_STRINGIFY_VAL(LOGL_ERROR)
456 " " OSMO_STRINGIFY(LOGL_FATAL) "=" OSMO_STRINGIFY_VAL(LOGL_FATAL)
457 "\n")
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200458{
Pau Espin Pedrold12f6982019-09-17 18:38:58 +0200459 struct log_target *tgt;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200460
Pau Espin Pedrold12f6982019-09-17 18:38:58 +0200461 ACQUIRE_VTY_LOG_TGT_WITH_LOCK(vty, tgt);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200462
Harald Weltea62648b2011-02-18 21:03:27 +0100463 log_parse_category_mask(tgt, argv[0]);
Pau Espin Pedrold12f6982019-09-17 18:38:58 +0200464 RET_WITH_UNLOCK(CMD_SUCCESS);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200465}
466
Holger Hans Peter Freyther146d1d32011-10-03 23:15:41 +0200467ALIAS_DEPRECATED(logging_set_category_mask,
468 logging_set_category_mask_old_cmd,
469 "logging set log mask MASK",
470 LOGGING_STR
471 "Decide which categories to output.\n"
Neels Hofmeyr84ea2e02017-12-09 05:53:18 +0100472 "Log commands\n" "Mask commands\n"
473 "'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 +0200474
475
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200476DEFUN(diable_logging,
477 disable_logging_cmd,
478 "logging disable",
479 LOGGING_STR
480 "Disables logging to this vty\n")
481{
Pau Espin Pedrold12f6982019-09-17 18:38:58 +0200482 struct log_target *tgt;
Harald Weltea62648b2011-02-18 21:03:27 +0100483 struct telnet_connection *conn = (struct telnet_connection *) vty->priv;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200484
Pau Espin Pedrold12f6982019-09-17 18:38:58 +0200485 ACQUIRE_VTY_LOG_TGT_WITH_LOCK(vty, tgt);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200486
Harald Weltea62648b2011-02-18 21:03:27 +0100487 log_del_target(tgt);
488 talloc_free(tgt);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200489 conn->dbg = NULL;
Harald Weltea62648b2011-02-18 21:03:27 +0100490
Pau Espin Pedrold12f6982019-09-17 18:38:58 +0200491 RET_WITH_UNLOCK(CMD_SUCCESS);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200492}
493
494static void vty_print_logtarget(struct vty *vty, const struct log_info *info,
495 const struct log_target *tgt)
496{
497 unsigned int i;
498
499 vty_out(vty, " Global Loglevel: %s%s",
500 log_level_str(tgt->loglevel), VTY_NEWLINE);
501 vty_out(vty, " Use color: %s, Print Timestamp: %s%s",
502 tgt->use_color ? "On" : "Off",
503 tgt->print_timestamp ? "On" : "Off", VTY_NEWLINE);
504
505 vty_out(vty, " Log Level specific information:%s", VTY_NEWLINE);
506
507 for (i = 0; i < info->num_cat; i++) {
508 const struct log_category *cat = &tgt->categories[i];
Daniel Willmann55363a92016-11-15 10:05:51 +0100509 /* Skip categories that were not initialized */
510 if (!info->cat[i].name)
511 continue;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200512 vty_out(vty, " %-10s %-10s %-8s %s%s",
513 info->cat[i].name+1, log_level_str(cat->loglevel),
514 cat->enabled ? "Enabled" : "Disabled",
515 info->cat[i].description,
516 VTY_NEWLINE);
517 }
Harald Welte6d2d4d62013-03-10 09:53:24 +0000518
519 vty_out(vty, " Log Filter 'ALL': %s%s",
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100520 tgt->filter_map & (1 << LOG_FLT_ALL) ? "Enabled" : "Disabled",
Harald Welte6d2d4d62013-03-10 09:53:24 +0000521 VTY_NEWLINE);
522
Harald Weltefb84f322013-06-06 07:33:54 +0200523 /* print application specific filters */
524 if (info->print_fn)
525 info->print_fn(vty, info, tgt);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200526}
527
528#define SHOW_LOG_STR "Show current logging configuration\n"
529
530DEFUN(show_logging_vty,
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000531 show_logging_vty_cmd,
532 "show logging vty",
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200533 SHOW_STR SHOW_LOG_STR
534 "Show current logging configuration for this vty\n")
535{
Pau Espin Pedrold12f6982019-09-17 18:38:58 +0200536 struct log_target *tgt;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200537
Pau Espin Pedrold12f6982019-09-17 18:38:58 +0200538 ACQUIRE_VTY_LOG_TGT_WITH_LOCK(vty, tgt);
Harald Weltea62648b2011-02-18 21:03:27 +0100539
540 vty_print_logtarget(vty, osmo_log_info, tgt);
Pau Espin Pedrold12f6982019-09-17 18:38:58 +0200541 RET_WITH_UNLOCK(CMD_SUCCESS);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200542}
543
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000544DEFUN(show_alarms,
545 show_alarms_cmd,
546 "show alarms",
547 SHOW_STR SHOW_LOG_STR
548 "Show the contents of the logging ringbuffer\n")
549{
550 int i, num_alarms;
551 struct osmo_strrb *rb;
Pau Espin Pedrold12f6982019-09-17 18:38:58 +0200552 struct log_target *tgt;
553
554 log_tgt_mutex_lock();
555 tgt = log_target_find(LOG_TGT_TYPE_STRRB, NULL);
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000556 if (!tgt) {
557 vty_out(vty, "%% No alarms, run 'log alarms <2-32700>'%s",
558 VTY_NEWLINE);
Pau Espin Pedrold12f6982019-09-17 18:38:58 +0200559 RET_WITH_UNLOCK(CMD_WARNING);
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000560 }
561
562 rb = tgt->tgt_rb.rb;
563 num_alarms = osmo_strrb_elements(rb);
564
565 vty_out(vty, "%% Showing %i alarms%s", num_alarms, VTY_NEWLINE);
566
567 for (i = 0; i < num_alarms; i++)
568 vty_out(vty, "%% %s%s", osmo_strrb_get_nth(rb, i),
569 VTY_NEWLINE);
Pau Espin Pedrold12f6982019-09-17 18:38:58 +0200570 RET_WITH_UNLOCK(CMD_SUCCESS);
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000571}
572
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200573gDEFUN(cfg_description, cfg_description_cmd,
574 "description .TEXT",
Thorsten Alteholza81055d2017-03-02 22:13:48 +0100575 "Save human-readable description of the object\n"
Holger Hans Peter Freytherc9b3e062012-07-25 13:02:49 +0200576 "Text until the end of the line\n")
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200577{
578 char **dptr = vty->index_sub;
579
580 if (!dptr) {
581 vty_out(vty, "vty->index_sub == NULL%s", VTY_NEWLINE);
582 return CMD_WARNING;
583 }
584
Holger Hans Peter Freytherff0670e2011-02-24 14:20:41 +0100585 if (*dptr)
586 talloc_free(*dptr);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200587 *dptr = argv_concat(argv, argc, 0);
Holger Hans Peter Freyther6a75d162013-07-14 09:07:11 +0200588 if (!*dptr)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200589 return CMD_WARNING;
590
591 return CMD_SUCCESS;
592}
593
594gDEFUN(cfg_no_description, cfg_no_description_cmd,
595 "no description",
596 NO_STR
597 "Remove description of the object\n")
598{
599 char **dptr = vty->index_sub;
600
601 if (!dptr) {
602 vty_out(vty, "vty->index_sub == NULL%s", VTY_NEWLINE);
603 return CMD_WARNING;
604 }
605
606 if (*dptr) {
607 talloc_free(*dptr);
608 *dptr = NULL;
609 }
610
611 return CMD_SUCCESS;
612}
613
Harald Welte28222962011-02-18 20:37:04 +0100614/* Support for configuration of log targets != the current vty */
615
616struct cmd_node cfg_log_node = {
617 CFG_LOG_NODE,
618 "%s(config-log)# ",
619 1
620};
621
Harald Welte28222962011-02-18 20:37:04 +0100622#ifdef HAVE_SYSLOG_H
623
624#include <syslog.h>
625
626static const int local_sysl_map[] = {
627 [0] = LOG_LOCAL0,
628 [1] = LOG_LOCAL1,
629 [2] = LOG_LOCAL2,
630 [3] = LOG_LOCAL3,
631 [4] = LOG_LOCAL4,
632 [5] = LOG_LOCAL5,
633 [6] = LOG_LOCAL6,
634 [7] = LOG_LOCAL7
635};
636
Harald Weltede79cee2011-02-24 23:47:57 +0100637/* From VTY core code */
638extern struct host host;
639
Harald Welte28222962011-02-18 20:37:04 +0100640static int _cfg_log_syslog(struct vty *vty, int facility)
641{
642 struct log_target *tgt;
643
Pau Espin Pedrold12f6982019-09-17 18:38:58 +0200644 log_tgt_mutex_lock();
Harald Welte28222962011-02-18 20:37:04 +0100645 /* First delete the old syslog target, if any */
646 tgt = log_target_find(LOG_TGT_TYPE_SYSLOG, NULL);
647 if (tgt)
648 log_target_destroy(tgt);
649
Harald Weltede79cee2011-02-24 23:47:57 +0100650 tgt = log_target_create_syslog(host.app_info->name, 0, facility);
Harald Welte28222962011-02-18 20:37:04 +0100651 if (!tgt) {
652 vty_out(vty, "%% Unable to open syslog%s", VTY_NEWLINE);
Pau Espin Pedrold12f6982019-09-17 18:38:58 +0200653 RET_WITH_UNLOCK(CMD_WARNING);
Harald Welte28222962011-02-18 20:37:04 +0100654 }
655 log_add_target(tgt);
656
657 vty->index = tgt;
658 vty->node = CFG_LOG_NODE;
659
Pau Espin Pedrold12f6982019-09-17 18:38:58 +0200660 RET_WITH_UNLOCK(CMD_SUCCESS);
Harald Welte28222962011-02-18 20:37:04 +0100661}
662
663DEFUN(cfg_log_syslog_local, cfg_log_syslog_local_cmd,
664 "log syslog local <0-7>",
665 LOG_STR "Logging via syslog\n" "Syslog LOCAL facility\n"
666 "Local facility number\n")
667{
668 int local = atoi(argv[0]);
669 int facility = local_sysl_map[local];
670
671 return _cfg_log_syslog(vty, facility);
672}
673
674static struct value_string sysl_level_names[] = {
675 { LOG_AUTHPRIV, "authpriv" },
676 { LOG_CRON, "cron" },
677 { LOG_DAEMON, "daemon" },
678 { LOG_FTP, "ftp" },
679 { LOG_LPR, "lpr" },
680 { LOG_MAIL, "mail" },
681 { LOG_NEWS, "news" },
682 { LOG_USER, "user" },
683 { LOG_UUCP, "uucp" },
684 /* only for value -> string conversion */
685 { LOG_LOCAL0, "local 0" },
686 { LOG_LOCAL1, "local 1" },
687 { LOG_LOCAL2, "local 2" },
688 { LOG_LOCAL3, "local 3" },
689 { LOG_LOCAL4, "local 4" },
690 { LOG_LOCAL5, "local 5" },
691 { LOG_LOCAL6, "local 6" },
692 { LOG_LOCAL7, "local 7" },
693 { 0, NULL }
694};
695
696DEFUN(cfg_log_syslog, cfg_log_syslog_cmd,
697 "log syslog (authpriv|cron|daemon|ftp|lpr|mail|news|user|uucp)",
Holger Hans Peter Freythera4463fd2011-10-03 23:17:36 +0200698 LOG_STR "Logging via syslog\n"
699 "Security/authorization messages facility\n"
700 "Clock daemon (cron/at) facility\n"
701 "General system daemon facility\n"
702 "Ftp daemon facility\n"
703 "Line printer facility\n"
704 "Mail facility\n"
705 "News facility\n"
706 "Generic facility\n"
707 "UUCP facility\n")
Harald Welte28222962011-02-18 20:37:04 +0100708{
709 int facility = get_string_value(sysl_level_names, argv[0]);
710
711 return _cfg_log_syslog(vty, facility);
712}
713
714DEFUN(cfg_no_log_syslog, cfg_no_log_syslog_cmd,
715 "no log syslog",
716 NO_STR LOG_STR "Logging via syslog\n")
717{
718 struct log_target *tgt;
719
Pau Espin Pedrold12f6982019-09-17 18:38:58 +0200720 log_tgt_mutex_lock();
Harald Welte28222962011-02-18 20:37:04 +0100721 tgt = log_target_find(LOG_TGT_TYPE_SYSLOG, NULL);
722 if (!tgt) {
723 vty_out(vty, "%% No syslog target found%s",
724 VTY_NEWLINE);
Pau Espin Pedrold12f6982019-09-17 18:38:58 +0200725 RET_WITH_UNLOCK(CMD_WARNING);
Harald Welte28222962011-02-18 20:37:04 +0100726 }
727
728 log_target_destroy(tgt);
729
Pau Espin Pedrold12f6982019-09-17 18:38:58 +0200730 RET_WITH_UNLOCK(CMD_SUCCESS);
Harald Welte28222962011-02-18 20:37:04 +0100731}
732#endif /* HAVE_SYSLOG_H */
733
Harald Welteaa00f992016-12-02 15:30:02 +0100734DEFUN(cfg_log_gsmtap, cfg_log_gsmtap_cmd,
735 "log gsmtap [HOSTNAME]",
Neels Hofmeyrfd9ec3b2016-12-11 01:48:26 +0100736 LOG_STR "Logging via GSMTAP\n"
737 "Host name to send the GSMTAP logging to (UDP port 4729)\n")
Harald Welteaa00f992016-12-02 15:30:02 +0100738{
Max2f153b52018-01-04 12:25:57 +0100739 const char *hostname = argc ? argv[0] : "127.0.0.1";
Harald Welteaa00f992016-12-02 15:30:02 +0100740 struct log_target *tgt;
741
Pau Espin Pedrold12f6982019-09-17 18:38:58 +0200742 log_tgt_mutex_lock();
Harald Welteaa00f992016-12-02 15:30:02 +0100743 tgt = log_target_find(LOG_TGT_TYPE_GSMTAP, hostname);
744 if (!tgt) {
745 tgt = log_target_create_gsmtap(hostname, GSMTAP_UDP_PORT,
746 host.app_info->name, false,
747 true);
748 if (!tgt) {
Max2f153b52018-01-04 12:25:57 +0100749 vty_out(vty, "%% Unable to create GSMTAP log for %s%s",
750 hostname, VTY_NEWLINE);
Pau Espin Pedrold12f6982019-09-17 18:38:58 +0200751 RET_WITH_UNLOCK(CMD_WARNING);
Harald Welteaa00f992016-12-02 15:30:02 +0100752 }
753 log_add_target(tgt);
754 }
755
756 vty->index = tgt;
757 vty->node = CFG_LOG_NODE;
758
Pau Espin Pedrold12f6982019-09-17 18:38:58 +0200759 RET_WITH_UNLOCK(CMD_SUCCESS);
Harald Welteaa00f992016-12-02 15:30:02 +0100760}
761
Harald Welte28222962011-02-18 20:37:04 +0100762DEFUN(cfg_log_stderr, cfg_log_stderr_cmd,
763 "log stderr",
764 LOG_STR "Logging via STDERR of the process\n")
765{
766 struct log_target *tgt;
767
Pau Espin Pedrold12f6982019-09-17 18:38:58 +0200768 log_tgt_mutex_lock();
Harald Welte28222962011-02-18 20:37:04 +0100769 tgt = log_target_find(LOG_TGT_TYPE_STDERR, NULL);
770 if (!tgt) {
771 tgt = log_target_create_stderr();
772 if (!tgt) {
773 vty_out(vty, "%% Unable to create stderr log%s",
774 VTY_NEWLINE);
Pau Espin Pedrold12f6982019-09-17 18:38:58 +0200775 RET_WITH_UNLOCK(CMD_WARNING);
Harald Welte28222962011-02-18 20:37:04 +0100776 }
777 log_add_target(tgt);
778 }
779
780 vty->index = tgt;
781 vty->node = CFG_LOG_NODE;
782
Pau Espin Pedrold12f6982019-09-17 18:38:58 +0200783 RET_WITH_UNLOCK(CMD_SUCCESS);
Harald Welte28222962011-02-18 20:37:04 +0100784}
785
786DEFUN(cfg_no_log_stderr, cfg_no_log_stderr_cmd,
787 "no log stderr",
788 NO_STR LOG_STR "Logging via STDERR of the process\n")
789{
790 struct log_target *tgt;
791
Pau Espin Pedrold12f6982019-09-17 18:38:58 +0200792 log_tgt_mutex_lock();
Harald Welte28222962011-02-18 20:37:04 +0100793 tgt = log_target_find(LOG_TGT_TYPE_STDERR, NULL);
794 if (!tgt) {
795 vty_out(vty, "%% No stderr logging active%s", VTY_NEWLINE);
Pau Espin Pedrold12f6982019-09-17 18:38:58 +0200796 RET_WITH_UNLOCK(CMD_WARNING);
Harald Welte28222962011-02-18 20:37:04 +0100797 }
798
799 log_target_destroy(tgt);
800
Pau Espin Pedrold12f6982019-09-17 18:38:58 +0200801 RET_WITH_UNLOCK(CMD_SUCCESS);
Harald Welte28222962011-02-18 20:37:04 +0100802}
803
804DEFUN(cfg_log_file, cfg_log_file_cmd,
805 "log file .FILENAME",
806 LOG_STR "Logging to text file\n" "Filename\n")
807{
808 const char *fname = argv[0];
809 struct log_target *tgt;
810
Pau Espin Pedrold12f6982019-09-17 18:38:58 +0200811 log_tgt_mutex_lock();
Harald Welte28222962011-02-18 20:37:04 +0100812 tgt = log_target_find(LOG_TGT_TYPE_FILE, fname);
813 if (!tgt) {
814 tgt = log_target_create_file(fname);
815 if (!tgt) {
816 vty_out(vty, "%% Unable to create file `%s'%s",
817 fname, VTY_NEWLINE);
Pau Espin Pedrold12f6982019-09-17 18:38:58 +0200818 RET_WITH_UNLOCK(CMD_WARNING);
Harald Welte28222962011-02-18 20:37:04 +0100819 }
820 log_add_target(tgt);
821 }
822
823 vty->index = tgt;
824 vty->node = CFG_LOG_NODE;
825
Pau Espin Pedrold12f6982019-09-17 18:38:58 +0200826 RET_WITH_UNLOCK(CMD_SUCCESS);
Harald Welte28222962011-02-18 20:37:04 +0100827}
828
829
830DEFUN(cfg_no_log_file, cfg_no_log_file_cmd,
831 "no log file .FILENAME",
832 NO_STR LOG_STR "Logging to text file\n" "Filename\n")
833{
834 const char *fname = argv[0];
835 struct log_target *tgt;
836
Pau Espin Pedrold12f6982019-09-17 18:38:58 +0200837 log_tgt_mutex_lock();
Harald Welte28222962011-02-18 20:37:04 +0100838 tgt = log_target_find(LOG_TGT_TYPE_FILE, fname);
839 if (!tgt) {
840 vty_out(vty, "%% No such log file `%s'%s",
841 fname, VTY_NEWLINE);
Pau Espin Pedrold12f6982019-09-17 18:38:58 +0200842 RET_WITH_UNLOCK(CMD_WARNING);
Harald Welte28222962011-02-18 20:37:04 +0100843 }
844
845 log_target_destroy(tgt);
846
Pau Espin Pedrold12f6982019-09-17 18:38:58 +0200847 RET_WITH_UNLOCK(CMD_SUCCESS);
Harald Welte28222962011-02-18 20:37:04 +0100848}
849
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000850DEFUN(cfg_log_alarms, cfg_log_alarms_cmd,
851 "log alarms <2-32700>",
852 LOG_STR "Logging alarms to osmo_strrb\n"
853 "Maximum number of messages to log\n")
854{
855 struct log_target *tgt;
856 unsigned int rbsize = atoi(argv[0]);
857
Pau Espin Pedrold12f6982019-09-17 18:38:58 +0200858
859 log_tgt_mutex_lock();
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000860 tgt = log_target_find(LOG_TGT_TYPE_STRRB, NULL);
861 if (tgt)
862 log_target_destroy(tgt);
863
864 tgt = log_target_create_rb(rbsize);
865 if (!tgt) {
866 vty_out(vty, "%% Unable to create osmo_strrb (size %u)%s",
867 rbsize, VTY_NEWLINE);
Pau Espin Pedrold12f6982019-09-17 18:38:58 +0200868 RET_WITH_UNLOCK(CMD_WARNING);
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000869 }
870 log_add_target(tgt);
871
872 vty->index = tgt;
873 vty->node = CFG_LOG_NODE;
874
Pau Espin Pedrold12f6982019-09-17 18:38:58 +0200875 RET_WITH_UNLOCK(CMD_SUCCESS);
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000876}
877
878DEFUN(cfg_no_log_alarms, cfg_no_log_alarms_cmd,
879 "no log alarms",
880 NO_STR LOG_STR "Logging alarms to osmo_strrb\n")
881{
882 struct log_target *tgt;
883
Pau Espin Pedrold12f6982019-09-17 18:38:58 +0200884 log_tgt_mutex_lock();
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000885 tgt = log_target_find(LOG_TGT_TYPE_STRRB, NULL);
886 if (!tgt) {
887 vty_out(vty, "%% No osmo_strrb target found%s", VTY_NEWLINE);
Pau Espin Pedrold12f6982019-09-17 18:38:58 +0200888 RET_WITH_UNLOCK(CMD_WARNING);
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000889 }
890
891 log_target_destroy(tgt);
892
Pau Espin Pedrold12f6982019-09-17 18:38:58 +0200893 RET_WITH_UNLOCK(CMD_SUCCESS);
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000894}
895
Harald Welte28222962011-02-18 20:37:04 +0100896static int config_write_log_single(struct vty *vty, struct log_target *tgt)
897{
Pau Espin Pedrolc21ba152019-08-02 20:32:34 +0200898 char level_buf[128];
Harald Welte28222962011-02-18 20:37:04 +0100899 int i;
Harald Welte28222962011-02-18 20:37:04 +0100900
901 switch (tgt->type) {
902 case LOG_TGT_TYPE_VTY:
903 return 1;
904 break;
905 case LOG_TGT_TYPE_STDERR:
906 vty_out(vty, "log stderr%s", VTY_NEWLINE);
907 break;
908 case LOG_TGT_TYPE_SYSLOG:
909#ifdef HAVE_SYSLOG_H
910 vty_out(vty, "log syslog %s%s",
911 get_value_string(sysl_level_names,
912 tgt->tgt_syslog.facility),
913 VTY_NEWLINE);
914#endif
915 break;
916 case LOG_TGT_TYPE_FILE:
917 vty_out(vty, "log file %s%s", tgt->tgt_file.fname, VTY_NEWLINE);
918 break;
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000919 case LOG_TGT_TYPE_STRRB:
920 vty_out(vty, "log alarms %zu%s",
921 log_target_rb_avail_size(tgt), VTY_NEWLINE);
922 break;
Harald Welteaa00f992016-12-02 15:30:02 +0100923 case LOG_TGT_TYPE_GSMTAP:
924 vty_out(vty, "log gsmtap %s%s",
925 tgt->tgt_gsmtap.hostname, VTY_NEWLINE);
926 break;
Harald Welte28222962011-02-18 20:37:04 +0100927 }
928
Harald Welte0d67f482018-09-25 20:16:14 +0200929 vty_out(vty, " logging filter all %u%s",
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100930 tgt->filter_map & (1 << LOG_FLT_ALL) ? 1 : 0, VTY_NEWLINE);
Harald Weltefb84f322013-06-06 07:33:54 +0200931 /* save filters outside of libosmocore, i.e. in app code */
932 if (osmo_log_info->save_fn)
933 osmo_log_info->save_fn(vty, osmo_log_info, tgt);
Harald Welte2da47f12012-10-22 19:31:54 +0200934
Harald Welte0d67f482018-09-25 20:16:14 +0200935 vty_out(vty, " logging color %u%s", tgt->use_color ? 1 : 0,
Harald Welte28222962011-02-18 20:37:04 +0100936 VTY_NEWLINE);
Vadim Yanitskiy5c4b9852019-07-28 04:36:37 +0700937 vty_out(vty, " logging print category-hex %d%s",
938 tgt->print_category_hex ? 1 : 0, VTY_NEWLINE);
Harald Welte0d67f482018-09-25 20:16:14 +0200939 vty_out(vty, " logging print category %d%s",
Michael McTernan78933462015-03-20 15:29:25 +0100940 tgt->print_category ? 1 : 0, VTY_NEWLINE);
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100941 if (tgt->print_ext_timestamp)
Harald Welte0d67f482018-09-25 20:16:14 +0200942 vty_out(vty, " logging print extended-timestamp 1%s", VTY_NEWLINE);
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100943 else
Harald Welte0d67f482018-09-25 20:16:14 +0200944 vty_out(vty, " logging timestamp %u%s",
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100945 tgt->print_timestamp ? 1 : 0, VTY_NEWLINE);
Neels Hofmeyr886e5482018-01-16 01:49:37 +0100946 if (tgt->print_level)
Harald Welte0d67f482018-09-25 20:16:14 +0200947 vty_out(vty, " logging print level 1%s", VTY_NEWLINE);
948 vty_out(vty, " logging print file %s%s",
Neels Hofmeyr22772cc2018-02-06 00:52:08 +0100949 get_value_string(logging_print_file_args, tgt->print_filename2),
950 VTY_NEWLINE);
Harald Welte28222962011-02-18 20:37:04 +0100951
Neels Hofmeyr098038a2018-09-11 23:49:13 +0200952 if (tgt->loglevel) {
953 const char *level_str = get_value_string_or_null(loglevel_strs, tgt->loglevel);
Pau Espin Pedrolc21ba152019-08-02 20:32:34 +0200954 if (!level_str) {
Neels Hofmeyr9540c242018-09-12 00:20:50 +0200955 vty_out(vty, "%% Invalid log level %u for 'force-all'%s",
956 tgt->loglevel, VTY_NEWLINE);
Pau Espin Pedrolc21ba152019-08-02 20:32:34 +0200957 } else {
958 osmo_str_tolower_buf(level_buf, sizeof(level_buf), level_str);
959 vty_out(vty, " logging level force-all %s%s", level_buf, VTY_NEWLINE);
960 }
Neels Hofmeyr098038a2018-09-11 23:49:13 +0200961 }
Harald Welte28222962011-02-18 20:37:04 +0100962
963 for (i = 0; i < osmo_log_info->num_cat; i++) {
964 const struct log_category *cat = &tgt->categories[i];
Pau Espin Pedrolc21ba152019-08-02 20:32:34 +0200965 char cat_name[128];
Neels Hofmeyr098038a2018-09-11 23:49:13 +0200966 const char *level_str;
Harald Welte28222962011-02-18 20:37:04 +0100967
Harald Welte1a02cfc2013-03-19 10:37:39 +0100968 /* skip empty entries in the array */
969 if (!osmo_log_info->cat[i].name)
970 continue;
971
Pau Espin Pedrolc21ba152019-08-02 20:32:34 +0200972 osmo_str_tolower_buf(cat_name, sizeof(cat_name), osmo_log_info->cat[i].name + 1);
Neels Hofmeyr098038a2018-09-11 23:49:13 +0200973
974 level_str = get_value_string_or_null(loglevel_strs, cat->loglevel);
975 if (!level_str) {
976 vty_out(vty, "%% Invalid log level %u for %s%s", cat->loglevel, cat_name,
977 VTY_NEWLINE);
978 continue;
979 }
980
Pau Espin Pedrolc21ba152019-08-02 20:32:34 +0200981 osmo_str_tolower_buf(level_buf, sizeof(level_buf), level_str);
Harald Welte0d67f482018-09-25 20:16:14 +0200982 vty_out(vty, " logging level %s", cat_name);
Pau Espin Pedrolc21ba152019-08-02 20:32:34 +0200983 vty_out(vty, " %s%s", level_buf, VTY_NEWLINE);
Harald Welte28222962011-02-18 20:37:04 +0100984 }
985
Harald Welte28222962011-02-18 20:37:04 +0100986 return 1;
987}
988
989static int config_write_log(struct vty *vty)
990{
Pau Espin Pedrold12f6982019-09-17 18:38:58 +0200991 log_tgt_mutex_lock();
Harald Welte28222962011-02-18 20:37:04 +0100992 struct log_target *dbg = vty->index;
993
994 llist_for_each_entry(dbg, &osmo_log_target_list, entry)
995 config_write_log_single(vty, dbg);
996
Pau Espin Pedrold12f6982019-09-17 18:38:58 +0200997 log_tgt_mutex_unlock();
Harald Welte28222962011-02-18 20:37:04 +0100998 return 1;
999}
1000
Harald Welte11eb4b52018-06-09 17:41:31 +02001001static int log_deprecated_func(struct cmd_element *cmd, struct vty *vty, int argc, const char *argv[])
1002{
1003 vty_out(vty, "%% Ignoring deprecated '%s'%s", cmd->string, VTY_NEWLINE);
Vadim Yanitskiy4abda9e2019-11-21 00:19:36 +07001004 return CMD_SUCCESS; /* Otherwise the process would terminate */
Harald Welte11eb4b52018-06-09 17:41:31 +02001005}
1006
1007void logging_vty_add_deprecated_subsys(void *ctx, const char *name)
1008{
1009 struct cmd_element *cmd = talloc_zero(ctx, struct cmd_element);
1010 OSMO_ASSERT(cmd);
Vadim Yanitskiy75c242e2019-11-21 00:06:53 +07001011 cmd->string = talloc_asprintf(cmd, "logging level %s " LOG_LEVEL_ARGS, name);
Harald Welte11eb4b52018-06-09 17:41:31 +02001012 cmd->func = log_deprecated_func;
Neels Hofmeyrba0762d2018-09-10 13:56:03 +02001013 cmd->doc = LEVEL_STR
Harald Welte11eb4b52018-06-09 17:41:31 +02001014 "Deprecated Category\n";
1015 cmd->attr = CMD_ATTR_DEPRECATED;
1016
1017 install_element(CFG_LOG_NODE, cmd);
1018}
1019
Neels Hofmeyrd0b3b9e2019-07-29 19:28:08 +02001020/* logp (<categories>) (debug|...|fatal) .LOGMESSAGE*/
1021DEFUN(vty_logp,
1022 vty_logp_cmd,
1023 NULL, /* cmdstr is dynamically set in gen_vty_logp_cmd_strs(). */
1024 NULL) /* same thing for helpstr. */
1025{
1026 int category = log_parse_category(argv[0]);
1027 int level = log_parse_level(argv[1]);
1028 char *str = argv_concat(argv, argc, 2);
1029 LOGP(category, level, "%s\n", str);
1030 return CMD_SUCCESS;
1031}
1032
1033static void gen_vty_logp_cmd_strs(struct cmd_element *cmd)
1034{
1035 char *cmd_str = NULL;
1036 char *doc_str = NULL;
1037
1038 assert_loginfo(__func__);
1039
1040 OSMO_ASSERT(cmd->string == NULL);
1041 OSMO_ASSERT(cmd->doc == NULL);
1042
1043 osmo_talloc_asprintf(tall_log_ctx, cmd_str, "logp (");
1044 osmo_talloc_asprintf(tall_log_ctx, doc_str,
1045 "Print a message on all log outputs; useful for placing markers in test logs\n");
1046 add_category_strings(&cmd_str, &doc_str, osmo_log_info);
1047 osmo_talloc_asprintf(tall_log_ctx, cmd_str, ") %s", LOG_LEVEL_ARGS);
1048 osmo_talloc_asprintf(tall_log_ctx, doc_str, "%s", LOG_LEVEL_STRS);
1049
1050 osmo_talloc_asprintf(tall_log_ctx, cmd_str, " .LOGMESSAGE");
1051 osmo_talloc_asprintf(tall_log_ctx, doc_str,
1052 "Arbitrary message to log on given category and log level\n");
1053
Harald Weltebebec212020-07-15 12:21:29 +02001054 talloc_set_name_const(cmd_str, "vty_logp_cmd_str");
1055 talloc_set_name_const(doc_str, "vty_logp_doc_str");
1056
Neels Hofmeyrd0b3b9e2019-07-29 19:28:08 +02001057 cmd->string = cmd_str;
1058 cmd->doc = doc_str;
1059}
1060
Harald Welte8c648252017-10-16 15:17:03 +02001061/*! Register logging related commands to the VTY. Call this once from
1062 * your application if you want to support those commands. */
Maxc65c5b42017-03-15 13:20:23 +01001063void logging_vty_add_cmds()
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001064{
1065 install_element_ve(&enable_logging_cmd);
1066 install_element_ve(&disable_logging_cmd);
1067 install_element_ve(&logging_fltr_all_cmd);
1068 install_element_ve(&logging_use_clr_cmd);
1069 install_element_ve(&logging_prnt_timestamp_cmd);
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +01001070 install_element_ve(&logging_prnt_ext_timestamp_cmd);
1071 install_element_ve(&logging_prnt_cat_cmd);
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +01001072 install_element_ve(&logging_prnt_cat_hex_cmd);
Neels Hofmeyr886e5482018-01-16 01:49:37 +01001073 install_element_ve(&logging_prnt_level_cmd);
Neels Hofmeyrc6fd2452018-01-16 01:57:38 +01001074 install_element_ve(&logging_prnt_file_cmd);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001075 install_element_ve(&logging_set_category_mask_cmd);
Holger Hans Peter Freyther146d1d32011-10-03 23:15:41 +02001076 install_element_ve(&logging_set_category_mask_old_cmd);
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +01001077
Neels Hofmeyr9540c242018-09-12 00:20:50 +02001078 /* logging level (<categories>) (debug|...|fatal) */
Neels Hofmeyrba0762d2018-09-10 13:56:03 +02001079 gen_logging_level_cmd_strs(&logging_level_cmd,
Neels Hofmeyr9540c242018-09-12 00:20:50 +02001080 LOG_LEVEL_ARGS,
Neels Hofmeyr7e0686c2018-09-10 20:58:52 +02001081 LOG_LEVEL_STRS);
Neels Hofmeyr9540c242018-09-12 00:20:50 +02001082 /* logging level (<categories>) everything */
Neels Hofmeyr7e0686c2018-09-10 20:58:52 +02001083 gen_logging_level_cmd_strs(&deprecated_logging_level_everything_cmd,
1084 "everything", EVERYTHING_STR);
Neels Hofmeyrba0762d2018-09-10 13:56:03 +02001085
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001086 install_element_ve(&logging_level_cmd);
Neels Hofmeyr28fc0782018-09-12 02:32:02 +02001087 install_element_ve(&logging_level_set_all_cmd);
Neels Hofmeyr9540c242018-09-12 00:20:50 +02001088 install_element_ve(&logging_level_force_all_cmd);
1089 install_element_ve(&no_logging_level_force_all_cmd);
Neels Hofmeyr7e0686c2018-09-10 20:58:52 +02001090 install_element_ve(&deprecated_logging_level_everything_cmd);
Neels Hofmeyr9540c242018-09-12 00:20:50 +02001091 install_element_ve(&deprecated_logging_level_all_cmd);
1092 install_element_ve(&deprecated_logging_level_all_everything_cmd);
Harald Welte28222962011-02-18 20:37:04 +01001093
Neels Hofmeyrd0b3b9e2019-07-29 19:28:08 +02001094 gen_vty_logp_cmd_strs(&vty_logp_cmd);
1095 install_element_ve(&vty_logp_cmd);
1096
Pau Espin Pedrol0c2a46d2019-08-20 10:39:05 +02001097 install_element_ve(&show_logging_vty_cmd);
1098 install_element_ve(&show_alarms_cmd);
1099
Harald Welte28222962011-02-18 20:37:04 +01001100 install_node(&cfg_log_node, config_write_log);
Harald Weltea62648b2011-02-18 21:03:27 +01001101 install_element(CFG_LOG_NODE, &logging_fltr_all_cmd);
1102 install_element(CFG_LOG_NODE, &logging_use_clr_cmd);
1103 install_element(CFG_LOG_NODE, &logging_prnt_timestamp_cmd);
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +01001104 install_element(CFG_LOG_NODE, &logging_prnt_ext_timestamp_cmd);
1105 install_element(CFG_LOG_NODE, &logging_prnt_cat_cmd);
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +01001106 install_element(CFG_LOG_NODE, &logging_prnt_cat_hex_cmd);
Neels Hofmeyr886e5482018-01-16 01:49:37 +01001107 install_element(CFG_LOG_NODE, &logging_prnt_level_cmd);
Neels Hofmeyrc6fd2452018-01-16 01:57:38 +01001108 install_element(CFG_LOG_NODE, &logging_prnt_file_cmd);
Harald Weltea62648b2011-02-18 21:03:27 +01001109 install_element(CFG_LOG_NODE, &logging_level_cmd);
Neels Hofmeyr28fc0782018-09-12 02:32:02 +02001110 install_element(CFG_LOG_NODE, &logging_level_set_all_cmd);
Neels Hofmeyr9540c242018-09-12 00:20:50 +02001111 install_element(CFG_LOG_NODE, &logging_level_force_all_cmd);
1112 install_element(CFG_LOG_NODE, &no_logging_level_force_all_cmd);
Neels Hofmeyr7e0686c2018-09-10 20:58:52 +02001113 install_element(CFG_LOG_NODE, &deprecated_logging_level_everything_cmd);
Neels Hofmeyr9540c242018-09-12 00:20:50 +02001114 install_element(CFG_LOG_NODE, &deprecated_logging_level_all_cmd);
1115 install_element(CFG_LOG_NODE, &deprecated_logging_level_all_everything_cmd);
Harald Welte28222962011-02-18 20:37:04 +01001116
1117 install_element(CONFIG_NODE, &cfg_log_stderr_cmd);
1118 install_element(CONFIG_NODE, &cfg_no_log_stderr_cmd);
1119 install_element(CONFIG_NODE, &cfg_log_file_cmd);
1120 install_element(CONFIG_NODE, &cfg_no_log_file_cmd);
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +00001121 install_element(CONFIG_NODE, &cfg_log_alarms_cmd);
1122 install_element(CONFIG_NODE, &cfg_no_log_alarms_cmd);
Harald Welte28222962011-02-18 20:37:04 +01001123#ifdef HAVE_SYSLOG_H
1124 install_element(CONFIG_NODE, &cfg_log_syslog_cmd);
1125 install_element(CONFIG_NODE, &cfg_log_syslog_local_cmd);
1126 install_element(CONFIG_NODE, &cfg_no_log_syslog_cmd);
1127#endif
Harald Welteaa00f992016-12-02 15:30:02 +01001128 install_element(CONFIG_NODE, &cfg_log_gsmtap_cmd);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001129}