]>
Commit | Line | Data |
---|---|---|
f73126c3 SW |
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" | |
f73126c3 | 25 | #include "lib/log.h" |
0bdeb5e5 DL |
26 | #include "lib/zlog_targets.h" |
27 | #include "lib/lib_errors.h" | |
28 | #include "lib/printfrr.h" | |
29 | ||
f73126c3 SW |
30 | #ifndef VTYSH_EXTRACT_PL |
31 | #include "lib/log_vty_clippy.c" | |
32 | #endif | |
33 | ||
0bdeb5e5 DL |
34 | #define ZLOG_MAXLVL(a, b) MAX(a, b) |
35 | ||
36 | DEFINE_HOOK(zlog_rotate, (), ()) | |
37 | ||
38 | static const int log_default_lvl = LOG_DEBUG; | |
39 | ||
40 | static int log_config_stdout_lvl = ZLOG_DISABLED; | |
41 | static int log_config_syslog_lvl = ZLOG_DISABLED; | |
42 | static int log_cmdline_stdout_lvl = ZLOG_DISABLED; | |
43 | static int log_cmdline_syslog_lvl = ZLOG_DISABLED; | |
44 | ||
45 | static struct zlog_cfg_file zt_file_cmdline = { | |
46 | .prio_min = ZLOG_DISABLED, | |
47 | }; | |
48 | static struct zlog_cfg_file zt_file = { | |
49 | .prio_min = ZLOG_DISABLED, | |
50 | }; | |
51 | static struct zlog_cfg_file zt_stdout = { | |
52 | .prio_min = ZLOG_DISABLED, | |
53 | }; | |
1c408628 DL |
54 | static struct zlog_cfg_filterfile zt_filterfile = { |
55 | .parent = { | |
56 | .prio_min = ZLOG_DISABLED, | |
57 | }, | |
58 | }; | |
0bdeb5e5 DL |
59 | |
60 | static const char *zlog_progname; | |
61 | static const char *zlog_protoname; | |
62 | ||
63 | static const struct facility_map { | |
64 | int facility; | |
65 | const char *name; | |
66 | size_t match; | |
67 | } syslog_facilities[] = { | |
68 | {LOG_KERN, "kern", 1}, | |
69 | {LOG_USER, "user", 2}, | |
70 | {LOG_MAIL, "mail", 1}, | |
71 | {LOG_DAEMON, "daemon", 1}, | |
72 | {LOG_AUTH, "auth", 1}, | |
73 | {LOG_SYSLOG, "syslog", 1}, | |
74 | {LOG_LPR, "lpr", 2}, | |
75 | {LOG_NEWS, "news", 1}, | |
76 | {LOG_UUCP, "uucp", 2}, | |
77 | {LOG_CRON, "cron", 1}, | |
78 | #ifdef LOG_FTP | |
79 | {LOG_FTP, "ftp", 1}, | |
80 | #endif | |
81 | {LOG_LOCAL0, "local0", 6}, | |
82 | {LOG_LOCAL1, "local1", 6}, | |
83 | {LOG_LOCAL2, "local2", 6}, | |
84 | {LOG_LOCAL3, "local3", 6}, | |
85 | {LOG_LOCAL4, "local4", 6}, | |
86 | {LOG_LOCAL5, "local5", 6}, | |
87 | {LOG_LOCAL6, "local6", 6}, | |
88 | {LOG_LOCAL7, "local7", 6}, | |
89 | {0, NULL, 0}, | |
90 | }; | |
91 | ||
92 | static const char * const zlog_priority[] = { | |
93 | "emergencies", "alerts", "critical", "errors", "warnings", | |
94 | "notifications", "informational", "debugging", NULL, | |
95 | }; | |
96 | ||
97 | static const char *facility_name(int facility) | |
98 | { | |
99 | const struct facility_map *fm; | |
100 | ||
101 | for (fm = syslog_facilities; fm->name; fm++) | |
102 | if (fm->facility == facility) | |
103 | return fm->name; | |
104 | return ""; | |
105 | } | |
106 | ||
107 | static int facility_match(const char *str) | |
f73126c3 | 108 | { |
0bdeb5e5 | 109 | const struct facility_map *fm; |
f73126c3 | 110 | |
0bdeb5e5 DL |
111 | for (fm = syslog_facilities; fm->name; fm++) |
112 | if (!strncmp(str, fm->name, fm->match)) | |
113 | return fm->facility; | |
114 | return -1; | |
115 | } | |
116 | ||
117 | int log_level_match(const char *s) | |
118 | { | |
119 | int level; | |
120 | ||
121 | for (level = 0; zlog_priority[level] != NULL; level++) | |
122 | if (!strncmp(s, zlog_priority[level], 2)) | |
123 | return level; | |
124 | return ZLOG_DISABLED; | |
125 | } | |
126 | ||
127 | void zlog_rotate(void) | |
128 | { | |
129 | zlog_file_rotate(&zt_file); | |
1c408628 | 130 | zlog_file_rotate(&zt_filterfile.parent); |
0bdeb5e5 DL |
131 | hook_call(zlog_rotate); |
132 | } | |
133 | ||
134 | ||
135 | void log_show_syslog(struct vty *vty) | |
136 | { | |
137 | int level = zlog_syslog_get_prio_min(); | |
138 | ||
139 | vty_out(vty, "Syslog logging: "); | |
140 | if (level == ZLOG_DISABLED) | |
141 | vty_out(vty, "disabled\n"); | |
f73126c3 | 142 | else |
0bdeb5e5 DL |
143 | vty_out(vty, "level %s, facility %s, ident %s\n", |
144 | zlog_priority[level], | |
145 | facility_name(zlog_syslog_get_facility()), | |
146 | zlog_progname); | |
147 | } | |
f73126c3 | 148 | |
0bdeb5e5 DL |
149 | DEFUN (show_logging, |
150 | show_logging_cmd, | |
151 | "show logging", | |
152 | SHOW_STR | |
153 | "Show current logging configuration\n") | |
154 | { | |
155 | log_show_syslog(vty); | |
156 | ||
157 | vty_out(vty, "Stdout logging: "); | |
158 | if (zt_stdout.prio_min == ZLOG_DISABLED) | |
159 | vty_out(vty, "disabled"); | |
160 | else | |
161 | vty_out(vty, "level %s", | |
162 | zlog_priority[zt_stdout.prio_min]); | |
163 | vty_out(vty, "\n"); | |
164 | ||
165 | vty_out(vty, "File logging: "); | |
166 | if (zt_file.prio_min == ZLOG_DISABLED || !zt_file.filename) | |
167 | vty_out(vty, "disabled"); | |
168 | else | |
169 | vty_out(vty, "level %s, filename %s", | |
170 | zlog_priority[zt_file.prio_min], zt_file.filename); | |
171 | vty_out(vty, "\n"); | |
172 | ||
1c408628 DL |
173 | if (zt_filterfile.parent.prio_min != ZLOG_DISABLED |
174 | && zt_filterfile.parent.filename) | |
175 | vty_out(vty, "Filtered-file logging: level %s, filename %s\n", | |
176 | zlog_priority[zt_filterfile.parent.prio_min], | |
177 | zt_filterfile.parent.filename); | |
178 | ||
0bdeb5e5 DL |
179 | if (log_cmdline_syslog_lvl != ZLOG_DISABLED) |
180 | vty_out(vty, | |
181 | "From command line: \"--log syslog --log-level %s\"\n", | |
182 | zlog_priority[log_cmdline_syslog_lvl]); | |
183 | if (log_cmdline_stdout_lvl != ZLOG_DISABLED) | |
184 | vty_out(vty, | |
185 | "From command line: \"--log stdout --log-level %s\"\n", | |
186 | zlog_priority[log_cmdline_stdout_lvl]); | |
187 | if (zt_file_cmdline.prio_min != ZLOG_DISABLED) | |
188 | vty_out(vty, | |
189 | "From command line: \"--log file:%s --log-level %s\"\n", | |
190 | zt_file_cmdline.filename, | |
191 | zlog_priority[zt_file_cmdline.prio_min]); | |
192 | ||
193 | vty_out(vty, "Protocol name: %s\n", zlog_protoname); | |
194 | vty_out(vty, "Record priority: %s\n", | |
195 | (zt_file.record_priority ? "enabled" : "disabled")); | |
196 | vty_out(vty, "Timestamp precision: %d\n", zt_file.ts_subsec); | |
f73126c3 SW |
197 | return CMD_SUCCESS; |
198 | } | |
199 | ||
0bdeb5e5 DL |
200 | DEFPY (config_log_stdout, |
201 | config_log_stdout_cmd, | |
202 | "log stdout [<emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>$levelarg]", | |
203 | "Logging control\n" | |
204 | "Set stdout logging level\n" | |
205 | LOG_LEVEL_DESC) | |
206 | { | |
207 | int level; | |
208 | ||
209 | if (levelarg) { | |
210 | level = log_level_match(levelarg); | |
211 | if (level == ZLOG_DISABLED) | |
212 | return CMD_ERR_NO_MATCH; | |
213 | } else | |
214 | level = log_default_lvl; | |
215 | ||
216 | log_config_stdout_lvl = level; | |
217 | zt_stdout.prio_min = ZLOG_MAXLVL(log_config_stdout_lvl, | |
218 | log_cmdline_stdout_lvl); | |
219 | zlog_file_set_other(&zt_stdout); | |
220 | return CMD_SUCCESS; | |
221 | } | |
222 | ||
223 | DEFUN (no_config_log_stdout, | |
224 | no_config_log_stdout_cmd, | |
225 | "no log stdout [<emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>]", | |
226 | NO_STR | |
227 | "Logging control\n" | |
228 | "Cancel logging to stdout\n" | |
229 | LOG_LEVEL_DESC) | |
f73126c3 | 230 | { |
0bdeb5e5 DL |
231 | log_config_stdout_lvl = ZLOG_DISABLED; |
232 | zt_stdout.prio_min = ZLOG_MAXLVL(log_config_stdout_lvl, | |
233 | log_cmdline_stdout_lvl); | |
234 | zlog_file_set_other(&zt_stdout); | |
f73126c3 SW |
235 | return CMD_SUCCESS; |
236 | } | |
237 | ||
0bdeb5e5 DL |
238 | DEFUN_HIDDEN (config_log_monitor, |
239 | config_log_monitor_cmd, | |
240 | "log monitor [<emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>]", | |
241 | "Logging control\n" | |
242 | "Set terminal line (monitor) logging level\n" | |
243 | LOG_LEVEL_DESC) | |
244 | { | |
245 | vty_out(vty, "%% \"log monitor\" is deprecated and does nothing.\n"); | |
246 | return CMD_SUCCESS; | |
247 | } | |
248 | ||
249 | DEFUN_HIDDEN (no_config_log_monitor, | |
250 | no_config_log_monitor_cmd, | |
251 | "no log monitor [<emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>]", | |
252 | NO_STR | |
253 | "Logging control\n" | |
254 | "Disable terminal line (monitor) logging\n" | |
255 | LOG_LEVEL_DESC) | |
256 | { | |
257 | return CMD_SUCCESS; | |
258 | } | |
259 | ||
260 | static int set_log_file(struct zlog_cfg_file *target, struct vty *vty, | |
261 | const char *fname, int loglevel) | |
262 | { | |
fc746f1c | 263 | char path[MAXPATHLEN + 1]; |
0bdeb5e5 DL |
264 | const char *fullpath; |
265 | bool ok; | |
266 | ||
fc746f1c | 267 | |
0bdeb5e5 DL |
268 | /* Path detection. */ |
269 | if (!IS_DIRECTORY_SEP(*fname)) { | |
270 | char cwd[MAXPATHLEN + 1]; | |
271 | ||
272 | cwd[MAXPATHLEN] = '\0'; | |
273 | ||
274 | if (getcwd(cwd, MAXPATHLEN) == NULL) { | |
275 | flog_err_sys(EC_LIB_SYSTEM_CALL, | |
276 | "config_log_file: Unable to alloc mem!"); | |
277 | return CMD_WARNING_CONFIG_FAILED; | |
278 | } | |
279 | ||
08808541 QY |
280 | int pr = snprintf(path, sizeof(path), "%s/%s", cwd, fname); |
281 | if (pr < 0 || (unsigned int)pr >= sizeof(path)) { | |
282 | flog_err_sys( | |
283 | EC_LIB_SYSTEM_CALL, | |
284 | "%s: Path too long ('%s/%s'); system maximum is %u", | |
285 | __func__, cwd, fname, MAXPATHLEN); | |
286 | return CMD_WARNING_CONFIG_FAILED; | |
287 | } | |
288 | ||
fc746f1c | 289 | fullpath = path; |
0bdeb5e5 DL |
290 | } else |
291 | fullpath = fname; | |
292 | ||
293 | target->prio_min = loglevel; | |
294 | ok = zlog_file_set_filename(target, fullpath); | |
295 | ||
0bdeb5e5 DL |
296 | if (!ok) { |
297 | if (vty) | |
298 | vty_out(vty, "can't open logfile %s\n", fname); | |
299 | return CMD_WARNING_CONFIG_FAILED; | |
300 | } | |
301 | return CMD_SUCCESS; | |
302 | } | |
303 | ||
304 | void command_setup_early_logging(const char *dest, const char *level) | |
305 | { | |
306 | int nlevel; | |
307 | char *sep; | |
308 | int len; | |
309 | char type[8]; | |
310 | ||
311 | if (level) { | |
312 | nlevel = log_level_match(level); | |
313 | ||
314 | if (nlevel == ZLOG_DISABLED) { | |
315 | fprintf(stderr, "invalid log level \"%s\"\n", level); | |
316 | exit(1); | |
317 | } | |
318 | } else | |
319 | nlevel = log_default_lvl; | |
320 | ||
321 | if (!dest) | |
322 | return; | |
323 | ||
324 | sep = strchr(dest, ':'); | |
325 | len = sep ? (int)(sep - dest) : (int)strlen(dest); | |
326 | ||
327 | snprintfrr(type, sizeof(type), "%.*s", len, dest); | |
328 | ||
329 | if (strcmp(type, "stdout") == 0) { | |
330 | log_cmdline_stdout_lvl = nlevel; | |
331 | zt_stdout.prio_min = ZLOG_MAXLVL(log_config_stdout_lvl, | |
332 | log_cmdline_stdout_lvl); | |
333 | zlog_file_set_other(&zt_stdout); | |
334 | return; | |
335 | } | |
336 | if (strcmp(type, "syslog") == 0) { | |
337 | log_cmdline_syslog_lvl = nlevel; | |
338 | zlog_syslog_set_prio_min(ZLOG_MAXLVL(log_config_syslog_lvl, | |
339 | log_cmdline_syslog_lvl)); | |
340 | return; | |
341 | } | |
342 | if (strcmp(type, "file") == 0 && sep) { | |
343 | sep++; | |
344 | set_log_file(&zt_file_cmdline, NULL, sep, nlevel); | |
345 | return; | |
346 | } | |
347 | ||
348 | fprintf(stderr, "invalid log target \"%s\" (\"%s\")\n", type, dest); | |
349 | exit(1); | |
350 | } | |
351 | ||
352 | DEFUN (clear_log_cmdline, | |
353 | clear_log_cmdline_cmd, | |
354 | "clear log cmdline-targets", | |
355 | CLEAR_STR | |
356 | "Logging control\n" | |
357 | "Disable log targets specified at startup by --log option\n") | |
f73126c3 | 358 | { |
0bdeb5e5 DL |
359 | zt_file_cmdline.prio_min = ZLOG_DISABLED; |
360 | zlog_file_set_other(&zt_file_cmdline); | |
361 | ||
362 | log_cmdline_syslog_lvl = ZLOG_DISABLED; | |
363 | zlog_syslog_set_prio_min(ZLOG_MAXLVL(log_config_syslog_lvl, | |
364 | log_cmdline_syslog_lvl)); | |
365 | ||
366 | log_cmdline_stdout_lvl = ZLOG_DISABLED; | |
367 | zt_stdout.prio_min = ZLOG_MAXLVL(log_config_stdout_lvl, | |
368 | log_cmdline_stdout_lvl); | |
369 | zlog_file_set_other(&zt_stdout); | |
f73126c3 | 370 | |
0bdeb5e5 DL |
371 | return CMD_SUCCESS; |
372 | } | |
f73126c3 | 373 | |
0bdeb5e5 DL |
374 | DEFPY (config_log_file, |
375 | config_log_file_cmd, | |
376 | "log file FILENAME [<emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>$levelarg]", | |
377 | "Logging control\n" | |
378 | "Logging to file\n" | |
379 | "Logging filename\n" | |
380 | LOG_LEVEL_DESC) | |
381 | { | |
382 | int level = log_default_lvl; | |
383 | ||
384 | if (levelarg) { | |
385 | level = log_level_match(levelarg); | |
386 | if (level == ZLOG_DISABLED) | |
387 | return CMD_ERR_NO_MATCH; | |
f73126c3 | 388 | } |
0bdeb5e5 DL |
389 | return set_log_file(&zt_file, vty, filename, level); |
390 | } | |
391 | ||
392 | DEFUN (no_config_log_file, | |
393 | no_config_log_file_cmd, | |
394 | "no log file [FILENAME [LEVEL]]", | |
395 | NO_STR | |
396 | "Logging control\n" | |
397 | "Cancel logging to file\n" | |
398 | "Logging file name\n" | |
399 | "Logging level\n") | |
400 | { | |
401 | zt_file.prio_min = ZLOG_DISABLED; | |
402 | zlog_file_set_other(&zt_file); | |
403 | return CMD_SUCCESS; | |
404 | } | |
405 | ||
406 | DEFPY (config_log_syslog, | |
407 | config_log_syslog_cmd, | |
408 | "log syslog [<emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>$levelarg]", | |
409 | "Logging control\n" | |
410 | "Set syslog logging level\n" | |
411 | LOG_LEVEL_DESC) | |
412 | { | |
413 | int level; | |
414 | ||
415 | if (levelarg) { | |
416 | level = log_level_match(levelarg); | |
417 | ||
418 | if (level == ZLOG_DISABLED) | |
419 | return CMD_ERR_NO_MATCH; | |
420 | } else | |
421 | level = log_default_lvl; | |
422 | ||
423 | log_config_syslog_lvl = level; | |
424 | zlog_syslog_set_prio_min(ZLOG_MAXLVL(log_config_syslog_lvl, | |
425 | log_cmdline_syslog_lvl)); | |
426 | return CMD_SUCCESS; | |
427 | } | |
428 | ||
429 | DEFUN (no_config_log_syslog, | |
430 | no_config_log_syslog_cmd, | |
431 | "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>]", | |
432 | NO_STR | |
433 | "Logging control\n" | |
434 | "Cancel logging to syslog\n" | |
435 | LOG_FACILITY_DESC | |
436 | LOG_LEVEL_DESC) | |
437 | { | |
438 | log_config_syslog_lvl = ZLOG_DISABLED; | |
439 | zlog_syslog_set_prio_min(ZLOG_MAXLVL(log_config_syslog_lvl, | |
440 | log_cmdline_syslog_lvl)); | |
441 | return CMD_SUCCESS; | |
442 | } | |
443 | ||
444 | DEFPY (config_log_facility, | |
445 | config_log_facility_cmd, | |
446 | "log facility <kern|user|mail|daemon|auth|syslog|lpr|news|uucp|cron|local0|local1|local2|local3|local4|local5|local6|local7>$facilityarg", | |
447 | "Logging control\n" | |
448 | "Facility parameter for syslog messages\n" | |
449 | LOG_FACILITY_DESC) | |
450 | { | |
451 | int facility = facility_match(facilityarg); | |
452 | ||
453 | zlog_syslog_set_facility(facility); | |
454 | return CMD_SUCCESS; | |
455 | } | |
456 | ||
457 | DEFUN (no_config_log_facility, | |
458 | no_config_log_facility_cmd, | |
459 | "no log facility [<kern|user|mail|daemon|auth|syslog|lpr|news|uucp|cron|local0|local1|local2|local3|local4|local5|local6|local7>]", | |
460 | NO_STR | |
461 | "Logging control\n" | |
462 | "Reset syslog facility to default (daemon)\n" | |
463 | LOG_FACILITY_DESC) | |
464 | { | |
465 | zlog_syslog_set_facility(LOG_DAEMON); | |
466 | return CMD_SUCCESS; | |
467 | } | |
468 | ||
469 | DEFUN (config_log_record_priority, | |
470 | config_log_record_priority_cmd, | |
471 | "log record-priority", | |
472 | "Logging control\n" | |
473 | "Log the priority of the message within the message\n") | |
474 | { | |
475 | zt_file.record_priority = true; | |
476 | zlog_file_set_other(&zt_file); | |
477 | zt_stdout.record_priority = true; | |
478 | zlog_file_set_other(&zt_stdout); | |
1c408628 DL |
479 | zt_filterfile.parent.record_priority = true; |
480 | zlog_file_set_other(&zt_filterfile.parent); | |
0bdeb5e5 DL |
481 | return CMD_SUCCESS; |
482 | } | |
483 | ||
484 | DEFUN (no_config_log_record_priority, | |
485 | no_config_log_record_priority_cmd, | |
486 | "no log record-priority", | |
487 | NO_STR | |
488 | "Logging control\n" | |
489 | "Do not log the priority of the message within the message\n") | |
490 | { | |
491 | zt_file.record_priority = false; | |
492 | zlog_file_set_other(&zt_file); | |
493 | zt_stdout.record_priority = false; | |
494 | zlog_file_set_other(&zt_stdout); | |
1c408628 DL |
495 | zt_filterfile.parent.record_priority = false; |
496 | zlog_file_set_other(&zt_filterfile.parent); | |
0bdeb5e5 DL |
497 | return CMD_SUCCESS; |
498 | } | |
f73126c3 | 499 | |
0bdeb5e5 DL |
500 | DEFPY (config_log_timestamp_precision, |
501 | config_log_timestamp_precision_cmd, | |
502 | "log timestamp precision (0-6)", | |
503 | "Logging control\n" | |
504 | "Timestamp configuration\n" | |
505 | "Set the timestamp precision\n" | |
506 | "Number of subsecond digits\n") | |
507 | { | |
508 | zt_file.ts_subsec = precision; | |
509 | zlog_file_set_other(&zt_file); | |
510 | zt_stdout.ts_subsec = precision; | |
511 | zlog_file_set_other(&zt_stdout); | |
1c408628 DL |
512 | zt_filterfile.parent.ts_subsec = precision; |
513 | zlog_file_set_other(&zt_filterfile.parent); | |
0bdeb5e5 DL |
514 | return CMD_SUCCESS; |
515 | } | |
f73126c3 | 516 | |
0bdeb5e5 DL |
517 | DEFUN (no_config_log_timestamp_precision, |
518 | no_config_log_timestamp_precision_cmd, | |
519 | "no log timestamp precision [(0-6)]", | |
520 | NO_STR | |
521 | "Logging control\n" | |
522 | "Timestamp configuration\n" | |
523 | "Reset the timestamp precision to the default value of 0\n" | |
524 | "Number of subsecond digits\n") | |
525 | { | |
526 | zt_file.ts_subsec = 0; | |
527 | zlog_file_set_other(&zt_file); | |
528 | zt_stdout.ts_subsec = 0; | |
529 | zlog_file_set_other(&zt_stdout); | |
1c408628 DL |
530 | zt_filterfile.parent.ts_subsec = 0; |
531 | zlog_file_set_other(&zt_filterfile.parent); | |
532 | return CMD_SUCCESS; | |
533 | } | |
534 | ||
535 | DEFPY (config_log_filterfile, | |
536 | config_log_filterfile_cmd, | |
537 | "log filtered-file FILENAME [<emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>$levelarg]", | |
538 | "Logging control\n" | |
539 | "Logging to file with string filter\n" | |
540 | "Logging filename\n" | |
541 | LOG_LEVEL_DESC) | |
542 | { | |
543 | int level = log_default_lvl; | |
544 | ||
545 | if (levelarg) { | |
546 | level = log_level_match(levelarg); | |
547 | if (level == ZLOG_DISABLED) | |
548 | return CMD_ERR_NO_MATCH; | |
549 | } | |
550 | return set_log_file(&zt_filterfile.parent, vty, filename, level); | |
551 | } | |
552 | ||
553 | DEFUN (no_config_log_filterfile, | |
554 | no_config_log_filterfile_cmd, | |
555 | "no log filtered-file [FILENAME [LEVEL]]", | |
556 | NO_STR | |
557 | "Logging control\n" | |
558 | "Cancel logging to file with string filter\n" | |
559 | "Logging file name\n" | |
560 | "Logging level\n") | |
561 | { | |
562 | zt_filterfile.parent.prio_min = ZLOG_DISABLED; | |
563 | zlog_file_set_other(&zt_filterfile.parent); | |
564 | return CMD_SUCCESS; | |
565 | } | |
566 | ||
567 | DEFPY (log_filter, | |
568 | log_filter_cmd, | |
569 | "[no] log-filter WORD$filter", | |
570 | NO_STR | |
571 | FILTER_LOG_STR | |
572 | "String to filter by\n") | |
573 | { | |
574 | int ret = 0; | |
575 | ||
576 | if (no) | |
577 | ret = zlog_filter_del(filter); | |
578 | else | |
579 | ret = zlog_filter_add(filter); | |
580 | ||
581 | if (ret == 1) { | |
582 | vty_out(vty, "%% filter table full\n"); | |
583 | return CMD_WARNING; | |
584 | } else if (ret != 0) { | |
585 | vty_out(vty, "%% failed to %s log filter\n", | |
586 | (no ? "remove" : "apply")); | |
587 | return CMD_WARNING; | |
588 | } | |
589 | ||
590 | vty_out(vty, " %s\n", filter); | |
591 | return CMD_SUCCESS; | |
592 | } | |
593 | ||
594 | /* Clear all log filters */ | |
595 | DEFPY (log_filter_clear, | |
596 | log_filter_clear_cmd, | |
597 | "clear log-filter", | |
598 | CLEAR_STR | |
599 | FILTER_LOG_STR) | |
600 | { | |
601 | zlog_filter_clear(); | |
602 | return CMD_SUCCESS; | |
603 | } | |
604 | ||
605 | /* Show log filter */ | |
606 | DEFPY (show_log_filter, | |
607 | show_log_filter_cmd, | |
608 | "show log-filter", | |
609 | SHOW_STR | |
610 | FILTER_LOG_STR) | |
611 | { | |
612 | char log_filters[ZLOG_FILTERS_MAX * (ZLOG_FILTER_LENGTH_MAX + 3)] = ""; | |
613 | int len = 0; | |
614 | ||
615 | len = zlog_filter_dump(log_filters, sizeof(log_filters)); | |
616 | ||
617 | if (len == -1) { | |
618 | vty_out(vty, "%% failed to get filters\n"); | |
619 | return CMD_WARNING; | |
620 | } | |
621 | ||
622 | if (len != 0) | |
623 | vty_out(vty, "%s", log_filters); | |
624 | ||
f73126c3 SW |
625 | return CMD_SUCCESS; |
626 | } | |
627 | ||
0bdeb5e5 | 628 | void log_config_write(struct vty *vty) |
f73126c3 | 629 | { |
0bdeb5e5 DL |
630 | bool show_cmdline_hint = false; |
631 | ||
632 | if (zt_file.prio_min != ZLOG_DISABLED && zt_file.filename) { | |
633 | vty_out(vty, "log file %s", zt_file.filename); | |
634 | ||
635 | if (zt_file.prio_min != log_default_lvl) | |
636 | vty_out(vty, " %s", zlog_priority[zt_file.prio_min]); | |
637 | vty_out(vty, "\n"); | |
638 | } | |
639 | ||
1c408628 DL |
640 | if (zt_filterfile.parent.prio_min != ZLOG_DISABLED |
641 | && zt_filterfile.parent.filename) { | |
642 | vty_out(vty, "log filtered-file %s", | |
643 | zt_filterfile.parent.filename); | |
644 | ||
645 | if (zt_filterfile.parent.prio_min != log_default_lvl) | |
646 | vty_out(vty, " %s", | |
647 | zlog_priority[zt_filterfile.parent.prio_min]); | |
648 | vty_out(vty, "\n"); | |
649 | } | |
650 | ||
0bdeb5e5 DL |
651 | if (log_config_stdout_lvl != ZLOG_DISABLED) { |
652 | vty_out(vty, "log stdout"); | |
653 | ||
654 | if (log_config_stdout_lvl != log_default_lvl) | |
655 | vty_out(vty, " %s", | |
656 | zlog_priority[log_config_stdout_lvl]); | |
657 | vty_out(vty, "\n"); | |
658 | } | |
659 | ||
660 | if (log_config_syslog_lvl != ZLOG_DISABLED) { | |
661 | vty_out(vty, "log syslog"); | |
662 | ||
663 | if (log_config_syslog_lvl != log_default_lvl) | |
664 | vty_out(vty, " %s", | |
665 | zlog_priority[log_config_syslog_lvl]); | |
666 | vty_out(vty, "\n"); | |
667 | } | |
668 | ||
669 | if (log_cmdline_syslog_lvl != ZLOG_DISABLED) { | |
670 | vty_out(vty, | |
671 | "! \"log syslog %s\" enabled by \"--log\" startup option\n", | |
672 | zlog_priority[log_cmdline_syslog_lvl]); | |
673 | show_cmdline_hint = true; | |
674 | } | |
675 | if (log_cmdline_stdout_lvl != ZLOG_DISABLED) { | |
676 | vty_out(vty, | |
677 | "! \"log stdout %s\" enabled by \"--log\" startup option\n", | |
678 | zlog_priority[log_cmdline_stdout_lvl]); | |
679 | show_cmdline_hint = true; | |
680 | } | |
681 | if (zt_file_cmdline.prio_min != ZLOG_DISABLED) { | |
682 | vty_out(vty, | |
683 | "! \"log file %s %s\" enabled by \"--log\" startup option\n", | |
684 | zt_file_cmdline.filename, | |
685 | zlog_priority[zt_file_cmdline.prio_min]); | |
686 | show_cmdline_hint = true; | |
687 | } | |
688 | if (show_cmdline_hint) | |
689 | vty_out(vty, | |
690 | "! use \"clear log cmdline-targets\" to remove this target\n"); | |
691 | ||
692 | if (zlog_syslog_get_facility() != LOG_DAEMON) | |
693 | vty_out(vty, "log facility %s\n", | |
694 | facility_name(zlog_syslog_get_facility())); | |
695 | ||
696 | if (zt_file.record_priority == 1) | |
697 | vty_out(vty, "log record-priority\n"); | |
698 | ||
699 | if (zt_file.ts_subsec > 0) | |
700 | vty_out(vty, "log timestamp precision %d\n", | |
701 | zt_file.ts_subsec); | |
702 | } | |
703 | ||
704 | static int log_vty_init(const char *progname, const char *protoname, | |
705 | unsigned short instance, uid_t uid, gid_t gid) | |
706 | { | |
707 | zlog_progname = progname; | |
708 | zlog_protoname = protoname; | |
709 | ||
1c408628 DL |
710 | zlog_filterfile_init(&zt_filterfile); |
711 | ||
0bdeb5e5 DL |
712 | zlog_file_set_fd(&zt_stdout, STDOUT_FILENO); |
713 | return 0; | |
714 | } | |
715 | ||
716 | __attribute__((_CONSTRUCTOR(475))) static void log_vty_preinit(void) | |
717 | { | |
718 | hook_register(zlog_init, log_vty_init); | |
719 | } | |
720 | ||
721 | void log_cmd_init(void) | |
722 | { | |
723 | install_element(VIEW_NODE, &show_logging_cmd); | |
724 | install_element(ENABLE_NODE, &clear_log_cmdline_cmd); | |
725 | ||
726 | install_element(CONFIG_NODE, &config_log_stdout_cmd); | |
727 | install_element(CONFIG_NODE, &no_config_log_stdout_cmd); | |
728 | install_element(CONFIG_NODE, &config_log_monitor_cmd); | |
729 | install_element(CONFIG_NODE, &no_config_log_monitor_cmd); | |
730 | install_element(CONFIG_NODE, &config_log_file_cmd); | |
731 | install_element(CONFIG_NODE, &no_config_log_file_cmd); | |
732 | install_element(CONFIG_NODE, &config_log_syslog_cmd); | |
733 | install_element(CONFIG_NODE, &no_config_log_syslog_cmd); | |
734 | install_element(CONFIG_NODE, &config_log_facility_cmd); | |
735 | install_element(CONFIG_NODE, &no_config_log_facility_cmd); | |
736 | install_element(CONFIG_NODE, &config_log_record_priority_cmd); | |
737 | install_element(CONFIG_NODE, &no_config_log_record_priority_cmd); | |
738 | install_element(CONFIG_NODE, &config_log_timestamp_precision_cmd); | |
739 | install_element(CONFIG_NODE, &no_config_log_timestamp_precision_cmd); | |
1c408628 DL |
740 | |
741 | install_element(VIEW_NODE, &show_log_filter_cmd); | |
742 | install_element(CONFIG_NODE, &log_filter_cmd); | |
743 | install_element(CONFIG_NODE, &log_filter_clear_cmd); | |
744 | install_element(CONFIG_NODE, &config_log_filterfile_cmd); | |
745 | install_element(CONFIG_NODE, &no_config_log_filterfile_cmd); | |
f73126c3 | 746 | } |