]>
Commit | Line | Data |
---|---|---|
1 | // SPDX-License-Identifier: GPL-2.0-or-later | |
2 | /* | |
3 | * Logging - Filtered file log target | |
4 | * Copyright (C) 2019 Cumulus Networks, Inc. | |
5 | * Stephen Worley | |
6 | */ | |
7 | ||
8 | #include <zebra.h> | |
9 | ||
10 | #include "frr_pthread.h" | |
11 | #include "log.h" | |
12 | ||
13 | static pthread_mutex_t logfilterlock = PTHREAD_MUTEX_INITIALIZER; | |
14 | static char zlog_filters[ZLOG_FILTERS_MAX][ZLOG_FILTER_LENGTH_MAX + 1]; | |
15 | static uint8_t zlog_filter_count; | |
16 | ||
17 | /* | |
18 | * look for a match on the filter in the current filters, | |
19 | * logfilterlock must be held | |
20 | */ | |
21 | static int zlog_filter_lookup(const char *lookup) | |
22 | { | |
23 | for (int i = 0; i < zlog_filter_count; i++) { | |
24 | if (strncmp(lookup, zlog_filters[i], sizeof(zlog_filters[0])) | |
25 | == 0) | |
26 | return i; | |
27 | } | |
28 | return -1; | |
29 | } | |
30 | ||
31 | void zlog_filter_clear(void) | |
32 | { | |
33 | frr_with_mutex (&logfilterlock) { | |
34 | zlog_filter_count = 0; | |
35 | } | |
36 | } | |
37 | ||
38 | int zlog_filter_add(const char *filter) | |
39 | { | |
40 | frr_with_mutex (&logfilterlock) { | |
41 | if (zlog_filter_count >= ZLOG_FILTERS_MAX) | |
42 | return 1; | |
43 | ||
44 | if (zlog_filter_lookup(filter) != -1) | |
45 | /* Filter already present */ | |
46 | return -1; | |
47 | ||
48 | strlcpy(zlog_filters[zlog_filter_count], filter, | |
49 | sizeof(zlog_filters[0])); | |
50 | ||
51 | if (zlog_filters[zlog_filter_count][0] == '\0') | |
52 | /* Filter was either empty or didn't get copied | |
53 | * correctly | |
54 | */ | |
55 | return -1; | |
56 | ||
57 | zlog_filter_count++; | |
58 | } | |
59 | return 0; | |
60 | } | |
61 | ||
62 | int zlog_filter_del(const char *filter) | |
63 | { | |
64 | frr_with_mutex (&logfilterlock) { | |
65 | int found_idx = zlog_filter_lookup(filter); | |
66 | int last_idx = zlog_filter_count - 1; | |
67 | ||
68 | if (found_idx == -1) | |
69 | /* Didn't find the filter to delete */ | |
70 | return -1; | |
71 | ||
72 | /* Adjust the filter array */ | |
73 | memmove(zlog_filters[found_idx], zlog_filters[found_idx + 1], | |
74 | (last_idx - found_idx) * sizeof(zlog_filters[0])); | |
75 | ||
76 | zlog_filter_count--; | |
77 | } | |
78 | return 0; | |
79 | } | |
80 | ||
81 | /* Dump all filters to buffer, delimited by new line */ | |
82 | int zlog_filter_dump(char *buf, size_t max_size) | |
83 | { | |
84 | int len = 0; | |
85 | ||
86 | frr_with_mutex (&logfilterlock) { | |
87 | for (int i = 0; i < zlog_filter_count; i++) { | |
88 | int ret; | |
89 | ||
90 | ret = snprintf(buf + len, max_size - len, " %s\n", | |
91 | zlog_filters[i]); | |
92 | len += ret; | |
93 | if ((ret < 0) || ((size_t)len >= max_size)) | |
94 | return -1; | |
95 | } | |
96 | } | |
97 | ||
98 | return len; | |
99 | } | |
100 | ||
101 | static int search_buf(const char *buf, size_t len) | |
102 | { | |
103 | char *found = NULL; | |
104 | ||
105 | frr_with_mutex (&logfilterlock) { | |
106 | for (int i = 0; i < zlog_filter_count; i++) { | |
107 | found = memmem(buf, len, zlog_filters[i], | |
108 | strlen(zlog_filters[i])); | |
109 | if (found != NULL) | |
110 | return 0; | |
111 | } | |
112 | } | |
113 | ||
114 | return -1; | |
115 | } | |
116 | ||
117 | static void zlog_filterfile_fd(struct zlog_target *zt, struct zlog_msg *msgs[], | |
118 | size_t nmsgs) | |
119 | { | |
120 | struct zlog_msg *msgfilt[nmsgs]; | |
121 | size_t i, o = 0; | |
122 | const char *text; | |
123 | size_t text_len; | |
124 | ||
125 | for (i = 0; i < nmsgs; i++) { | |
126 | if (zlog_msg_prio(msgs[i]) >= LOG_DEBUG) { | |
127 | text = zlog_msg_text(msgs[i], &text_len); | |
128 | if (search_buf(text, text_len) < 0) | |
129 | continue; | |
130 | } | |
131 | msgfilt[o++] = msgs[i]; | |
132 | } | |
133 | ||
134 | if (o) | |
135 | zlog_fd(zt, msgfilt, o); | |
136 | } | |
137 | ||
138 | void zlog_filterfile_init(struct zlog_cfg_filterfile *zcf) | |
139 | { | |
140 | zlog_file_init(&zcf->parent); | |
141 | zcf->parent.zlog_wrap = zlog_filterfile_fd; | |
142 | } | |
143 | ||
144 | void zlog_filterfile_fini(struct zlog_cfg_filterfile *zcf) | |
145 | { | |
146 | zlog_file_fini(&zcf->parent); | |
147 | } |