1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Logging - Filtered file log target
4 * Copyright (C) 2019 Cumulus Networks, Inc.
10 #include "frr_pthread.h"
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
;
18 * look for a match on the filter in the current filters,
19 * logfilterlock must be held
21 static int zlog_filter_lookup(const char *lookup
)
23 for (int i
= 0; i
< zlog_filter_count
; i
++) {
24 if (strncmp(lookup
, zlog_filters
[i
], sizeof(zlog_filters
[0]))
31 void zlog_filter_clear(void)
33 frr_with_mutex (&logfilterlock
) {
34 zlog_filter_count
= 0;
38 int zlog_filter_add(const char *filter
)
40 frr_with_mutex (&logfilterlock
) {
41 if (zlog_filter_count
>= ZLOG_FILTERS_MAX
)
44 if (zlog_filter_lookup(filter
) != -1)
45 /* Filter already present */
48 strlcpy(zlog_filters
[zlog_filter_count
], filter
,
49 sizeof(zlog_filters
[0]));
51 if (zlog_filters
[zlog_filter_count
][0] == '\0')
52 /* Filter was either empty or didn't get copied
62 int zlog_filter_del(const char *filter
)
64 frr_with_mutex (&logfilterlock
) {
65 int found_idx
= zlog_filter_lookup(filter
);
66 int last_idx
= zlog_filter_count
- 1;
69 /* Didn't find the filter to delete */
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]));
81 /* Dump all filters to buffer, delimited by new line */
82 int zlog_filter_dump(char *buf
, size_t max_size
)
86 frr_with_mutex (&logfilterlock
) {
87 for (int i
= 0; i
< zlog_filter_count
; i
++) {
90 ret
= snprintf(buf
+ len
, max_size
- len
, " %s\n",
93 if ((ret
< 0) || ((size_t)len
>= max_size
))
101 static int search_buf(const char *buf
, size_t len
)
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
]));
117 static void zlog_filterfile_fd(struct zlog_target
*zt
, struct zlog_msg
*msgs
[],
120 struct zlog_msg
*msgfilt
[nmsgs
];
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)
131 msgfilt
[o
++] = msgs
[i
];
135 zlog_fd(zt
, msgfilt
, o
);
138 void zlog_filterfile_init(struct zlog_cfg_filterfile
*zcf
)
140 zlog_file_init(&zcf
->parent
);
141 zcf
->parent
.zlog_wrap
= zlog_filterfile_fd
;
144 void zlog_filterfile_fini(struct zlog_cfg_filterfile
*zcf
)
146 zlog_file_fini(&zcf
->parent
);