]> git.proxmox.com Git - mirror_frr.git/commitdiff
lib: Add log filter manipulation code
authorStephen Worley <sworley@cumulusnetworks.com>
Thu, 13 Jun 2019 21:15:32 +0000 (17:15 -0400)
committerStephen Worley <sworley@cumulusnetworks.com>
Wed, 19 Jun 2019 21:20:24 +0000 (17:20 -0400)
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>
lib/log.c
lib/log.h

index 5ce3bd7020b4738cd73b2d56786466b9ed8ae787..80ca66ca8d977da49adced8d765647346fe7b001 100644 (file)
--- 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
  *
index c5ae6fe32f295cb52cccd7f3db1f062bb60cfff1..501da88a54c36bae272419602396dccc21f8ec5f 100644 (file)
--- 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. */