blob: 4cb137921baecc5e0eea4b10c8b27d1dba78b3e7 [file] [log] [blame]
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001/* OpenBSC logging helper for the VTY */
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 Welte96e2a002017-06-12 21:44:18 +020042/*! \addtogroup logging
43 * @{
Neels Hofmeyr87e45502017-06-20 00:17:59 +020044 * Configuration of logging from VTY
Harald Welte96e2a002017-06-12 21:44:18 +020045 *
46 * This module implements functions that permit configuration of
47 * the libosmocore logging framework from VTY commands. This applies
48 * both to logging to the VTY (telnet sessions), as well as logging to
49 * other targets, such as sysslog, file, gsmtap, ...
50 */
51
Harald Welte4ebdf742010-05-19 19:54:00 +020052extern const struct log_info *osmo_log_info;
Harald Welte3fb0b6f2010-05-19 19:02:52 +020053
Harald Welte76e72ab2011-02-17 15:52:39 +010054static void _vty_output(struct log_target *tgt,
55 unsigned int level, const char *line)
Harald Welte3fb0b6f2010-05-19 19:02:52 +020056{
57 struct vty *vty = tgt->tgt_vty.vty;
58 vty_out(vty, "%s", line);
59 /* This is an ugly hack, but there is no easy way... */
60 if (strchr(line, '\n'))
61 vty_out(vty, "\r");
62}
63
64struct log_target *log_target_create_vty(struct vty *vty)
65{
66 struct log_target *target;
67
68 target = log_target_create();
69 if (!target)
70 return NULL;
71
72 target->tgt_vty.vty = vty;
73 target->output = _vty_output;
74 return target;
75}
76
77DEFUN(enable_logging,
78 enable_logging_cmd,
79 "logging enable",
80 LOGGING_STR
81 "Enables logging to this vty\n")
82{
83 struct telnet_connection *conn;
84
85 conn = (struct telnet_connection *) vty->priv;
86 if (conn->dbg) {
87 vty_out(vty, "Logging already enabled.%s", VTY_NEWLINE);
88 return CMD_WARNING;
89 }
90
91 conn->dbg = log_target_create_vty(vty);
92 if (!conn->dbg)
93 return CMD_WARNING;
94
95 log_add_target(conn->dbg);
96 return CMD_SUCCESS;
97}
98
Harald Weltea62648b2011-02-18 21:03:27 +010099struct log_target *osmo_log_vty2tgt(struct vty *vty)
100{
101 struct telnet_connection *conn;
102
103 if (vty->node == CFG_LOG_NODE)
104 return vty->index;
105
106
107 conn = (struct telnet_connection *) vty->priv;
108 if (!conn->dbg)
109 vty_out(vty, "Logging was not enabled.%s", VTY_NEWLINE);
110
111 return conn->dbg;
112}
113
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200114DEFUN(logging_fltr_all,
115 logging_fltr_all_cmd,
116 "logging filter all (0|1)",
117 LOGGING_STR FILTER_STR
118 "Do you want to log all messages?\n"
119 "Only print messages matched by other filters\n"
120 "Bypass filter and print all messages\n")
121{
Harald Weltea62648b2011-02-18 21:03:27 +0100122 struct log_target *tgt = osmo_log_vty2tgt(vty);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200123
Harald Weltea62648b2011-02-18 21:03:27 +0100124 if (!tgt)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200125 return CMD_WARNING;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200126
Harald Weltea62648b2011-02-18 21:03:27 +0100127 log_set_all_filter(tgt, atoi(argv[0]));
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200128 return CMD_SUCCESS;
129}
130
131DEFUN(logging_use_clr,
132 logging_use_clr_cmd,
133 "logging color (0|1)",
134 LOGGING_STR "Configure color-printing for log messages\n"
135 "Don't use color for printing messages\n"
136 "Use color for printing messages\n")
137{
Harald Weltea62648b2011-02-18 21:03:27 +0100138 struct log_target *tgt = osmo_log_vty2tgt(vty);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200139
Harald Weltea62648b2011-02-18 21:03:27 +0100140 if (!tgt)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200141 return CMD_WARNING;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200142
Harald Weltea62648b2011-02-18 21:03:27 +0100143 log_set_use_color(tgt, atoi(argv[0]));
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200144 return CMD_SUCCESS;
145}
146
147DEFUN(logging_prnt_timestamp,
148 logging_prnt_timestamp_cmd,
149 "logging timestamp (0|1)",
150 LOGGING_STR "Configure log message timestamping\n"
151 "Don't prefix each log message\n"
152 "Prefix each log message with current timestamp\n")
153{
Harald Weltea62648b2011-02-18 21:03:27 +0100154 struct log_target *tgt = osmo_log_vty2tgt(vty);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200155
Harald Weltea62648b2011-02-18 21:03:27 +0100156 if (!tgt)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200157 return CMD_WARNING;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200158
Harald Weltea62648b2011-02-18 21:03:27 +0100159 log_set_print_timestamp(tgt, atoi(argv[0]));
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200160 return CMD_SUCCESS;
161}
162
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100163DEFUN(logging_prnt_ext_timestamp,
164 logging_prnt_ext_timestamp_cmd,
165 "logging print extended-timestamp (0|1)",
166 LOGGING_STR "Log output settings\n"
167 "Configure log message timestamping\n"
168 "Don't prefix each log message\n"
169 "Prefix each log message with current timestamp with YYYYMMDDhhmmssnnn\n")
170{
171 struct log_target *tgt = osmo_log_vty2tgt(vty);
172
173 if (!tgt)
174 return CMD_WARNING;
175
176 log_set_print_extended_timestamp(tgt, atoi(argv[0]));
177 return CMD_SUCCESS;
178}
179
180DEFUN(logging_prnt_cat,
181 logging_prnt_cat_cmd,
182 "logging print category (0|1)",
183 LOGGING_STR "Log output settings\n"
184 "Configure log message\n"
185 "Don't prefix each log message\n"
186 "Prefix each log message with category/subsystem name\n")
187{
188 struct log_target *tgt = osmo_log_vty2tgt(vty);
189
190 if (!tgt)
191 return CMD_WARNING;
192
193 log_set_print_category(tgt, atoi(argv[0]));
194 return CMD_SUCCESS;
195}
196
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200197DEFUN(logging_level,
198 logging_level_cmd,
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100199 NULL, /* cmdstr is dynamically set in logging_vty_add_cmds(). */
200 NULL) /* same thing for helpstr. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200201{
Harald Weltea62648b2011-02-18 21:03:27 +0100202 int category = log_parse_category(argv[0]);
203 int level = log_parse_level(argv[1]);
204 struct log_target *tgt = osmo_log_vty2tgt(vty);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200205
Harald Weltea62648b2011-02-18 21:03:27 +0100206 if (!tgt)
207 return CMD_WARNING;
208
209 if (level < 0) {
210 vty_out(vty, "Invalid level `%s'%s", argv[1], VTY_NEWLINE);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200211 return CMD_WARNING;
212 }
213
Harald Weltea62648b2011-02-18 21:03:27 +0100214 /* Check for special case where we want to set global log level */
215 if (!strcmp(argv[0], "all")) {
216 log_set_log_level(tgt, level);
217 return CMD_SUCCESS;
218 }
219
220 if (category < 0) {
221 vty_out(vty, "Invalid category `%s'%s", argv[0], VTY_NEWLINE);
222 return CMD_WARNING;
223 }
224
225 tgt->categories[category].enabled = 1;
226 tgt->categories[category].loglevel = level;
227
228 return CMD_SUCCESS;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200229}
230
231DEFUN(logging_set_category_mask,
232 logging_set_category_mask_cmd,
Holger Hans Peter Freyther146d1d32011-10-03 23:15:41 +0200233 "logging set-log-mask MASK",
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200234 LOGGING_STR
Holger Hans Peter Freyther146d1d32011-10-03 23:15:41 +0200235 "Set the logmask of this logging target\n"
236 "The logmask to use\n")
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200237{
Harald Weltea62648b2011-02-18 21:03:27 +0100238 struct log_target *tgt = osmo_log_vty2tgt(vty);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200239
Harald Weltea62648b2011-02-18 21:03:27 +0100240 if (!tgt)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200241 return CMD_WARNING;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200242
Harald Weltea62648b2011-02-18 21:03:27 +0100243 log_parse_category_mask(tgt, argv[0]);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200244 return CMD_SUCCESS;
245}
246
Holger Hans Peter Freyther146d1d32011-10-03 23:15:41 +0200247ALIAS_DEPRECATED(logging_set_category_mask,
248 logging_set_category_mask_old_cmd,
249 "logging set log mask MASK",
250 LOGGING_STR
251 "Decide which categories to output.\n"
252 "Log commands\n" "Mask commands\n" "The logmask to use\n");
253
254
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200255DEFUN(diable_logging,
256 disable_logging_cmd,
257 "logging disable",
258 LOGGING_STR
259 "Disables logging to this vty\n")
260{
Harald Weltea62648b2011-02-18 21:03:27 +0100261 struct log_target *tgt = osmo_log_vty2tgt(vty);
262 struct telnet_connection *conn = (struct telnet_connection *) vty->priv;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200263
Harald Weltea62648b2011-02-18 21:03:27 +0100264 if (!tgt)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200265 return CMD_WARNING;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200266
Harald Weltea62648b2011-02-18 21:03:27 +0100267 log_del_target(tgt);
268 talloc_free(tgt);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200269 conn->dbg = NULL;
Harald Weltea62648b2011-02-18 21:03:27 +0100270
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200271 return CMD_SUCCESS;
272}
273
274static void vty_print_logtarget(struct vty *vty, const struct log_info *info,
275 const struct log_target *tgt)
276{
277 unsigned int i;
278
279 vty_out(vty, " Global Loglevel: %s%s",
280 log_level_str(tgt->loglevel), VTY_NEWLINE);
281 vty_out(vty, " Use color: %s, Print Timestamp: %s%s",
282 tgt->use_color ? "On" : "Off",
283 tgt->print_timestamp ? "On" : "Off", VTY_NEWLINE);
284
285 vty_out(vty, " Log Level specific information:%s", VTY_NEWLINE);
286
287 for (i = 0; i < info->num_cat; i++) {
288 const struct log_category *cat = &tgt->categories[i];
Daniel Willmann55363a92016-11-15 10:05:51 +0100289 /* Skip categories that were not initialized */
290 if (!info->cat[i].name)
291 continue;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200292 vty_out(vty, " %-10s %-10s %-8s %s%s",
293 info->cat[i].name+1, log_level_str(cat->loglevel),
294 cat->enabled ? "Enabled" : "Disabled",
295 info->cat[i].description,
296 VTY_NEWLINE);
297 }
Harald Welte6d2d4d62013-03-10 09:53:24 +0000298
299 vty_out(vty, " Log Filter 'ALL': %s%s",
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100300 tgt->filter_map & (1 << LOG_FLT_ALL) ? "Enabled" : "Disabled",
Harald Welte6d2d4d62013-03-10 09:53:24 +0000301 VTY_NEWLINE);
302
Harald Weltefb84f322013-06-06 07:33:54 +0200303 /* print application specific filters */
304 if (info->print_fn)
305 info->print_fn(vty, info, tgt);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200306}
307
308#define SHOW_LOG_STR "Show current logging configuration\n"
309
310DEFUN(show_logging_vty,
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000311 show_logging_vty_cmd,
312 "show logging vty",
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200313 SHOW_STR SHOW_LOG_STR
314 "Show current logging configuration for this vty\n")
315{
Harald Weltea62648b2011-02-18 21:03:27 +0100316 struct log_target *tgt = osmo_log_vty2tgt(vty);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200317
Harald Weltea62648b2011-02-18 21:03:27 +0100318 if (!tgt)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200319 return CMD_WARNING;
Harald Weltea62648b2011-02-18 21:03:27 +0100320
321 vty_print_logtarget(vty, osmo_log_info, tgt);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200322
323 return CMD_SUCCESS;
324}
325
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000326DEFUN(show_alarms,
327 show_alarms_cmd,
328 "show alarms",
329 SHOW_STR SHOW_LOG_STR
330 "Show the contents of the logging ringbuffer\n")
331{
332 int i, num_alarms;
333 struct osmo_strrb *rb;
334 struct log_target *tgt = log_target_find(LOG_TGT_TYPE_STRRB, NULL);
335 if (!tgt) {
336 vty_out(vty, "%% No alarms, run 'log alarms <2-32700>'%s",
337 VTY_NEWLINE);
338 return CMD_WARNING;
339 }
340
341 rb = tgt->tgt_rb.rb;
342 num_alarms = osmo_strrb_elements(rb);
343
344 vty_out(vty, "%% Showing %i alarms%s", num_alarms, VTY_NEWLINE);
345
346 for (i = 0; i < num_alarms; i++)
347 vty_out(vty, "%% %s%s", osmo_strrb_get_nth(rb, i),
348 VTY_NEWLINE);
349
350 return CMD_SUCCESS;
351}
352
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200353gDEFUN(cfg_description, cfg_description_cmd,
354 "description .TEXT",
Thorsten Alteholza81055d2017-03-02 22:13:48 +0100355 "Save human-readable description of the object\n"
Holger Hans Peter Freytherc9b3e062012-07-25 13:02:49 +0200356 "Text until the end of the line\n")
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200357{
358 char **dptr = vty->index_sub;
359
360 if (!dptr) {
361 vty_out(vty, "vty->index_sub == NULL%s", VTY_NEWLINE);
362 return CMD_WARNING;
363 }
364
Holger Hans Peter Freytherff0670e2011-02-24 14:20:41 +0100365 if (*dptr)
366 talloc_free(*dptr);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200367 *dptr = argv_concat(argv, argc, 0);
Holger Hans Peter Freyther6a75d162013-07-14 09:07:11 +0200368 if (!*dptr)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200369 return CMD_WARNING;
370
371 return CMD_SUCCESS;
372}
373
374gDEFUN(cfg_no_description, cfg_no_description_cmd,
375 "no description",
376 NO_STR
377 "Remove description of the object\n")
378{
379 char **dptr = vty->index_sub;
380
381 if (!dptr) {
382 vty_out(vty, "vty->index_sub == NULL%s", VTY_NEWLINE);
383 return CMD_WARNING;
384 }
385
386 if (*dptr) {
387 talloc_free(*dptr);
388 *dptr = NULL;
389 }
390
391 return CMD_SUCCESS;
392}
393
Harald Welte28222962011-02-18 20:37:04 +0100394/* Support for configuration of log targets != the current vty */
395
396struct cmd_node cfg_log_node = {
397 CFG_LOG_NODE,
398 "%s(config-log)# ",
399 1
400};
401
Harald Welte28222962011-02-18 20:37:04 +0100402#ifdef HAVE_SYSLOG_H
403
404#include <syslog.h>
405
406static const int local_sysl_map[] = {
407 [0] = LOG_LOCAL0,
408 [1] = LOG_LOCAL1,
409 [2] = LOG_LOCAL2,
410 [3] = LOG_LOCAL3,
411 [4] = LOG_LOCAL4,
412 [5] = LOG_LOCAL5,
413 [6] = LOG_LOCAL6,
414 [7] = LOG_LOCAL7
415};
416
Harald Weltede79cee2011-02-24 23:47:57 +0100417/* From VTY core code */
418extern struct host host;
419
Harald Welte28222962011-02-18 20:37:04 +0100420static int _cfg_log_syslog(struct vty *vty, int facility)
421{
422 struct log_target *tgt;
423
424 /* First delete the old syslog target, if any */
425 tgt = log_target_find(LOG_TGT_TYPE_SYSLOG, NULL);
426 if (tgt)
427 log_target_destroy(tgt);
428
Harald Weltede79cee2011-02-24 23:47:57 +0100429 tgt = log_target_create_syslog(host.app_info->name, 0, facility);
Harald Welte28222962011-02-18 20:37:04 +0100430 if (!tgt) {
431 vty_out(vty, "%% Unable to open syslog%s", VTY_NEWLINE);
432 return CMD_WARNING;
433 }
434 log_add_target(tgt);
435
436 vty->index = tgt;
437 vty->node = CFG_LOG_NODE;
438
439 return CMD_SUCCESS;
440}
441
442DEFUN(cfg_log_syslog_local, cfg_log_syslog_local_cmd,
443 "log syslog local <0-7>",
444 LOG_STR "Logging via syslog\n" "Syslog LOCAL facility\n"
445 "Local facility number\n")
446{
447 int local = atoi(argv[0]);
448 int facility = local_sysl_map[local];
449
450 return _cfg_log_syslog(vty, facility);
451}
452
453static struct value_string sysl_level_names[] = {
454 { LOG_AUTHPRIV, "authpriv" },
455 { LOG_CRON, "cron" },
456 { LOG_DAEMON, "daemon" },
457 { LOG_FTP, "ftp" },
458 { LOG_LPR, "lpr" },
459 { LOG_MAIL, "mail" },
460 { LOG_NEWS, "news" },
461 { LOG_USER, "user" },
462 { LOG_UUCP, "uucp" },
463 /* only for value -> string conversion */
464 { LOG_LOCAL0, "local 0" },
465 { LOG_LOCAL1, "local 1" },
466 { LOG_LOCAL2, "local 2" },
467 { LOG_LOCAL3, "local 3" },
468 { LOG_LOCAL4, "local 4" },
469 { LOG_LOCAL5, "local 5" },
470 { LOG_LOCAL6, "local 6" },
471 { LOG_LOCAL7, "local 7" },
472 { 0, NULL }
473};
474
475DEFUN(cfg_log_syslog, cfg_log_syslog_cmd,
476 "log syslog (authpriv|cron|daemon|ftp|lpr|mail|news|user|uucp)",
Holger Hans Peter Freythera4463fd2011-10-03 23:17:36 +0200477 LOG_STR "Logging via syslog\n"
478 "Security/authorization messages facility\n"
479 "Clock daemon (cron/at) facility\n"
480 "General system daemon facility\n"
481 "Ftp daemon facility\n"
482 "Line printer facility\n"
483 "Mail facility\n"
484 "News facility\n"
485 "Generic facility\n"
486 "UUCP facility\n")
Harald Welte28222962011-02-18 20:37:04 +0100487{
488 int facility = get_string_value(sysl_level_names, argv[0]);
489
490 return _cfg_log_syslog(vty, facility);
491}
492
493DEFUN(cfg_no_log_syslog, cfg_no_log_syslog_cmd,
494 "no log syslog",
495 NO_STR LOG_STR "Logging via syslog\n")
496{
497 struct log_target *tgt;
498
499 tgt = log_target_find(LOG_TGT_TYPE_SYSLOG, NULL);
500 if (!tgt) {
501 vty_out(vty, "%% No syslog target found%s",
502 VTY_NEWLINE);
503 return CMD_WARNING;
504 }
505
506 log_target_destroy(tgt);
507
508 return CMD_SUCCESS;
509}
510#endif /* HAVE_SYSLOG_H */
511
Harald Welteaa00f992016-12-02 15:30:02 +0100512DEFUN(cfg_log_gsmtap, cfg_log_gsmtap_cmd,
513 "log gsmtap [HOSTNAME]",
Neels Hofmeyrfd9ec3b2016-12-11 01:48:26 +0100514 LOG_STR "Logging via GSMTAP\n"
515 "Host name to send the GSMTAP logging to (UDP port 4729)\n")
Harald Welteaa00f992016-12-02 15:30:02 +0100516{
517 const char *hostname = argv[0];
518 struct log_target *tgt;
519
520 tgt = log_target_find(LOG_TGT_TYPE_GSMTAP, hostname);
521 if (!tgt) {
522 tgt = log_target_create_gsmtap(hostname, GSMTAP_UDP_PORT,
523 host.app_info->name, false,
524 true);
525 if (!tgt) {
526 vty_out(vty, "%% Unable to create GSMTAP log%s",
527 VTY_NEWLINE);
528 return CMD_WARNING;
529 }
530 log_add_target(tgt);
531 }
532
533 vty->index = tgt;
534 vty->node = CFG_LOG_NODE;
535
536 return CMD_SUCCESS;
537}
538
Harald Welte28222962011-02-18 20:37:04 +0100539DEFUN(cfg_log_stderr, cfg_log_stderr_cmd,
540 "log stderr",
541 LOG_STR "Logging via STDERR of the process\n")
542{
543 struct log_target *tgt;
544
545 tgt = log_target_find(LOG_TGT_TYPE_STDERR, NULL);
546 if (!tgt) {
547 tgt = log_target_create_stderr();
548 if (!tgt) {
549 vty_out(vty, "%% Unable to create stderr log%s",
550 VTY_NEWLINE);
551 return CMD_WARNING;
552 }
553 log_add_target(tgt);
554 }
555
556 vty->index = tgt;
557 vty->node = CFG_LOG_NODE;
558
559 return CMD_SUCCESS;
560}
561
562DEFUN(cfg_no_log_stderr, cfg_no_log_stderr_cmd,
563 "no log stderr",
564 NO_STR LOG_STR "Logging via STDERR of the process\n")
565{
566 struct log_target *tgt;
567
568 tgt = log_target_find(LOG_TGT_TYPE_STDERR, NULL);
569 if (!tgt) {
570 vty_out(vty, "%% No stderr logging active%s", VTY_NEWLINE);
571 return CMD_WARNING;
572 }
573
574 log_target_destroy(tgt);
575
576 return CMD_SUCCESS;
577}
578
579DEFUN(cfg_log_file, cfg_log_file_cmd,
580 "log file .FILENAME",
581 LOG_STR "Logging to text file\n" "Filename\n")
582{
583 const char *fname = argv[0];
584 struct log_target *tgt;
585
586 tgt = log_target_find(LOG_TGT_TYPE_FILE, fname);
587 if (!tgt) {
588 tgt = log_target_create_file(fname);
589 if (!tgt) {
590 vty_out(vty, "%% Unable to create file `%s'%s",
591 fname, VTY_NEWLINE);
592 return CMD_WARNING;
593 }
594 log_add_target(tgt);
595 }
596
597 vty->index = tgt;
598 vty->node = CFG_LOG_NODE;
599
600 return CMD_SUCCESS;
601}
602
603
604DEFUN(cfg_no_log_file, cfg_no_log_file_cmd,
605 "no log file .FILENAME",
606 NO_STR LOG_STR "Logging to text file\n" "Filename\n")
607{
608 const char *fname = argv[0];
609 struct log_target *tgt;
610
611 tgt = log_target_find(LOG_TGT_TYPE_FILE, fname);
612 if (!tgt) {
613 vty_out(vty, "%% No such log file `%s'%s",
614 fname, VTY_NEWLINE);
615 return CMD_WARNING;
616 }
617
618 log_target_destroy(tgt);
619
620 return CMD_SUCCESS;
621}
622
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000623DEFUN(cfg_log_alarms, cfg_log_alarms_cmd,
624 "log alarms <2-32700>",
625 LOG_STR "Logging alarms to osmo_strrb\n"
626 "Maximum number of messages to log\n")
627{
628 struct log_target *tgt;
629 unsigned int rbsize = atoi(argv[0]);
630
631 tgt = log_target_find(LOG_TGT_TYPE_STRRB, NULL);
632 if (tgt)
633 log_target_destroy(tgt);
634
635 tgt = log_target_create_rb(rbsize);
636 if (!tgt) {
637 vty_out(vty, "%% Unable to create osmo_strrb (size %u)%s",
638 rbsize, VTY_NEWLINE);
639 return CMD_WARNING;
640 }
641 log_add_target(tgt);
642
643 vty->index = tgt;
644 vty->node = CFG_LOG_NODE;
645
646 return CMD_SUCCESS;
647}
648
649DEFUN(cfg_no_log_alarms, cfg_no_log_alarms_cmd,
650 "no log alarms",
651 NO_STR LOG_STR "Logging alarms to osmo_strrb\n")
652{
653 struct log_target *tgt;
654
655 tgt = log_target_find(LOG_TGT_TYPE_STRRB, NULL);
656 if (!tgt) {
657 vty_out(vty, "%% No osmo_strrb target found%s", VTY_NEWLINE);
658 return CMD_WARNING;
659 }
660
661 log_target_destroy(tgt);
662
663 return CMD_SUCCESS;
664}
665
Harald Welte28222962011-02-18 20:37:04 +0100666static int config_write_log_single(struct vty *vty, struct log_target *tgt)
667{
668 int i;
669 char level_lower[32];
670
671 switch (tgt->type) {
672 case LOG_TGT_TYPE_VTY:
673 return 1;
674 break;
675 case LOG_TGT_TYPE_STDERR:
676 vty_out(vty, "log stderr%s", VTY_NEWLINE);
677 break;
678 case LOG_TGT_TYPE_SYSLOG:
679#ifdef HAVE_SYSLOG_H
680 vty_out(vty, "log syslog %s%s",
681 get_value_string(sysl_level_names,
682 tgt->tgt_syslog.facility),
683 VTY_NEWLINE);
684#endif
685 break;
686 case LOG_TGT_TYPE_FILE:
687 vty_out(vty, "log file %s%s", tgt->tgt_file.fname, VTY_NEWLINE);
688 break;
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000689 case LOG_TGT_TYPE_STRRB:
690 vty_out(vty, "log alarms %zu%s",
691 log_target_rb_avail_size(tgt), VTY_NEWLINE);
692 break;
Harald Welteaa00f992016-12-02 15:30:02 +0100693 case LOG_TGT_TYPE_GSMTAP:
694 vty_out(vty, "log gsmtap %s%s",
695 tgt->tgt_gsmtap.hostname, VTY_NEWLINE);
696 break;
Harald Welte28222962011-02-18 20:37:04 +0100697 }
698
Harald Welte2da47f12012-10-22 19:31:54 +0200699 vty_out(vty, " logging filter all %u%s",
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100700 tgt->filter_map & (1 << LOG_FLT_ALL) ? 1 : 0, VTY_NEWLINE);
Harald Weltefb84f322013-06-06 07:33:54 +0200701 /* save filters outside of libosmocore, i.e. in app code */
702 if (osmo_log_info->save_fn)
703 osmo_log_info->save_fn(vty, osmo_log_info, tgt);
Harald Welte2da47f12012-10-22 19:31:54 +0200704
Harald Welte28222962011-02-18 20:37:04 +0100705 vty_out(vty, " logging color %u%s", tgt->use_color ? 1 : 0,
706 VTY_NEWLINE);
Holger Hans Peter Freyther879acef2015-01-27 11:06:51 +0100707 vty_out(vty, " logging print category %d%s",
Michael McTernan78933462015-03-20 15:29:25 +0100708 tgt->print_category ? 1 : 0, VTY_NEWLINE);
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100709 if (tgt->print_ext_timestamp)
710 vty_out(vty, " logging print extended-timestamp 1%s", VTY_NEWLINE);
711 else
712 vty_out(vty, " logging timestamp %u%s",
713 tgt->print_timestamp ? 1 : 0, VTY_NEWLINE);
Harald Welte28222962011-02-18 20:37:04 +0100714
715 /* stupid old osmo logging API uses uppercase strings... */
716 osmo_str2lower(level_lower, log_level_str(tgt->loglevel));
717 vty_out(vty, " logging level all %s%s", level_lower, VTY_NEWLINE);
718
719 for (i = 0; i < osmo_log_info->num_cat; i++) {
720 const struct log_category *cat = &tgt->categories[i];
721 char cat_lower[32];
722
Harald Welte1a02cfc2013-03-19 10:37:39 +0100723 /* skip empty entries in the array */
724 if (!osmo_log_info->cat[i].name)
725 continue;
726
Harald Welte28222962011-02-18 20:37:04 +0100727 /* stupid old osmo logging API uses uppercase strings... */
728 osmo_str2lower(cat_lower, osmo_log_info->cat[i].name+1);
729 osmo_str2lower(level_lower, log_level_str(cat->loglevel));
730
731 vty_out(vty, " logging level %s %s%s", cat_lower, level_lower,
732 VTY_NEWLINE);
733 }
734
735 /* FIXME: levels */
736
737 return 1;
738}
739
740static int config_write_log(struct vty *vty)
741{
742 struct log_target *dbg = vty->index;
743
744 llist_for_each_entry(dbg, &osmo_log_target_list, entry)
745 config_write_log_single(vty, dbg);
746
747 return 1;
748}
749
Maxc65c5b42017-03-15 13:20:23 +0100750void logging_vty_add_cmds()
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200751{
752 install_element_ve(&enable_logging_cmd);
753 install_element_ve(&disable_logging_cmd);
754 install_element_ve(&logging_fltr_all_cmd);
755 install_element_ve(&logging_use_clr_cmd);
756 install_element_ve(&logging_prnt_timestamp_cmd);
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100757 install_element_ve(&logging_prnt_ext_timestamp_cmd);
758 install_element_ve(&logging_prnt_cat_cmd);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200759 install_element_ve(&logging_set_category_mask_cmd);
Holger Hans Peter Freyther146d1d32011-10-03 23:15:41 +0200760 install_element_ve(&logging_set_category_mask_old_cmd);
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100761
762 /* Logging level strings are generated dynamically. */
Maxc65c5b42017-03-15 13:20:23 +0100763 logging_level_cmd.string = log_vty_command_string();
764 logging_level_cmd.doc = log_vty_command_description();
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200765 install_element_ve(&logging_level_cmd);
766 install_element_ve(&show_logging_vty_cmd);
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000767 install_element_ve(&show_alarms_cmd);
Harald Welte28222962011-02-18 20:37:04 +0100768
769 install_node(&cfg_log_node, config_write_log);
Jacob Erlbeck0c987bd2013-09-06 16:52:00 +0200770 vty_install_default(CFG_LOG_NODE);
Harald Weltea62648b2011-02-18 21:03:27 +0100771 install_element(CFG_LOG_NODE, &logging_fltr_all_cmd);
772 install_element(CFG_LOG_NODE, &logging_use_clr_cmd);
773 install_element(CFG_LOG_NODE, &logging_prnt_timestamp_cmd);
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100774 install_element(CFG_LOG_NODE, &logging_prnt_ext_timestamp_cmd);
775 install_element(CFG_LOG_NODE, &logging_prnt_cat_cmd);
Harald Weltea62648b2011-02-18 21:03:27 +0100776 install_element(CFG_LOG_NODE, &logging_level_cmd);
Harald Welte28222962011-02-18 20:37:04 +0100777
778 install_element(CONFIG_NODE, &cfg_log_stderr_cmd);
779 install_element(CONFIG_NODE, &cfg_no_log_stderr_cmd);
780 install_element(CONFIG_NODE, &cfg_log_file_cmd);
781 install_element(CONFIG_NODE, &cfg_no_log_file_cmd);
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000782 install_element(CONFIG_NODE, &cfg_log_alarms_cmd);
783 install_element(CONFIG_NODE, &cfg_no_log_alarms_cmd);
Harald Welte28222962011-02-18 20:37:04 +0100784#ifdef HAVE_SYSLOG_H
785 install_element(CONFIG_NODE, &cfg_log_syslog_cmd);
786 install_element(CONFIG_NODE, &cfg_log_syslog_local_cmd);
787 install_element(CONFIG_NODE, &cfg_no_log_syslog_cmd);
788#endif
Harald Welteaa00f992016-12-02 15:30:02 +0100789 install_element(CONFIG_NODE, &cfg_log_gsmtap_cmd);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200790}
Harald Welte96e2a002017-06-12 21:44:18 +0200791
792/* @} */