blob: d2fca819fcf1fe7d12d20e0d5ee5b6e3e72a7faa [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 Welte3fb0b6f2010-05-19 19:02:52 +020032
33#include <osmocom/vty/command.h>
34#include <osmocom/vty/buffer.h>
35#include <osmocom/vty/vty.h>
36#include <osmocom/vty/telnet_interface.h>
37#include <osmocom/vty/logging.h>
38
Harald Welte28222962011-02-18 20:37:04 +010039#define LOG_STR "Configure logging sub-system\n"
40
Harald Welte4ebdf742010-05-19 19:54:00 +020041extern const struct log_info *osmo_log_info;
Harald Welte3fb0b6f2010-05-19 19:02:52 +020042
Harald Welte76e72ab2011-02-17 15:52:39 +010043static void _vty_output(struct log_target *tgt,
44 unsigned int level, const char *line)
Harald Welte3fb0b6f2010-05-19 19:02:52 +020045{
46 struct vty *vty = tgt->tgt_vty.vty;
47 vty_out(vty, "%s", line);
48 /* This is an ugly hack, but there is no easy way... */
49 if (strchr(line, '\n'))
50 vty_out(vty, "\r");
51}
52
53struct log_target *log_target_create_vty(struct vty *vty)
54{
55 struct log_target *target;
56
57 target = log_target_create();
58 if (!target)
59 return NULL;
60
61 target->tgt_vty.vty = vty;
62 target->output = _vty_output;
63 return target;
64}
65
66DEFUN(enable_logging,
67 enable_logging_cmd,
68 "logging enable",
69 LOGGING_STR
70 "Enables logging to this vty\n")
71{
72 struct telnet_connection *conn;
73
74 conn = (struct telnet_connection *) vty->priv;
75 if (conn->dbg) {
76 vty_out(vty, "Logging already enabled.%s", VTY_NEWLINE);
77 return CMD_WARNING;
78 }
79
80 conn->dbg = log_target_create_vty(vty);
81 if (!conn->dbg)
82 return CMD_WARNING;
83
84 log_add_target(conn->dbg);
85 return CMD_SUCCESS;
86}
87
Harald Weltea62648b2011-02-18 21:03:27 +010088struct log_target *osmo_log_vty2tgt(struct vty *vty)
89{
90 struct telnet_connection *conn;
91
92 if (vty->node == CFG_LOG_NODE)
93 return vty->index;
94
95
96 conn = (struct telnet_connection *) vty->priv;
97 if (!conn->dbg)
98 vty_out(vty, "Logging was not enabled.%s", VTY_NEWLINE);
99
100 return conn->dbg;
101}
102
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200103DEFUN(logging_fltr_all,
104 logging_fltr_all_cmd,
105 "logging filter all (0|1)",
106 LOGGING_STR FILTER_STR
107 "Do you want to log all messages?\n"
108 "Only print messages matched by other filters\n"
109 "Bypass filter and print all messages\n")
110{
Harald Weltea62648b2011-02-18 21:03:27 +0100111 struct log_target *tgt = osmo_log_vty2tgt(vty);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200112
Harald Weltea62648b2011-02-18 21:03:27 +0100113 if (!tgt)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200114 return CMD_WARNING;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200115
Harald Weltea62648b2011-02-18 21:03:27 +0100116 log_set_all_filter(tgt, atoi(argv[0]));
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200117 return CMD_SUCCESS;
118}
119
120DEFUN(logging_use_clr,
121 logging_use_clr_cmd,
122 "logging color (0|1)",
123 LOGGING_STR "Configure color-printing for log messages\n"
124 "Don't use color for printing messages\n"
125 "Use color for printing messages\n")
126{
Harald Weltea62648b2011-02-18 21:03:27 +0100127 struct log_target *tgt = osmo_log_vty2tgt(vty);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200128
Harald Weltea62648b2011-02-18 21:03:27 +0100129 if (!tgt)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200130 return CMD_WARNING;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200131
Harald Weltea62648b2011-02-18 21:03:27 +0100132 log_set_use_color(tgt, atoi(argv[0]));
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200133 return CMD_SUCCESS;
134}
135
136DEFUN(logging_prnt_timestamp,
137 logging_prnt_timestamp_cmd,
138 "logging timestamp (0|1)",
139 LOGGING_STR "Configure log message timestamping\n"
140 "Don't prefix each log message\n"
141 "Prefix each log message with current timestamp\n")
142{
Harald Weltea62648b2011-02-18 21:03:27 +0100143 struct log_target *tgt = osmo_log_vty2tgt(vty);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200144
Harald Weltea62648b2011-02-18 21:03:27 +0100145 if (!tgt)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200146 return CMD_WARNING;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200147
Harald Weltea62648b2011-02-18 21:03:27 +0100148 log_set_print_timestamp(tgt, atoi(argv[0]));
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200149 return CMD_SUCCESS;
150}
151
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100152DEFUN(logging_prnt_ext_timestamp,
153 logging_prnt_ext_timestamp_cmd,
154 "logging print extended-timestamp (0|1)",
155 LOGGING_STR "Log output settings\n"
156 "Configure log message timestamping\n"
157 "Don't prefix each log message\n"
158 "Prefix each log message with current timestamp with YYYYMMDDhhmmssnnn\n")
159{
160 struct log_target *tgt = osmo_log_vty2tgt(vty);
161
162 if (!tgt)
163 return CMD_WARNING;
164
165 log_set_print_extended_timestamp(tgt, atoi(argv[0]));
166 return CMD_SUCCESS;
167}
168
169DEFUN(logging_prnt_cat,
170 logging_prnt_cat_cmd,
171 "logging print category (0|1)",
172 LOGGING_STR "Log output settings\n"
173 "Configure log message\n"
174 "Don't prefix each log message\n"
175 "Prefix each log message with category/subsystem name\n")
176{
177 struct log_target *tgt = osmo_log_vty2tgt(vty);
178
179 if (!tgt)
180 return CMD_WARNING;
181
182 log_set_print_category(tgt, atoi(argv[0]));
183 return CMD_SUCCESS;
184}
185
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200186DEFUN(logging_level,
187 logging_level_cmd,
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100188 NULL, /* cmdstr is dynamically set in logging_vty_add_cmds(). */
189 NULL) /* same thing for helpstr. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200190{
Harald Weltea62648b2011-02-18 21:03:27 +0100191 int category = log_parse_category(argv[0]);
192 int level = log_parse_level(argv[1]);
193 struct log_target *tgt = osmo_log_vty2tgt(vty);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200194
Harald Weltea62648b2011-02-18 21:03:27 +0100195 if (!tgt)
196 return CMD_WARNING;
197
198 if (level < 0) {
199 vty_out(vty, "Invalid level `%s'%s", argv[1], VTY_NEWLINE);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200200 return CMD_WARNING;
201 }
202
Harald Weltea62648b2011-02-18 21:03:27 +0100203 /* Check for special case where we want to set global log level */
204 if (!strcmp(argv[0], "all")) {
205 log_set_log_level(tgt, level);
206 return CMD_SUCCESS;
207 }
208
209 if (category < 0) {
210 vty_out(vty, "Invalid category `%s'%s", argv[0], VTY_NEWLINE);
211 return CMD_WARNING;
212 }
213
214 tgt->categories[category].enabled = 1;
215 tgt->categories[category].loglevel = level;
216
217 return CMD_SUCCESS;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200218}
219
220DEFUN(logging_set_category_mask,
221 logging_set_category_mask_cmd,
Holger Hans Peter Freyther146d1d32011-10-03 23:15:41 +0200222 "logging set-log-mask MASK",
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200223 LOGGING_STR
Holger Hans Peter Freyther146d1d32011-10-03 23:15:41 +0200224 "Set the logmask of this logging target\n"
225 "The logmask to use\n")
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200226{
Harald Weltea62648b2011-02-18 21:03:27 +0100227 struct log_target *tgt = osmo_log_vty2tgt(vty);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200228
Harald Weltea62648b2011-02-18 21:03:27 +0100229 if (!tgt)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200230 return CMD_WARNING;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200231
Harald Weltea62648b2011-02-18 21:03:27 +0100232 log_parse_category_mask(tgt, argv[0]);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200233 return CMD_SUCCESS;
234}
235
Holger Hans Peter Freyther146d1d32011-10-03 23:15:41 +0200236ALIAS_DEPRECATED(logging_set_category_mask,
237 logging_set_category_mask_old_cmd,
238 "logging set log mask MASK",
239 LOGGING_STR
240 "Decide which categories to output.\n"
241 "Log commands\n" "Mask commands\n" "The logmask to use\n");
242
243
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200244DEFUN(diable_logging,
245 disable_logging_cmd,
246 "logging disable",
247 LOGGING_STR
248 "Disables logging to this vty\n")
249{
Harald Weltea62648b2011-02-18 21:03:27 +0100250 struct log_target *tgt = osmo_log_vty2tgt(vty);
251 struct telnet_connection *conn = (struct telnet_connection *) vty->priv;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200252
Harald Weltea62648b2011-02-18 21:03:27 +0100253 if (!tgt)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200254 return CMD_WARNING;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200255
Harald Weltea62648b2011-02-18 21:03:27 +0100256 log_del_target(tgt);
257 talloc_free(tgt);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200258 conn->dbg = NULL;
Harald Weltea62648b2011-02-18 21:03:27 +0100259
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200260 return CMD_SUCCESS;
261}
262
263static void vty_print_logtarget(struct vty *vty, const struct log_info *info,
264 const struct log_target *tgt)
265{
266 unsigned int i;
267
268 vty_out(vty, " Global Loglevel: %s%s",
269 log_level_str(tgt->loglevel), VTY_NEWLINE);
270 vty_out(vty, " Use color: %s, Print Timestamp: %s%s",
271 tgt->use_color ? "On" : "Off",
272 tgt->print_timestamp ? "On" : "Off", VTY_NEWLINE);
273
274 vty_out(vty, " Log Level specific information:%s", VTY_NEWLINE);
275
276 for (i = 0; i < info->num_cat; i++) {
277 const struct log_category *cat = &tgt->categories[i];
Daniel Willmann55363a92016-11-15 10:05:51 +0100278 /* Skip categories that were not initialized */
279 if (!info->cat[i].name)
280 continue;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200281 vty_out(vty, " %-10s %-10s %-8s %s%s",
282 info->cat[i].name+1, log_level_str(cat->loglevel),
283 cat->enabled ? "Enabled" : "Disabled",
284 info->cat[i].description,
285 VTY_NEWLINE);
286 }
Harald Welte6d2d4d62013-03-10 09:53:24 +0000287
288 vty_out(vty, " Log Filter 'ALL': %s%s",
289 tgt->filter_map & LOG_FILTER_ALL ? "Enabled" : "Disabled",
290 VTY_NEWLINE);
291
Harald Weltefb84f322013-06-06 07:33:54 +0200292 /* print application specific filters */
293 if (info->print_fn)
294 info->print_fn(vty, info, tgt);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200295}
296
297#define SHOW_LOG_STR "Show current logging configuration\n"
298
299DEFUN(show_logging_vty,
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000300 show_logging_vty_cmd,
301 "show logging vty",
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200302 SHOW_STR SHOW_LOG_STR
303 "Show current logging configuration for this vty\n")
304{
Harald Weltea62648b2011-02-18 21:03:27 +0100305 struct log_target *tgt = osmo_log_vty2tgt(vty);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200306
Harald Weltea62648b2011-02-18 21:03:27 +0100307 if (!tgt)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200308 return CMD_WARNING;
Harald Weltea62648b2011-02-18 21:03:27 +0100309
310 vty_print_logtarget(vty, osmo_log_info, tgt);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200311
312 return CMD_SUCCESS;
313}
314
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000315DEFUN(show_alarms,
316 show_alarms_cmd,
317 "show alarms",
318 SHOW_STR SHOW_LOG_STR
319 "Show the contents of the logging ringbuffer\n")
320{
321 int i, num_alarms;
322 struct osmo_strrb *rb;
323 struct log_target *tgt = log_target_find(LOG_TGT_TYPE_STRRB, NULL);
324 if (!tgt) {
325 vty_out(vty, "%% No alarms, run 'log alarms <2-32700>'%s",
326 VTY_NEWLINE);
327 return CMD_WARNING;
328 }
329
330 rb = tgt->tgt_rb.rb;
331 num_alarms = osmo_strrb_elements(rb);
332
333 vty_out(vty, "%% Showing %i alarms%s", num_alarms, VTY_NEWLINE);
334
335 for (i = 0; i < num_alarms; i++)
336 vty_out(vty, "%% %s%s", osmo_strrb_get_nth(rb, i),
337 VTY_NEWLINE);
338
339 return CMD_SUCCESS;
340}
341
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200342gDEFUN(cfg_description, cfg_description_cmd,
343 "description .TEXT",
Holger Hans Peter Freytherc9b3e062012-07-25 13:02:49 +0200344 "Save human-readable decription of the object\n"
345 "Text until the end of the line\n")
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200346{
347 char **dptr = vty->index_sub;
348
349 if (!dptr) {
350 vty_out(vty, "vty->index_sub == NULL%s", VTY_NEWLINE);
351 return CMD_WARNING;
352 }
353
Holger Hans Peter Freytherff0670e2011-02-24 14:20:41 +0100354 if (*dptr)
355 talloc_free(*dptr);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200356 *dptr = argv_concat(argv, argc, 0);
Holger Hans Peter Freyther6a75d162013-07-14 09:07:11 +0200357 if (!*dptr)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200358 return CMD_WARNING;
359
360 return CMD_SUCCESS;
361}
362
363gDEFUN(cfg_no_description, cfg_no_description_cmd,
364 "no description",
365 NO_STR
366 "Remove description of the object\n")
367{
368 char **dptr = vty->index_sub;
369
370 if (!dptr) {
371 vty_out(vty, "vty->index_sub == NULL%s", VTY_NEWLINE);
372 return CMD_WARNING;
373 }
374
375 if (*dptr) {
376 talloc_free(*dptr);
377 *dptr = NULL;
378 }
379
380 return CMD_SUCCESS;
381}
382
Harald Welte28222962011-02-18 20:37:04 +0100383/* Support for configuration of log targets != the current vty */
384
385struct cmd_node cfg_log_node = {
386 CFG_LOG_NODE,
387 "%s(config-log)# ",
388 1
389};
390
Harald Welte28222962011-02-18 20:37:04 +0100391#ifdef HAVE_SYSLOG_H
392
393#include <syslog.h>
394
395static const int local_sysl_map[] = {
396 [0] = LOG_LOCAL0,
397 [1] = LOG_LOCAL1,
398 [2] = LOG_LOCAL2,
399 [3] = LOG_LOCAL3,
400 [4] = LOG_LOCAL4,
401 [5] = LOG_LOCAL5,
402 [6] = LOG_LOCAL6,
403 [7] = LOG_LOCAL7
404};
405
Harald Weltede79cee2011-02-24 23:47:57 +0100406/* From VTY core code */
407extern struct host host;
408
Harald Welte28222962011-02-18 20:37:04 +0100409static int _cfg_log_syslog(struct vty *vty, int facility)
410{
411 struct log_target *tgt;
412
413 /* First delete the old syslog target, if any */
414 tgt = log_target_find(LOG_TGT_TYPE_SYSLOG, NULL);
415 if (tgt)
416 log_target_destroy(tgt);
417
Harald Weltede79cee2011-02-24 23:47:57 +0100418 tgt = log_target_create_syslog(host.app_info->name, 0, facility);
Harald Welte28222962011-02-18 20:37:04 +0100419 if (!tgt) {
420 vty_out(vty, "%% Unable to open syslog%s", VTY_NEWLINE);
421 return CMD_WARNING;
422 }
423 log_add_target(tgt);
424
425 vty->index = tgt;
426 vty->node = CFG_LOG_NODE;
427
428 return CMD_SUCCESS;
429}
430
431DEFUN(cfg_log_syslog_local, cfg_log_syslog_local_cmd,
432 "log syslog local <0-7>",
433 LOG_STR "Logging via syslog\n" "Syslog LOCAL facility\n"
434 "Local facility number\n")
435{
436 int local = atoi(argv[0]);
437 int facility = local_sysl_map[local];
438
439 return _cfg_log_syslog(vty, facility);
440}
441
442static struct value_string sysl_level_names[] = {
443 { LOG_AUTHPRIV, "authpriv" },
444 { LOG_CRON, "cron" },
445 { LOG_DAEMON, "daemon" },
446 { LOG_FTP, "ftp" },
447 { LOG_LPR, "lpr" },
448 { LOG_MAIL, "mail" },
449 { LOG_NEWS, "news" },
450 { LOG_USER, "user" },
451 { LOG_UUCP, "uucp" },
452 /* only for value -> string conversion */
453 { LOG_LOCAL0, "local 0" },
454 { LOG_LOCAL1, "local 1" },
455 { LOG_LOCAL2, "local 2" },
456 { LOG_LOCAL3, "local 3" },
457 { LOG_LOCAL4, "local 4" },
458 { LOG_LOCAL5, "local 5" },
459 { LOG_LOCAL6, "local 6" },
460 { LOG_LOCAL7, "local 7" },
461 { 0, NULL }
462};
463
464DEFUN(cfg_log_syslog, cfg_log_syslog_cmd,
465 "log syslog (authpriv|cron|daemon|ftp|lpr|mail|news|user|uucp)",
Holger Hans Peter Freythera4463fd2011-10-03 23:17:36 +0200466 LOG_STR "Logging via syslog\n"
467 "Security/authorization messages facility\n"
468 "Clock daemon (cron/at) facility\n"
469 "General system daemon facility\n"
470 "Ftp daemon facility\n"
471 "Line printer facility\n"
472 "Mail facility\n"
473 "News facility\n"
474 "Generic facility\n"
475 "UUCP facility\n")
Harald Welte28222962011-02-18 20:37:04 +0100476{
477 int facility = get_string_value(sysl_level_names, argv[0]);
478
479 return _cfg_log_syslog(vty, facility);
480}
481
482DEFUN(cfg_no_log_syslog, cfg_no_log_syslog_cmd,
483 "no log syslog",
484 NO_STR LOG_STR "Logging via syslog\n")
485{
486 struct log_target *tgt;
487
488 tgt = log_target_find(LOG_TGT_TYPE_SYSLOG, NULL);
489 if (!tgt) {
490 vty_out(vty, "%% No syslog target found%s",
491 VTY_NEWLINE);
492 return CMD_WARNING;
493 }
494
495 log_target_destroy(tgt);
496
497 return CMD_SUCCESS;
498}
499#endif /* HAVE_SYSLOG_H */
500
501DEFUN(cfg_log_stderr, cfg_log_stderr_cmd,
502 "log stderr",
503 LOG_STR "Logging via STDERR of the process\n")
504{
505 struct log_target *tgt;
506
507 tgt = log_target_find(LOG_TGT_TYPE_STDERR, NULL);
508 if (!tgt) {
509 tgt = log_target_create_stderr();
510 if (!tgt) {
511 vty_out(vty, "%% Unable to create stderr log%s",
512 VTY_NEWLINE);
513 return CMD_WARNING;
514 }
515 log_add_target(tgt);
516 }
517
518 vty->index = tgt;
519 vty->node = CFG_LOG_NODE;
520
521 return CMD_SUCCESS;
522}
523
524DEFUN(cfg_no_log_stderr, cfg_no_log_stderr_cmd,
525 "no log stderr",
526 NO_STR LOG_STR "Logging via STDERR of the process\n")
527{
528 struct log_target *tgt;
529
530 tgt = log_target_find(LOG_TGT_TYPE_STDERR, NULL);
531 if (!tgt) {
532 vty_out(vty, "%% No stderr logging active%s", VTY_NEWLINE);
533 return CMD_WARNING;
534 }
535
536 log_target_destroy(tgt);
537
538 return CMD_SUCCESS;
539}
540
541DEFUN(cfg_log_file, cfg_log_file_cmd,
542 "log file .FILENAME",
543 LOG_STR "Logging to text file\n" "Filename\n")
544{
545 const char *fname = argv[0];
546 struct log_target *tgt;
547
548 tgt = log_target_find(LOG_TGT_TYPE_FILE, fname);
549 if (!tgt) {
550 tgt = log_target_create_file(fname);
551 if (!tgt) {
552 vty_out(vty, "%% Unable to create file `%s'%s",
553 fname, VTY_NEWLINE);
554 return CMD_WARNING;
555 }
556 log_add_target(tgt);
557 }
558
559 vty->index = tgt;
560 vty->node = CFG_LOG_NODE;
561
562 return CMD_SUCCESS;
563}
564
565
566DEFUN(cfg_no_log_file, cfg_no_log_file_cmd,
567 "no log file .FILENAME",
568 NO_STR LOG_STR "Logging to text file\n" "Filename\n")
569{
570 const char *fname = argv[0];
571 struct log_target *tgt;
572
573 tgt = log_target_find(LOG_TGT_TYPE_FILE, fname);
574 if (!tgt) {
575 vty_out(vty, "%% No such log file `%s'%s",
576 fname, VTY_NEWLINE);
577 return CMD_WARNING;
578 }
579
580 log_target_destroy(tgt);
581
582 return CMD_SUCCESS;
583}
584
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000585DEFUN(cfg_log_alarms, cfg_log_alarms_cmd,
586 "log alarms <2-32700>",
587 LOG_STR "Logging alarms to osmo_strrb\n"
588 "Maximum number of messages to log\n")
589{
590 struct log_target *tgt;
591 unsigned int rbsize = atoi(argv[0]);
592
593 tgt = log_target_find(LOG_TGT_TYPE_STRRB, NULL);
594 if (tgt)
595 log_target_destroy(tgt);
596
597 tgt = log_target_create_rb(rbsize);
598 if (!tgt) {
599 vty_out(vty, "%% Unable to create osmo_strrb (size %u)%s",
600 rbsize, VTY_NEWLINE);
601 return CMD_WARNING;
602 }
603 log_add_target(tgt);
604
605 vty->index = tgt;
606 vty->node = CFG_LOG_NODE;
607
608 return CMD_SUCCESS;
609}
610
611DEFUN(cfg_no_log_alarms, cfg_no_log_alarms_cmd,
612 "no log alarms",
613 NO_STR LOG_STR "Logging alarms to osmo_strrb\n")
614{
615 struct log_target *tgt;
616
617 tgt = log_target_find(LOG_TGT_TYPE_STRRB, NULL);
618 if (!tgt) {
619 vty_out(vty, "%% No osmo_strrb target found%s", VTY_NEWLINE);
620 return CMD_WARNING;
621 }
622
623 log_target_destroy(tgt);
624
625 return CMD_SUCCESS;
626}
627
Harald Welte28222962011-02-18 20:37:04 +0100628static int config_write_log_single(struct vty *vty, struct log_target *tgt)
629{
630 int i;
631 char level_lower[32];
632
633 switch (tgt->type) {
634 case LOG_TGT_TYPE_VTY:
635 return 1;
636 break;
637 case LOG_TGT_TYPE_STDERR:
638 vty_out(vty, "log stderr%s", VTY_NEWLINE);
639 break;
640 case LOG_TGT_TYPE_SYSLOG:
641#ifdef HAVE_SYSLOG_H
642 vty_out(vty, "log syslog %s%s",
643 get_value_string(sysl_level_names,
644 tgt->tgt_syslog.facility),
645 VTY_NEWLINE);
646#endif
647 break;
648 case LOG_TGT_TYPE_FILE:
649 vty_out(vty, "log file %s%s", tgt->tgt_file.fname, VTY_NEWLINE);
650 break;
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000651 case LOG_TGT_TYPE_STRRB:
652 vty_out(vty, "log alarms %zu%s",
653 log_target_rb_avail_size(tgt), VTY_NEWLINE);
654 break;
Harald Welte28222962011-02-18 20:37:04 +0100655 }
656
Harald Welte2da47f12012-10-22 19:31:54 +0200657 vty_out(vty, " logging filter all %u%s",
658 tgt->filter_map & LOG_FILTER_ALL ? 1 : 0, VTY_NEWLINE);
Harald Weltefb84f322013-06-06 07:33:54 +0200659 /* save filters outside of libosmocore, i.e. in app code */
660 if (osmo_log_info->save_fn)
661 osmo_log_info->save_fn(vty, osmo_log_info, tgt);
Harald Welte2da47f12012-10-22 19:31:54 +0200662
Harald Welte28222962011-02-18 20:37:04 +0100663 vty_out(vty, " logging color %u%s", tgt->use_color ? 1 : 0,
664 VTY_NEWLINE);
Holger Hans Peter Freyther879acef2015-01-27 11:06:51 +0100665 vty_out(vty, " logging print category %d%s",
Michael McTernan78933462015-03-20 15:29:25 +0100666 tgt->print_category ? 1 : 0, VTY_NEWLINE);
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100667 if (tgt->print_ext_timestamp)
668 vty_out(vty, " logging print extended-timestamp 1%s", VTY_NEWLINE);
669 else
670 vty_out(vty, " logging timestamp %u%s",
671 tgt->print_timestamp ? 1 : 0, VTY_NEWLINE);
Harald Welte28222962011-02-18 20:37:04 +0100672
673 /* stupid old osmo logging API uses uppercase strings... */
674 osmo_str2lower(level_lower, log_level_str(tgt->loglevel));
675 vty_out(vty, " logging level all %s%s", level_lower, VTY_NEWLINE);
676
677 for (i = 0; i < osmo_log_info->num_cat; i++) {
678 const struct log_category *cat = &tgt->categories[i];
679 char cat_lower[32];
680
Harald Welte1a02cfc2013-03-19 10:37:39 +0100681 /* skip empty entries in the array */
682 if (!osmo_log_info->cat[i].name)
683 continue;
684
Harald Welte28222962011-02-18 20:37:04 +0100685 /* stupid old osmo logging API uses uppercase strings... */
686 osmo_str2lower(cat_lower, osmo_log_info->cat[i].name+1);
687 osmo_str2lower(level_lower, log_level_str(cat->loglevel));
688
689 vty_out(vty, " logging level %s %s%s", cat_lower, level_lower,
690 VTY_NEWLINE);
691 }
692
693 /* FIXME: levels */
694
695 return 1;
696}
697
698static int config_write_log(struct vty *vty)
699{
700 struct log_target *dbg = vty->index;
701
702 llist_for_each_entry(dbg, &osmo_log_target_list, entry)
703 config_write_log_single(vty, dbg);
704
705 return 1;
706}
707
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100708void logging_vty_add_cmds(const struct log_info *cat)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200709{
710 install_element_ve(&enable_logging_cmd);
711 install_element_ve(&disable_logging_cmd);
712 install_element_ve(&logging_fltr_all_cmd);
713 install_element_ve(&logging_use_clr_cmd);
714 install_element_ve(&logging_prnt_timestamp_cmd);
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100715 install_element_ve(&logging_prnt_ext_timestamp_cmd);
716 install_element_ve(&logging_prnt_cat_cmd);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200717 install_element_ve(&logging_set_category_mask_cmd);
Holger Hans Peter Freyther146d1d32011-10-03 23:15:41 +0200718 install_element_ve(&logging_set_category_mask_old_cmd);
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100719
720 /* Logging level strings are generated dynamically. */
721 logging_level_cmd.string = log_vty_command_string(cat);
722 logging_level_cmd.doc = log_vty_command_description(cat);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200723 install_element_ve(&logging_level_cmd);
724 install_element_ve(&show_logging_vty_cmd);
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000725 install_element_ve(&show_alarms_cmd);
Harald Welte28222962011-02-18 20:37:04 +0100726
727 install_node(&cfg_log_node, config_write_log);
Jacob Erlbeck0c987bd2013-09-06 16:52:00 +0200728 vty_install_default(CFG_LOG_NODE);
Harald Weltea62648b2011-02-18 21:03:27 +0100729 install_element(CFG_LOG_NODE, &logging_fltr_all_cmd);
730 install_element(CFG_LOG_NODE, &logging_use_clr_cmd);
731 install_element(CFG_LOG_NODE, &logging_prnt_timestamp_cmd);
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100732 install_element(CFG_LOG_NODE, &logging_prnt_ext_timestamp_cmd);
733 install_element(CFG_LOG_NODE, &logging_prnt_cat_cmd);
Harald Weltea62648b2011-02-18 21:03:27 +0100734 install_element(CFG_LOG_NODE, &logging_level_cmd);
Harald Welte28222962011-02-18 20:37:04 +0100735
736 install_element(CONFIG_NODE, &cfg_log_stderr_cmd);
737 install_element(CONFIG_NODE, &cfg_no_log_stderr_cmd);
738 install_element(CONFIG_NODE, &cfg_log_file_cmd);
739 install_element(CONFIG_NODE, &cfg_no_log_file_cmd);
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000740 install_element(CONFIG_NODE, &cfg_log_alarms_cmd);
741 install_element(CONFIG_NODE, &cfg_no_log_alarms_cmd);
Harald Welte28222962011-02-18 20:37:04 +0100742#ifdef HAVE_SYSLOG_H
743 install_element(CONFIG_NODE, &cfg_log_syslog_cmd);
744 install_element(CONFIG_NODE, &cfg_log_syslog_local_cmd);
745 install_element(CONFIG_NODE, &cfg_no_log_syslog_cmd);
746#endif
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200747}