add log macros for plugins
authorJosé Bollo <jose.bollo@iot.bzh>
Tue, 24 May 2016 09:24:47 +0000 (11:24 +0200)
committerJosé Bollo <jose.bollo@iot.bzh>
Tue, 24 May 2016 10:25:46 +0000 (12:25 +0200)
Change-Id: I3de30aeb90a41ed8ee63ec1e19c6032440d65574
Signed-off-by: José Bollo <jose.bollo@iot.bzh>
include/afb/afb-plugin.h
src/afb-api-dbus.c
src/afb-api-so.c
src/main.c
src/verbose.c
src/verbose.h

index 41e53ce..6f5cd08 100644 (file)
@@ -17,6 +17,8 @@
 
 #pragma once
 
+#include <stdarg.h>
+
 /*****************************************************************************
  * 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
index edbb1ed..f4fd0eb 100644 (file)
@@ -16,6 +16,7 @@
  */
 
 #define _GNU_SOURCE
+#define NO_PLUGIN_VERBOSE_MACRO
 
 #include <stdlib.h>
 #include <string.h>
index 78d6d1b..3cf1fea 100644 (file)
@@ -16,6 +16,7 @@
  */
 
 #define _GNU_SOURCE
+#define NO_PLUGIN_VERBOSE_MACRO
 
 #include <stdio.h>
 #include <assert.h>
 #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;
index d1a0745..defab84 100644 (file)
@@ -17,6 +17,7 @@
  */
 
 #define _GNU_SOURCE
+#define NO_PLUGIN_VERBOSE_MACRO
 
 #include <stdlib.h>
 #include <stdio.h>
index 892aa9a..53811c0 100644 (file)
  limitations under the License.
 */
 
-#include "verbose.h"
-
-#if !defined(VERBOSE_WITH_SYSLOG)
-
 #include <stdio.h>
 #include <stdarg.h>
 
+#include "verbose.h"
+
 int verbosity = 1;
 
+#define LEVEL(x) ((x) < 0 ? 0 : (x) > 7 ? 7 : (x))
+
+#if defined(VERBOSE_WITH_SYSLOG)
+
+#include <syslog.h>
+
+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);
+}
 
index 5c9f886..3382d39 100644 (file)
 
 #pragma once
 
-#if !defined(VERBOSE_WITH_SYSLOG)
+#include <stdarg.h>
 
-  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 <syslog.h>
-
-# 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)