blob: 47877fe988d438c7e317bc8e876be9b1d886e972 [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>
3 * (C) 2009-2010 by Holger Hans Peter Freyther
4 * 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
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200152DEFUN(logging_level,
153 logging_level_cmd,
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100154 NULL, /* cmdstr is dynamically set in logging_vty_add_cmds(). */
155 NULL) /* same thing for helpstr. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200156{
Harald Weltea62648b2011-02-18 21:03:27 +0100157 int category = log_parse_category(argv[0]);
158 int level = log_parse_level(argv[1]);
159 struct log_target *tgt = osmo_log_vty2tgt(vty);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200160
Harald Weltea62648b2011-02-18 21:03:27 +0100161 if (!tgt)
162 return CMD_WARNING;
163
164 if (level < 0) {
165 vty_out(vty, "Invalid level `%s'%s", argv[1], VTY_NEWLINE);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200166 return CMD_WARNING;
167 }
168
Harald Weltea62648b2011-02-18 21:03:27 +0100169 /* Check for special case where we want to set global log level */
170 if (!strcmp(argv[0], "all")) {
171 log_set_log_level(tgt, level);
172 return CMD_SUCCESS;
173 }
174
175 if (category < 0) {
176 vty_out(vty, "Invalid category `%s'%s", argv[0], VTY_NEWLINE);
177 return CMD_WARNING;
178 }
179
180 tgt->categories[category].enabled = 1;
181 tgt->categories[category].loglevel = level;
182
183 return CMD_SUCCESS;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200184}
185
186DEFUN(logging_set_category_mask,
187 logging_set_category_mask_cmd,
Holger Hans Peter Freyther146d1d32011-10-03 23:15:41 +0200188 "logging set-log-mask MASK",
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200189 LOGGING_STR
Holger Hans Peter Freyther146d1d32011-10-03 23:15:41 +0200190 "Set the logmask of this logging target\n"
191 "The logmask to use\n")
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200192{
Harald Weltea62648b2011-02-18 21:03:27 +0100193 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)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200196 return CMD_WARNING;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200197
Harald Weltea62648b2011-02-18 21:03:27 +0100198 log_parse_category_mask(tgt, argv[0]);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200199 return CMD_SUCCESS;
200}
201
Holger Hans Peter Freyther146d1d32011-10-03 23:15:41 +0200202ALIAS_DEPRECATED(logging_set_category_mask,
203 logging_set_category_mask_old_cmd,
204 "logging set log mask MASK",
205 LOGGING_STR
206 "Decide which categories to output.\n"
207 "Log commands\n" "Mask commands\n" "The logmask to use\n");
208
209
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200210DEFUN(diable_logging,
211 disable_logging_cmd,
212 "logging disable",
213 LOGGING_STR
214 "Disables logging to this vty\n")
215{
Harald Weltea62648b2011-02-18 21:03:27 +0100216 struct log_target *tgt = osmo_log_vty2tgt(vty);
217 struct telnet_connection *conn = (struct telnet_connection *) vty->priv;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200218
Harald Weltea62648b2011-02-18 21:03:27 +0100219 if (!tgt)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200220 return CMD_WARNING;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200221
Harald Weltea62648b2011-02-18 21:03:27 +0100222 log_del_target(tgt);
223 talloc_free(tgt);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200224 conn->dbg = NULL;
Harald Weltea62648b2011-02-18 21:03:27 +0100225
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200226 return CMD_SUCCESS;
227}
228
229static void vty_print_logtarget(struct vty *vty, const struct log_info *info,
230 const struct log_target *tgt)
231{
232 unsigned int i;
233
234 vty_out(vty, " Global Loglevel: %s%s",
235 log_level_str(tgt->loglevel), VTY_NEWLINE);
236 vty_out(vty, " Use color: %s, Print Timestamp: %s%s",
237 tgt->use_color ? "On" : "Off",
238 tgt->print_timestamp ? "On" : "Off", VTY_NEWLINE);
239
240 vty_out(vty, " Log Level specific information:%s", VTY_NEWLINE);
241
242 for (i = 0; i < info->num_cat; i++) {
243 const struct log_category *cat = &tgt->categories[i];
244 vty_out(vty, " %-10s %-10s %-8s %s%s",
245 info->cat[i].name+1, log_level_str(cat->loglevel),
246 cat->enabled ? "Enabled" : "Disabled",
247 info->cat[i].description,
248 VTY_NEWLINE);
249 }
Harald Welte6d2d4d62013-03-10 09:53:24 +0000250
251 vty_out(vty, " Log Filter 'ALL': %s%s",
252 tgt->filter_map & LOG_FILTER_ALL ? "Enabled" : "Disabled",
253 VTY_NEWLINE);
254
Harald Weltefb84f322013-06-06 07:33:54 +0200255 /* print application specific filters */
256 if (info->print_fn)
257 info->print_fn(vty, info, tgt);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200258}
259
260#define SHOW_LOG_STR "Show current logging configuration\n"
261
262DEFUN(show_logging_vty,
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000263 show_logging_vty_cmd,
264 "show logging vty",
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200265 SHOW_STR SHOW_LOG_STR
266 "Show current logging configuration for this vty\n")
267{
Harald Weltea62648b2011-02-18 21:03:27 +0100268 struct log_target *tgt = osmo_log_vty2tgt(vty);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200269
Harald Weltea62648b2011-02-18 21:03:27 +0100270 if (!tgt)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200271 return CMD_WARNING;
Harald Weltea62648b2011-02-18 21:03:27 +0100272
273 vty_print_logtarget(vty, osmo_log_info, tgt);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200274
275 return CMD_SUCCESS;
276}
277
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000278DEFUN(show_alarms,
279 show_alarms_cmd,
280 "show alarms",
281 SHOW_STR SHOW_LOG_STR
282 "Show the contents of the logging ringbuffer\n")
283{
284 int i, num_alarms;
285 struct osmo_strrb *rb;
286 struct log_target *tgt = log_target_find(LOG_TGT_TYPE_STRRB, NULL);
287 if (!tgt) {
288 vty_out(vty, "%% No alarms, run 'log alarms <2-32700>'%s",
289 VTY_NEWLINE);
290 return CMD_WARNING;
291 }
292
293 rb = tgt->tgt_rb.rb;
294 num_alarms = osmo_strrb_elements(rb);
295
296 vty_out(vty, "%% Showing %i alarms%s", num_alarms, VTY_NEWLINE);
297
298 for (i = 0; i < num_alarms; i++)
299 vty_out(vty, "%% %s%s", osmo_strrb_get_nth(rb, i),
300 VTY_NEWLINE);
301
302 return CMD_SUCCESS;
303}
304
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200305gDEFUN(cfg_description, cfg_description_cmd,
306 "description .TEXT",
Holger Hans Peter Freytherc9b3e062012-07-25 13:02:49 +0200307 "Save human-readable decription of the object\n"
308 "Text until the end of the line\n")
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200309{
310 char **dptr = vty->index_sub;
311
312 if (!dptr) {
313 vty_out(vty, "vty->index_sub == NULL%s", VTY_NEWLINE);
314 return CMD_WARNING;
315 }
316
Holger Hans Peter Freytherff0670e2011-02-24 14:20:41 +0100317 if (*dptr)
318 talloc_free(*dptr);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200319 *dptr = argv_concat(argv, argc, 0);
Holger Hans Peter Freyther6a75d162013-07-14 09:07:11 +0200320 if (!*dptr)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200321 return CMD_WARNING;
322
323 return CMD_SUCCESS;
324}
325
326gDEFUN(cfg_no_description, cfg_no_description_cmd,
327 "no description",
328 NO_STR
329 "Remove description of the object\n")
330{
331 char **dptr = vty->index_sub;
332
333 if (!dptr) {
334 vty_out(vty, "vty->index_sub == NULL%s", VTY_NEWLINE);
335 return CMD_WARNING;
336 }
337
338 if (*dptr) {
339 talloc_free(*dptr);
340 *dptr = NULL;
341 }
342
343 return CMD_SUCCESS;
344}
345
Harald Welte28222962011-02-18 20:37:04 +0100346/* Support for configuration of log targets != the current vty */
347
348struct cmd_node cfg_log_node = {
349 CFG_LOG_NODE,
350 "%s(config-log)# ",
351 1
352};
353
Harald Welte28222962011-02-18 20:37:04 +0100354#ifdef HAVE_SYSLOG_H
355
356#include <syslog.h>
357
358static const int local_sysl_map[] = {
359 [0] = LOG_LOCAL0,
360 [1] = LOG_LOCAL1,
361 [2] = LOG_LOCAL2,
362 [3] = LOG_LOCAL3,
363 [4] = LOG_LOCAL4,
364 [5] = LOG_LOCAL5,
365 [6] = LOG_LOCAL6,
366 [7] = LOG_LOCAL7
367};
368
Harald Weltede79cee2011-02-24 23:47:57 +0100369/* From VTY core code */
370extern struct host host;
371
Harald Welte28222962011-02-18 20:37:04 +0100372static int _cfg_log_syslog(struct vty *vty, int facility)
373{
374 struct log_target *tgt;
375
376 /* First delete the old syslog target, if any */
377 tgt = log_target_find(LOG_TGT_TYPE_SYSLOG, NULL);
378 if (tgt)
379 log_target_destroy(tgt);
380
Harald Weltede79cee2011-02-24 23:47:57 +0100381 tgt = log_target_create_syslog(host.app_info->name, 0, facility);
Harald Welte28222962011-02-18 20:37:04 +0100382 if (!tgt) {
383 vty_out(vty, "%% Unable to open syslog%s", VTY_NEWLINE);
384 return CMD_WARNING;
385 }
386 log_add_target(tgt);
387
388 vty->index = tgt;
389 vty->node = CFG_LOG_NODE;
390
391 return CMD_SUCCESS;
392}
393
394DEFUN(cfg_log_syslog_local, cfg_log_syslog_local_cmd,
395 "log syslog local <0-7>",
396 LOG_STR "Logging via syslog\n" "Syslog LOCAL facility\n"
397 "Local facility number\n")
398{
399 int local = atoi(argv[0]);
400 int facility = local_sysl_map[local];
401
402 return _cfg_log_syslog(vty, facility);
403}
404
405static struct value_string sysl_level_names[] = {
406 { LOG_AUTHPRIV, "authpriv" },
407 { LOG_CRON, "cron" },
408 { LOG_DAEMON, "daemon" },
409 { LOG_FTP, "ftp" },
410 { LOG_LPR, "lpr" },
411 { LOG_MAIL, "mail" },
412 { LOG_NEWS, "news" },
413 { LOG_USER, "user" },
414 { LOG_UUCP, "uucp" },
415 /* only for value -> string conversion */
416 { LOG_LOCAL0, "local 0" },
417 { LOG_LOCAL1, "local 1" },
418 { LOG_LOCAL2, "local 2" },
419 { LOG_LOCAL3, "local 3" },
420 { LOG_LOCAL4, "local 4" },
421 { LOG_LOCAL5, "local 5" },
422 { LOG_LOCAL6, "local 6" },
423 { LOG_LOCAL7, "local 7" },
424 { 0, NULL }
425};
426
427DEFUN(cfg_log_syslog, cfg_log_syslog_cmd,
428 "log syslog (authpriv|cron|daemon|ftp|lpr|mail|news|user|uucp)",
Holger Hans Peter Freythera4463fd2011-10-03 23:17:36 +0200429 LOG_STR "Logging via syslog\n"
430 "Security/authorization messages facility\n"
431 "Clock daemon (cron/at) facility\n"
432 "General system daemon facility\n"
433 "Ftp daemon facility\n"
434 "Line printer facility\n"
435 "Mail facility\n"
436 "News facility\n"
437 "Generic facility\n"
438 "UUCP facility\n")
Harald Welte28222962011-02-18 20:37:04 +0100439{
440 int facility = get_string_value(sysl_level_names, argv[0]);
441
442 return _cfg_log_syslog(vty, facility);
443}
444
445DEFUN(cfg_no_log_syslog, cfg_no_log_syslog_cmd,
446 "no log syslog",
447 NO_STR LOG_STR "Logging via syslog\n")
448{
449 struct log_target *tgt;
450
451 tgt = log_target_find(LOG_TGT_TYPE_SYSLOG, NULL);
452 if (!tgt) {
453 vty_out(vty, "%% No syslog target found%s",
454 VTY_NEWLINE);
455 return CMD_WARNING;
456 }
457
458 log_target_destroy(tgt);
459
460 return CMD_SUCCESS;
461}
462#endif /* HAVE_SYSLOG_H */
463
464DEFUN(cfg_log_stderr, cfg_log_stderr_cmd,
465 "log stderr",
466 LOG_STR "Logging via STDERR of the process\n")
467{
468 struct log_target *tgt;
469
470 tgt = log_target_find(LOG_TGT_TYPE_STDERR, NULL);
471 if (!tgt) {
472 tgt = log_target_create_stderr();
473 if (!tgt) {
474 vty_out(vty, "%% Unable to create stderr log%s",
475 VTY_NEWLINE);
476 return CMD_WARNING;
477 }
478 log_add_target(tgt);
479 }
480
481 vty->index = tgt;
482 vty->node = CFG_LOG_NODE;
483
484 return CMD_SUCCESS;
485}
486
487DEFUN(cfg_no_log_stderr, cfg_no_log_stderr_cmd,
488 "no log stderr",
489 NO_STR LOG_STR "Logging via STDERR of the process\n")
490{
491 struct log_target *tgt;
492
493 tgt = log_target_find(LOG_TGT_TYPE_STDERR, NULL);
494 if (!tgt) {
495 vty_out(vty, "%% No stderr logging active%s", VTY_NEWLINE);
496 return CMD_WARNING;
497 }
498
499 log_target_destroy(tgt);
500
501 return CMD_SUCCESS;
502}
503
504DEFUN(cfg_log_file, cfg_log_file_cmd,
505 "log file .FILENAME",
506 LOG_STR "Logging to text file\n" "Filename\n")
507{
508 const char *fname = argv[0];
509 struct log_target *tgt;
510
511 tgt = log_target_find(LOG_TGT_TYPE_FILE, fname);
512 if (!tgt) {
513 tgt = log_target_create_file(fname);
514 if (!tgt) {
515 vty_out(vty, "%% Unable to create file `%s'%s",
516 fname, VTY_NEWLINE);
517 return CMD_WARNING;
518 }
519 log_add_target(tgt);
520 }
521
522 vty->index = tgt;
523 vty->node = CFG_LOG_NODE;
524
525 return CMD_SUCCESS;
526}
527
528
529DEFUN(cfg_no_log_file, cfg_no_log_file_cmd,
530 "no log file .FILENAME",
531 NO_STR LOG_STR "Logging to text file\n" "Filename\n")
532{
533 const char *fname = argv[0];
534 struct log_target *tgt;
535
536 tgt = log_target_find(LOG_TGT_TYPE_FILE, fname);
537 if (!tgt) {
538 vty_out(vty, "%% No such log file `%s'%s",
539 fname, VTY_NEWLINE);
540 return CMD_WARNING;
541 }
542
543 log_target_destroy(tgt);
544
545 return CMD_SUCCESS;
546}
547
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000548DEFUN(cfg_log_alarms, cfg_log_alarms_cmd,
549 "log alarms <2-32700>",
550 LOG_STR "Logging alarms to osmo_strrb\n"
551 "Maximum number of messages to log\n")
552{
553 struct log_target *tgt;
554 unsigned int rbsize = atoi(argv[0]);
555
556 tgt = log_target_find(LOG_TGT_TYPE_STRRB, NULL);
557 if (tgt)
558 log_target_destroy(tgt);
559
560 tgt = log_target_create_rb(rbsize);
561 if (!tgt) {
562 vty_out(vty, "%% Unable to create osmo_strrb (size %u)%s",
563 rbsize, VTY_NEWLINE);
564 return CMD_WARNING;
565 }
566 log_add_target(tgt);
567
568 vty->index = tgt;
569 vty->node = CFG_LOG_NODE;
570
571 return CMD_SUCCESS;
572}
573
574DEFUN(cfg_no_log_alarms, cfg_no_log_alarms_cmd,
575 "no log alarms",
576 NO_STR LOG_STR "Logging alarms to osmo_strrb\n")
577{
578 struct log_target *tgt;
579
580 tgt = log_target_find(LOG_TGT_TYPE_STRRB, NULL);
581 if (!tgt) {
582 vty_out(vty, "%% No osmo_strrb target found%s", VTY_NEWLINE);
583 return CMD_WARNING;
584 }
585
586 log_target_destroy(tgt);
587
588 return CMD_SUCCESS;
589}
590
Harald Welte28222962011-02-18 20:37:04 +0100591static int config_write_log_single(struct vty *vty, struct log_target *tgt)
592{
593 int i;
594 char level_lower[32];
595
596 switch (tgt->type) {
597 case LOG_TGT_TYPE_VTY:
598 return 1;
599 break;
600 case LOG_TGT_TYPE_STDERR:
601 vty_out(vty, "log stderr%s", VTY_NEWLINE);
602 break;
603 case LOG_TGT_TYPE_SYSLOG:
604#ifdef HAVE_SYSLOG_H
605 vty_out(vty, "log syslog %s%s",
606 get_value_string(sysl_level_names,
607 tgt->tgt_syslog.facility),
608 VTY_NEWLINE);
609#endif
610 break;
611 case LOG_TGT_TYPE_FILE:
612 vty_out(vty, "log file %s%s", tgt->tgt_file.fname, VTY_NEWLINE);
613 break;
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000614 case LOG_TGT_TYPE_STRRB:
615 vty_out(vty, "log alarms %zu%s",
616 log_target_rb_avail_size(tgt), VTY_NEWLINE);
617 break;
Harald Welte28222962011-02-18 20:37:04 +0100618 }
619
Harald Welte2da47f12012-10-22 19:31:54 +0200620 vty_out(vty, " logging filter all %u%s",
621 tgt->filter_map & LOG_FILTER_ALL ? 1 : 0, VTY_NEWLINE);
Harald Weltefb84f322013-06-06 07:33:54 +0200622 /* save filters outside of libosmocore, i.e. in app code */
623 if (osmo_log_info->save_fn)
624 osmo_log_info->save_fn(vty, osmo_log_info, tgt);
Harald Welte2da47f12012-10-22 19:31:54 +0200625
Harald Welte28222962011-02-18 20:37:04 +0100626 vty_out(vty, " logging color %u%s", tgt->use_color ? 1 : 0,
627 VTY_NEWLINE);
628 vty_out(vty, " logging timestamp %u%s", tgt->print_timestamp ? 1 : 0,
629 VTY_NEWLINE);
630
631 /* stupid old osmo logging API uses uppercase strings... */
632 osmo_str2lower(level_lower, log_level_str(tgt->loglevel));
633 vty_out(vty, " logging level all %s%s", level_lower, VTY_NEWLINE);
634
635 for (i = 0; i < osmo_log_info->num_cat; i++) {
636 const struct log_category *cat = &tgt->categories[i];
637 char cat_lower[32];
638
Harald Welte1a02cfc2013-03-19 10:37:39 +0100639 /* skip empty entries in the array */
640 if (!osmo_log_info->cat[i].name)
641 continue;
642
Harald Welte28222962011-02-18 20:37:04 +0100643 /* stupid old osmo logging API uses uppercase strings... */
644 osmo_str2lower(cat_lower, osmo_log_info->cat[i].name+1);
645 osmo_str2lower(level_lower, log_level_str(cat->loglevel));
646
647 vty_out(vty, " logging level %s %s%s", cat_lower, level_lower,
648 VTY_NEWLINE);
649 }
650
651 /* FIXME: levels */
652
653 return 1;
654}
655
656static int config_write_log(struct vty *vty)
657{
658 struct log_target *dbg = vty->index;
659
660 llist_for_each_entry(dbg, &osmo_log_target_list, entry)
661 config_write_log_single(vty, dbg);
662
663 return 1;
664}
665
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100666void logging_vty_add_cmds(const struct log_info *cat)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200667{
668 install_element_ve(&enable_logging_cmd);
669 install_element_ve(&disable_logging_cmd);
670 install_element_ve(&logging_fltr_all_cmd);
671 install_element_ve(&logging_use_clr_cmd);
672 install_element_ve(&logging_prnt_timestamp_cmd);
673 install_element_ve(&logging_set_category_mask_cmd);
Holger Hans Peter Freyther146d1d32011-10-03 23:15:41 +0200674 install_element_ve(&logging_set_category_mask_old_cmd);
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100675
676 /* Logging level strings are generated dynamically. */
677 logging_level_cmd.string = log_vty_command_string(cat);
678 logging_level_cmd.doc = log_vty_command_description(cat);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200679 install_element_ve(&logging_level_cmd);
680 install_element_ve(&show_logging_vty_cmd);
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000681 install_element_ve(&show_alarms_cmd);
Harald Welte28222962011-02-18 20:37:04 +0100682
683 install_node(&cfg_log_node, config_write_log);
Jacob Erlbeck0c987bd2013-09-06 16:52:00 +0200684 vty_install_default(CFG_LOG_NODE);
Harald Weltea62648b2011-02-18 21:03:27 +0100685 install_element(CFG_LOG_NODE, &logging_fltr_all_cmd);
686 install_element(CFG_LOG_NODE, &logging_use_clr_cmd);
687 install_element(CFG_LOG_NODE, &logging_prnt_timestamp_cmd);
688 install_element(CFG_LOG_NODE, &logging_level_cmd);
Harald Welte28222962011-02-18 20:37:04 +0100689
690 install_element(CONFIG_NODE, &cfg_log_stderr_cmd);
691 install_element(CONFIG_NODE, &cfg_no_log_stderr_cmd);
692 install_element(CONFIG_NODE, &cfg_log_file_cmd);
693 install_element(CONFIG_NODE, &cfg_no_log_file_cmd);
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000694 install_element(CONFIG_NODE, &cfg_log_alarms_cmd);
695 install_element(CONFIG_NODE, &cfg_no_log_alarms_cmd);
Harald Welte28222962011-02-18 20:37:04 +0100696#ifdef HAVE_SYSLOG_H
697 install_element(CONFIG_NODE, &cfg_log_syslog_cmd);
698 install_element(CONFIG_NODE, &cfg_log_syslog_local_cmd);
699 install_element(CONFIG_NODE, &cfg_no_log_syslog_cmd);
700#endif
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200701}