blob: 9b7d10afd0e222c4d92c1c3e413c27f428f7f10d [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 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 *
20 */
21
22#include <stdlib.h>
23#include <string.h>
24
Harald Welte28222962011-02-18 20:37:04 +010025#include "../../config.h"
26
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010027#include <osmocom/core/talloc.h>
28#include <osmocom/core/logging.h>
29#include <osmocom/core/utils.h>
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +000030#include <osmocom/core/strrb.h>
31#include <osmocom/core/loggingrb.h>
Harald Welteaa00f992016-12-02 15:30:02 +010032#include <osmocom/core/gsmtap.h>
Harald Welte3fb0b6f2010-05-19 19:02:52 +020033
34#include <osmocom/vty/command.h>
35#include <osmocom/vty/buffer.h>
36#include <osmocom/vty/vty.h>
37#include <osmocom/vty/telnet_interface.h>
38#include <osmocom/vty/logging.h>
39
Harald Welte28222962011-02-18 20:37:04 +010040#define LOG_STR "Configure logging sub-system\n"
41
Harald Welte8c648252017-10-16 15:17:03 +020042/*! \file logging_vty.c
Neels Hofmeyr87e45502017-06-20 00:17:59 +020043 * Configuration of logging from VTY
Harald Welte96e2a002017-06-12 21:44:18 +020044 *
Harald Welte8c648252017-10-16 15:17:03 +020045 * This module implements
46 * - functions that permit configuration of the libosmocore logging
47 * framework from VTY commands in the configure -> logging node.
48 *
49 * - functions that permit logging *to* a VTY session. Basically each
50 * VTY session gets its own log target, with configurable
51 * per-subsystem log levels. This is performed internally via the
52 * \ref log_target_create_vty function.
53 *
54 * You have to call \ref logging_vty_add_cmds from your application
55 * once to enable both of the above.
56 *
Harald Welte96e2a002017-06-12 21:44:18 +020057 */
58
Harald Welte4ebdf742010-05-19 19:54:00 +020059extern const struct log_info *osmo_log_info;
Harald Welte3fb0b6f2010-05-19 19:02:52 +020060
Harald Welte76e72ab2011-02-17 15:52:39 +010061static void _vty_output(struct log_target *tgt,
62 unsigned int level, const char *line)
Harald Welte3fb0b6f2010-05-19 19:02:52 +020063{
64 struct vty *vty = tgt->tgt_vty.vty;
65 vty_out(vty, "%s", line);
66 /* This is an ugly hack, but there is no easy way... */
67 if (strchr(line, '\n'))
68 vty_out(vty, "\r");
69}
70
71struct log_target *log_target_create_vty(struct vty *vty)
72{
73 struct log_target *target;
74
75 target = log_target_create();
76 if (!target)
77 return NULL;
78
79 target->tgt_vty.vty = vty;
80 target->output = _vty_output;
81 return target;
82}
83
84DEFUN(enable_logging,
85 enable_logging_cmd,
86 "logging enable",
87 LOGGING_STR
88 "Enables logging to this vty\n")
89{
90 struct telnet_connection *conn;
91
92 conn = (struct telnet_connection *) vty->priv;
93 if (conn->dbg) {
94 vty_out(vty, "Logging already enabled.%s", VTY_NEWLINE);
95 return CMD_WARNING;
96 }
97
98 conn->dbg = log_target_create_vty(vty);
99 if (!conn->dbg)
100 return CMD_WARNING;
101
102 log_add_target(conn->dbg);
103 return CMD_SUCCESS;
104}
105
Harald Weltea62648b2011-02-18 21:03:27 +0100106struct log_target *osmo_log_vty2tgt(struct vty *vty)
107{
108 struct telnet_connection *conn;
109
110 if (vty->node == CFG_LOG_NODE)
111 return vty->index;
112
113
114 conn = (struct telnet_connection *) vty->priv;
115 if (!conn->dbg)
116 vty_out(vty, "Logging was not enabled.%s", VTY_NEWLINE);
117
118 return conn->dbg;
119}
120
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200121DEFUN(logging_fltr_all,
122 logging_fltr_all_cmd,
123 "logging filter all (0|1)",
124 LOGGING_STR FILTER_STR
125 "Do you want to log all messages?\n"
126 "Only print messages matched by other filters\n"
127 "Bypass filter and print all messages\n")
128{
Harald Weltea62648b2011-02-18 21:03:27 +0100129 struct log_target *tgt = osmo_log_vty2tgt(vty);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200130
Harald Weltea62648b2011-02-18 21:03:27 +0100131 if (!tgt)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200132 return CMD_WARNING;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200133
Harald Weltea62648b2011-02-18 21:03:27 +0100134 log_set_all_filter(tgt, atoi(argv[0]));
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200135 return CMD_SUCCESS;
136}
137
138DEFUN(logging_use_clr,
139 logging_use_clr_cmd,
140 "logging color (0|1)",
141 LOGGING_STR "Configure color-printing for log messages\n"
142 "Don't use color for printing messages\n"
143 "Use color for printing messages\n")
144{
Harald Weltea62648b2011-02-18 21:03:27 +0100145 struct log_target *tgt = osmo_log_vty2tgt(vty);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200146
Harald Weltea62648b2011-02-18 21:03:27 +0100147 if (!tgt)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200148 return CMD_WARNING;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200149
Harald Weltea62648b2011-02-18 21:03:27 +0100150 log_set_use_color(tgt, atoi(argv[0]));
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200151 return CMD_SUCCESS;
152}
153
154DEFUN(logging_prnt_timestamp,
155 logging_prnt_timestamp_cmd,
156 "logging timestamp (0|1)",
157 LOGGING_STR "Configure log message timestamping\n"
158 "Don't prefix each log message\n"
159 "Prefix each log message with current timestamp\n")
160{
Harald Weltea62648b2011-02-18 21:03:27 +0100161 struct log_target *tgt = osmo_log_vty2tgt(vty);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200162
Harald Weltea62648b2011-02-18 21:03:27 +0100163 if (!tgt)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200164 return CMD_WARNING;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200165
Harald Weltea62648b2011-02-18 21:03:27 +0100166 log_set_print_timestamp(tgt, atoi(argv[0]));
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200167 return CMD_SUCCESS;
168}
169
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100170DEFUN(logging_prnt_ext_timestamp,
171 logging_prnt_ext_timestamp_cmd,
172 "logging print extended-timestamp (0|1)",
173 LOGGING_STR "Log output settings\n"
174 "Configure log message timestamping\n"
175 "Don't prefix each log message\n"
176 "Prefix each log message with current timestamp with YYYYMMDDhhmmssnnn\n")
177{
178 struct log_target *tgt = osmo_log_vty2tgt(vty);
179
180 if (!tgt)
181 return CMD_WARNING;
182
183 log_set_print_extended_timestamp(tgt, atoi(argv[0]));
184 return CMD_SUCCESS;
185}
186
187DEFUN(logging_prnt_cat,
188 logging_prnt_cat_cmd,
189 "logging print category (0|1)",
190 LOGGING_STR "Log output settings\n"
191 "Configure log message\n"
192 "Don't prefix each log message\n"
193 "Prefix each log message with category/subsystem name\n")
194{
195 struct log_target *tgt = osmo_log_vty2tgt(vty);
196
197 if (!tgt)
198 return CMD_WARNING;
199
200 log_set_print_category(tgt, atoi(argv[0]));
201 return CMD_SUCCESS;
202}
203
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200204DEFUN(logging_level,
205 logging_level_cmd,
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100206 NULL, /* cmdstr is dynamically set in logging_vty_add_cmds(). */
207 NULL) /* same thing for helpstr. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200208{
Harald Weltea62648b2011-02-18 21:03:27 +0100209 int category = log_parse_category(argv[0]);
210 int level = log_parse_level(argv[1]);
211 struct log_target *tgt = osmo_log_vty2tgt(vty);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200212
Harald Weltea62648b2011-02-18 21:03:27 +0100213 if (!tgt)
214 return CMD_WARNING;
215
216 if (level < 0) {
217 vty_out(vty, "Invalid level `%s'%s", argv[1], VTY_NEWLINE);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200218 return CMD_WARNING;
219 }
220
Maxed0eda22017-07-06 17:09:01 +0200221 if (strcmp(argv[1], "everything") == 0) { /* FIXME: remove this check once 'everything' is phased out */
222 vty_out(vty, "%% Ignoring deprecated logging level %s%s", argv[1], VTY_NEWLINE);
223 return CMD_SUCCESS;
224 }
225
Harald Weltea62648b2011-02-18 21:03:27 +0100226 /* Check for special case where we want to set global log level */
227 if (!strcmp(argv[0], "all")) {
228 log_set_log_level(tgt, level);
229 return CMD_SUCCESS;
230 }
231
232 if (category < 0) {
233 vty_out(vty, "Invalid category `%s'%s", argv[0], VTY_NEWLINE);
234 return CMD_WARNING;
235 }
236
237 tgt->categories[category].enabled = 1;
238 tgt->categories[category].loglevel = level;
239
240 return CMD_SUCCESS;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200241}
242
243DEFUN(logging_set_category_mask,
244 logging_set_category_mask_cmd,
Holger Hans Peter Freyther146d1d32011-10-03 23:15:41 +0200245 "logging set-log-mask MASK",
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200246 LOGGING_STR
Holger Hans Peter Freyther146d1d32011-10-03 23:15:41 +0200247 "Set the logmask of this logging target\n"
248 "The logmask to use\n")
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200249{
Harald Weltea62648b2011-02-18 21:03:27 +0100250 struct log_target *tgt = osmo_log_vty2tgt(vty);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200251
Harald Weltea62648b2011-02-18 21:03:27 +0100252 if (!tgt)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200253 return CMD_WARNING;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200254
Harald Weltea62648b2011-02-18 21:03:27 +0100255 log_parse_category_mask(tgt, argv[0]);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200256 return CMD_SUCCESS;
257}
258
Holger Hans Peter Freyther146d1d32011-10-03 23:15:41 +0200259ALIAS_DEPRECATED(logging_set_category_mask,
260 logging_set_category_mask_old_cmd,
261 "logging set log mask MASK",
262 LOGGING_STR
263 "Decide which categories to output.\n"
264 "Log commands\n" "Mask commands\n" "The logmask to use\n");
265
266
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200267DEFUN(diable_logging,
268 disable_logging_cmd,
269 "logging disable",
270 LOGGING_STR
271 "Disables logging to this vty\n")
272{
Harald Weltea62648b2011-02-18 21:03:27 +0100273 struct log_target *tgt = osmo_log_vty2tgt(vty);
274 struct telnet_connection *conn = (struct telnet_connection *) vty->priv;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200275
Harald Weltea62648b2011-02-18 21:03:27 +0100276 if (!tgt)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200277 return CMD_WARNING;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200278
Harald Weltea62648b2011-02-18 21:03:27 +0100279 log_del_target(tgt);
280 talloc_free(tgt);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200281 conn->dbg = NULL;
Harald Weltea62648b2011-02-18 21:03:27 +0100282
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200283 return CMD_SUCCESS;
284}
285
286static void vty_print_logtarget(struct vty *vty, const struct log_info *info,
287 const struct log_target *tgt)
288{
289 unsigned int i;
290
291 vty_out(vty, " Global Loglevel: %s%s",
292 log_level_str(tgt->loglevel), VTY_NEWLINE);
293 vty_out(vty, " Use color: %s, Print Timestamp: %s%s",
294 tgt->use_color ? "On" : "Off",
295 tgt->print_timestamp ? "On" : "Off", VTY_NEWLINE);
296
297 vty_out(vty, " Log Level specific information:%s", VTY_NEWLINE);
298
299 for (i = 0; i < info->num_cat; i++) {
300 const struct log_category *cat = &tgt->categories[i];
Daniel Willmann55363a92016-11-15 10:05:51 +0100301 /* Skip categories that were not initialized */
302 if (!info->cat[i].name)
303 continue;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200304 vty_out(vty, " %-10s %-10s %-8s %s%s",
305 info->cat[i].name+1, log_level_str(cat->loglevel),
306 cat->enabled ? "Enabled" : "Disabled",
307 info->cat[i].description,
308 VTY_NEWLINE);
309 }
Harald Welte6d2d4d62013-03-10 09:53:24 +0000310
311 vty_out(vty, " Log Filter 'ALL': %s%s",
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100312 tgt->filter_map & (1 << LOG_FLT_ALL) ? "Enabled" : "Disabled",
Harald Welte6d2d4d62013-03-10 09:53:24 +0000313 VTY_NEWLINE);
314
Harald Weltefb84f322013-06-06 07:33:54 +0200315 /* print application specific filters */
316 if (info->print_fn)
317 info->print_fn(vty, info, tgt);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200318}
319
320#define SHOW_LOG_STR "Show current logging configuration\n"
321
322DEFUN(show_logging_vty,
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000323 show_logging_vty_cmd,
324 "show logging vty",
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200325 SHOW_STR SHOW_LOG_STR
326 "Show current logging configuration for this vty\n")
327{
Harald Weltea62648b2011-02-18 21:03:27 +0100328 struct log_target *tgt = osmo_log_vty2tgt(vty);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200329
Harald Weltea62648b2011-02-18 21:03:27 +0100330 if (!tgt)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200331 return CMD_WARNING;
Harald Weltea62648b2011-02-18 21:03:27 +0100332
333 vty_print_logtarget(vty, osmo_log_info, tgt);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200334
335 return CMD_SUCCESS;
336}
337
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000338DEFUN(show_alarms,
339 show_alarms_cmd,
340 "show alarms",
341 SHOW_STR SHOW_LOG_STR
342 "Show the contents of the logging ringbuffer\n")
343{
344 int i, num_alarms;
345 struct osmo_strrb *rb;
346 struct log_target *tgt = log_target_find(LOG_TGT_TYPE_STRRB, NULL);
347 if (!tgt) {
348 vty_out(vty, "%% No alarms, run 'log alarms <2-32700>'%s",
349 VTY_NEWLINE);
350 return CMD_WARNING;
351 }
352
353 rb = tgt->tgt_rb.rb;
354 num_alarms = osmo_strrb_elements(rb);
355
356 vty_out(vty, "%% Showing %i alarms%s", num_alarms, VTY_NEWLINE);
357
358 for (i = 0; i < num_alarms; i++)
359 vty_out(vty, "%% %s%s", osmo_strrb_get_nth(rb, i),
360 VTY_NEWLINE);
361
362 return CMD_SUCCESS;
363}
364
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200365gDEFUN(cfg_description, cfg_description_cmd,
366 "description .TEXT",
Thorsten Alteholza81055d2017-03-02 22:13:48 +0100367 "Save human-readable description of the object\n"
Holger Hans Peter Freytherc9b3e062012-07-25 13:02:49 +0200368 "Text until the end of the line\n")
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200369{
370 char **dptr = vty->index_sub;
371
372 if (!dptr) {
373 vty_out(vty, "vty->index_sub == NULL%s", VTY_NEWLINE);
374 return CMD_WARNING;
375 }
376
Holger Hans Peter Freytherff0670e2011-02-24 14:20:41 +0100377 if (*dptr)
378 talloc_free(*dptr);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200379 *dptr = argv_concat(argv, argc, 0);
Holger Hans Peter Freyther6a75d162013-07-14 09:07:11 +0200380 if (!*dptr)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200381 return CMD_WARNING;
382
383 return CMD_SUCCESS;
384}
385
386gDEFUN(cfg_no_description, cfg_no_description_cmd,
387 "no description",
388 NO_STR
389 "Remove description of the object\n")
390{
391 char **dptr = vty->index_sub;
392
393 if (!dptr) {
394 vty_out(vty, "vty->index_sub == NULL%s", VTY_NEWLINE);
395 return CMD_WARNING;
396 }
397
398 if (*dptr) {
399 talloc_free(*dptr);
400 *dptr = NULL;
401 }
402
403 return CMD_SUCCESS;
404}
405
Harald Welte28222962011-02-18 20:37:04 +0100406/* Support for configuration of log targets != the current vty */
407
408struct cmd_node cfg_log_node = {
409 CFG_LOG_NODE,
410 "%s(config-log)# ",
411 1
412};
413
Harald Welte28222962011-02-18 20:37:04 +0100414#ifdef HAVE_SYSLOG_H
415
416#include <syslog.h>
417
418static const int local_sysl_map[] = {
419 [0] = LOG_LOCAL0,
420 [1] = LOG_LOCAL1,
421 [2] = LOG_LOCAL2,
422 [3] = LOG_LOCAL3,
423 [4] = LOG_LOCAL4,
424 [5] = LOG_LOCAL5,
425 [6] = LOG_LOCAL6,
426 [7] = LOG_LOCAL7
427};
428
Harald Weltede79cee2011-02-24 23:47:57 +0100429/* From VTY core code */
430extern struct host host;
431
Harald Welte28222962011-02-18 20:37:04 +0100432static int _cfg_log_syslog(struct vty *vty, int facility)
433{
434 struct log_target *tgt;
435
436 /* First delete the old syslog target, if any */
437 tgt = log_target_find(LOG_TGT_TYPE_SYSLOG, NULL);
438 if (tgt)
439 log_target_destroy(tgt);
440
Harald Weltede79cee2011-02-24 23:47:57 +0100441 tgt = log_target_create_syslog(host.app_info->name, 0, facility);
Harald Welte28222962011-02-18 20:37:04 +0100442 if (!tgt) {
443 vty_out(vty, "%% Unable to open syslog%s", VTY_NEWLINE);
444 return CMD_WARNING;
445 }
446 log_add_target(tgt);
447
448 vty->index = tgt;
449 vty->node = CFG_LOG_NODE;
450
451 return CMD_SUCCESS;
452}
453
454DEFUN(cfg_log_syslog_local, cfg_log_syslog_local_cmd,
455 "log syslog local <0-7>",
456 LOG_STR "Logging via syslog\n" "Syslog LOCAL facility\n"
457 "Local facility number\n")
458{
459 int local = atoi(argv[0]);
460 int facility = local_sysl_map[local];
461
462 return _cfg_log_syslog(vty, facility);
463}
464
465static struct value_string sysl_level_names[] = {
466 { LOG_AUTHPRIV, "authpriv" },
467 { LOG_CRON, "cron" },
468 { LOG_DAEMON, "daemon" },
469 { LOG_FTP, "ftp" },
470 { LOG_LPR, "lpr" },
471 { LOG_MAIL, "mail" },
472 { LOG_NEWS, "news" },
473 { LOG_USER, "user" },
474 { LOG_UUCP, "uucp" },
475 /* only for value -> string conversion */
476 { LOG_LOCAL0, "local 0" },
477 { LOG_LOCAL1, "local 1" },
478 { LOG_LOCAL2, "local 2" },
479 { LOG_LOCAL3, "local 3" },
480 { LOG_LOCAL4, "local 4" },
481 { LOG_LOCAL5, "local 5" },
482 { LOG_LOCAL6, "local 6" },
483 { LOG_LOCAL7, "local 7" },
484 { 0, NULL }
485};
486
487DEFUN(cfg_log_syslog, cfg_log_syslog_cmd,
488 "log syslog (authpriv|cron|daemon|ftp|lpr|mail|news|user|uucp)",
Holger Hans Peter Freythera4463fd2011-10-03 23:17:36 +0200489 LOG_STR "Logging via syslog\n"
490 "Security/authorization messages facility\n"
491 "Clock daemon (cron/at) facility\n"
492 "General system daemon facility\n"
493 "Ftp daemon facility\n"
494 "Line printer facility\n"
495 "Mail facility\n"
496 "News facility\n"
497 "Generic facility\n"
498 "UUCP facility\n")
Harald Welte28222962011-02-18 20:37:04 +0100499{
500 int facility = get_string_value(sysl_level_names, argv[0]);
501
502 return _cfg_log_syslog(vty, facility);
503}
504
505DEFUN(cfg_no_log_syslog, cfg_no_log_syslog_cmd,
506 "no log syslog",
507 NO_STR LOG_STR "Logging via syslog\n")
508{
509 struct log_target *tgt;
510
511 tgt = log_target_find(LOG_TGT_TYPE_SYSLOG, NULL);
512 if (!tgt) {
513 vty_out(vty, "%% No syslog target found%s",
514 VTY_NEWLINE);
515 return CMD_WARNING;
516 }
517
518 log_target_destroy(tgt);
519
520 return CMD_SUCCESS;
521}
522#endif /* HAVE_SYSLOG_H */
523
Harald Welteaa00f992016-12-02 15:30:02 +0100524DEFUN(cfg_log_gsmtap, cfg_log_gsmtap_cmd,
525 "log gsmtap [HOSTNAME]",
Neels Hofmeyrfd9ec3b2016-12-11 01:48:26 +0100526 LOG_STR "Logging via GSMTAP\n"
527 "Host name to send the GSMTAP logging to (UDP port 4729)\n")
Harald Welteaa00f992016-12-02 15:30:02 +0100528{
529 const char *hostname = argv[0];
530 struct log_target *tgt;
531
532 tgt = log_target_find(LOG_TGT_TYPE_GSMTAP, hostname);
533 if (!tgt) {
534 tgt = log_target_create_gsmtap(hostname, GSMTAP_UDP_PORT,
535 host.app_info->name, false,
536 true);
537 if (!tgt) {
538 vty_out(vty, "%% Unable to create GSMTAP log%s",
539 VTY_NEWLINE);
540 return CMD_WARNING;
541 }
542 log_add_target(tgt);
543 }
544
545 vty->index = tgt;
546 vty->node = CFG_LOG_NODE;
547
548 return CMD_SUCCESS;
549}
550
Harald Welte28222962011-02-18 20:37:04 +0100551DEFUN(cfg_log_stderr, cfg_log_stderr_cmd,
552 "log stderr",
553 LOG_STR "Logging via STDERR of the process\n")
554{
555 struct log_target *tgt;
556
557 tgt = log_target_find(LOG_TGT_TYPE_STDERR, NULL);
558 if (!tgt) {
559 tgt = log_target_create_stderr();
560 if (!tgt) {
561 vty_out(vty, "%% Unable to create stderr log%s",
562 VTY_NEWLINE);
563 return CMD_WARNING;
564 }
565 log_add_target(tgt);
566 }
567
568 vty->index = tgt;
569 vty->node = CFG_LOG_NODE;
570
571 return CMD_SUCCESS;
572}
573
574DEFUN(cfg_no_log_stderr, cfg_no_log_stderr_cmd,
575 "no log stderr",
576 NO_STR LOG_STR "Logging via STDERR of the process\n")
577{
578 struct log_target *tgt;
579
580 tgt = log_target_find(LOG_TGT_TYPE_STDERR, NULL);
581 if (!tgt) {
582 vty_out(vty, "%% No stderr logging active%s", VTY_NEWLINE);
583 return CMD_WARNING;
584 }
585
586 log_target_destroy(tgt);
587
588 return CMD_SUCCESS;
589}
590
591DEFUN(cfg_log_file, cfg_log_file_cmd,
592 "log file .FILENAME",
593 LOG_STR "Logging to text file\n" "Filename\n")
594{
595 const char *fname = argv[0];
596 struct log_target *tgt;
597
598 tgt = log_target_find(LOG_TGT_TYPE_FILE, fname);
599 if (!tgt) {
600 tgt = log_target_create_file(fname);
601 if (!tgt) {
602 vty_out(vty, "%% Unable to create file `%s'%s",
603 fname, VTY_NEWLINE);
604 return CMD_WARNING;
605 }
606 log_add_target(tgt);
607 }
608
609 vty->index = tgt;
610 vty->node = CFG_LOG_NODE;
611
612 return CMD_SUCCESS;
613}
614
615
616DEFUN(cfg_no_log_file, cfg_no_log_file_cmd,
617 "no log file .FILENAME",
618 NO_STR LOG_STR "Logging to text file\n" "Filename\n")
619{
620 const char *fname = argv[0];
621 struct log_target *tgt;
622
623 tgt = log_target_find(LOG_TGT_TYPE_FILE, fname);
624 if (!tgt) {
625 vty_out(vty, "%% No such log file `%s'%s",
626 fname, VTY_NEWLINE);
627 return CMD_WARNING;
628 }
629
630 log_target_destroy(tgt);
631
632 return CMD_SUCCESS;
633}
634
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000635DEFUN(cfg_log_alarms, cfg_log_alarms_cmd,
636 "log alarms <2-32700>",
637 LOG_STR "Logging alarms to osmo_strrb\n"
638 "Maximum number of messages to log\n")
639{
640 struct log_target *tgt;
641 unsigned int rbsize = atoi(argv[0]);
642
643 tgt = log_target_find(LOG_TGT_TYPE_STRRB, NULL);
644 if (tgt)
645 log_target_destroy(tgt);
646
647 tgt = log_target_create_rb(rbsize);
648 if (!tgt) {
649 vty_out(vty, "%% Unable to create osmo_strrb (size %u)%s",
650 rbsize, VTY_NEWLINE);
651 return CMD_WARNING;
652 }
653 log_add_target(tgt);
654
655 vty->index = tgt;
656 vty->node = CFG_LOG_NODE;
657
658 return CMD_SUCCESS;
659}
660
661DEFUN(cfg_no_log_alarms, cfg_no_log_alarms_cmd,
662 "no log alarms",
663 NO_STR LOG_STR "Logging alarms to osmo_strrb\n")
664{
665 struct log_target *tgt;
666
667 tgt = log_target_find(LOG_TGT_TYPE_STRRB, NULL);
668 if (!tgt) {
669 vty_out(vty, "%% No osmo_strrb target found%s", VTY_NEWLINE);
670 return CMD_WARNING;
671 }
672
673 log_target_destroy(tgt);
674
675 return CMD_SUCCESS;
676}
677
Harald Welte28222962011-02-18 20:37:04 +0100678static int config_write_log_single(struct vty *vty, struct log_target *tgt)
679{
680 int i;
681 char level_lower[32];
682
683 switch (tgt->type) {
684 case LOG_TGT_TYPE_VTY:
685 return 1;
686 break;
687 case LOG_TGT_TYPE_STDERR:
688 vty_out(vty, "log stderr%s", VTY_NEWLINE);
689 break;
690 case LOG_TGT_TYPE_SYSLOG:
691#ifdef HAVE_SYSLOG_H
692 vty_out(vty, "log syslog %s%s",
693 get_value_string(sysl_level_names,
694 tgt->tgt_syslog.facility),
695 VTY_NEWLINE);
696#endif
697 break;
698 case LOG_TGT_TYPE_FILE:
699 vty_out(vty, "log file %s%s", tgt->tgt_file.fname, VTY_NEWLINE);
700 break;
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000701 case LOG_TGT_TYPE_STRRB:
702 vty_out(vty, "log alarms %zu%s",
703 log_target_rb_avail_size(tgt), VTY_NEWLINE);
704 break;
Harald Welteaa00f992016-12-02 15:30:02 +0100705 case LOG_TGT_TYPE_GSMTAP:
706 vty_out(vty, "log gsmtap %s%s",
707 tgt->tgt_gsmtap.hostname, VTY_NEWLINE);
708 break;
Harald Welte28222962011-02-18 20:37:04 +0100709 }
710
Harald Welte2da47f12012-10-22 19:31:54 +0200711 vty_out(vty, " logging filter all %u%s",
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100712 tgt->filter_map & (1 << LOG_FLT_ALL) ? 1 : 0, VTY_NEWLINE);
Harald Weltefb84f322013-06-06 07:33:54 +0200713 /* save filters outside of libosmocore, i.e. in app code */
714 if (osmo_log_info->save_fn)
715 osmo_log_info->save_fn(vty, osmo_log_info, tgt);
Harald Welte2da47f12012-10-22 19:31:54 +0200716
Harald Welte28222962011-02-18 20:37:04 +0100717 vty_out(vty, " logging color %u%s", tgt->use_color ? 1 : 0,
718 VTY_NEWLINE);
Holger Hans Peter Freyther879acef2015-01-27 11:06:51 +0100719 vty_out(vty, " logging print category %d%s",
Michael McTernan78933462015-03-20 15:29:25 +0100720 tgt->print_category ? 1 : 0, VTY_NEWLINE);
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100721 if (tgt->print_ext_timestamp)
722 vty_out(vty, " logging print extended-timestamp 1%s", VTY_NEWLINE);
723 else
724 vty_out(vty, " logging timestamp %u%s",
725 tgt->print_timestamp ? 1 : 0, VTY_NEWLINE);
Harald Welte28222962011-02-18 20:37:04 +0100726
727 /* stupid old osmo logging API uses uppercase strings... */
728 osmo_str2lower(level_lower, log_level_str(tgt->loglevel));
729 vty_out(vty, " logging level all %s%s", level_lower, VTY_NEWLINE);
730
731 for (i = 0; i < osmo_log_info->num_cat; i++) {
732 const struct log_category *cat = &tgt->categories[i];
733 char cat_lower[32];
734
Harald Welte1a02cfc2013-03-19 10:37:39 +0100735 /* skip empty entries in the array */
736 if (!osmo_log_info->cat[i].name)
737 continue;
738
Harald Welte28222962011-02-18 20:37:04 +0100739 /* stupid old osmo logging API uses uppercase strings... */
740 osmo_str2lower(cat_lower, osmo_log_info->cat[i].name+1);
741 osmo_str2lower(level_lower, log_level_str(cat->loglevel));
742
Maxed0eda22017-07-06 17:09:01 +0200743 if (strcmp(level_lower, "everything") != 0) /* FIXME: remove this check once 'everything' is phased out */
744 vty_out(vty, " logging level %s %s%s", cat_lower, level_lower, VTY_NEWLINE);
745 else
746 LOGP(DLSTATS, LOGL_ERROR, "logging level everything is deprecated and should not be used\n");
Harald Welte28222962011-02-18 20:37:04 +0100747 }
748
749 /* FIXME: levels */
750
751 return 1;
752}
753
754static int config_write_log(struct vty *vty)
755{
756 struct log_target *dbg = vty->index;
757
758 llist_for_each_entry(dbg, &osmo_log_target_list, entry)
759 config_write_log_single(vty, dbg);
760
761 return 1;
762}
763
Harald Welte8c648252017-10-16 15:17:03 +0200764/*! Register logging related commands to the VTY. Call this once from
765 * your application if you want to support those commands. */
Maxc65c5b42017-03-15 13:20:23 +0100766void logging_vty_add_cmds()
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200767{
768 install_element_ve(&enable_logging_cmd);
769 install_element_ve(&disable_logging_cmd);
770 install_element_ve(&logging_fltr_all_cmd);
771 install_element_ve(&logging_use_clr_cmd);
772 install_element_ve(&logging_prnt_timestamp_cmd);
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100773 install_element_ve(&logging_prnt_ext_timestamp_cmd);
774 install_element_ve(&logging_prnt_cat_cmd);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200775 install_element_ve(&logging_set_category_mask_cmd);
Holger Hans Peter Freyther146d1d32011-10-03 23:15:41 +0200776 install_element_ve(&logging_set_category_mask_old_cmd);
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100777
778 /* Logging level strings are generated dynamically. */
Maxc65c5b42017-03-15 13:20:23 +0100779 logging_level_cmd.string = log_vty_command_string();
780 logging_level_cmd.doc = log_vty_command_description();
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200781 install_element_ve(&logging_level_cmd);
782 install_element_ve(&show_logging_vty_cmd);
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000783 install_element_ve(&show_alarms_cmd);
Harald Welte28222962011-02-18 20:37:04 +0100784
785 install_node(&cfg_log_node, config_write_log);
Harald Weltea62648b2011-02-18 21:03:27 +0100786 install_element(CFG_LOG_NODE, &logging_fltr_all_cmd);
787 install_element(CFG_LOG_NODE, &logging_use_clr_cmd);
788 install_element(CFG_LOG_NODE, &logging_prnt_timestamp_cmd);
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100789 install_element(CFG_LOG_NODE, &logging_prnt_ext_timestamp_cmd);
790 install_element(CFG_LOG_NODE, &logging_prnt_cat_cmd);
Harald Weltea62648b2011-02-18 21:03:27 +0100791 install_element(CFG_LOG_NODE, &logging_level_cmd);
Harald Welte28222962011-02-18 20:37:04 +0100792
793 install_element(CONFIG_NODE, &cfg_log_stderr_cmd);
794 install_element(CONFIG_NODE, &cfg_no_log_stderr_cmd);
795 install_element(CONFIG_NODE, &cfg_log_file_cmd);
796 install_element(CONFIG_NODE, &cfg_no_log_file_cmd);
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000797 install_element(CONFIG_NODE, &cfg_log_alarms_cmd);
798 install_element(CONFIG_NODE, &cfg_no_log_alarms_cmd);
Harald Welte28222962011-02-18 20:37:04 +0100799#ifdef HAVE_SYSLOG_H
800 install_element(CONFIG_NODE, &cfg_log_syslog_cmd);
801 install_element(CONFIG_NODE, &cfg_log_syslog_local_cmd);
802 install_element(CONFIG_NODE, &cfg_no_log_syslog_cmd);
803#endif
Harald Welteaa00f992016-12-02 15:30:02 +0100804 install_element(CONFIG_NODE, &cfg_log_gsmtap_cmd);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200805}