]> git.proxmox.com Git - mirror_frr.git/blob - lib/log_vty.c
lib: autodetect systemd/journald log on stdout
[mirror_frr.git] / lib / log_vty.c
1 /*
2 * Logging - VTY code
3 * Copyright (C) 2019 Cumulus Networks, Inc.
4 * Stephen Worley
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation; either version 2 of the License, or (at your option)
9 * any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include <zebra.h>
22
23 #include "lib/log_vty.h"
24 #include "command.h"
25 #include "lib/log.h"
26 #include "lib/zlog_targets.h"
27 #include "lib/zlog_5424.h"
28 #include "lib/lib_errors.h"
29 #include "lib/printfrr.h"
30 #include "lib/systemd.h"
31
32 #ifndef VTYSH_EXTRACT_PL
33 #include "lib/log_vty_clippy.c"
34 #endif
35
36 #define ZLOG_MAXLVL(a, b) MAX(a, b)
37
38 DEFINE_HOOK(zlog_rotate, (), ());
39 DEFINE_HOOK(zlog_cli_show, (struct vty * vty), (vty));
40
41 static unsigned logmsgs_with_persist_bt;
42
43 static const int log_default_lvl = LOG_DEBUG;
44
45 static int log_config_stdout_lvl = ZLOG_DISABLED;
46 static int log_config_syslog_lvl = ZLOG_DISABLED;
47 static int log_cmdline_stdout_lvl = ZLOG_DISABLED;
48 static int log_cmdline_syslog_lvl = ZLOG_DISABLED;
49
50 static struct zlog_cfg_file zt_file_cmdline = {
51 .prio_min = ZLOG_DISABLED,
52 };
53 static struct zlog_cfg_file zt_file = {
54 .prio_min = ZLOG_DISABLED,
55 };
56 static struct zlog_cfg_filterfile zt_filterfile = {
57 .parent = {
58 .prio_min = ZLOG_DISABLED,
59 },
60 };
61
62 static struct zlog_cfg_file zt_stdout_file = {
63 .prio_min = ZLOG_DISABLED,
64 };
65 static struct zlog_cfg_5424 zt_stdout_journald = {
66 .prio_min = ZLOG_DISABLED,
67
68 .fmt = ZLOG_FMT_JOURNALD,
69 .dst = ZLOG_5424_DST_UNIX,
70 .filename = "/run/systemd/journal/socket",
71
72 /* this can't be changed through config since this target substitutes
73 * in for the "plain" stdout target
74 */
75 .facility = LOG_DAEMON,
76 .kw_version = false,
77 .kw_location = true,
78 .kw_uid = true,
79 .kw_ec = true,
80 .kw_args = true,
81 };
82 static bool stdout_journald_in_use;
83
84 const char *zlog_progname;
85 static const char *zlog_protoname;
86
87 static const struct facility_map {
88 int facility;
89 const char *name;
90 size_t match;
91 } syslog_facilities[] = {
92 {LOG_KERN, "kern", 1},
93 {LOG_USER, "user", 2},
94 {LOG_MAIL, "mail", 1},
95 {LOG_DAEMON, "daemon", 1},
96 {LOG_AUTH, "auth", 1},
97 {LOG_SYSLOG, "syslog", 1},
98 {LOG_LPR, "lpr", 2},
99 {LOG_NEWS, "news", 1},
100 {LOG_UUCP, "uucp", 2},
101 {LOG_CRON, "cron", 1},
102 #ifdef LOG_FTP
103 {LOG_FTP, "ftp", 1},
104 #endif
105 {LOG_LOCAL0, "local0", 6},
106 {LOG_LOCAL1, "local1", 6},
107 {LOG_LOCAL2, "local2", 6},
108 {LOG_LOCAL3, "local3", 6},
109 {LOG_LOCAL4, "local4", 6},
110 {LOG_LOCAL5, "local5", 6},
111 {LOG_LOCAL6, "local6", 6},
112 {LOG_LOCAL7, "local7", 6},
113 {0, NULL, 0},
114 };
115
116 static const char * const zlog_priority[] = {
117 "emergencies", "alerts", "critical", "errors", "warnings",
118 "notifications", "informational", "debugging", NULL,
119 };
120
121 const char *zlog_priority_str(int priority)
122 {
123 if (priority > LOG_DEBUG)
124 return "???";
125 return zlog_priority[priority];
126 }
127
128 const char *facility_name(int facility)
129 {
130 const struct facility_map *fm;
131
132 for (fm = syslog_facilities; fm->name; fm++)
133 if (fm->facility == facility)
134 return fm->name;
135 return "";
136 }
137
138 int facility_match(const char *str)
139 {
140 const struct facility_map *fm;
141
142 for (fm = syslog_facilities; fm->name; fm++)
143 if (!strncmp(str, fm->name, fm->match))
144 return fm->facility;
145 return -1;
146 }
147
148 int log_level_match(const char *s)
149 {
150 int level;
151
152 for (level = 0; zlog_priority[level] != NULL; level++)
153 if (!strncmp(s, zlog_priority[level], 2))
154 return level;
155 return ZLOG_DISABLED;
156 }
157
158 void zlog_rotate(void)
159 {
160 zlog_file_rotate(&zt_file);
161 zlog_file_rotate(&zt_filterfile.parent);
162 hook_call(zlog_rotate);
163 }
164
165
166 void log_show_syslog(struct vty *vty)
167 {
168 int level = zlog_syslog_get_prio_min();
169
170 vty_out(vty, "Syslog logging: ");
171 if (level == ZLOG_DISABLED)
172 vty_out(vty, "disabled\n");
173 else
174 vty_out(vty, "level %s, facility %s, ident %s\n",
175 zlog_priority[level],
176 facility_name(zlog_syslog_get_facility()),
177 zlog_progname);
178 }
179
180 DEFUN_NOSH (show_logging,
181 show_logging_cmd,
182 "show logging",
183 SHOW_STR
184 "Show current logging configuration\n")
185 {
186 int stdout_prio;
187
188 log_show_syslog(vty);
189
190 stdout_prio = stdout_journald_in_use ? zt_stdout_journald.prio_min
191 : zt_stdout_file.prio_min;
192
193 vty_out(vty, "Stdout logging: ");
194 if (stdout_prio == ZLOG_DISABLED)
195 vty_out(vty, "disabled");
196 else
197 vty_out(vty, "level %s", zlog_priority[stdout_prio]);
198 vty_out(vty, "\n");
199
200 vty_out(vty, "File logging: ");
201 if (zt_file.prio_min == ZLOG_DISABLED || !zt_file.filename)
202 vty_out(vty, "disabled");
203 else
204 vty_out(vty, "level %s, filename %s",
205 zlog_priority[zt_file.prio_min], zt_file.filename);
206 vty_out(vty, "\n");
207
208 if (zt_filterfile.parent.prio_min != ZLOG_DISABLED
209 && zt_filterfile.parent.filename)
210 vty_out(vty, "Filtered-file logging: level %s, filename %s\n",
211 zlog_priority[zt_filterfile.parent.prio_min],
212 zt_filterfile.parent.filename);
213
214 if (log_cmdline_syslog_lvl != ZLOG_DISABLED)
215 vty_out(vty,
216 "From command line: \"--log syslog --log-level %s\"\n",
217 zlog_priority[log_cmdline_syslog_lvl]);
218 if (log_cmdline_stdout_lvl != ZLOG_DISABLED)
219 vty_out(vty,
220 "From command line: \"--log stdout --log-level %s\"\n",
221 zlog_priority[log_cmdline_stdout_lvl]);
222 if (zt_file_cmdline.prio_min != ZLOG_DISABLED)
223 vty_out(vty,
224 "From command line: \"--log file:%s --log-level %s\"\n",
225 zt_file_cmdline.filename,
226 zlog_priority[zt_file_cmdline.prio_min]);
227
228 vty_out(vty, "Protocol name: %s\n", zlog_protoname);
229 vty_out(vty, "Record priority: %s\n",
230 (zt_file.record_priority ? "enabled" : "disabled"));
231 vty_out(vty, "Timestamp precision: %d\n", zt_file.ts_subsec);
232
233 hook_call(zlog_cli_show, vty);
234 return CMD_SUCCESS;
235 }
236
237 static void log_stdout_apply_level(void)
238 {
239 int maxlvl;
240
241 maxlvl = ZLOG_MAXLVL(log_config_stdout_lvl, log_cmdline_stdout_lvl);
242
243 if (stdout_journald_in_use) {
244 zt_stdout_journald.prio_min = maxlvl;
245 zlog_5424_apply_meta(&zt_stdout_journald);
246 } else {
247 zt_stdout_file.prio_min = maxlvl;
248 zlog_file_set_other(&zt_stdout_file);
249 }
250 }
251
252 DEFPY (config_log_stdout,
253 config_log_stdout_cmd,
254 "log stdout [<emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>$levelarg]",
255 "Logging control\n"
256 "Set stdout logging level\n"
257 LOG_LEVEL_DESC)
258 {
259 int level;
260
261 if (levelarg) {
262 level = log_level_match(levelarg);
263 if (level == ZLOG_DISABLED)
264 return CMD_ERR_NO_MATCH;
265 } else
266 level = log_default_lvl;
267
268 log_config_stdout_lvl = level;
269 log_stdout_apply_level();
270 return CMD_SUCCESS;
271 }
272
273 DEFUN (no_config_log_stdout,
274 no_config_log_stdout_cmd,
275 "no log stdout [<emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>]",
276 NO_STR
277 "Logging control\n"
278 "Cancel logging to stdout\n"
279 LOG_LEVEL_DESC)
280 {
281 log_config_stdout_lvl = ZLOG_DISABLED;
282 log_stdout_apply_level();
283 return CMD_SUCCESS;
284 }
285
286 DEFUN_HIDDEN (config_log_monitor,
287 config_log_monitor_cmd,
288 "log monitor [<emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>]",
289 "Logging control\n"
290 "Set terminal line (monitor) logging level\n"
291 LOG_LEVEL_DESC)
292 {
293 vty_out(vty, "%% \"log monitor\" is deprecated and does nothing.\n");
294 return CMD_SUCCESS;
295 }
296
297 DEFUN_HIDDEN (no_config_log_monitor,
298 no_config_log_monitor_cmd,
299 "no log monitor [<emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>]",
300 NO_STR
301 "Logging control\n"
302 "Disable terminal line (monitor) logging\n"
303 LOG_LEVEL_DESC)
304 {
305 return CMD_SUCCESS;
306 }
307
308 DEFPY_NOSH (debug_uid_backtrace,
309 debug_uid_backtrace_cmd,
310 "[no] debug unique-id UID backtrace",
311 NO_STR
312 DEBUG_STR
313 "Options per individual log message, by unique ID\n"
314 "Log message unique ID (XXXXX-XXXXX)\n"
315 "Add backtrace to log when message is printed\n")
316 {
317 struct xrefdata search, *xrd;
318 struct xrefdata_logmsg *xrdl;
319 uint8_t flag;
320
321 strlcpy(search.uid, uid, sizeof(search.uid));
322 xrd = xrefdata_uid_find(&xrefdata_uid, &search);
323
324 if (!xrd)
325 return CMD_ERR_NOTHING_TODO;
326
327 if (xrd->xref->type != XREFT_LOGMSG) {
328 vty_out(vty, "%% ID \"%s\" is not a log message\n", uid);
329 return CMD_WARNING;
330 }
331 xrdl = container_of(xrd, struct xrefdata_logmsg, xrefdata);
332
333 flag = (vty->node == CONFIG_NODE) ? LOGMSG_FLAG_PERSISTENT
334 : LOGMSG_FLAG_EPHEMERAL;
335
336 if ((xrdl->fl_print_bt & flag) == (no ? 0 : flag))
337 return CMD_SUCCESS;
338 if (flag == LOGMSG_FLAG_PERSISTENT)
339 logmsgs_with_persist_bt += no ? -1 : 1;
340
341 xrdl->fl_print_bt ^= flag;
342 return CMD_SUCCESS;
343 }
344
345 static int set_log_file(struct zlog_cfg_file *target, struct vty *vty,
346 const char *fname, int loglevel)
347 {
348 char path[MAXPATHLEN + 1];
349 const char *fullpath;
350 bool ok;
351
352
353 /* Path detection. */
354 if (!IS_DIRECTORY_SEP(*fname)) {
355 char cwd[MAXPATHLEN + 1];
356
357 cwd[MAXPATHLEN] = '\0';
358
359 if (getcwd(cwd, MAXPATHLEN) == NULL) {
360 flog_err_sys(EC_LIB_SYSTEM_CALL,
361 "config_log_file: Unable to alloc mem!");
362 return CMD_WARNING_CONFIG_FAILED;
363 }
364
365 int pr = snprintf(path, sizeof(path), "%s/%s", cwd, fname);
366 if (pr < 0 || (unsigned int)pr >= sizeof(path)) {
367 flog_err_sys(
368 EC_LIB_SYSTEM_CALL,
369 "%s: Path too long ('%s/%s'); system maximum is %u",
370 __func__, cwd, fname, MAXPATHLEN);
371 return CMD_WARNING_CONFIG_FAILED;
372 }
373
374 fullpath = path;
375 } else
376 fullpath = fname;
377
378 target->prio_min = loglevel;
379 ok = zlog_file_set_filename(target, fullpath);
380
381 if (!ok) {
382 if (vty)
383 vty_out(vty, "can't open logfile %s\n", fname);
384 return CMD_WARNING_CONFIG_FAILED;
385 }
386 return CMD_SUCCESS;
387 }
388
389 void command_setup_early_logging(const char *dest, const char *level)
390 {
391 int nlevel;
392 char *sep;
393 int len;
394 char type[8];
395
396 if (level) {
397 nlevel = log_level_match(level);
398
399 if (nlevel == ZLOG_DISABLED) {
400 fprintf(stderr, "invalid log level \"%s\"\n", level);
401 exit(1);
402 }
403 } else
404 nlevel = log_default_lvl;
405
406 if (!dest)
407 return;
408
409 sep = strchr(dest, ':');
410 len = sep ? (int)(sep - dest) : (int)strlen(dest);
411
412 snprintfrr(type, sizeof(type), "%.*s", len, dest);
413
414 if (strcmp(type, "stdout") == 0) {
415 log_cmdline_stdout_lvl = nlevel;
416 log_stdout_apply_level();
417 return;
418 }
419 if (strcmp(type, "syslog") == 0) {
420 log_cmdline_syslog_lvl = nlevel;
421 zlog_syslog_set_prio_min(ZLOG_MAXLVL(log_config_syslog_lvl,
422 log_cmdline_syslog_lvl));
423 return;
424 }
425 if (strcmp(type, "file") == 0 && sep) {
426 sep++;
427 set_log_file(&zt_file_cmdline, NULL, sep, nlevel);
428 return;
429 }
430
431 fprintf(stderr, "invalid log target \"%s\" (\"%s\")\n", type, dest);
432 exit(1);
433 }
434
435 DEFUN (clear_log_cmdline,
436 clear_log_cmdline_cmd,
437 "clear log cmdline-targets",
438 CLEAR_STR
439 "Logging control\n"
440 "Disable log targets specified at startup by --log option\n")
441 {
442 zt_file_cmdline.prio_min = ZLOG_DISABLED;
443 zlog_file_set_other(&zt_file_cmdline);
444
445 log_cmdline_syslog_lvl = ZLOG_DISABLED;
446 zlog_syslog_set_prio_min(ZLOG_MAXLVL(log_config_syslog_lvl,
447 log_cmdline_syslog_lvl));
448
449 log_cmdline_stdout_lvl = ZLOG_DISABLED;
450 log_stdout_apply_level();
451
452 return CMD_SUCCESS;
453 }
454
455 DEFPY (config_log_file,
456 config_log_file_cmd,
457 "log file FILENAME [<emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>$levelarg]",
458 "Logging control\n"
459 "Logging to file\n"
460 "Logging filename\n"
461 LOG_LEVEL_DESC)
462 {
463 int level = log_default_lvl;
464
465 if (levelarg) {
466 level = log_level_match(levelarg);
467 if (level == ZLOG_DISABLED)
468 return CMD_ERR_NO_MATCH;
469 }
470 return set_log_file(&zt_file, vty, filename, level);
471 }
472
473 DEFUN (no_config_log_file,
474 no_config_log_file_cmd,
475 "no log file [FILENAME [LEVEL]]",
476 NO_STR
477 "Logging control\n"
478 "Cancel logging to file\n"
479 "Logging file name\n"
480 "Logging level\n")
481 {
482 zt_file.prio_min = ZLOG_DISABLED;
483 zlog_file_set_other(&zt_file);
484 return CMD_SUCCESS;
485 }
486
487 DEFPY (config_log_syslog,
488 config_log_syslog_cmd,
489 "log syslog [<emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>$levelarg]",
490 "Logging control\n"
491 "Set syslog logging level\n"
492 LOG_LEVEL_DESC)
493 {
494 int level;
495
496 if (levelarg) {
497 level = log_level_match(levelarg);
498
499 if (level == ZLOG_DISABLED)
500 return CMD_ERR_NO_MATCH;
501 } else
502 level = log_default_lvl;
503
504 log_config_syslog_lvl = level;
505 zlog_syslog_set_prio_min(ZLOG_MAXLVL(log_config_syslog_lvl,
506 log_cmdline_syslog_lvl));
507 return CMD_SUCCESS;
508 }
509
510 DEFUN (no_config_log_syslog,
511 no_config_log_syslog_cmd,
512 "no log syslog [<kern|user|mail|daemon|auth|syslog|lpr|news|uucp|cron|local0|local1|local2|local3|local4|local5|local6|local7>] [<emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>]",
513 NO_STR
514 "Logging control\n"
515 "Cancel logging to syslog\n"
516 LOG_FACILITY_DESC
517 LOG_LEVEL_DESC)
518 {
519 log_config_syslog_lvl = ZLOG_DISABLED;
520 zlog_syslog_set_prio_min(ZLOG_MAXLVL(log_config_syslog_lvl,
521 log_cmdline_syslog_lvl));
522 return CMD_SUCCESS;
523 }
524
525 DEFPY (config_log_facility,
526 config_log_facility_cmd,
527 "log facility <kern|user|mail|daemon|auth|syslog|lpr|news|uucp|cron|local0|local1|local2|local3|local4|local5|local6|local7>$facilityarg",
528 "Logging control\n"
529 "Facility parameter for syslog messages\n"
530 LOG_FACILITY_DESC)
531 {
532 int facility = facility_match(facilityarg);
533
534 zlog_syslog_set_facility(facility);
535 return CMD_SUCCESS;
536 }
537
538 DEFUN (no_config_log_facility,
539 no_config_log_facility_cmd,
540 "no log facility [<kern|user|mail|daemon|auth|syslog|lpr|news|uucp|cron|local0|local1|local2|local3|local4|local5|local6|local7>]",
541 NO_STR
542 "Logging control\n"
543 "Reset syslog facility to default (daemon)\n"
544 LOG_FACILITY_DESC)
545 {
546 zlog_syslog_set_facility(LOG_DAEMON);
547 return CMD_SUCCESS;
548 }
549
550 DEFUN (config_log_record_priority,
551 config_log_record_priority_cmd,
552 "log record-priority",
553 "Logging control\n"
554 "Log the priority of the message within the message\n")
555 {
556 zt_file.record_priority = true;
557 zlog_file_set_other(&zt_file);
558 if (!stdout_journald_in_use) {
559 zt_stdout_file.record_priority = true;
560 zlog_file_set_other(&zt_stdout_file);
561 }
562 zt_filterfile.parent.record_priority = true;
563 zlog_file_set_other(&zt_filterfile.parent);
564 return CMD_SUCCESS;
565 }
566
567 DEFUN (no_config_log_record_priority,
568 no_config_log_record_priority_cmd,
569 "no log record-priority",
570 NO_STR
571 "Logging control\n"
572 "Do not log the priority of the message within the message\n")
573 {
574 zt_file.record_priority = false;
575 zlog_file_set_other(&zt_file);
576 if (!stdout_journald_in_use) {
577 zt_stdout_file.record_priority = false;
578 zlog_file_set_other(&zt_stdout_file);
579 }
580 zt_filterfile.parent.record_priority = false;
581 zlog_file_set_other(&zt_filterfile.parent);
582 return CMD_SUCCESS;
583 }
584
585 DEFPY (config_log_timestamp_precision,
586 config_log_timestamp_precision_cmd,
587 "log timestamp precision (0-6)",
588 "Logging control\n"
589 "Timestamp configuration\n"
590 "Set the timestamp precision\n"
591 "Number of subsecond digits\n")
592 {
593 zt_file.ts_subsec = precision;
594 zlog_file_set_other(&zt_file);
595 if (!stdout_journald_in_use) {
596 zt_stdout_file.ts_subsec = precision;
597 zlog_file_set_other(&zt_stdout_file);
598 }
599 zt_filterfile.parent.ts_subsec = precision;
600 zlog_file_set_other(&zt_filterfile.parent);
601 return CMD_SUCCESS;
602 }
603
604 DEFUN (no_config_log_timestamp_precision,
605 no_config_log_timestamp_precision_cmd,
606 "no log timestamp precision [(0-6)]",
607 NO_STR
608 "Logging control\n"
609 "Timestamp configuration\n"
610 "Reset the timestamp precision to the default value of 0\n"
611 "Number of subsecond digits\n")
612 {
613 zt_file.ts_subsec = 0;
614 zlog_file_set_other(&zt_file);
615 if (!stdout_journald_in_use) {
616 zt_stdout_file.ts_subsec = 0;
617 zlog_file_set_other(&zt_stdout_file);
618 }
619 zt_filterfile.parent.ts_subsec = 0;
620 zlog_file_set_other(&zt_filterfile.parent);
621 return CMD_SUCCESS;
622 }
623
624 DEFPY (config_log_ec,
625 config_log_ec_cmd,
626 "[no] log error-category",
627 NO_STR
628 "Logging control\n"
629 "Prefix log message text with [EC 9999] code\n")
630 {
631 zlog_set_prefix_ec(!no);
632 return CMD_SUCCESS;
633 }
634
635 DEFPY (config_log_xid,
636 config_log_xid_cmd,
637 "[no] log unique-id",
638 NO_STR
639 "Logging control\n"
640 "Prefix log message text with [XXXXX-XXXXX] identifier\n")
641 {
642 zlog_set_prefix_xid(!no);
643 return CMD_SUCCESS;
644 }
645
646 DEFPY (config_log_filterfile,
647 config_log_filterfile_cmd,
648 "log filtered-file FILENAME [<emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>$levelarg]",
649 "Logging control\n"
650 "Logging to file with string filter\n"
651 "Logging filename\n"
652 LOG_LEVEL_DESC)
653 {
654 int level = log_default_lvl;
655
656 if (levelarg) {
657 level = log_level_match(levelarg);
658 if (level == ZLOG_DISABLED)
659 return CMD_ERR_NO_MATCH;
660 }
661 return set_log_file(&zt_filterfile.parent, vty, filename, level);
662 }
663
664 DEFUN (no_config_log_filterfile,
665 no_config_log_filterfile_cmd,
666 "no log filtered-file [FILENAME [LEVEL]]",
667 NO_STR
668 "Logging control\n"
669 "Cancel logging to file with string filter\n"
670 "Logging file name\n"
671 "Logging level\n")
672 {
673 zt_filterfile.parent.prio_min = ZLOG_DISABLED;
674 zlog_file_set_other(&zt_filterfile.parent);
675 return CMD_SUCCESS;
676 }
677
678 DEFPY (log_filter,
679 log_filter_cmd,
680 "[no] log filter-text WORD$filter",
681 NO_STR
682 "Logging control\n"
683 FILTER_LOG_STR
684 "String to filter by\n")
685 {
686 int ret = 0;
687
688 if (no)
689 ret = zlog_filter_del(filter);
690 else
691 ret = zlog_filter_add(filter);
692
693 if (ret == 1) {
694 vty_out(vty, "%% filter table full\n");
695 return CMD_WARNING;
696 } else if (ret != 0) {
697 vty_out(vty, "%% failed to %s log filter\n",
698 (no ? "remove" : "apply"));
699 return CMD_WARNING;
700 }
701
702 vty_out(vty, " %s\n", filter);
703 return CMD_SUCCESS;
704 }
705
706 /* Clear all log filters */
707 DEFPY (log_filter_clear,
708 log_filter_clear_cmd,
709 "clear log filter-text",
710 CLEAR_STR
711 "Logging control\n"
712 FILTER_LOG_STR)
713 {
714 zlog_filter_clear();
715 return CMD_SUCCESS;
716 }
717
718 /* Show log filter */
719 DEFPY (show_log_filter,
720 show_log_filter_cmd,
721 "show logging filter-text",
722 SHOW_STR
723 "Show current logging configuration\n"
724 FILTER_LOG_STR)
725 {
726 char log_filters[ZLOG_FILTERS_MAX * (ZLOG_FILTER_LENGTH_MAX + 3)] = "";
727 int len = 0;
728
729 len = zlog_filter_dump(log_filters, sizeof(log_filters));
730
731 if (len == -1) {
732 vty_out(vty, "%% failed to get filters\n");
733 return CMD_WARNING;
734 }
735
736 if (len != 0)
737 vty_out(vty, "%s", log_filters);
738
739 return CMD_SUCCESS;
740 }
741
742 /* Enable/disable 'immediate' mode, with no output buffering */
743 DEFPY (log_immediate_mode,
744 log_immediate_mode_cmd,
745 "[no] log immediate-mode",
746 NO_STR
747 "Logging control"
748 "Output immediately, without buffering")
749 {
750 zlog_set_immediate(!no);
751 return CMD_SUCCESS;
752 }
753
754 void log_config_write(struct vty *vty)
755 {
756 bool show_cmdline_hint = false;
757
758 if (zt_file.prio_min != ZLOG_DISABLED && zt_file.filename) {
759 vty_out(vty, "log file %s", zt_file.filename);
760
761 if (zt_file.prio_min != log_default_lvl)
762 vty_out(vty, " %s", zlog_priority[zt_file.prio_min]);
763 vty_out(vty, "\n");
764 }
765
766 if (zt_filterfile.parent.prio_min != ZLOG_DISABLED
767 && zt_filterfile.parent.filename) {
768 vty_out(vty, "log filtered-file %s",
769 zt_filterfile.parent.filename);
770
771 if (zt_filterfile.parent.prio_min != log_default_lvl)
772 vty_out(vty, " %s",
773 zlog_priority[zt_filterfile.parent.prio_min]);
774 vty_out(vty, "\n");
775 }
776
777 if (log_config_stdout_lvl != ZLOG_DISABLED) {
778 vty_out(vty, "log stdout");
779
780 if (log_config_stdout_lvl != log_default_lvl)
781 vty_out(vty, " %s",
782 zlog_priority[log_config_stdout_lvl]);
783 vty_out(vty, "\n");
784 }
785
786 if (log_config_syslog_lvl != ZLOG_DISABLED) {
787 vty_out(vty, "log syslog");
788
789 if (log_config_syslog_lvl != log_default_lvl)
790 vty_out(vty, " %s",
791 zlog_priority[log_config_syslog_lvl]);
792 vty_out(vty, "\n");
793 }
794
795 if (log_cmdline_syslog_lvl != ZLOG_DISABLED) {
796 vty_out(vty,
797 "! \"log syslog %s\" enabled by \"--log\" startup option\n",
798 zlog_priority[log_cmdline_syslog_lvl]);
799 show_cmdline_hint = true;
800 }
801 if (log_cmdline_stdout_lvl != ZLOG_DISABLED) {
802 vty_out(vty,
803 "! \"log stdout %s\" enabled by \"--log\" startup option\n",
804 zlog_priority[log_cmdline_stdout_lvl]);
805 show_cmdline_hint = true;
806 }
807 if (zt_file_cmdline.prio_min != ZLOG_DISABLED) {
808 vty_out(vty,
809 "! \"log file %s %s\" enabled by \"--log\" startup option\n",
810 zt_file_cmdline.filename,
811 zlog_priority[zt_file_cmdline.prio_min]);
812 show_cmdline_hint = true;
813 }
814 if (show_cmdline_hint)
815 vty_out(vty,
816 "! use \"clear log cmdline-targets\" to remove this target\n");
817
818 if (zlog_syslog_get_facility() != LOG_DAEMON)
819 vty_out(vty, "log facility %s\n",
820 facility_name(zlog_syslog_get_facility()));
821
822 if (zt_file.record_priority == 1)
823 vty_out(vty, "log record-priority\n");
824
825 if (zt_file.ts_subsec > 0)
826 vty_out(vty, "log timestamp precision %d\n",
827 zt_file.ts_subsec);
828
829 if (!zlog_get_prefix_ec())
830 vty_out(vty, "no log error-category\n");
831 if (!zlog_get_prefix_xid())
832 vty_out(vty, "no log unique-id\n");
833
834 if (logmsgs_with_persist_bt) {
835 struct xrefdata *xrd;
836 struct xrefdata_logmsg *xrdl;
837
838 vty_out(vty, "!\n");
839
840 frr_each (xrefdata_uid, &xrefdata_uid, xrd) {
841 if (xrd->xref->type != XREFT_LOGMSG)
842 continue;
843
844 xrdl = container_of(xrd, struct xrefdata_logmsg,
845 xrefdata);
846 if (xrdl->fl_print_bt & LOGMSG_FLAG_PERSISTENT)
847 vty_out(vty, "debug unique-id %s backtrace\n",
848 xrd->uid);
849 }
850 }
851 }
852
853 static int log_vty_init(const char *progname, const char *protoname,
854 unsigned short instance, uid_t uid, gid_t gid)
855 {
856 zlog_progname = progname;
857 zlog_protoname = protoname;
858
859 zlog_set_prefix_ec(true);
860 zlog_set_prefix_xid(true);
861
862 zlog_filterfile_init(&zt_filterfile);
863
864 if (sd_stdout_is_journal) {
865 stdout_journald_in_use = true;
866 zlog_5424_init(&zt_stdout_journald);
867 zlog_5424_apply_dst(&zt_stdout_journald);
868 } else
869 zlog_file_set_fd(&zt_stdout_file, STDOUT_FILENO);
870 return 0;
871 }
872
873 __attribute__((_CONSTRUCTOR(475))) static void log_vty_preinit(void)
874 {
875 hook_register(zlog_init, log_vty_init);
876 }
877
878 void log_cmd_init(void)
879 {
880 install_element(VIEW_NODE, &show_logging_cmd);
881 install_element(ENABLE_NODE, &clear_log_cmdline_cmd);
882
883 install_element(CONFIG_NODE, &config_log_stdout_cmd);
884 install_element(CONFIG_NODE, &no_config_log_stdout_cmd);
885 install_element(CONFIG_NODE, &config_log_monitor_cmd);
886 install_element(CONFIG_NODE, &no_config_log_monitor_cmd);
887 install_element(CONFIG_NODE, &config_log_file_cmd);
888 install_element(CONFIG_NODE, &no_config_log_file_cmd);
889 install_element(CONFIG_NODE, &config_log_syslog_cmd);
890 install_element(CONFIG_NODE, &no_config_log_syslog_cmd);
891 install_element(CONFIG_NODE, &config_log_facility_cmd);
892 install_element(CONFIG_NODE, &no_config_log_facility_cmd);
893 install_element(CONFIG_NODE, &config_log_record_priority_cmd);
894 install_element(CONFIG_NODE, &no_config_log_record_priority_cmd);
895 install_element(CONFIG_NODE, &config_log_timestamp_precision_cmd);
896 install_element(CONFIG_NODE, &no_config_log_timestamp_precision_cmd);
897 install_element(CONFIG_NODE, &config_log_ec_cmd);
898 install_element(CONFIG_NODE, &config_log_xid_cmd);
899
900 install_element(VIEW_NODE, &show_log_filter_cmd);
901 install_element(CONFIG_NODE, &log_filter_cmd);
902 install_element(CONFIG_NODE, &log_filter_clear_cmd);
903 install_element(CONFIG_NODE, &config_log_filterfile_cmd);
904 install_element(CONFIG_NODE, &no_config_log_filterfile_cmd);
905 install_element(CONFIG_NODE, &log_immediate_mode_cmd);
906
907 install_element(ENABLE_NODE, &debug_uid_backtrace_cmd);
908 install_element(CONFIG_NODE, &debug_uid_backtrace_cmd);
909
910 log_5424_cmd_init();
911 }