New vlog function allowing to register a callback that will receive all log messages.
Signed-off-by: Thomas Graf <tg...@redhat.com> --- lib/vlog.c | 40 +++++++++++++++++++++++++++++++++++++++- lib/vlog.h | 8 +++++++- 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/lib/vlog.c b/lib/vlog.c index 0bd9bd1..c57991a 100644 --- a/lib/vlog.c +++ b/lib/vlog.c @@ -569,6 +569,37 @@ vlog_is_enabled(const struct vlog_module *module, enum vlog_level level) return module->min_level >= level; } +static struct list cb_log_funcs = LIST_INITIALIZER(&cb_log_funcs); + +struct vlog_cb +{ + struct list list_node; + vlog_cb_func_t func; +}; + +void +vlog_register_cb(vlog_cb_func_t func) +{ + struct vlog_cb *cb; + + if (!func) + return; + + cb = xcalloc(1, sizeof(*cb)); + cb->func = func; + list_push_front(&cb_log_funcs, &cb->list_node); +} + +static void +vlog_cb_broadcast(enum vlog_level level, unsigned int msg_num, const char *text) +{ + struct vlog_cb *cb; + + LIST_FOR_EACH (cb, list_node, &cb_log_funcs) { + cb->func(level, msg_num, text); + } +} + static const char * fetch_braces(const char *p, const char *def, char *out, size_t out_size) { @@ -698,7 +729,8 @@ vlog_valist(const struct vlog_module *module, enum vlog_level level, bool log_to_console = module->levels[VLF_CONSOLE] >= level; bool log_to_syslog = module->levels[VLF_SYSLOG] >= level; bool log_to_file = module->levels[VLF_FILE] >= level && log_fd >= 0; - if (log_to_console || log_to_syslog || log_to_file) { + bool log_to_cb = !list_is_empty(&cb_log_funcs); + if (log_to_console || log_to_syslog || log_to_file || log_to_cb) { int save_errno = errno; static unsigned int msg_num; struct ds s; @@ -736,6 +768,12 @@ vlog_valist(const struct vlog_module *module, enum vlog_level level, vlog_write_file(&s); } + if (log_to_cb) { + format_log_message(module, level, VLF_CB, msg_num, + message, args, &s); + vlog_cb_broadcast(level, msg_num, s.string); + } + ds_destroy(&s); errno = save_errno; } diff --git a/lib/vlog.h b/lib/vlog.h index 9570b0e..8d4eac2 100644 --- a/lib/vlog.h +++ b/lib/vlog.h @@ -54,7 +54,8 @@ enum vlog_level vlog_get_level_val(const char *name); #define VLOG_FACILITIES \ VLOG_FACILITY(SYSLOG, "%05N|%c%T|%p|%m") \ VLOG_FACILITY(CONSOLE, "%D{%Y-%m-%dT%H:%M:%SZ}|%05N|%c%T|%p|%m") \ - VLOG_FACILITY(FILE, "%D{%Y-%m-%dT%H:%M:%SZ}|%05N|%c%T|%p|%m") + VLOG_FACILITY(FILE, "%D{%Y-%m-%dT%H:%M:%SZ}|%05N|%c%T|%p|%m") \ + VLOG_FACILITY(CB, "%D{%Y-%m-%dT%H:%M:%SZ} [%c%T] %p: %m") enum vlog_facility { #define VLOG_FACILITY(NAME, PATTERN) VLF_##NAME, VLOG_FACILITIES @@ -126,6 +127,11 @@ const char *vlog_get_log_file(void); int vlog_set_log_file(const char *file_name); int vlog_reopen_log_file(void); +/* Callback log registration */ +typedef void (*vlog_cb_func_t)(enum vlog_level lvl, unsigned int msg_num, + const char *text); +void vlog_register_cb(vlog_cb_func_t func); + /* Initialization. */ void vlog_init(void); void vlog_exit(void); -- 1.7.11.7 _______________________________________________ dev mailing list dev@openvswitch.org http://openvswitch.org/mailman/listinfo/dev