blob: bb19a31d5ff2c197039b23133083b2efd26676b6 [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];
278 vty_out(vty, " %-10s %-10s %-8s %s%s",
279 info->cat[i].name+1, log_level_str(cat->loglevel),
280 cat->enabled ? "Enabled" : "Disabled",
281 info->cat[i].description,
282 VTY_NEWLINE);
283 }
Harald Welte6d2d4d62013-03-10 09:53:24 +0000284
285 vty_out(vty, " Log Filter 'ALL': %s%s",
286 tgt->filter_map & LOG_FILTER_ALL ? "Enabled" : "Disabled",
287 VTY_NEWLINE);
288
Harald Weltefb84f322013-06-06 07:33:54 +0200289 /* print application specific filters */
290 if (info->print_fn)
291 info->print_fn(vty, info, tgt);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200292}
293
294#define SHOW_LOG_STR "Show current logging configuration\n"
295
296DEFUN(show_logging_vty,
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000297 show_logging_vty_cmd,
298 "show logging vty",
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200299 SHOW_STR SHOW_LOG_STR
300 "Show current logging configuration for this vty\n")
301{
Harald Weltea62648b2011-02-18 21:03:27 +0100302 struct log_target *tgt = osmo_log_vty2tgt(vty);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200303
Harald Weltea62648b2011-02-18 21:03:27 +0100304 if (!tgt)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200305 return CMD_WARNING;
Harald Weltea62648b2011-02-18 21:03:27 +0100306
307 vty_print_logtarget(vty, osmo_log_info, tgt);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200308
309 return CMD_SUCCESS;
310}
311
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000312DEFUN(show_alarms,
313 show_alarms_cmd,
314 "show alarms",
315 SHOW_STR SHOW_LOG_STR
316 "Show the contents of the logging ringbuffer\n")
317{
318 int i, num_alarms;
319 struct osmo_strrb *rb;
320 struct log_target *tgt = log_target_find(LOG_TGT_TYPE_STRRB, NULL);
321 if (!tgt) {
322 vty_out(vty, "%% No alarms, run 'log alarms <2-32700>'%s",
323 VTY_NEWLINE);
324 return CMD_WARNING;
325 }
326
327 rb = tgt->tgt_rb.rb;
328 num_alarms = osmo_strrb_elements(rb);
329
330 vty_out(vty, "%% Showing %i alarms%s", num_alarms, VTY_NEWLINE);
331
332 for (i = 0; i < num_alarms; i++)
333 vty_out(vty, "%% %s%s", osmo_strrb_get_nth(rb, i),
334 VTY_NEWLINE);
335
336 return CMD_SUCCESS;
337}
338
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200339gDEFUN(cfg_description, cfg_description_cmd,
340 "description .TEXT",
Holger Hans Peter Freytherc9b3e062012-07-25 13:02:49 +0200341 "Save human-readable decription of the object\n"
342 "Text until the end of the line\n")
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200343{
344 char **dptr = vty->index_sub;
345
346 if (!dptr) {
347 vty_out(vty, "vty->index_sub == NULL%s", VTY_NEWLINE);
348 return CMD_WARNING;
349 }
350
Holger Hans Peter Freytherff0670e2011-02-24 14:20:41 +0100351 if (*dptr)
352 talloc_free(*dptr);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200353 *dptr = argv_concat(argv, argc, 0);
Holger Hans Peter Freyther6a75d162013-07-14 09:07:11 +0200354 if (!*dptr)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200355 return CMD_WARNING;
356
357 return CMD_SUCCESS;
358}
359
360gDEFUN(cfg_no_description, cfg_no_description_cmd,
361 "no description",
362 NO_STR
363 "Remove description of the object\n")
364{
365 char **dptr = vty->index_sub;
366
367 if (!dptr) {
368 vty_out(vty, "vty->index_sub == NULL%s", VTY_NEWLINE);
369 return CMD_WARNING;
370 }
371
372 if (*dptr) {
373 talloc_free(*dptr);
374 *dptr = NULL;
375 }
376
377 return CMD_SUCCESS;
378}
379
Harald Welte28222962011-02-18 20:37:04 +0100380/* Support for configuration of log targets != the current vty */
381
382struct cmd_node cfg_log_node = {
383 CFG_LOG_NODE,
384 "%s(config-log)# ",
385 1
386};
387
Harald Welte28222962011-02-18 20:37:04 +0100388#ifdef HAVE_SYSLOG_H
389
390#include <syslog.h>
391
392static const int local_sysl_map[] = {
393 [0] = LOG_LOCAL0,
394 [1] = LOG_LOCAL1,
395 [2] = LOG_LOCAL2,
396 [3] = LOG_LOCAL3,
397 [4] = LOG_LOCAL4,
398 [5] = LOG_LOCAL5,
399 [6] = LOG_LOCAL6,
400 [7] = LOG_LOCAL7
401};
402
Harald Weltede79cee2011-02-24 23:47:57 +0100403/* From VTY core code */
404extern struct host host;
405
Harald Welte28222962011-02-18 20:37:04 +0100406static int _cfg_log_syslog(struct vty *vty, int facility)
407{
408 struct log_target *tgt;
409
410 /* First delete the old syslog target, if any */
411 tgt = log_target_find(LOG_TGT_TYPE_SYSLOG, NULL);
412 if (tgt)
413 log_target_destroy(tgt);
414
Harald Weltede79cee2011-02-24 23:47:57 +0100415 tgt = log_target_create_syslog(host.app_info->name, 0, facility);
Harald Welte28222962011-02-18 20:37:04 +0100416 if (!tgt) {
417 vty_out(vty, "%% Unable to open syslog%s", VTY_NEWLINE);
418 return CMD_WARNING;
419 }
420 log_add_target(tgt);
421
422 vty->index = tgt;
423 vty->node = CFG_LOG_NODE;
424
425 return CMD_SUCCESS;
426}
427
428DEFUN(cfg_log_syslog_local, cfg_log_syslog_local_cmd,
429 "log syslog local <0-7>",
430 LOG_STR "Logging via syslog\n" "Syslog LOCAL facility\n"
431 "Local facility number\n")
432{
433 int local = atoi(argv[0]);
434 int facility = local_sysl_map[local];
435
436 return _cfg_log_syslog(vty, facility);
437}
438
439static struct value_string sysl_level_names[] = {
440 { LOG_AUTHPRIV, "authpriv" },
441 { LOG_CRON, "cron" },
442 { LOG_DAEMON, "daemon" },
443 { LOG_FTP, "ftp" },
444 { LOG_LPR, "lpr" },
445 { LOG_MAIL, "mail" },
446 { LOG_NEWS, "news" },
447 { LOG_USER, "user" },
448 { LOG_UUCP, "uucp" },
449 /* only for value -> string conversion */
450 { LOG_LOCAL0, "local 0" },
451 { LOG_LOCAL1, "local 1" },
452 { LOG_LOCAL2, "local 2" },
453 { LOG_LOCAL3, "local 3" },
454 { LOG_LOCAL4, "local 4" },
455 { LOG_LOCAL5, "local 5" },
456 { LOG_LOCAL6, "local 6" },
457 { LOG_LOCAL7, "local 7" },
458 { 0, NULL }
459};
460
461DEFUN(cfg_log_syslog, cfg_log_syslog_cmd,
462 "log syslog (authpriv|cron|daemon|ftp|lpr|mail|news|user|uucp)",
Holger Hans Peter Freythera4463fd2011-10-03 23:17:36 +0200463 LOG_STR "Logging via syslog\n"
464 "Security/authorization messages facility\n"
465 "Clock daemon (cron/at) facility\n"
466 "General system daemon facility\n"
467 "Ftp daemon facility\n"
468 "Line printer facility\n"
469 "Mail facility\n"
470 "News facility\n"
471 "Generic facility\n"
472 "UUCP facility\n")
Harald Welte28222962011-02-18 20:37:04 +0100473{
474 int facility = get_string_value(sysl_level_names, argv[0]);
475
476 return _cfg_log_syslog(vty, facility);
477}
478
479DEFUN(cfg_no_log_syslog, cfg_no_log_syslog_cmd,
480 "no log syslog",
481 NO_STR LOG_STR "Logging via syslog\n")
482{
483 struct log_target *tgt;
484
485 tgt = log_target_find(LOG_TGT_TYPE_SYSLOG, NULL);
486 if (!tgt) {
487 vty_out(vty, "%% No syslog target found%s",
488 VTY_NEWLINE);
489 return CMD_WARNING;
490 }
491
492 log_target_destroy(tgt);
493
494 return CMD_SUCCESS;
495}
496#endif /* HAVE_SYSLOG_H */
497
498DEFUN(cfg_log_stderr, cfg_log_stderr_cmd,
499 "log stderr",
500 LOG_STR "Logging via STDERR of the process\n")
501{
502 struct log_target *tgt;
503
504 tgt = log_target_find(LOG_TGT_TYPE_STDERR, NULL);
505 if (!tgt) {
506 tgt = log_target_create_stderr();
507 if (!tgt) {
508 vty_out(vty, "%% Unable to create stderr log%s",
509 VTY_NEWLINE);
510 return CMD_WARNING;
511 }
512 log_add_target(tgt);
513 }
514
515 vty->index = tgt;
516 vty->node = CFG_LOG_NODE;
517
518 return CMD_SUCCESS;
519}
520
521DEFUN(cfg_no_log_stderr, cfg_no_log_stderr_cmd,
522 "no log stderr",
523 NO_STR LOG_STR "Logging via STDERR of the process\n")
524{
525 struct log_target *tgt;
526
527 tgt = log_target_find(LOG_TGT_TYPE_STDERR, NULL);
528 if (!tgt) {
529 vty_out(vty, "%% No stderr logging active%s", VTY_NEWLINE);
530 return CMD_WARNING;
531 }
532
533 log_target_destroy(tgt);
534
535 return CMD_SUCCESS;
536}
537
538DEFUN(cfg_log_file, cfg_log_file_cmd,
539 "log file .FILENAME",
540 LOG_STR "Logging to text file\n" "Filename\n")
541{
542 const char *fname = argv[0];
543 struct log_target *tgt;
544
545 tgt = log_target_find(LOG_TGT_TYPE_FILE, fname);
546 if (!tgt) {
547 tgt = log_target_create_file(fname);
548 if (!tgt) {
549 vty_out(vty, "%% Unable to create file `%s'%s",
550 fname, 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
562
563DEFUN(cfg_no_log_file, cfg_no_log_file_cmd,
564 "no log file .FILENAME",
565 NO_STR LOG_STR "Logging to text file\n" "Filename\n")
566{
567 const char *fname = argv[0];
568 struct log_target *tgt;
569
570 tgt = log_target_find(LOG_TGT_TYPE_FILE, fname);
571 if (!tgt) {
572 vty_out(vty, "%% No such log file `%s'%s",
573 fname, VTY_NEWLINE);
574 return CMD_WARNING;
575 }
576
577 log_target_destroy(tgt);
578
579 return CMD_SUCCESS;
580}
581
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000582DEFUN(cfg_log_alarms, cfg_log_alarms_cmd,
583 "log alarms <2-32700>",
584 LOG_STR "Logging alarms to osmo_strrb\n"
585 "Maximum number of messages to log\n")
586{
587 struct log_target *tgt;
588 unsigned int rbsize = atoi(argv[0]);
589
590 tgt = log_target_find(LOG_TGT_TYPE_STRRB, NULL);
591 if (tgt)
592 log_target_destroy(tgt);
593
594 tgt = log_target_create_rb(rbsize);
595 if (!tgt) {
596 vty_out(vty, "%% Unable to create osmo_strrb (size %u)%s",
597 rbsize, VTY_NEWLINE);
598 return CMD_WARNING;
599 }
600 log_add_target(tgt);
601
602 vty->index = tgt;
603 vty->node = CFG_LOG_NODE;
604
605 return CMD_SUCCESS;
606}
607
608DEFUN(cfg_no_log_alarms, cfg_no_log_alarms_cmd,
609 "no log alarms",
610 NO_STR LOG_STR "Logging alarms to osmo_strrb\n")
611{
612 struct log_target *tgt;
613
614 tgt = log_target_find(LOG_TGT_TYPE_STRRB, NULL);
615 if (!tgt) {
616 vty_out(vty, "%% No osmo_strrb target found%s", VTY_NEWLINE);
617 return CMD_WARNING;
618 }
619
620 log_target_destroy(tgt);
621
622 return CMD_SUCCESS;
623}
624
Harald Welte28222962011-02-18 20:37:04 +0100625static int config_write_log_single(struct vty *vty, struct log_target *tgt)
626{
627 int i;
628 char level_lower[32];
629
630 switch (tgt->type) {
631 case LOG_TGT_TYPE_VTY:
632 return 1;
633 break;
634 case LOG_TGT_TYPE_STDERR:
635 vty_out(vty, "log stderr%s", VTY_NEWLINE);
636 break;
637 case LOG_TGT_TYPE_SYSLOG:
638#ifdef HAVE_SYSLOG_H
639 vty_out(vty, "log syslog %s%s",
640 get_value_string(sysl_level_names,
641 tgt->tgt_syslog.facility),
642 VTY_NEWLINE);
643#endif
644 break;
645 case LOG_TGT_TYPE_FILE:
646 vty_out(vty, "log file %s%s", tgt->tgt_file.fname, VTY_NEWLINE);
647 break;
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000648 case LOG_TGT_TYPE_STRRB:
649 vty_out(vty, "log alarms %zu%s",
650 log_target_rb_avail_size(tgt), VTY_NEWLINE);
651 break;
Harald Welte28222962011-02-18 20:37:04 +0100652 }
653
Harald Welte2da47f12012-10-22 19:31:54 +0200654 vty_out(vty, " logging filter all %u%s",
655 tgt->filter_map & LOG_FILTER_ALL ? 1 : 0, VTY_NEWLINE);
Harald Weltefb84f322013-06-06 07:33:54 +0200656 /* save filters outside of libosmocore, i.e. in app code */
657 if (osmo_log_info->save_fn)
658 osmo_log_info->save_fn(vty, osmo_log_info, tgt);
Harald Welte2da47f12012-10-22 19:31:54 +0200659
Harald Welte28222962011-02-18 20:37:04 +0100660 vty_out(vty, " logging color %u%s", tgt->use_color ? 1 : 0,
661 VTY_NEWLINE);
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100662 vty_out(vty, " logging print cateyory %d%s",
663 tgt->print_ext_timestamp ? 1 : 0, VTY_NEWLINE);
664 if (tgt->print_ext_timestamp)
665 vty_out(vty, " logging print extended-timestamp 1%s", VTY_NEWLINE);
666 else
667 vty_out(vty, " logging timestamp %u%s",
668 tgt->print_timestamp ? 1 : 0, VTY_NEWLINE);
Harald Welte28222962011-02-18 20:37:04 +0100669
670 /* stupid old osmo logging API uses uppercase strings... */
671 osmo_str2lower(level_lower, log_level_str(tgt->loglevel));
672 vty_out(vty, " logging level all %s%s", level_lower, VTY_NEWLINE);
673
674 for (i = 0; i < osmo_log_info->num_cat; i++) {
675 const struct log_category *cat = &tgt->categories[i];
676 char cat_lower[32];
677
Harald Welte1a02cfc2013-03-19 10:37:39 +0100678 /* skip empty entries in the array */
679 if (!osmo_log_info->cat[i].name)
680 continue;
681
Harald Welte28222962011-02-18 20:37:04 +0100682 /* stupid old osmo logging API uses uppercase strings... */
683 osmo_str2lower(cat_lower, osmo_log_info->cat[i].name+1);
684 osmo_str2lower(level_lower, log_level_str(cat->loglevel));
685
686 vty_out(vty, " logging level %s %s%s", cat_lower, level_lower,
687 VTY_NEWLINE);
688 }
689
690 /* FIXME: levels */
691
692 return 1;
693}
694
695static int config_write_log(struct vty *vty)
696{
697 struct log_target *dbg = vty->index;
698
699 llist_for_each_entry(dbg, &osmo_log_target_list, entry)
700 config_write_log_single(vty, dbg);
701
702 return 1;
703}
704
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100705void logging_vty_add_cmds(const struct log_info *cat)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200706{
707 install_element_ve(&enable_logging_cmd);
708 install_element_ve(&disable_logging_cmd);
709 install_element_ve(&logging_fltr_all_cmd);
710 install_element_ve(&logging_use_clr_cmd);
711 install_element_ve(&logging_prnt_timestamp_cmd);
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100712 install_element_ve(&logging_prnt_ext_timestamp_cmd);
713 install_element_ve(&logging_prnt_cat_cmd);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200714 install_element_ve(&logging_set_category_mask_cmd);
Holger Hans Peter Freyther146d1d32011-10-03 23:15:41 +0200715 install_element_ve(&logging_set_category_mask_old_cmd);
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100716
717 /* Logging level strings are generated dynamically. */
718 logging_level_cmd.string = log_vty_command_string(cat);
719 logging_level_cmd.doc = log_vty_command_description(cat);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200720 install_element_ve(&logging_level_cmd);
721 install_element_ve(&show_logging_vty_cmd);
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000722 install_element_ve(&show_alarms_cmd);
Harald Welte28222962011-02-18 20:37:04 +0100723
724 install_node(&cfg_log_node, config_write_log);
Jacob Erlbeck0c987bd2013-09-06 16:52:00 +0200725 vty_install_default(CFG_LOG_NODE);
Harald Weltea62648b2011-02-18 21:03:27 +0100726 install_element(CFG_LOG_NODE, &logging_fltr_all_cmd);
727 install_element(CFG_LOG_NODE, &logging_use_clr_cmd);
728 install_element(CFG_LOG_NODE, &logging_prnt_timestamp_cmd);
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100729 install_element(CFG_LOG_NODE, &logging_prnt_ext_timestamp_cmd);
730 install_element(CFG_LOG_NODE, &logging_prnt_cat_cmd);
Harald Weltea62648b2011-02-18 21:03:27 +0100731 install_element(CFG_LOG_NODE, &logging_level_cmd);
Harald Welte28222962011-02-18 20:37:04 +0100732
733 install_element(CONFIG_NODE, &cfg_log_stderr_cmd);
734 install_element(CONFIG_NODE, &cfg_no_log_stderr_cmd);
735 install_element(CONFIG_NODE, &cfg_log_file_cmd);
736 install_element(CONFIG_NODE, &cfg_no_log_file_cmd);
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000737 install_element(CONFIG_NODE, &cfg_log_alarms_cmd);
738 install_element(CONFIG_NODE, &cfg_no_log_alarms_cmd);
Harald Welte28222962011-02-18 20:37:04 +0100739#ifdef HAVE_SYSLOG_H
740 install_element(CONFIG_NODE, &cfg_log_syslog_cmd);
741 install_element(CONFIG_NODE, &cfg_log_syslog_local_cmd);
742 install_element(CONFIG_NODE, &cfg_no_log_syslog_cmd);
743#endif
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200744}