summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStephen Worley <sworley@cumulusnetworks.com>2019-06-13 17:15:32 -0400
committerStephen Worley <sworley@cumulusnetworks.com>2019-06-19 17:20:24 -0400
commitc0dc23460ba43192703a3bf76eca08c71257d42c (patch)
tree921b9ff7ea7239b04e162d18ebae923ef2a9be64
parent2e77de95162842c85cd00b281896082eb566553e (diff)
lib: Add log filter manipulation code
Add code for manipulation/creation of log filters and their table. Specifically, add lookup,clear,add,del,dump functionality. Signed-off-by: Stephen Worley <sworley@cumulusnetworks.com>
-rw-r--r--lib/log.c97
-rw-r--r--lib/log.h9
2 files changed, 106 insertions, 0 deletions
diff --git a/lib/log.c b/lib/log.c
index 5ce3bd7020..80ca66ca8d 100644
--- a/lib/log.c
+++ b/lib/log.c
@@ -65,6 +65,103 @@ const char *zlog_priority[] = {
"notifications", "informational", "debugging", NULL,
};
+static char zlog_filters[ZLOG_FILTERS_MAX][ZLOG_FILTER_LENGTH_MAX + 1];
+static uint8_t zlog_filter_count;
+
+static int zlog_filter_lookup(const char *lookup)
+{
+ /* look for a match on the filter in the current filters */
+ for (int i = 0; i < zlog_filter_count; i++) {
+ if (strncmp(lookup, zlog_filters[i], sizeof(zlog_filters[0]))
+ == 0)
+ return i;
+ }
+ return -1;
+}
+
+void zlog_filter_clear(void)
+{
+ pthread_mutex_lock(&loglock);
+ zlog_filter_count = 0;
+ pthread_mutex_unlock(&loglock);
+}
+
+int zlog_filter_add(const char *filter)
+{
+ pthread_mutex_lock(&loglock);
+
+ if (zlog_filter_count >= ZLOG_FILTERS_MAX) {
+ pthread_mutex_unlock(&loglock);
+ return 1;
+ }
+
+ if (zlog_filter_lookup(filter) != -1) {
+ /* Filter already present */
+ pthread_mutex_unlock(&loglock);
+ return -1;
+ }
+
+ strlcpy(zlog_filters[zlog_filter_count], filter,
+ sizeof(zlog_filters[0]));
+
+ if (zlog_filters[zlog_filter_count] == NULL
+ || zlog_filters[zlog_filter_count][0] == '\0') {
+ pthread_mutex_unlock(&loglock);
+ return -1;
+ }
+
+ zlog_filter_count++;
+
+ pthread_mutex_unlock(&loglock);
+ return 0;
+}
+
+int zlog_filter_del(const char *filter)
+{
+ pthread_mutex_lock(&loglock);
+
+ int found_idx = zlog_filter_lookup(filter);
+
+ if (found_idx == -1) {
+ /* Didn't find the filter to delete */
+ pthread_mutex_unlock(&loglock);
+ return -1;
+ }
+
+ /* Remove and adjust the filter array */
+ for (int i = found_idx; i < zlog_filter_count - 1; i++)
+ strlcpy(zlog_filters[i], zlog_filters[i + 1],
+ sizeof(zlog_filters[0]));
+
+ zlog_filter_count--;
+
+ pthread_mutex_unlock(&loglock);
+ return 0;
+}
+
+/* Dump all filters to buffer, delimited by new line */
+int zlog_filter_dump(char *buf, size_t max_size)
+{
+ pthread_mutex_lock(&loglock);
+
+ int ret = 0;
+ int len = 0;
+
+ for (int i = 0; i < zlog_filter_count; i++) {
+ ret = snprintf(buf + len, max_size - len, "\t%s\n",
+ zlog_filters[i]);
+ len += ret;
+ if ((ret < 0) || ((size_t)len >= max_size)) {
+ pthread_mutex_unlock(&loglock);
+ return -1;
+ }
+ }
+
+ pthread_mutex_unlock(&loglock);
+
+ return len;
+}
+
/*
* write_wrapper
*
diff --git a/lib/log.h b/lib/log.h
index c5ae6fe32f..501da88a54 100644
--- a/lib/log.h
+++ b/lib/log.h
@@ -115,6 +115,15 @@ extern int zlog_reset_file(void);
/* Rotate log. */
extern int zlog_rotate(void);
+#define ZLOG_FILTERS_MAX 100 /* Max # of filters at once */
+#define ZLOG_FILTER_LENGTH_MAX 80 /* 80 character filter limit */
+
+/* Add/Del/Dump log filters */
+extern void zlog_filter_clear(void);
+extern int zlog_filter_add(const char *filter);
+extern int zlog_filter_del(const char *filter);
+extern int zlog_filter_dump(char *buf, size_t max_size);
+
const char *lookup_msg(const struct message *mz, int kz, const char *nf);
/* Safe version of strerror -- never returns NULL. */