From be4e0e2d3855ccbd3b683bfe6cc4c7f9d3254314 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jos=C3=A9=20Bollo?= Date: Tue, 24 May 2016 11:24:47 +0200 Subject: [PATCH] add log macros for plugins MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Change-Id: I3de30aeb90a41ed8ee63ec1e19c6032440d65574 Signed-off-by: José Bollo --- include/afb/afb-plugin.h | 23 ++++++++++++++++ src/afb-api-dbus.c | 1 + src/afb-api-so.c | 29 ++++++++++++++++----- src/main.c | 1 + src/verbose.c | 68 +++++++++++++++++++++++++++++++----------------- src/verbose.h | 42 ++++++------------------------ 6 files changed, 100 insertions(+), 64 deletions(-) diff --git a/include/afb/afb-plugin.h b/include/afb/afb-plugin.h index 41e53ce1..6f5cd08c 100644 --- a/include/afb/afb-plugin.h +++ b/include/afb/afb-plugin.h @@ -17,6 +17,8 @@ #pragma once +#include + /***************************************************************************** * This files is the main file to include for writing plugins dedicated to * @@ -142,6 +144,7 @@ struct afb_daemon_itf { struct sd_event *(*get_event_loop)(void *closure); /* get the common systemd's event loop */ struct sd_bus *(*get_user_bus)(void *closure); /* get the common systemd's user d-bus */ struct sd_bus *(*get_system_bus)(void *closure); /* get the common systemd's system d-bus */ + void (*vverbose)(void*closure, int level, const char *file, int line, const char *fmt, va_list args); }; /* @@ -204,4 +207,24 @@ static inline struct sd_bus *afb_daemon_get_system_bus(struct afb_daemon daemon) return daemon.itf->get_system_bus(daemon.closure); } +/* + * Send a message described by 'fmt' and following parameters + * to the journal for the verbosity 'level'. + * 'file' and 'line' are indicators of position of the code in source files. + * 'daemon' MUST be the daemon given in interface when activating the plugin. + */ +static inline void afb_daemon_verbose(struct afb_daemon daemon, int level, const char *file, int line, const char *fmt, ...) +{ + va_list args; + va_start(args, fmt); + return daemon.itf->vverbose(daemon.closure, level, file, line, fmt, args); + va_end(args); +} +#if !defined(NO_PLUGIN_VERBOSE_MACRO) +# define ERROR(itf,...) do{if(itf->verbosity>=0)afb_daemon_verbose(itf->daemon,3,__FILE__,__LINE__,__VA_ARGS__);}while(0) +# define WARNING(itf,...) do{if(itf->verbosity>=1)afb_daemon_verbose(itf->daemon,4,__FILE__,__LINE__,__VA_ARGS__);}while(0) +# define NOTICE(itf,...) do{if(itf->verbosity>=1)afb_daemon_verbose(itf->daemon,5,__FILE__,__LINE__,__VA_ARGS__);}while(0) +# define INFO(itf,...) do{if(itf->verbosity>=2)afb_daemon_verbose(itf->daemon,6,__FILE__,__LINE__,__VA_ARGS__);}while(0) +# define DEBUG(itf,...) do{if(itf->verbosity>=3)afb_daemon_verbose(itf->daemon,7,__FILE__,__LINE__,__VA_ARGS__);}while(0) +#endif diff --git a/src/afb-api-dbus.c b/src/afb-api-dbus.c index edbb1edd..f4fd0eba 100644 --- a/src/afb-api-dbus.c +++ b/src/afb-api-dbus.c @@ -16,6 +16,7 @@ */ #define _GNU_SOURCE +#define NO_PLUGIN_VERBOSE_MACRO #include #include diff --git a/src/afb-api-so.c b/src/afb-api-so.c index 78d6d1b6..3cf1feab 100644 --- a/src/afb-api-so.c +++ b/src/afb-api-so.c @@ -16,6 +16,7 @@ */ #define _GNU_SOURCE +#define NO_PLUGIN_VERBOSE_MACRO #include #include @@ -39,16 +40,19 @@ #include "afb-sig-handler.h" #include "verbose.h" +/* + * Description of a plugin + */ struct api_so_desc { struct AFB_plugin *plugin; /* descriptor */ - size_t apilength; + size_t apilength; /* length of the API name */ void *handle; /* context of dlopen */ - struct AFB_interface interface; /* interface */ + struct AFB_interface interface; /* interface for the plugin */ }; static int api_timeout = 15; -static const char plugin_register_function[] = "pluginAfbV1Register"; +static const char plugin_register_function_v1[] = "pluginAfbV1Register"; static void afb_api_so_event_sender_push(struct api_so_desc *desc, const char *name, struct json_object *object) { @@ -73,11 +77,24 @@ static struct afb_event_sender afb_api_so_get_event_sender(struct api_so_desc *d return (struct afb_event_sender){ .itf = &event_sender_itf, .closure = desc }; } +static void afb_api_so_vverbose(struct api_so_desc *desc, int level, const char *file, int line, const char *fmt, va_list args) +{ + char *p; + + if (vasprintf(&p, fmt, args) < 0) + vverbose(level, file, line, fmt, args); + else { + verbose(level, file, line, "%s {plugin %s}", p, desc->plugin->v1.prefix); + free(p); + } +} + static const struct afb_daemon_itf daemon_itf = { .get_event_sender = (void*)afb_api_so_get_event_sender, .get_event_loop = (void*)afb_common_get_event_loop, .get_user_bus = (void*)afb_common_get_user_bus, - .get_system_bus = (void*)afb_common_get_system_bus + .get_system_bus = (void*)afb_common_get_system_bus, + .vverbose = (void*)afb_api_so_vverbose }; struct monitoring { @@ -174,7 +191,7 @@ int afb_api_so_add_plugin(const char *path) } /* retrieves the register function */ - pluginAfbV1RegisterFct = dlsym(handle, plugin_register_function); + pluginAfbV1RegisterFct = dlsym(handle, plugin_register_function_v1); if (!pluginAfbV1RegisterFct) { ERROR("plugin [%s] is not an AFB plugin", path); goto error2; @@ -191,7 +208,7 @@ int afb_api_so_add_plugin(const char *path) desc->handle = handle; /* init the interface */ - desc->interface.verbosity = 0; + desc->interface.verbosity = verbosity; desc->interface.mode = AFB_MODE_LOCAL; desc->interface.daemon.itf = &daemon_itf; desc->interface.daemon.closure = desc; diff --git a/src/main.c b/src/main.c index d1a0745b..defab848 100644 --- a/src/main.c +++ b/src/main.c @@ -17,6 +17,7 @@ */ #define _GNU_SOURCE +#define NO_PLUGIN_VERBOSE_MACRO #include #include diff --git a/src/verbose.c b/src/verbose.c index 892aa9a3..53811c00 100644 --- a/src/verbose.c +++ b/src/verbose.c @@ -16,15 +16,38 @@ limitations under the License. */ -#include "verbose.h" - -#if !defined(VERBOSE_WITH_SYSLOG) - #include #include +#include "verbose.h" + int verbosity = 1; +#define LEVEL(x) ((x) < 0 ? 0 : (x) > 7 ? 7 : (x)) + +#if defined(VERBOSE_WITH_SYSLOG) + +#include + +void vverbose(int level, const char *file, int line, const char *fmt, va_list args) +{ + char *p; + + if (file == NULL || vasprintf(&p, fmt, args) < 0) + vsyslog(level, fmt, args); + else { + syslog(LEVEL(level), "%s [%s:%d]", p, file, line); + free(p); + } +} + +void verbose_set_name(const char *name, int authority) +{ + openlog(name, LOG_PERROR, authority ? LOG_AUTH : LOG_USER); +} + +#else + static const char *prefixes[] = { "<0> EMERGENCY", "<1> ALERT", @@ -36,32 +59,29 @@ static const char *prefixes[] = { "<7> DEBUG" }; -void verbose(int level, const char *file, int line, const char *fmt, ...) +void vverbose(int level, const char *file, int line, const char *fmt, va_list args) { - va_list ap; - - fprintf(stderr, "%s: ", prefixes[level < 0 ? 0 : level > 7 ? 7 : level]); - va_start(ap, fmt); - vfprintf(stderr, fmt, ap); - va_end(ap); - fprintf(stderr, " [%s:%d]\n", file, line); + fprintf(stderr, "%s: ", prefixes[LEVEL(level)]); + vfprintf(stderr, fmt, args); + if (file != NULL) + fprintf(stderr, " [%s:%d]\n", file, line); + else + fprintf(stderr, "\n"); } -#endif - -#if defined(VERBOSE_WITH_SYSLOG) && !defined(NDEBUG) - -int verbosity = 1; - -#endif - -#if defined(VERBOSE_WITH_SYSLOG) && defined(NDEBUG) - -void verbose_error(const char *file, int line) +void verbose_set_name(const char *name, int authority) { - syslog(LOG_ERR, "error file %s line %d", file, line); + fprintf(stderr, "%s: application name is '%s' for '%s'\n", prefixes[5], name, authority ? "AUTHORITY" : "USER"); } #endif +void verbose(int level, const char *file, int line, const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + vverbose(level, file, line, fmt, ap); + va_end(ap); +} diff --git a/src/verbose.h b/src/verbose.h index 5c9f8864..3382d39f 100644 --- a/src/verbose.h +++ b/src/verbose.h @@ -18,45 +18,19 @@ #pragma once -#if !defined(VERBOSE_WITH_SYSLOG) +#include - extern int verbosity; - extern void verbose(int level, const char *file, int line, const char *fmt, ...); +extern int verbosity; + +extern void verbose_set_name(const char *name, int authority); +extern void verbose(int level, const char *file, int line, const char *fmt, ...); +extern void vverbose(int level, const char *file, int line, const char *fmt, va_list args); # define ERROR(...) do{if(verbosity>=0)verbose(3,__FILE__,__LINE__,__VA_ARGS__);}while(0) # define WARNING(...) do{if(verbosity>=1)verbose(4,__FILE__,__LINE__,__VA_ARGS__);}while(0) # define NOTICE(...) do{if(verbosity>=1)verbose(5,__FILE__,__LINE__,__VA_ARGS__);}while(0) # define INFO(...) do{if(verbosity>=2)verbose(6,__FILE__,__LINE__,__VA_ARGS__);}while(0) # define DEBUG(...) do{if(verbosity>=3)verbose(7,__FILE__,__LINE__,__VA_ARGS__);}while(0) -# define LOGUSER(app) NOTICE("Starting user application %s",app) -# define LOGAUTH(app) NOTICE("Starting auth application %s",app) - -#else /* VERBOSE_WITH_SYSLOG is defined */ - -# include - -# define LOGUSER(app) openlog(app,LOG_PERROR,LOG_USER) -# define LOGAUTH(app) openlog(app,LOG_PERROR,LOG_AUTH) - -# if !defined(NDEBUG) - - extern int verbosity; -# define ERROR(...) syslog(LOG_ERR,__VA_ARGS__) -# define WARNING(...) do{if(verbosity)syslog(LOG_WARNING,__VA_ARGS__);}while(0) -# define NOTICE(...) do{if(verbosity)syslog(LOG_NOTICE,__VA_ARGS__);}while(0) -# define INFO(...) do{if(verbosity>1)syslog(LOG_INFO,__VA_ARGS__);}while(0) -# define DEBUG(...) do{if(verbosity>2)syslog(LOG_DEBUG,__VA_ARGS__);}while(0) - -# else - - extern void verbose_error(const char *file, int line); -# define ERROR(...) verbose_error(__FILE__,__LINE__) -# define WARNING(...) do{/*nothing*/}while(0) -# define NOTICE(...) do{/*nothing*/}while(0) -# define INFO(...) do{/*nothing*/}while(0) -# define DEBUG(...) do{/*nothing*/}while(0) - -# endif - -#endif +# define LOGUSER(app) verbose_set_name(app,0) +# define LOGAUTH(app) verbose_set_name(app,1) -- 2.16.6