blob: ace346a2797d5e4d175e8ce1431593d6cf9d17d8 [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 }
250}
251
252#define SHOW_LOG_STR "Show current logging configuration\n"
253
254DEFUN(show_logging_vty,
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000255 show_logging_vty_cmd,
256 "show logging vty",
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200257 SHOW_STR SHOW_LOG_STR
258 "Show current logging configuration for this vty\n")
259{
Harald Weltea62648b2011-02-18 21:03:27 +0100260 struct log_target *tgt = osmo_log_vty2tgt(vty);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200261
Harald Weltea62648b2011-02-18 21:03:27 +0100262 if (!tgt)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200263 return CMD_WARNING;
Harald Weltea62648b2011-02-18 21:03:27 +0100264
265 vty_print_logtarget(vty, osmo_log_info, tgt);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200266
267 return CMD_SUCCESS;
268}
269
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000270DEFUN(show_alarms,
271 show_alarms_cmd,
272 "show alarms",
273 SHOW_STR SHOW_LOG_STR
274 "Show the contents of the logging ringbuffer\n")
275{
276 int i, num_alarms;
277 struct osmo_strrb *rb;
278 struct log_target *tgt = log_target_find(LOG_TGT_TYPE_STRRB, NULL);
279 if (!tgt) {
280 vty_out(vty, "%% No alarms, run 'log alarms <2-32700>'%s",
281 VTY_NEWLINE);
282 return CMD_WARNING;
283 }
284
285 rb = tgt->tgt_rb.rb;
286 num_alarms = osmo_strrb_elements(rb);
287
288 vty_out(vty, "%% Showing %i alarms%s", num_alarms, VTY_NEWLINE);
289
290 for (i = 0; i < num_alarms; i++)
291 vty_out(vty, "%% %s%s", osmo_strrb_get_nth(rb, i),
292 VTY_NEWLINE);
293
294 return CMD_SUCCESS;
295}
296
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200297gDEFUN(cfg_description, cfg_description_cmd,
298 "description .TEXT",
Holger Hans Peter Freytherc9b3e062012-07-25 13:02:49 +0200299 "Save human-readable decription of the object\n"
300 "Text until the end of the line\n")
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200301{
302 char **dptr = vty->index_sub;
303
304 if (!dptr) {
305 vty_out(vty, "vty->index_sub == NULL%s", VTY_NEWLINE);
306 return CMD_WARNING;
307 }
308
Holger Hans Peter Freytherff0670e2011-02-24 14:20:41 +0100309 if (*dptr)
310 talloc_free(*dptr);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200311 *dptr = argv_concat(argv, argc, 0);
312 if (!dptr)
313 return CMD_WARNING;
314
315 return CMD_SUCCESS;
316}
317
318gDEFUN(cfg_no_description, cfg_no_description_cmd,
319 "no description",
320 NO_STR
321 "Remove description of the object\n")
322{
323 char **dptr = vty->index_sub;
324
325 if (!dptr) {
326 vty_out(vty, "vty->index_sub == NULL%s", VTY_NEWLINE);
327 return CMD_WARNING;
328 }
329
330 if (*dptr) {
331 talloc_free(*dptr);
332 *dptr = NULL;
333 }
334
335 return CMD_SUCCESS;
336}
337
Harald Welte28222962011-02-18 20:37:04 +0100338/* Support for configuration of log targets != the current vty */
339
340struct cmd_node cfg_log_node = {
341 CFG_LOG_NODE,
342 "%s(config-log)# ",
343 1
344};
345
Harald Welte28222962011-02-18 20:37:04 +0100346#ifdef HAVE_SYSLOG_H
347
348#include <syslog.h>
349
350static const int local_sysl_map[] = {
351 [0] = LOG_LOCAL0,
352 [1] = LOG_LOCAL1,
353 [2] = LOG_LOCAL2,
354 [3] = LOG_LOCAL3,
355 [4] = LOG_LOCAL4,
356 [5] = LOG_LOCAL5,
357 [6] = LOG_LOCAL6,
358 [7] = LOG_LOCAL7
359};
360
Harald Weltede79cee2011-02-24 23:47:57 +0100361/* From VTY core code */
362extern struct host host;
363
Harald Welte28222962011-02-18 20:37:04 +0100364static int _cfg_log_syslog(struct vty *vty, int facility)
365{
366 struct log_target *tgt;
367
368 /* First delete the old syslog target, if any */
369 tgt = log_target_find(LOG_TGT_TYPE_SYSLOG, NULL);
370 if (tgt)
371 log_target_destroy(tgt);
372
Harald Weltede79cee2011-02-24 23:47:57 +0100373 tgt = log_target_create_syslog(host.app_info->name, 0, facility);
Harald Welte28222962011-02-18 20:37:04 +0100374 if (!tgt) {
375 vty_out(vty, "%% Unable to open syslog%s", VTY_NEWLINE);
376 return CMD_WARNING;
377 }
378 log_add_target(tgt);
379
380 vty->index = tgt;
381 vty->node = CFG_LOG_NODE;
382
383 return CMD_SUCCESS;
384}
385
386DEFUN(cfg_log_syslog_local, cfg_log_syslog_local_cmd,
387 "log syslog local <0-7>",
388 LOG_STR "Logging via syslog\n" "Syslog LOCAL facility\n"
389 "Local facility number\n")
390{
391 int local = atoi(argv[0]);
392 int facility = local_sysl_map[local];
393
394 return _cfg_log_syslog(vty, facility);
395}
396
397static struct value_string sysl_level_names[] = {
398 { LOG_AUTHPRIV, "authpriv" },
399 { LOG_CRON, "cron" },
400 { LOG_DAEMON, "daemon" },
401 { LOG_FTP, "ftp" },
402 { LOG_LPR, "lpr" },
403 { LOG_MAIL, "mail" },
404 { LOG_NEWS, "news" },
405 { LOG_USER, "user" },
406 { LOG_UUCP, "uucp" },
407 /* only for value -> string conversion */
408 { LOG_LOCAL0, "local 0" },
409 { LOG_LOCAL1, "local 1" },
410 { LOG_LOCAL2, "local 2" },
411 { LOG_LOCAL3, "local 3" },
412 { LOG_LOCAL4, "local 4" },
413 { LOG_LOCAL5, "local 5" },
414 { LOG_LOCAL6, "local 6" },
415 { LOG_LOCAL7, "local 7" },
416 { 0, NULL }
417};
418
419DEFUN(cfg_log_syslog, cfg_log_syslog_cmd,
420 "log syslog (authpriv|cron|daemon|ftp|lpr|mail|news|user|uucp)",
Holger Hans Peter Freythera4463fd2011-10-03 23:17:36 +0200421 LOG_STR "Logging via syslog\n"
422 "Security/authorization messages facility\n"
423 "Clock daemon (cron/at) facility\n"
424 "General system daemon facility\n"
425 "Ftp daemon facility\n"
426 "Line printer facility\n"
427 "Mail facility\n"
428 "News facility\n"
429 "Generic facility\n"
430 "UUCP facility\n")
Harald Welte28222962011-02-18 20:37:04 +0100431{
432 int facility = get_string_value(sysl_level_names, argv[0]);
433
434 return _cfg_log_syslog(vty, facility);
435}
436
437DEFUN(cfg_no_log_syslog, cfg_no_log_syslog_cmd,
438 "no log syslog",
439 NO_STR LOG_STR "Logging via syslog\n")
440{
441 struct log_target *tgt;
442
443 tgt = log_target_find(LOG_TGT_TYPE_SYSLOG, NULL);
444 if (!tgt) {
445 vty_out(vty, "%% No syslog target found%s",
446 VTY_NEWLINE);
447 return CMD_WARNING;
448 }
449
450 log_target_destroy(tgt);
451
452 return CMD_SUCCESS;
453}
454#endif /* HAVE_SYSLOG_H */
455
456DEFUN(cfg_log_stderr, cfg_log_stderr_cmd,
457 "log stderr",
458 LOG_STR "Logging via STDERR of the process\n")
459{
460 struct log_target *tgt;
461
462 tgt = log_target_find(LOG_TGT_TYPE_STDERR, NULL);
463 if (!tgt) {
464 tgt = log_target_create_stderr();
465 if (!tgt) {
466 vty_out(vty, "%% Unable to create stderr log%s",
467 VTY_NEWLINE);
468 return CMD_WARNING;
469 }
470 log_add_target(tgt);
471 }
472
473 vty->index = tgt;
474 vty->node = CFG_LOG_NODE;
475
476 return CMD_SUCCESS;
477}
478
479DEFUN(cfg_no_log_stderr, cfg_no_log_stderr_cmd,
480 "no log stderr",
481 NO_STR LOG_STR "Logging via STDERR of the process\n")
482{
483 struct log_target *tgt;
484
485 tgt = log_target_find(LOG_TGT_TYPE_STDERR, NULL);
486 if (!tgt) {
487 vty_out(vty, "%% No stderr logging active%s", VTY_NEWLINE);
488 return CMD_WARNING;
489 }
490
491 log_target_destroy(tgt);
492
493 return CMD_SUCCESS;
494}
495
496DEFUN(cfg_log_file, cfg_log_file_cmd,
497 "log file .FILENAME",
498 LOG_STR "Logging to text file\n" "Filename\n")
499{
500 const char *fname = argv[0];
501 struct log_target *tgt;
502
503 tgt = log_target_find(LOG_TGT_TYPE_FILE, fname);
504 if (!tgt) {
505 tgt = log_target_create_file(fname);
506 if (!tgt) {
507 vty_out(vty, "%% Unable to create file `%s'%s",
508 fname, VTY_NEWLINE);
509 return CMD_WARNING;
510 }
511 log_add_target(tgt);
512 }
513
514 vty->index = tgt;
515 vty->node = CFG_LOG_NODE;
516
517 return CMD_SUCCESS;
518}
519
520
521DEFUN(cfg_no_log_file, cfg_no_log_file_cmd,
522 "no log file .FILENAME",
523 NO_STR LOG_STR "Logging to text file\n" "Filename\n")
524{
525 const char *fname = argv[0];
526 struct log_target *tgt;
527
528 tgt = log_target_find(LOG_TGT_TYPE_FILE, fname);
529 if (!tgt) {
530 vty_out(vty, "%% No such log file `%s'%s",
531 fname, VTY_NEWLINE);
532 return CMD_WARNING;
533 }
534
535 log_target_destroy(tgt);
536
537 return CMD_SUCCESS;
538}
539
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000540DEFUN(cfg_log_alarms, cfg_log_alarms_cmd,
541 "log alarms <2-32700>",
542 LOG_STR "Logging alarms to osmo_strrb\n"
543 "Maximum number of messages to log\n")
544{
545 struct log_target *tgt;
546 unsigned int rbsize = atoi(argv[0]);
547
548 tgt = log_target_find(LOG_TGT_TYPE_STRRB, NULL);
549 if (tgt)
550 log_target_destroy(tgt);
551
552 tgt = log_target_create_rb(rbsize);
553 if (!tgt) {
554 vty_out(vty, "%% Unable to create osmo_strrb (size %u)%s",
555 rbsize, VTY_NEWLINE);
556 return CMD_WARNING;
557 }
558 log_add_target(tgt);
559
560 vty->index = tgt;
561 vty->node = CFG_LOG_NODE;
562
563 return CMD_SUCCESS;
564}
565
566DEFUN(cfg_no_log_alarms, cfg_no_log_alarms_cmd,
567 "no log alarms",
568 NO_STR LOG_STR "Logging alarms to osmo_strrb\n")
569{
570 struct log_target *tgt;
571
572 tgt = log_target_find(LOG_TGT_TYPE_STRRB, NULL);
573 if (!tgt) {
574 vty_out(vty, "%% No osmo_strrb target found%s", VTY_NEWLINE);
575 return CMD_WARNING;
576 }
577
578 log_target_destroy(tgt);
579
580 return CMD_SUCCESS;
581}
582
Harald Welte28222962011-02-18 20:37:04 +0100583static int config_write_log_single(struct vty *vty, struct log_target *tgt)
584{
585 int i;
586 char level_lower[32];
587
588 switch (tgt->type) {
589 case LOG_TGT_TYPE_VTY:
590 return 1;
591 break;
592 case LOG_TGT_TYPE_STDERR:
593 vty_out(vty, "log stderr%s", VTY_NEWLINE);
594 break;
595 case LOG_TGT_TYPE_SYSLOG:
596#ifdef HAVE_SYSLOG_H
597 vty_out(vty, "log syslog %s%s",
598 get_value_string(sysl_level_names,
599 tgt->tgt_syslog.facility),
600 VTY_NEWLINE);
601#endif
602 break;
603 case LOG_TGT_TYPE_FILE:
604 vty_out(vty, "log file %s%s", tgt->tgt_file.fname, VTY_NEWLINE);
605 break;
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000606 case LOG_TGT_TYPE_STRRB:
607 vty_out(vty, "log alarms %zu%s",
608 log_target_rb_avail_size(tgt), VTY_NEWLINE);
609 break;
Harald Welte28222962011-02-18 20:37:04 +0100610 }
611
Harald Welte2da47f12012-10-22 19:31:54 +0200612 vty_out(vty, " logging filter all %u%s",
613 tgt->filter_map & LOG_FILTER_ALL ? 1 : 0, VTY_NEWLINE);
614 /* FIXME: how to do this for filters outside of libosmocore? */
615
Harald Welte28222962011-02-18 20:37:04 +0100616 vty_out(vty, " logging color %u%s", tgt->use_color ? 1 : 0,
617 VTY_NEWLINE);
618 vty_out(vty, " logging timestamp %u%s", tgt->print_timestamp ? 1 : 0,
619 VTY_NEWLINE);
620
621 /* stupid old osmo logging API uses uppercase strings... */
622 osmo_str2lower(level_lower, log_level_str(tgt->loglevel));
623 vty_out(vty, " logging level all %s%s", level_lower, VTY_NEWLINE);
624
625 for (i = 0; i < osmo_log_info->num_cat; i++) {
626 const struct log_category *cat = &tgt->categories[i];
627 char cat_lower[32];
628
629 /* stupid old osmo logging API uses uppercase strings... */
630 osmo_str2lower(cat_lower, osmo_log_info->cat[i].name+1);
631 osmo_str2lower(level_lower, log_level_str(cat->loglevel));
632
633 vty_out(vty, " logging level %s %s%s", cat_lower, level_lower,
634 VTY_NEWLINE);
635 }
636
637 /* FIXME: levels */
638
639 return 1;
640}
641
642static int config_write_log(struct vty *vty)
643{
644 struct log_target *dbg = vty->index;
645
646 llist_for_each_entry(dbg, &osmo_log_target_list, entry)
647 config_write_log_single(vty, dbg);
648
649 return 1;
650}
651
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100652void logging_vty_add_cmds(const struct log_info *cat)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200653{
654 install_element_ve(&enable_logging_cmd);
655 install_element_ve(&disable_logging_cmd);
656 install_element_ve(&logging_fltr_all_cmd);
657 install_element_ve(&logging_use_clr_cmd);
658 install_element_ve(&logging_prnt_timestamp_cmd);
659 install_element_ve(&logging_set_category_mask_cmd);
Holger Hans Peter Freyther146d1d32011-10-03 23:15:41 +0200660 install_element_ve(&logging_set_category_mask_old_cmd);
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100661
662 /* Logging level strings are generated dynamically. */
663 logging_level_cmd.string = log_vty_command_string(cat);
664 logging_level_cmd.doc = log_vty_command_description(cat);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200665 install_element_ve(&logging_level_cmd);
666 install_element_ve(&show_logging_vty_cmd);
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000667 install_element_ve(&show_alarms_cmd);
Harald Welte28222962011-02-18 20:37:04 +0100668
669 install_node(&cfg_log_node, config_write_log);
Harald Welte8ec7f902012-10-22 19:43:26 +0200670 install_default(CFG_LOG_NODE);
671 install_element(CFG_LOG_NODE, &config_end_cmd);
Harald Weltea62648b2011-02-18 21:03:27 +0100672 install_element(CFG_LOG_NODE, &logging_fltr_all_cmd);
673 install_element(CFG_LOG_NODE, &logging_use_clr_cmd);
674 install_element(CFG_LOG_NODE, &logging_prnt_timestamp_cmd);
675 install_element(CFG_LOG_NODE, &logging_level_cmd);
Harald Welte28222962011-02-18 20:37:04 +0100676
677 install_element(CONFIG_NODE, &cfg_log_stderr_cmd);
678 install_element(CONFIG_NODE, &cfg_no_log_stderr_cmd);
679 install_element(CONFIG_NODE, &cfg_log_file_cmd);
680 install_element(CONFIG_NODE, &cfg_no_log_file_cmd);
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000681 install_element(CONFIG_NODE, &cfg_log_alarms_cmd);
682 install_element(CONFIG_NODE, &cfg_no_log_alarms_cmd);
Harald Welte28222962011-02-18 20:37:04 +0100683#ifdef HAVE_SYSLOG_H
684 install_element(CONFIG_NODE, &cfg_log_syslog_cmd);
685 install_element(CONFIG_NODE, &cfg_log_syslog_local_cmd);
686 install_element(CONFIG_NODE, &cfg_no_log_syslog_cmd);
687#endif
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200688}