From 00d951e6d6754b01257a827fbc8d6f22b3d1e0ac Mon Sep 17 00:00:00 2001 From: "Roger A. Light" Date: Mon, 3 Nov 2014 08:45:56 +0000 Subject: [PATCH] Allow plugins to call mosquitto_log_printf() (Linux only at the moment). --- config.mk | 2 +- src/CMakeLists.txt | 10 ++++++++++ src/logging.c | 26 ++++++++++++++++++++++---- src/mosquitto_plugin.h | 42 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 75 insertions(+), 5 deletions(-) diff --git a/config.mk b/config.mk index 8d8965f5..8fc8ff65 100644 --- a/config.mk +++ b/config.mk @@ -120,7 +120,7 @@ LIB_LIBS:= PASSWD_LIBS:= ifeq ($(UNAME),Linux) - BROKER_LIBS:=$(BROKER_LIBS) -lrt + BROKER_LIBS:=$(BROKER_LIBS) -lrt -Wl,--dynamic-list=linker.syms LIB_LIBS:=$(LIB_LIBS) -lrt endif diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index fffcc1f9..7be2dd65 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -2,6 +2,16 @@ include_directories(${mosquitto_SOURCE_DIR} ${mosquitto_SOURCE_DIR}/src ${mosquitto_SOURCE_DIR}/lib ${OPENSSL_INCLUDE_DIR} ${STDBOOL_H_PATH} ${STDINT_H_PATH}) +if (UNIX) + set (CMAKE_EXE_LINKER_FLAGS "-Wl,--dynamic-list=${mosquitto_SOURCE_DIR}/src/linker.syms") + set (CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "") +endif (UNIX) + +# FIXME - something else needs fixing before this will work. +#if (APPLE) +# set (CMAKE_EXE_LINKER_FLAGS "-Wl,-exported_symbols_list -Wl,${mosquitto_SOURCE_DIR}/src/linker-macosx.syms") +#endif (APPLE) + set (MOSQ_SRCS conf.c context.c diff --git a/src/logging.c b/src/logging.c index 26de0993..7c6fd03d 100644 --- a/src/logging.c +++ b/src/logging.c @@ -81,9 +81,8 @@ int mqtt3_log_close(void) return MOSQ_ERR_SUCCESS; } -int _mosquitto_log_printf(struct mosquitto *mosq, int priority, const char *fmt, ...) +int _mosquitto_log_vprintf(struct mosquitto *mosq, int priority, const char *fmt, va_list va) { - va_list va; char *s; char *st; int len; @@ -164,9 +163,7 @@ int _mosquitto_log_printf(struct mosquitto *mosq, int priority, const char *fmt, s = _mosquitto_malloc(len*sizeof(char)); if(!s) return MOSQ_ERR_NOMEM; - va_start(va, fmt); vsnprintf(s, len, fmt, va); - va_end(va); s[len-1] = '\0'; /* Ensure string is null terminated. */ if(log_destinations & MQTT3_LOG_STDOUT){ @@ -221,3 +218,24 @@ int _mosquitto_log_printf(struct mosquitto *mosq, int priority, const char *fmt, return MOSQ_ERR_SUCCESS; } +int _mosquitto_log_printf(struct mosquitto *mosq, int priority, const char *fmt, ...) +{ + va_list va; + int rc; + + va_start(va, fmt); + rc = _mosquitto_log_vprintf(mosq, priority, fmt, va); + va_end(va); + + return rc; +} + +void mosquitto_log_printf(int level, const char *fmt, ...) +{ + va_list va; + + va_start(va, fmt); + _mosquitto_log_vprintf(NULL, level, fmt, va); + va_end(va); +} + diff --git a/src/mosquitto_plugin.h b/src/mosquitto_plugin.h index 59bc4f42..e6ec5c0b 100644 --- a/src/mosquitto_plugin.h +++ b/src/mosquitto_plugin.h @@ -36,6 +36,48 @@ struct mosquitto_auth_opt { * gcc -I -fPIC -shared plugin.c -o plugin.so */ +/* ========================================================================= + * + * Utility Functions + * + * Use these functions from within your plugin. + * + * There are also very useful functions in libmosquitto. + * + * ========================================================================= */ + +/* + * Function: mosquitto_log_printf + * + * Write a log message using the broker configured logging. + * + * Parameters: + * level - Log message priority. Can currently be one of: + * + * MOSQ_LOG_INFO + * MOSQ_LOG_NOTICE + * MOSQ_LOG_WARNING + * MOSQ_LOG_ERR + * MOSQ_LOG_DEBUG + * MOSQ_LOG_SUBSCRIBE (not recommended for use by plugins) + * MOSQ_LOG_UNSUBSCRIBE (not recommended for use by plugins) + * + * These values are defined in mosquitto.h. + * + * fmt, ... - printf style format and arguments. + */ +void mosquitto_log_printf(int level, const char *fmt, ...); + + + +/* ========================================================================= + * + * Plugin Functions + * + * You must implement these functions in your plugin. + * + * ========================================================================= */ + /* * Function: mosquitto_auth_plugin_version *