blob: 243d6ebdf0971da113f9a4fbf2e2a143ce9d01a3 [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
104DEFUN(enable_logging,
105 enable_logging_cmd,
106 "logging enable",
107 LOGGING_STR
108 "Enables logging to this vty\n")
109{
110 struct telnet_connection *conn;
111
112 conn = (struct telnet_connection *) vty->priv;
113 if (conn->dbg) {
114 vty_out(vty, "Logging already enabled.%s", VTY_NEWLINE);
115 return CMD_WARNING;
116 }
117
118 conn->dbg = log_target_create_vty(vty);
119 if (!conn->dbg)
120 return CMD_WARNING;
121
122 log_add_target(conn->dbg);
123 return CMD_SUCCESS;
124}
125
Harald Weltea62648b2011-02-18 21:03:27 +0100126struct log_target *osmo_log_vty2tgt(struct vty *vty)
127{
128 struct telnet_connection *conn;
129
130 if (vty->node == CFG_LOG_NODE)
131 return vty->index;
132
133
134 conn = (struct telnet_connection *) vty->priv;
135 if (!conn->dbg)
136 vty_out(vty, "Logging was not enabled.%s", VTY_NEWLINE);
137
138 return conn->dbg;
139}
140
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200141DEFUN(logging_fltr_all,
142 logging_fltr_all_cmd,
143 "logging filter all (0|1)",
144 LOGGING_STR FILTER_STR
145 "Do you want to log all messages?\n"
146 "Only print messages matched by other filters\n"
147 "Bypass filter and print all messages\n")
148{
Harald Weltea62648b2011-02-18 21:03:27 +0100149 struct log_target *tgt = osmo_log_vty2tgt(vty);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200150
Harald Weltea62648b2011-02-18 21:03:27 +0100151 if (!tgt)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200152 return CMD_WARNING;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200153
Harald Weltea62648b2011-02-18 21:03:27 +0100154 log_set_all_filter(tgt, atoi(argv[0]));
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200155 return CMD_SUCCESS;
156}
157
158DEFUN(logging_use_clr,
159 logging_use_clr_cmd,
160 "logging color (0|1)",
161 LOGGING_STR "Configure color-printing for log messages\n"
162 "Don't use color for printing messages\n"
163 "Use color for printing messages\n")
164{
Harald Weltea62648b2011-02-18 21:03:27 +0100165 struct log_target *tgt = osmo_log_vty2tgt(vty);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200166
Harald Weltea62648b2011-02-18 21:03:27 +0100167 if (!tgt)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200168 return CMD_WARNING;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200169
Harald Weltea62648b2011-02-18 21:03:27 +0100170 log_set_use_color(tgt, atoi(argv[0]));
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200171 return CMD_SUCCESS;
172}
173
174DEFUN(logging_prnt_timestamp,
175 logging_prnt_timestamp_cmd,
176 "logging timestamp (0|1)",
177 LOGGING_STR "Configure log message timestamping\n"
178 "Don't prefix each log message\n"
179 "Prefix each log message with current timestamp\n")
180{
Harald Weltea62648b2011-02-18 21:03:27 +0100181 struct log_target *tgt = osmo_log_vty2tgt(vty);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200182
Harald Weltea62648b2011-02-18 21:03:27 +0100183 if (!tgt)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200184 return CMD_WARNING;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200185
Harald Weltea62648b2011-02-18 21:03:27 +0100186 log_set_print_timestamp(tgt, atoi(argv[0]));
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200187 return CMD_SUCCESS;
188}
189
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100190DEFUN(logging_prnt_ext_timestamp,
191 logging_prnt_ext_timestamp_cmd,
192 "logging print extended-timestamp (0|1)",
193 LOGGING_STR "Log output settings\n"
194 "Configure log message timestamping\n"
195 "Don't prefix each log message\n"
196 "Prefix each log message with current timestamp with YYYYMMDDhhmmssnnn\n")
197{
198 struct log_target *tgt = osmo_log_vty2tgt(vty);
199
200 if (!tgt)
201 return CMD_WARNING;
202
203 log_set_print_extended_timestamp(tgt, atoi(argv[0]));
204 return CMD_SUCCESS;
205}
206
207DEFUN(logging_prnt_cat,
208 logging_prnt_cat_cmd,
209 "logging print category (0|1)",
210 LOGGING_STR "Log output settings\n"
211 "Configure log message\n"
212 "Don't prefix each log message\n"
213 "Prefix each log message with category/subsystem name\n")
214{
215 struct log_target *tgt = osmo_log_vty2tgt(vty);
216
217 if (!tgt)
218 return CMD_WARNING;
219
220 log_set_print_category(tgt, atoi(argv[0]));
221 return CMD_SUCCESS;
222}
223
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100224DEFUN(logging_prnt_cat_hex,
225 logging_prnt_cat_hex_cmd,
226 "logging print category-hex (0|1)",
227 LOGGING_STR "Log output settings\n"
228 "Configure log message\n"
229 "Don't prefix each log message\n"
230 "Prefix each log message with category/subsystem nr in hex ('<000b>')\n")
231{
232 struct log_target *tgt = osmo_log_vty2tgt(vty);
233
234 if (!tgt)
235 return CMD_WARNING;
236
237 log_set_print_category_hex(tgt, atoi(argv[0]));
238 return CMD_SUCCESS;
239}
240
Neels Hofmeyr886e5482018-01-16 01:49:37 +0100241DEFUN(logging_prnt_level,
242 logging_prnt_level_cmd,
243 "logging print level (0|1)",
244 LOGGING_STR "Log output settings\n"
245 "Configure log message\n"
246 "Don't prefix each log message\n"
247 "Prefix each log message with the log level name\n")
248{
249 struct log_target *tgt = osmo_log_vty2tgt(vty);
250
251 if (!tgt)
252 return CMD_WARNING;
253
254 log_set_print_level(tgt, atoi(argv[0]));
255 return CMD_SUCCESS;
256}
257
Neels Hofmeyr22772cc2018-02-06 00:52:08 +0100258static const struct value_string logging_print_file_args[] = {
259 { LOG_FILENAME_NONE, "0" },
260 { LOG_FILENAME_PATH, "1" },
261 { LOG_FILENAME_BASENAME, "basename" },
262 { 0, NULL }
263};
264
Neels Hofmeyrc6fd2452018-01-16 01:57:38 +0100265DEFUN(logging_prnt_file,
266 logging_prnt_file_cmd,
Neels Hofmeyr77ae45d2018-08-27 20:32:36 +0200267 "logging print file (0|1|basename) [last]",
Neels Hofmeyrc6fd2452018-01-16 01:57:38 +0100268 LOGGING_STR "Log output settings\n"
269 "Configure log message\n"
270 "Don't prefix each log message\n"
271 "Prefix each log message with the source file and line\n"
Neels Hofmeyr77ae45d2018-08-27 20:32:36 +0200272 "Prefix each log message with the source file's basename (strip leading paths) and line\n"
273 "Log source file info at the end of a log line. If omitted, log source file info just"
274 " before the log text.\n")
Neels Hofmeyrc6fd2452018-01-16 01:57:38 +0100275{
276 struct log_target *tgt = osmo_log_vty2tgt(vty);
Neels Hofmeyrc6fd2452018-01-16 01:57:38 +0100277
278 if (!tgt)
279 return CMD_WARNING;
280
Neels Hofmeyr22772cc2018-02-06 00:52:08 +0100281 log_set_print_filename2(tgt, get_string_value(logging_print_file_args, argv[0]));
Neels Hofmeyr77ae45d2018-08-27 20:32:36 +0200282 if (argc > 1)
283 log_set_print_filename_pos(tgt, LOG_FILENAME_POS_LINE_END);
284 else
285 log_set_print_filename_pos(tgt, LOG_FILENAME_POS_HEADER_END);
Neels Hofmeyrc6fd2452018-01-16 01:57:38 +0100286 return CMD_SUCCESS;
287}
288
Neels Hofmeyrba0762d2018-09-10 13:56:03 +0200289static void add_category_strings(char **cmd_str_p, char **doc_str_p,
290 const struct log_info *categories)
291{
Pau Espin Pedrolc21ba152019-08-02 20:32:34 +0200292 char buf[128];
Neels Hofmeyrba0762d2018-09-10 13:56:03 +0200293 int i;
294 for (i = 0; i < categories->num_cat; i++) {
295 if (categories->cat[i].name == NULL)
296 continue;
297 /* skip the leading 'D' in each category name, hence '+ 1' */
Pau Espin Pedrolc21ba152019-08-02 20:32:34 +0200298 osmo_str_tolower_buf(buf, sizeof(buf), categories->cat[i].name + 1);
Neels Hofmeyrba0762d2018-09-10 13:56:03 +0200299 osmo_talloc_asprintf(tall_log_ctx, *cmd_str_p, "%s%s",
Pau Espin Pedrolc21ba152019-08-02 20:32:34 +0200300 i ? "|" : "", buf);
Neels Hofmeyrba0762d2018-09-10 13:56:03 +0200301 osmo_talloc_asprintf(tall_log_ctx, *doc_str_p, "%s\n",
302 categories->cat[i].description);
303 }
304}
305
306static void gen_logging_level_cmd_strs(struct cmd_element *cmd,
307 const char *level_args, const char *level_strs)
308{
309 char *cmd_str = NULL;
310 char *doc_str = NULL;
311
312 assert_loginfo(__func__);
313
314 OSMO_ASSERT(cmd->string == NULL);
315 OSMO_ASSERT(cmd->doc == NULL);
316
Neels Hofmeyr9540c242018-09-12 00:20:50 +0200317 osmo_talloc_asprintf(tall_log_ctx, cmd_str, "logging level (");
Neels Hofmeyrba0762d2018-09-10 13:56:03 +0200318 osmo_talloc_asprintf(tall_log_ctx, doc_str,
319 LOGGING_STR
Neels Hofmeyr9540c242018-09-12 00:20:50 +0200320 LEVEL_STR);
Neels Hofmeyrba0762d2018-09-10 13:56:03 +0200321 add_category_strings(&cmd_str, &doc_str, osmo_log_info);
322 osmo_talloc_asprintf(tall_log_ctx, cmd_str, ") %s", level_args);
323 osmo_talloc_asprintf(tall_log_ctx, doc_str, "%s", level_strs);
324
325 cmd->string = cmd_str;
326 cmd->doc = doc_str;
327}
328
Neels Hofmeyr9540c242018-09-12 00:20:50 +0200329/* logging level (<categories>) (debug|...|fatal) */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200330DEFUN(logging_level,
331 logging_level_cmd,
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100332 NULL, /* cmdstr is dynamically set in logging_vty_add_cmds(). */
333 NULL) /* same thing for helpstr. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200334{
Harald Weltea62648b2011-02-18 21:03:27 +0100335 int category = log_parse_category(argv[0]);
336 int level = log_parse_level(argv[1]);
337 struct log_target *tgt = osmo_log_vty2tgt(vty);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200338
Harald Weltea62648b2011-02-18 21:03:27 +0100339 if (!tgt)
340 return CMD_WARNING;
341
342 if (level < 0) {
343 vty_out(vty, "Invalid level `%s'%s", argv[1], VTY_NEWLINE);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200344 return CMD_WARNING;
345 }
346
Harald Weltea62648b2011-02-18 21:03:27 +0100347 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 Hofmeyr28fc0782018-09-12 02:32:02 +0200358DEFUN(logging_level_set_all, logging_level_set_all_cmd,
359 "logging level set-all " LOG_LEVEL_ARGS,
360 LOGGING_STR LEVEL_STR
361 "Once-off set all categories to the given log level. There is no single command"
362 " to take back these changes -- each category is set to the given level, period.\n"
363 LOG_LEVEL_STRS)
364{
365 struct log_target *tgt = osmo_log_vty2tgt(vty);
366 int level = log_parse_level(argv[0]);
367 int i;
Neels Hofmeyrea6f5192018-10-01 15:51:18 +0200368
369 if (!tgt)
370 return CMD_WARNING;
371
Neels Hofmeyr28fc0782018-09-12 02:32:02 +0200372 for (i = 0; i < osmo_log_info->num_cat; i++) {
373 struct log_category *cat = &tgt->categories[i];
374 /* skip empty entries in the array */
375 if (!osmo_log_info->cat[i].name)
376 continue;
377
378 cat->enabled = 1;
379 cat->loglevel = level;
380 }
381 return CMD_SUCCESS;
382}
383
Neels Hofmeyr9540c242018-09-12 00:20:50 +0200384/* logging level (<categories>) everything */
Neels Hofmeyr7e0686c2018-09-10 20:58:52 +0200385DEFUN_DEPRECATED(deprecated_logging_level_everything, deprecated_logging_level_everything_cmd,
386 NULL, /* cmdstr is dynamically set in logging_vty_add_cmds(). */
387 NULL) /* same thing for helpstr. */
388{
389 vty_out(vty, "%% Ignoring deprecated logging level 'everything' keyword%s", VTY_NEWLINE);
390 return CMD_SUCCESS;
391}
392
Neels Hofmeyr9540c242018-09-12 00:20:50 +0200393DEFUN(logging_level_force_all, logging_level_force_all_cmd,
394 "logging level force-all " LOG_LEVEL_ARGS,
395 LOGGING_STR LEVEL_STR FORCE_ALL_STR LOG_LEVEL_STRS)
396{
397 struct log_target *tgt = osmo_log_vty2tgt(vty);
398 int level = log_parse_level(argv[0]);
Neels Hofmeyrea6f5192018-10-01 15:51:18 +0200399 if (!tgt)
400 return CMD_WARNING;
Neels Hofmeyr9540c242018-09-12 00:20:50 +0200401 log_set_log_level(tgt, level);
402 return CMD_SUCCESS;
403}
404
405DEFUN(no_logging_level_force_all, no_logging_level_force_all_cmd,
406 "no logging level force-all",
407 NO_STR LOGGING_STR LEVEL_STR NO_FORCE_ALL_STR)
408{
409 struct log_target *tgt = osmo_log_vty2tgt(vty);
Neels Hofmeyrea6f5192018-10-01 15:51:18 +0200410 if (!tgt)
411 return CMD_WARNING;
Neels Hofmeyr9540c242018-09-12 00:20:50 +0200412 log_set_log_level(tgt, 0);
413 return CMD_SUCCESS;
414}
415
416/* 'logging level all (debug|...|fatal)' */
417ALIAS_DEPRECATED(logging_level_force_all, deprecated_logging_level_all_cmd,
418 "logging level all " LOG_LEVEL_ARGS,
419 LOGGING_STR LEVEL_STR CATEGORY_ALL_STR LOG_LEVEL_STRS);
420
421/* 'logging level all everything' */
422ALIAS_DEPRECATED(no_logging_level_force_all, deprecated_logging_level_all_everything_cmd,
423 "logging level all everything",
424 LOGGING_STR LEVEL_STR CATEGORY_ALL_STR EVERYTHING_STR);
425
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200426DEFUN(logging_set_category_mask,
427 logging_set_category_mask_cmd,
Holger Hans Peter Freyther146d1d32011-10-03 23:15:41 +0200428 "logging set-log-mask MASK",
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200429 LOGGING_STR
Holger Hans Peter Freyther146d1d32011-10-03 23:15:41 +0200430 "Set the logmask of this logging target\n"
Neels Hofmeyr84ea2e02017-12-09 05:53:18 +0100431 "List of logging categories to log, e.g. 'abc:mno:xyz'. Available log categories depend on the specific"
432 " application, refer to the 'logging level' command. Optionally add individual log levels like"
433 " 'abc,1:mno,3:xyz,5', where the level numbers are"
434 " " OSMO_STRINGIFY(LOGL_DEBUG) "=" OSMO_STRINGIFY_VAL(LOGL_DEBUG)
435 " " OSMO_STRINGIFY(LOGL_INFO) "=" OSMO_STRINGIFY_VAL(LOGL_INFO)
436 " " OSMO_STRINGIFY(LOGL_NOTICE) "=" OSMO_STRINGIFY_VAL(LOGL_NOTICE)
437 " " OSMO_STRINGIFY(LOGL_ERROR) "=" OSMO_STRINGIFY_VAL(LOGL_ERROR)
438 " " OSMO_STRINGIFY(LOGL_FATAL) "=" OSMO_STRINGIFY_VAL(LOGL_FATAL)
439 "\n")
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200440{
Harald Weltea62648b2011-02-18 21:03:27 +0100441 struct log_target *tgt = osmo_log_vty2tgt(vty);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200442
Harald Weltea62648b2011-02-18 21:03:27 +0100443 if (!tgt)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200444 return CMD_WARNING;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200445
Harald Weltea62648b2011-02-18 21:03:27 +0100446 log_parse_category_mask(tgt, argv[0]);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200447 return CMD_SUCCESS;
448}
449
Holger Hans Peter Freyther146d1d32011-10-03 23:15:41 +0200450ALIAS_DEPRECATED(logging_set_category_mask,
451 logging_set_category_mask_old_cmd,
452 "logging set log mask MASK",
453 LOGGING_STR
454 "Decide which categories to output.\n"
Neels Hofmeyr84ea2e02017-12-09 05:53:18 +0100455 "Log commands\n" "Mask commands\n"
456 "'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 +0200457
458
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200459DEFUN(diable_logging,
460 disable_logging_cmd,
461 "logging disable",
462 LOGGING_STR
463 "Disables logging to this vty\n")
464{
Harald Weltea62648b2011-02-18 21:03:27 +0100465 struct log_target *tgt = osmo_log_vty2tgt(vty);
466 struct telnet_connection *conn = (struct telnet_connection *) vty->priv;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200467
Harald Weltea62648b2011-02-18 21:03:27 +0100468 if (!tgt)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200469 return CMD_WARNING;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200470
Harald Weltea62648b2011-02-18 21:03:27 +0100471 log_del_target(tgt);
472 talloc_free(tgt);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200473 conn->dbg = NULL;
Harald Weltea62648b2011-02-18 21:03:27 +0100474
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200475 return CMD_SUCCESS;
476}
477
478static void vty_print_logtarget(struct vty *vty, const struct log_info *info,
479 const struct log_target *tgt)
480{
481 unsigned int i;
482
483 vty_out(vty, " Global Loglevel: %s%s",
484 log_level_str(tgt->loglevel), VTY_NEWLINE);
485 vty_out(vty, " Use color: %s, Print Timestamp: %s%s",
486 tgt->use_color ? "On" : "Off",
487 tgt->print_timestamp ? "On" : "Off", VTY_NEWLINE);
488
489 vty_out(vty, " Log Level specific information:%s", VTY_NEWLINE);
490
491 for (i = 0; i < info->num_cat; i++) {
492 const struct log_category *cat = &tgt->categories[i];
Daniel Willmann55363a92016-11-15 10:05:51 +0100493 /* Skip categories that were not initialized */
494 if (!info->cat[i].name)
495 continue;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200496 vty_out(vty, " %-10s %-10s %-8s %s%s",
497 info->cat[i].name+1, log_level_str(cat->loglevel),
498 cat->enabled ? "Enabled" : "Disabled",
499 info->cat[i].description,
500 VTY_NEWLINE);
501 }
Harald Welte6d2d4d62013-03-10 09:53:24 +0000502
503 vty_out(vty, " Log Filter 'ALL': %s%s",
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100504 tgt->filter_map & (1 << LOG_FLT_ALL) ? "Enabled" : "Disabled",
Harald Welte6d2d4d62013-03-10 09:53:24 +0000505 VTY_NEWLINE);
506
Harald Weltefb84f322013-06-06 07:33:54 +0200507 /* print application specific filters */
508 if (info->print_fn)
509 info->print_fn(vty, info, tgt);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200510}
511
512#define SHOW_LOG_STR "Show current logging configuration\n"
513
514DEFUN(show_logging_vty,
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000515 show_logging_vty_cmd,
516 "show logging vty",
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200517 SHOW_STR SHOW_LOG_STR
518 "Show current logging configuration for this vty\n")
519{
Harald Weltea62648b2011-02-18 21:03:27 +0100520 struct log_target *tgt = osmo_log_vty2tgt(vty);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200521
Harald Weltea62648b2011-02-18 21:03:27 +0100522 if (!tgt)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200523 return CMD_WARNING;
Harald Weltea62648b2011-02-18 21:03:27 +0100524
525 vty_print_logtarget(vty, osmo_log_info, tgt);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200526
527 return CMD_SUCCESS;
528}
529
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000530DEFUN(show_alarms,
531 show_alarms_cmd,
532 "show alarms",
533 SHOW_STR SHOW_LOG_STR
534 "Show the contents of the logging ringbuffer\n")
535{
536 int i, num_alarms;
537 struct osmo_strrb *rb;
538 struct log_target *tgt = log_target_find(LOG_TGT_TYPE_STRRB, NULL);
539 if (!tgt) {
540 vty_out(vty, "%% No alarms, run 'log alarms <2-32700>'%s",
541 VTY_NEWLINE);
542 return CMD_WARNING;
543 }
544
545 rb = tgt->tgt_rb.rb;
546 num_alarms = osmo_strrb_elements(rb);
547
548 vty_out(vty, "%% Showing %i alarms%s", num_alarms, VTY_NEWLINE);
549
550 for (i = 0; i < num_alarms; i++)
551 vty_out(vty, "%% %s%s", osmo_strrb_get_nth(rb, i),
552 VTY_NEWLINE);
553
554 return CMD_SUCCESS;
555}
556
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200557gDEFUN(cfg_description, cfg_description_cmd,
558 "description .TEXT",
Thorsten Alteholza81055d2017-03-02 22:13:48 +0100559 "Save human-readable description of the object\n"
Holger Hans Peter Freytherc9b3e062012-07-25 13:02:49 +0200560 "Text until the end of the line\n")
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200561{
562 char **dptr = vty->index_sub;
563
564 if (!dptr) {
565 vty_out(vty, "vty->index_sub == NULL%s", VTY_NEWLINE);
566 return CMD_WARNING;
567 }
568
Holger Hans Peter Freytherff0670e2011-02-24 14:20:41 +0100569 if (*dptr)
570 talloc_free(*dptr);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200571 *dptr = argv_concat(argv, argc, 0);
Holger Hans Peter Freyther6a75d162013-07-14 09:07:11 +0200572 if (!*dptr)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200573 return CMD_WARNING;
574
575 return CMD_SUCCESS;
576}
577
578gDEFUN(cfg_no_description, cfg_no_description_cmd,
579 "no description",
580 NO_STR
581 "Remove description of the object\n")
582{
583 char **dptr = vty->index_sub;
584
585 if (!dptr) {
586 vty_out(vty, "vty->index_sub == NULL%s", VTY_NEWLINE);
587 return CMD_WARNING;
588 }
589
590 if (*dptr) {
591 talloc_free(*dptr);
592 *dptr = NULL;
593 }
594
595 return CMD_SUCCESS;
596}
597
Harald Welte28222962011-02-18 20:37:04 +0100598/* Support for configuration of log targets != the current vty */
599
600struct cmd_node cfg_log_node = {
601 CFG_LOG_NODE,
602 "%s(config-log)# ",
603 1
604};
605
Harald Welte28222962011-02-18 20:37:04 +0100606#ifdef HAVE_SYSLOG_H
607
608#include <syslog.h>
609
610static const int local_sysl_map[] = {
611 [0] = LOG_LOCAL0,
612 [1] = LOG_LOCAL1,
613 [2] = LOG_LOCAL2,
614 [3] = LOG_LOCAL3,
615 [4] = LOG_LOCAL4,
616 [5] = LOG_LOCAL5,
617 [6] = LOG_LOCAL6,
618 [7] = LOG_LOCAL7
619};
620
Harald Weltede79cee2011-02-24 23:47:57 +0100621/* From VTY core code */
622extern struct host host;
623
Harald Welte28222962011-02-18 20:37:04 +0100624static int _cfg_log_syslog(struct vty *vty, int facility)
625{
626 struct log_target *tgt;
627
628 /* First delete the old syslog target, if any */
629 tgt = log_target_find(LOG_TGT_TYPE_SYSLOG, NULL);
630 if (tgt)
631 log_target_destroy(tgt);
632
Harald Weltede79cee2011-02-24 23:47:57 +0100633 tgt = log_target_create_syslog(host.app_info->name, 0, facility);
Harald Welte28222962011-02-18 20:37:04 +0100634 if (!tgt) {
635 vty_out(vty, "%% Unable to open syslog%s", VTY_NEWLINE);
636 return CMD_WARNING;
637 }
638 log_add_target(tgt);
639
640 vty->index = tgt;
641 vty->node = CFG_LOG_NODE;
642
643 return CMD_SUCCESS;
644}
645
646DEFUN(cfg_log_syslog_local, cfg_log_syslog_local_cmd,
647 "log syslog local <0-7>",
648 LOG_STR "Logging via syslog\n" "Syslog LOCAL facility\n"
649 "Local facility number\n")
650{
651 int local = atoi(argv[0]);
652 int facility = local_sysl_map[local];
653
654 return _cfg_log_syslog(vty, facility);
655}
656
657static struct value_string sysl_level_names[] = {
658 { LOG_AUTHPRIV, "authpriv" },
659 { LOG_CRON, "cron" },
660 { LOG_DAEMON, "daemon" },
661 { LOG_FTP, "ftp" },
662 { LOG_LPR, "lpr" },
663 { LOG_MAIL, "mail" },
664 { LOG_NEWS, "news" },
665 { LOG_USER, "user" },
666 { LOG_UUCP, "uucp" },
667 /* only for value -> string conversion */
668 { LOG_LOCAL0, "local 0" },
669 { LOG_LOCAL1, "local 1" },
670 { LOG_LOCAL2, "local 2" },
671 { LOG_LOCAL3, "local 3" },
672 { LOG_LOCAL4, "local 4" },
673 { LOG_LOCAL5, "local 5" },
674 { LOG_LOCAL6, "local 6" },
675 { LOG_LOCAL7, "local 7" },
676 { 0, NULL }
677};
678
679DEFUN(cfg_log_syslog, cfg_log_syslog_cmd,
680 "log syslog (authpriv|cron|daemon|ftp|lpr|mail|news|user|uucp)",
Holger Hans Peter Freythera4463fd2011-10-03 23:17:36 +0200681 LOG_STR "Logging via syslog\n"
682 "Security/authorization messages facility\n"
683 "Clock daemon (cron/at) facility\n"
684 "General system daemon facility\n"
685 "Ftp daemon facility\n"
686 "Line printer facility\n"
687 "Mail facility\n"
688 "News facility\n"
689 "Generic facility\n"
690 "UUCP facility\n")
Harald Welte28222962011-02-18 20:37:04 +0100691{
692 int facility = get_string_value(sysl_level_names, argv[0]);
693
694 return _cfg_log_syslog(vty, facility);
695}
696
697DEFUN(cfg_no_log_syslog, cfg_no_log_syslog_cmd,
698 "no log syslog",
699 NO_STR LOG_STR "Logging via syslog\n")
700{
701 struct log_target *tgt;
702
703 tgt = log_target_find(LOG_TGT_TYPE_SYSLOG, NULL);
704 if (!tgt) {
705 vty_out(vty, "%% No syslog target found%s",
706 VTY_NEWLINE);
707 return CMD_WARNING;
708 }
709
710 log_target_destroy(tgt);
711
712 return CMD_SUCCESS;
713}
714#endif /* HAVE_SYSLOG_H */
715
Harald Welteaa00f992016-12-02 15:30:02 +0100716DEFUN(cfg_log_gsmtap, cfg_log_gsmtap_cmd,
717 "log gsmtap [HOSTNAME]",
Neels Hofmeyrfd9ec3b2016-12-11 01:48:26 +0100718 LOG_STR "Logging via GSMTAP\n"
719 "Host name to send the GSMTAP logging to (UDP port 4729)\n")
Harald Welteaa00f992016-12-02 15:30:02 +0100720{
Max2f153b52018-01-04 12:25:57 +0100721 const char *hostname = argc ? argv[0] : "127.0.0.1";
Harald Welteaa00f992016-12-02 15:30:02 +0100722 struct log_target *tgt;
723
724 tgt = log_target_find(LOG_TGT_TYPE_GSMTAP, hostname);
725 if (!tgt) {
726 tgt = log_target_create_gsmtap(hostname, GSMTAP_UDP_PORT,
727 host.app_info->name, false,
728 true);
729 if (!tgt) {
Max2f153b52018-01-04 12:25:57 +0100730 vty_out(vty, "%% Unable to create GSMTAP log for %s%s",
731 hostname, VTY_NEWLINE);
Harald Welteaa00f992016-12-02 15:30:02 +0100732 return CMD_WARNING;
733 }
734 log_add_target(tgt);
735 }
736
737 vty->index = tgt;
738 vty->node = CFG_LOG_NODE;
739
740 return CMD_SUCCESS;
741}
742
Harald Welte28222962011-02-18 20:37:04 +0100743DEFUN(cfg_log_stderr, cfg_log_stderr_cmd,
744 "log stderr",
745 LOG_STR "Logging via STDERR of the process\n")
746{
747 struct log_target *tgt;
748
749 tgt = log_target_find(LOG_TGT_TYPE_STDERR, NULL);
750 if (!tgt) {
751 tgt = log_target_create_stderr();
752 if (!tgt) {
753 vty_out(vty, "%% Unable to create stderr log%s",
754 VTY_NEWLINE);
755 return CMD_WARNING;
756 }
757 log_add_target(tgt);
758 }
759
760 vty->index = tgt;
761 vty->node = CFG_LOG_NODE;
762
763 return CMD_SUCCESS;
764}
765
766DEFUN(cfg_no_log_stderr, cfg_no_log_stderr_cmd,
767 "no log stderr",
768 NO_STR LOG_STR "Logging via STDERR of the process\n")
769{
770 struct log_target *tgt;
771
772 tgt = log_target_find(LOG_TGT_TYPE_STDERR, NULL);
773 if (!tgt) {
774 vty_out(vty, "%% No stderr logging active%s", VTY_NEWLINE);
775 return CMD_WARNING;
776 }
777
778 log_target_destroy(tgt);
779
780 return CMD_SUCCESS;
781}
782
783DEFUN(cfg_log_file, cfg_log_file_cmd,
784 "log file .FILENAME",
785 LOG_STR "Logging to text file\n" "Filename\n")
786{
787 const char *fname = argv[0];
788 struct log_target *tgt;
789
790 tgt = log_target_find(LOG_TGT_TYPE_FILE, fname);
791 if (!tgt) {
792 tgt = log_target_create_file(fname);
793 if (!tgt) {
794 vty_out(vty, "%% Unable to create file `%s'%s",
795 fname, VTY_NEWLINE);
796 return CMD_WARNING;
797 }
798 log_add_target(tgt);
799 }
800
801 vty->index = tgt;
802 vty->node = CFG_LOG_NODE;
803
804 return CMD_SUCCESS;
805}
806
807
808DEFUN(cfg_no_log_file, cfg_no_log_file_cmd,
809 "no log file .FILENAME",
810 NO_STR LOG_STR "Logging to text file\n" "Filename\n")
811{
812 const char *fname = argv[0];
813 struct log_target *tgt;
814
815 tgt = log_target_find(LOG_TGT_TYPE_FILE, fname);
816 if (!tgt) {
817 vty_out(vty, "%% No such log file `%s'%s",
818 fname, VTY_NEWLINE);
819 return CMD_WARNING;
820 }
821
822 log_target_destroy(tgt);
823
824 return CMD_SUCCESS;
825}
826
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000827DEFUN(cfg_log_alarms, cfg_log_alarms_cmd,
828 "log alarms <2-32700>",
829 LOG_STR "Logging alarms to osmo_strrb\n"
830 "Maximum number of messages to log\n")
831{
832 struct log_target *tgt;
833 unsigned int rbsize = atoi(argv[0]);
834
835 tgt = log_target_find(LOG_TGT_TYPE_STRRB, NULL);
836 if (tgt)
837 log_target_destroy(tgt);
838
839 tgt = log_target_create_rb(rbsize);
840 if (!tgt) {
841 vty_out(vty, "%% Unable to create osmo_strrb (size %u)%s",
842 rbsize, VTY_NEWLINE);
843 return CMD_WARNING;
844 }
845 log_add_target(tgt);
846
847 vty->index = tgt;
848 vty->node = CFG_LOG_NODE;
849
850 return CMD_SUCCESS;
851}
852
853DEFUN(cfg_no_log_alarms, cfg_no_log_alarms_cmd,
854 "no log alarms",
855 NO_STR LOG_STR "Logging alarms to osmo_strrb\n")
856{
857 struct log_target *tgt;
858
859 tgt = log_target_find(LOG_TGT_TYPE_STRRB, NULL);
860 if (!tgt) {
861 vty_out(vty, "%% No osmo_strrb target found%s", VTY_NEWLINE);
862 return CMD_WARNING;
863 }
864
865 log_target_destroy(tgt);
866
867 return CMD_SUCCESS;
868}
869
Harald Welte28222962011-02-18 20:37:04 +0100870static int config_write_log_single(struct vty *vty, struct log_target *tgt)
871{
Pau Espin Pedrolc21ba152019-08-02 20:32:34 +0200872 char level_buf[128];
Harald Welte28222962011-02-18 20:37:04 +0100873 int i;
Harald Welte28222962011-02-18 20:37:04 +0100874
875 switch (tgt->type) {
876 case LOG_TGT_TYPE_VTY:
877 return 1;
878 break;
879 case LOG_TGT_TYPE_STDERR:
880 vty_out(vty, "log stderr%s", VTY_NEWLINE);
881 break;
882 case LOG_TGT_TYPE_SYSLOG:
883#ifdef HAVE_SYSLOG_H
884 vty_out(vty, "log syslog %s%s",
885 get_value_string(sysl_level_names,
886 tgt->tgt_syslog.facility),
887 VTY_NEWLINE);
888#endif
889 break;
890 case LOG_TGT_TYPE_FILE:
891 vty_out(vty, "log file %s%s", tgt->tgt_file.fname, VTY_NEWLINE);
892 break;
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000893 case LOG_TGT_TYPE_STRRB:
894 vty_out(vty, "log alarms %zu%s",
895 log_target_rb_avail_size(tgt), VTY_NEWLINE);
896 break;
Harald Welteaa00f992016-12-02 15:30:02 +0100897 case LOG_TGT_TYPE_GSMTAP:
898 vty_out(vty, "log gsmtap %s%s",
899 tgt->tgt_gsmtap.hostname, VTY_NEWLINE);
900 break;
Harald Welte28222962011-02-18 20:37:04 +0100901 }
902
Harald Welte0d67f482018-09-25 20:16:14 +0200903 vty_out(vty, " logging filter all %u%s",
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100904 tgt->filter_map & (1 << LOG_FLT_ALL) ? 1 : 0, VTY_NEWLINE);
Harald Weltefb84f322013-06-06 07:33:54 +0200905 /* save filters outside of libosmocore, i.e. in app code */
906 if (osmo_log_info->save_fn)
907 osmo_log_info->save_fn(vty, osmo_log_info, tgt);
Harald Welte2da47f12012-10-22 19:31:54 +0200908
Harald Welte0d67f482018-09-25 20:16:14 +0200909 vty_out(vty, " logging color %u%s", tgt->use_color ? 1 : 0,
Harald Welte28222962011-02-18 20:37:04 +0100910 VTY_NEWLINE);
Vadim Yanitskiy5c4b9852019-07-28 04:36:37 +0700911 vty_out(vty, " logging print category-hex %d%s",
912 tgt->print_category_hex ? 1 : 0, VTY_NEWLINE);
Harald Welte0d67f482018-09-25 20:16:14 +0200913 vty_out(vty, " logging print category %d%s",
Michael McTernan78933462015-03-20 15:29:25 +0100914 tgt->print_category ? 1 : 0, VTY_NEWLINE);
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100915 if (tgt->print_ext_timestamp)
Harald Welte0d67f482018-09-25 20:16:14 +0200916 vty_out(vty, " logging print extended-timestamp 1%s", VTY_NEWLINE);
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100917 else
Harald Welte0d67f482018-09-25 20:16:14 +0200918 vty_out(vty, " logging timestamp %u%s",
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100919 tgt->print_timestamp ? 1 : 0, VTY_NEWLINE);
Neels Hofmeyr886e5482018-01-16 01:49:37 +0100920 if (tgt->print_level)
Harald Welte0d67f482018-09-25 20:16:14 +0200921 vty_out(vty, " logging print level 1%s", VTY_NEWLINE);
922 vty_out(vty, " logging print file %s%s",
Neels Hofmeyr22772cc2018-02-06 00:52:08 +0100923 get_value_string(logging_print_file_args, tgt->print_filename2),
924 VTY_NEWLINE);
Harald Welte28222962011-02-18 20:37:04 +0100925
Neels Hofmeyr098038a2018-09-11 23:49:13 +0200926 if (tgt->loglevel) {
927 const char *level_str = get_value_string_or_null(loglevel_strs, tgt->loglevel);
Pau Espin Pedrolc21ba152019-08-02 20:32:34 +0200928 if (!level_str) {
Neels Hofmeyr9540c242018-09-12 00:20:50 +0200929 vty_out(vty, "%% Invalid log level %u for 'force-all'%s",
930 tgt->loglevel, VTY_NEWLINE);
Pau Espin Pedrolc21ba152019-08-02 20:32:34 +0200931 } else {
932 osmo_str_tolower_buf(level_buf, sizeof(level_buf), level_str);
933 vty_out(vty, " logging level force-all %s%s", level_buf, VTY_NEWLINE);
934 }
Neels Hofmeyr098038a2018-09-11 23:49:13 +0200935 }
Harald Welte28222962011-02-18 20:37:04 +0100936
937 for (i = 0; i < osmo_log_info->num_cat; i++) {
938 const struct log_category *cat = &tgt->categories[i];
Pau Espin Pedrolc21ba152019-08-02 20:32:34 +0200939 char cat_name[128];
Neels Hofmeyr098038a2018-09-11 23:49:13 +0200940 const char *level_str;
Harald Welte28222962011-02-18 20:37:04 +0100941
Harald Welte1a02cfc2013-03-19 10:37:39 +0100942 /* skip empty entries in the array */
943 if (!osmo_log_info->cat[i].name)
944 continue;
945
Pau Espin Pedrolc21ba152019-08-02 20:32:34 +0200946 osmo_str_tolower_buf(cat_name, sizeof(cat_name), osmo_log_info->cat[i].name + 1);
Neels Hofmeyr098038a2018-09-11 23:49:13 +0200947
948 level_str = get_value_string_or_null(loglevel_strs, cat->loglevel);
949 if (!level_str) {
950 vty_out(vty, "%% Invalid log level %u for %s%s", cat->loglevel, cat_name,
951 VTY_NEWLINE);
952 continue;
953 }
954
Pau Espin Pedrolc21ba152019-08-02 20:32:34 +0200955 osmo_str_tolower_buf(level_buf, sizeof(level_buf), level_str);
Harald Welte0d67f482018-09-25 20:16:14 +0200956 vty_out(vty, " logging level %s", cat_name);
Pau Espin Pedrolc21ba152019-08-02 20:32:34 +0200957 vty_out(vty, " %s%s", level_buf, VTY_NEWLINE);
Harald Welte28222962011-02-18 20:37:04 +0100958 }
959
Harald Welte28222962011-02-18 20:37:04 +0100960 return 1;
961}
962
963static int config_write_log(struct vty *vty)
964{
965 struct log_target *dbg = vty->index;
966
967 llist_for_each_entry(dbg, &osmo_log_target_list, entry)
968 config_write_log_single(vty, dbg);
969
970 return 1;
971}
972
Harald Welte11eb4b52018-06-09 17:41:31 +0200973static int log_deprecated_func(struct cmd_element *cmd, struct vty *vty, int argc, const char *argv[])
974{
975 vty_out(vty, "%% Ignoring deprecated '%s'%s", cmd->string, VTY_NEWLINE);
976 return CMD_WARNING;
977}
978
979void logging_vty_add_deprecated_subsys(void *ctx, const char *name)
980{
981 struct cmd_element *cmd = talloc_zero(ctx, struct cmd_element);
982 OSMO_ASSERT(cmd);
Neels Hofmeyr7e0686c2018-09-10 20:58:52 +0200983 cmd->string = talloc_asprintf(cmd, "logging level %s (debug|info|notice|error|fatal)",
Harald Welte11eb4b52018-06-09 17:41:31 +0200984 name);
985 printf("%s\n", cmd->string);
986 cmd->func = log_deprecated_func;
Neels Hofmeyrba0762d2018-09-10 13:56:03 +0200987 cmd->doc = LEVEL_STR
Harald Welte11eb4b52018-06-09 17:41:31 +0200988 "Deprecated Category\n";
989 cmd->attr = CMD_ATTR_DEPRECATED;
990
991 install_element(CFG_LOG_NODE, cmd);
992}
993
Neels Hofmeyrd0b3b9e2019-07-29 19:28:08 +0200994/* logp (<categories>) (debug|...|fatal) .LOGMESSAGE*/
995DEFUN(vty_logp,
996 vty_logp_cmd,
997 NULL, /* cmdstr is dynamically set in gen_vty_logp_cmd_strs(). */
998 NULL) /* same thing for helpstr. */
999{
1000 int category = log_parse_category(argv[0]);
1001 int level = log_parse_level(argv[1]);
1002 char *str = argv_concat(argv, argc, 2);
1003 LOGP(category, level, "%s\n", str);
1004 return CMD_SUCCESS;
1005}
1006
1007static void gen_vty_logp_cmd_strs(struct cmd_element *cmd)
1008{
1009 char *cmd_str = NULL;
1010 char *doc_str = NULL;
1011
1012 assert_loginfo(__func__);
1013
1014 OSMO_ASSERT(cmd->string == NULL);
1015 OSMO_ASSERT(cmd->doc == NULL);
1016
1017 osmo_talloc_asprintf(tall_log_ctx, cmd_str, "logp (");
1018 osmo_talloc_asprintf(tall_log_ctx, doc_str,
1019 "Print a message on all log outputs; useful for placing markers in test logs\n");
1020 add_category_strings(&cmd_str, &doc_str, osmo_log_info);
1021 osmo_talloc_asprintf(tall_log_ctx, cmd_str, ") %s", LOG_LEVEL_ARGS);
1022 osmo_talloc_asprintf(tall_log_ctx, doc_str, "%s", LOG_LEVEL_STRS);
1023
1024 osmo_talloc_asprintf(tall_log_ctx, cmd_str, " .LOGMESSAGE");
1025 osmo_talloc_asprintf(tall_log_ctx, doc_str,
1026 "Arbitrary message to log on given category and log level\n");
1027
1028 cmd->string = cmd_str;
1029 cmd->doc = doc_str;
1030}
1031
Harald Welte8c648252017-10-16 15:17:03 +02001032/*! Register logging related commands to the VTY. Call this once from
1033 * your application if you want to support those commands. */
Maxc65c5b42017-03-15 13:20:23 +01001034void logging_vty_add_cmds()
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001035{
1036 install_element_ve(&enable_logging_cmd);
1037 install_element_ve(&disable_logging_cmd);
1038 install_element_ve(&logging_fltr_all_cmd);
1039 install_element_ve(&logging_use_clr_cmd);
1040 install_element_ve(&logging_prnt_timestamp_cmd);
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +01001041 install_element_ve(&logging_prnt_ext_timestamp_cmd);
1042 install_element_ve(&logging_prnt_cat_cmd);
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +01001043 install_element_ve(&logging_prnt_cat_hex_cmd);
Neels Hofmeyr886e5482018-01-16 01:49:37 +01001044 install_element_ve(&logging_prnt_level_cmd);
Neels Hofmeyrc6fd2452018-01-16 01:57:38 +01001045 install_element_ve(&logging_prnt_file_cmd);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001046 install_element_ve(&logging_set_category_mask_cmd);
Holger Hans Peter Freyther146d1d32011-10-03 23:15:41 +02001047 install_element_ve(&logging_set_category_mask_old_cmd);
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +01001048
Neels Hofmeyr9540c242018-09-12 00:20:50 +02001049 /* logging level (<categories>) (debug|...|fatal) */
Neels Hofmeyrba0762d2018-09-10 13:56:03 +02001050 gen_logging_level_cmd_strs(&logging_level_cmd,
Neels Hofmeyr9540c242018-09-12 00:20:50 +02001051 LOG_LEVEL_ARGS,
Neels Hofmeyr7e0686c2018-09-10 20:58:52 +02001052 LOG_LEVEL_STRS);
Neels Hofmeyr9540c242018-09-12 00:20:50 +02001053 /* logging level (<categories>) everything */
Neels Hofmeyr7e0686c2018-09-10 20:58:52 +02001054 gen_logging_level_cmd_strs(&deprecated_logging_level_everything_cmd,
1055 "everything", EVERYTHING_STR);
Neels Hofmeyrba0762d2018-09-10 13:56:03 +02001056
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001057 install_element_ve(&logging_level_cmd);
Neels Hofmeyr28fc0782018-09-12 02:32:02 +02001058 install_element_ve(&logging_level_set_all_cmd);
Neels Hofmeyr9540c242018-09-12 00:20:50 +02001059 install_element_ve(&logging_level_force_all_cmd);
1060 install_element_ve(&no_logging_level_force_all_cmd);
Neels Hofmeyr7e0686c2018-09-10 20:58:52 +02001061 install_element_ve(&deprecated_logging_level_everything_cmd);
Neels Hofmeyr9540c242018-09-12 00:20:50 +02001062 install_element_ve(&deprecated_logging_level_all_cmd);
1063 install_element_ve(&deprecated_logging_level_all_everything_cmd);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001064 install_element_ve(&show_logging_vty_cmd);
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +00001065 install_element_ve(&show_alarms_cmd);
Harald Welte28222962011-02-18 20:37:04 +01001066
Neels Hofmeyrd0b3b9e2019-07-29 19:28:08 +02001067 gen_vty_logp_cmd_strs(&vty_logp_cmd);
1068 install_element_ve(&vty_logp_cmd);
1069
Harald Welte28222962011-02-18 20:37:04 +01001070 install_node(&cfg_log_node, config_write_log);
Harald Weltea62648b2011-02-18 21:03:27 +01001071 install_element(CFG_LOG_NODE, &logging_fltr_all_cmd);
1072 install_element(CFG_LOG_NODE, &logging_use_clr_cmd);
1073 install_element(CFG_LOG_NODE, &logging_prnt_timestamp_cmd);
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +01001074 install_element(CFG_LOG_NODE, &logging_prnt_ext_timestamp_cmd);
1075 install_element(CFG_LOG_NODE, &logging_prnt_cat_cmd);
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +01001076 install_element(CFG_LOG_NODE, &logging_prnt_cat_hex_cmd);
Neels Hofmeyr886e5482018-01-16 01:49:37 +01001077 install_element(CFG_LOG_NODE, &logging_prnt_level_cmd);
Neels Hofmeyrc6fd2452018-01-16 01:57:38 +01001078 install_element(CFG_LOG_NODE, &logging_prnt_file_cmd);
Harald Weltea62648b2011-02-18 21:03:27 +01001079 install_element(CFG_LOG_NODE, &logging_level_cmd);
Neels Hofmeyr28fc0782018-09-12 02:32:02 +02001080 install_element(CFG_LOG_NODE, &logging_level_set_all_cmd);
Neels Hofmeyr9540c242018-09-12 00:20:50 +02001081 install_element(CFG_LOG_NODE, &logging_level_force_all_cmd);
1082 install_element(CFG_LOG_NODE, &no_logging_level_force_all_cmd);
Neels Hofmeyr7e0686c2018-09-10 20:58:52 +02001083 install_element(CFG_LOG_NODE, &deprecated_logging_level_everything_cmd);
Neels Hofmeyr9540c242018-09-12 00:20:50 +02001084 install_element(CFG_LOG_NODE, &deprecated_logging_level_all_cmd);
1085 install_element(CFG_LOG_NODE, &deprecated_logging_level_all_everything_cmd);
Harald Welte28222962011-02-18 20:37:04 +01001086
1087 install_element(CONFIG_NODE, &cfg_log_stderr_cmd);
1088 install_element(CONFIG_NODE, &cfg_no_log_stderr_cmd);
1089 install_element(CONFIG_NODE, &cfg_log_file_cmd);
1090 install_element(CONFIG_NODE, &cfg_no_log_file_cmd);
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +00001091 install_element(CONFIG_NODE, &cfg_log_alarms_cmd);
1092 install_element(CONFIG_NODE, &cfg_no_log_alarms_cmd);
Harald Welte28222962011-02-18 20:37:04 +01001093#ifdef HAVE_SYSLOG_H
1094 install_element(CONFIG_NODE, &cfg_log_syslog_cmd);
1095 install_element(CONFIG_NODE, &cfg_log_syslog_local_cmd);
1096 install_element(CONFIG_NODE, &cfg_no_log_syslog_cmd);
1097#endif
Harald Welteaa00f992016-12-02 15:30:02 +01001098 install_element(CONFIG_NODE, &cfg_log_gsmtap_cmd);
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001099}