From 1aec989bbceccff555f9fb60c0897f325d4feaf7 Mon Sep 17 00:00:00 2001 From: Kamil Trzcinski Date: Thu, 7 Apr 2022 18:30:36 +0200 Subject: [PATCH] Allow to filter logs --- cmd/main.c | 1 + hw/links.c | 4 ++++ opts/log.c | 42 ++++++++++++++++++++++++++++++++++++++++++ opts/log.h | 8 ++++++-- opts/opts.c | 18 ++++++++++-------- 5 files changed, 63 insertions(+), 10 deletions(-) create mode 100644 opts/log.c diff --git a/cmd/main.c b/cmd/main.c index 771ce60..f55b827 100644 --- a/cmd/main.c +++ b/cmd/main.c @@ -69,6 +69,7 @@ option_t all_options[] = { DEFINE_OPTION(http, maxcons, uint), DEFINE_OPTION_DEFAULT(log, debug, bool, "1"), DEFINE_OPTION_DEFAULT(log, verbose, bool, "1"), + DEFINE_OPTION_PTR(log, filter, string), {} }; diff --git a/hw/links.c b/hw/links.c index 9c137cd..4ed094c 100644 --- a/hw/links.c +++ b/hw/links.c @@ -177,6 +177,10 @@ int links_step(link_t *all_links, int *timeout_ms) if (!buf_list->device->paused && buf_list->do_capture && buf_list->do_mmap) { buffer_t *buf; while (buf = buffer_list_find_slot(buf_list)) { + int count_enqueued = buffer_list_count_enqueued(buf_list); + if (count_enqueued > 1) + break; + buffer_consumed(buf, "enqueued"); } } diff --git a/opts/log.c b/opts/log.c new file mode 100644 index 0000000..122962d --- /dev/null +++ b/opts/log.c @@ -0,0 +1,42 @@ +#include "opts/log.h" + +#define _GNU_SOURCE +#include + +char * +strstrn(const char *s, const char *find, size_t len) +{ + char c, sc; + if ((c = *find++) != 0) { + len--; + + do { + do { + if ((sc = *s++) == 0) + return (NULL); + } while (sc != c); + } while (strncmp(s, find, len) != 0); + s--; + } + return ((char *)s); +} + +bool filter_log(const char *filename) +{ + if (!log_options.filter[0]) + return true; + + const char *ptr = log_options.filter; + do { + const char *next = strchr(ptr, ','); + if (!next) + next = ptr + strlen(ptr); + + if(strstrn(filename, ptr, next - ptr)) + return true; + + ptr = next; + } while(*ptr++); + + return false; +} diff --git a/opts/log.h b/opts/log.h index c9b701f..909c6f3 100644 --- a/opts/log.h +++ b/opts/log.h @@ -1,12 +1,14 @@ #pragma once #include +#include #define __FILENAME__ __FILE__ typedef struct log_options_s { bool debug; bool verbose; + char filter[256]; } log_options_t; extern log_options_t log_options; @@ -14,10 +16,12 @@ extern log_options_t log_options; #define MIN(a, b) ((a) < (b) ? (a) : (b)) #define MAX(a, b) ((a) > (b) ? (a) : (b)) +bool filter_log(const char *filename); + // assumes that name is first item #define dev_name(dev) (dev ? *(const char**)dev : "?") #define E_LOG_ERROR(dev, _msg, ...) do { fprintf(stderr, "%s: %s: " _msg "\n", __FILENAME__, dev_name(dev), ##__VA_ARGS__); goto error; } while(0) #define E_LOG_PERROR(dev, _msg, ...) do { fprintf(stderr, "%s: %s: " _msg "\n", __FILENAME__, dev_name(dev), ##__VA_ARGS__); exit(-1); } while(0) #define E_LOG_INFO(dev, _msg, ...) do { fprintf(stderr, "%s: %s: " _msg "\n", __FILENAME__, dev_name(dev), ##__VA_ARGS__); } while(0) -#define E_LOG_VERBOSE(dev, _msg, ...) do { if (log_options.debug || log_options.verbose) { fprintf(stderr, "%s: %s: " _msg "\n", __FILENAME__, dev_name(dev), ##__VA_ARGS__); } } while(0) -#define E_LOG_DEBUG(dev, _msg, ...) do { if (log_options.debug) { fprintf(stderr, "%s: %s: " _msg "\n", __FILENAME__, dev_name(dev), ##__VA_ARGS__); } } while(0) +#define E_LOG_VERBOSE(dev, _msg, ...) do { if ((log_options.debug || log_options.verbose) && filter_log(__FILENAME__)) { fprintf(stderr, "%s: %s: " _msg "\n", __FILENAME__, dev_name(dev), ##__VA_ARGS__); } } while(0) +#define E_LOG_DEBUG(dev, _msg, ...) do { if (log_options.debug && filter_log(__FILENAME__)) { fprintf(stderr, "%s: %s: " _msg "\n", __FILENAME__, dev_name(dev), ##__VA_ARGS__); } } while(0) diff --git a/opts/opts.c b/opts/opts.c index 4711c26..6cb806c 100644 --- a/opts/opts.c +++ b/opts/opts.c @@ -3,6 +3,7 @@ #include #include +#include static void print_help(option_t *options) { @@ -12,17 +13,18 @@ static void print_help(option_t *options) if (option->value_string) { printf(option->format, option->value_string); - } else if (option->value_mapping) { - for (int j = 0; option->value_mapping[j].name; j++) { - if (option->value_mapping[j].value == *option->value) { - printf("%s - ", option->value_mapping[j].name); - break; + } else { + if (option->value_mapping) { + for (int j = 0; option->value_mapping[j].name; j++) { + if (option->value_mapping[j].value == *option->value) { + printf("%s - ", option->value_mapping[j].name); + break; + } } } - printf(option->format, *option->value); - } else { - printf(option->format, *option->value); + unsigned mask = UINT_MAX >> ((sizeof(*option->value) - option->size) * 8); + printf(option->format, *option->value & mask); } printf("\n");