Allow to filter logs

This commit is contained in:
Kamil Trzcinski 2022-04-07 18:30:36 +02:00
parent c817f73051
commit 1aec989bbc
5 changed files with 63 additions and 10 deletions

View File

@ -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),
{}
};

View File

@ -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");
}
}

42
opts/log.c Normal file
View File

@ -0,0 +1,42 @@
#include "opts/log.h"
#define _GNU_SOURCE
#include <string.h>
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;
}

View File

@ -1,12 +1,14 @@
#pragma once
#include <stdio.h>
#include <stdbool.h>
#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)

View File

@ -3,6 +3,7 @@
#include <stddef.h>
#include <stdio.h>
#include <limits.h>
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");