Signed-off-by: Angus Salkeld <asalkeld(a)redhat.com>
---
examples/simplelog.c | 25 ++++++++++++++++++-------
include/qb/qblog.h | 8 ++++++++
lib/log.c | 26 ++++++++++++++++++++++++++
3 files changed, 52 insertions(+), 7 deletions(-)
diff --git a/examples/simplelog.c b/examples/simplelog.c
index 593dd5c..c0607c2 100644
--- a/examples/simplelog.c
+++ b/examples/simplelog.c
@@ -29,6 +29,8 @@
#define MY_TAG_TWO (1 << 1)
#define MY_TAG_THREE (1 << 2)
+static int32_t _log_priority = LOG_WARNING;
+
static void func_one(void)
{
FILE* fd;
@@ -115,12 +117,23 @@ trace_logger(int32_t t,
fprintf(stderr, "%s\n", output_buffer);
}
+static void
+m_filter(struct qb_log_callsite *cs)
+{
+ if ((cs->priority >= LOG_ALERT &&
+ cs->priority <= _log_priority) &&
+ strcmp(cs->filename, __FILE__) == 0) {
+ qb_bit_set(cs->targets, QB_LOG_STDERR);
+ } else {
+ qb_bit_clear(cs->targets, QB_LOG_STDERR);
+ }
+}
+
int32_t main(int32_t argc, char *argv[])
{
const char *options = "vhtebdf:";
int32_t opt;
int32_t tracer;
- int32_t priority = LOG_WARNING;
int32_t do_stderr = QB_FALSE;
int32_t do_dump_blackbox = QB_FALSE;
char *logfile = NULL;
@@ -144,7 +157,7 @@ int32_t main(int32_t argc, char *argv[])
logfile = optarg;
break;
case 'v':
- priority++;
+ _log_priority++;
break;
case 'h':
default:
@@ -168,10 +181,8 @@ int32_t main(int32_t argc, char *argv[])
tracer = qb_log_custom_open(trace_logger, NULL, NULL, NULL);
if (do_stderr) {
- qb_log_filter_ctl2(QB_LOG_STDERR, QB_LOG_FILTER_ADD,
- QB_LOG_FILTER_FILE, __FILE__,
- LOG_ALERT, QB_MIN(LOG_DEBUG, priority));
- qb_log_format_set(QB_LOG_STDERR, "%4g: %f:%l [%p] %b");
+ qb_log_filter_fn_set(m_filter);
+ qb_log_format_set(QB_LOG_STDERR, "[%p] %4g: %f:%l %b");
qb_log_ctl(QB_LOG_STDERR, QB_LOG_CONF_ENABLED, QB_TRUE);
qb_log_ctl(tracer, QB_LOG_CONF_ENABLED, QB_TRUE);
@@ -190,7 +201,7 @@ int32_t main(int32_t argc, char *argv[])
if (logfile) {
log_fd = qb_log_file_open(logfile);
qb_log_filter_ctl(log_fd, QB_LOG_FILTER_ADD,
- QB_LOG_FILTER_FILE, __FILE__, priority);
+ QB_LOG_FILTER_FILE, __FILE__, _log_priority);
qb_log_format_set(log_fd, "[%N] %t %n() [%p] %b");
qb_log_ctl(log_fd, QB_LOG_CONF_THREADED, do_threaded);
qb_log_ctl(log_fd, QB_LOG_CONF_ENABLED, QB_TRUE);
diff --git a/include/qb/qblog.h b/include/qb/qblog.h
index f1a6305..2d57966 100644
--- a/include/qb/qblog.h
+++ b/include/qb/qblog.h
@@ -237,6 +237,8 @@ struct qb_log_callsite {
uint32_t tags;
} __attribute__((aligned(8)));
+typedef void (*qb_log_filter_fn)(struct qb_log_callsite * cs);
+
/* will be assigned by ld linker magic */
#ifdef QB_HAVE_ATTRIBUTE_SECTION
extern struct qb_log_callsite __start___verbose[];
@@ -462,6 +464,12 @@ int32_t qb_log_filter_ctl2(int32_t value, enum qb_log_filter_conf c,
enum qb_log_filter_type type, const char * text,
uint8_t high_priority, uint8_t low_priority);
+
+/**
+ *
+ */
+int32_t qb_log_filter_fn_set(qb_log_filter_fn fn);
+
/**
* Set the callback to map the 'tags' bit map to a string.
*/
diff --git a/lib/log.c b/lib/log.c
index 867334f..9b9ea9d 100644
--- a/lib/log.c
+++ b/lib/log.c
@@ -43,6 +43,7 @@ static uint32_t conf_active_max = 0;
static int32_t in_logger = QB_FALSE;
static int32_t logger_inited = QB_FALSE;
static pthread_rwlock_t _listlock;
+static qb_log_filter_fn _custom_filter_fn = NULL;
static QB_LIST_DECLARE(tags_head);
static QB_LIST_DECLARE(callsite_sections);
@@ -281,6 +282,9 @@ qb_log_from_external_source_va(const char *function,
} else {
cs->tags = tags;
}
+ if (_custom_filter_fn) {
+ _custom_filter_fn(cs);
+ }
pthread_rwlock_unlock(&_listlock);
}
qb_log_real_va_(cs, ap);
@@ -588,6 +592,28 @@ qb_log_filter_ctl2(int32_t t, enum qb_log_filter_conf c,
}
int32_t
+qb_log_filter_fn_set(qb_log_filter_fn fn)
+{
+ struct callsite_section *sect;
+ struct qb_log_callsite *cs;
+
+ if (!logger_inited) {
+ return -EINVAL;
+ }
+ _custom_filter_fn = fn;
+
+ qb_list_for_each_entry(sect, &callsite_sections, list) {
+ for (cs = sect->start; cs < sect->stop; cs++) {
+ if (cs->lineno == 0) {
+ break;
+ }
+ _custom_filter_fn(cs);
+ }
+ }
+ return 0;
+}
+
+int32_t
qb_log_filter_ctl(int32_t t, enum qb_log_filter_conf c,
enum qb_log_filter_type type,
const char *text, uint8_t priority)
--
1.7.7.6