Improve log messages
authorJosé Bollo <jose.bollo@iot.bzh>
Fri, 7 Apr 2017 10:52:58 +0000 (12:52 +0200)
committerJosé Bollo <jose.bollo@iot.bzh>
Fri, 7 Apr 2017 10:52:58 +0000 (12:52 +0200)
Add ability to discuss directly with systemd journal.
Add report of functions name when logging.

Change-Id: Ia7c5836e387b621b47e3700a7abca40bc0e481c8
Signed-off-by: José Bollo <jose.bollo@iot.bzh>
src/afb-ditf.c
src/verbose.c
src/verbose.h

index 414824f..3eedbdb 100644 (file)
 #include "verbose.h"
 
 
-static struct afb_event event_make_cb(void *closure, const char *name);
-static int event_broadcast_cb(void *closure, const char *name, struct json_object *object);
-static void vverbose_cb(void *closure, int level, const char *file, int line, const char *fmt, va_list args);
-static int rootdir_open_locale_cb(void *closure, const char *filename, int flags, const char *locale);
-
-static const struct afb_daemon_itf daemon_itf = {
-       .vverbose = vverbose_cb,
-       .event_make = event_make_cb,
-       .event_broadcast = event_broadcast_cb,
-       .get_event_loop = afb_common_get_event_loop,
-       .get_user_bus = afb_common_get_user_bus,
-       .get_system_bus = afb_common_get_system_bus,
-       .rootdir_get_fd = afb_common_rootdir_get_fd,
-       .rootdir_open_locale = rootdir_open_locale_cb
-};
-
-static void vverbose_cb(void *closure, int level, const char *file, int line, const char *fmt, va_list args)
+static void vverbose_cb(void *closure, int level, const char *file, int line, const char *function, const char *fmt, va_list args)
 {
        char *p;
        struct afb_ditf *ditf = closure;
 
        if (vasprintf(&p, fmt, args) < 0)
-               vverbose(level, file, line, fmt, args);
+               vverbose(level, file, line, function, fmt, args);
        else {
-               verbose(level, file, line, "%s {binding %s}", p, ditf->prefix);
+               verbose(level, file, line, function, "%s {binding %s}", p, ditf->prefix);
                free(p);
        }
 }
 
+static void old_vverbose_cb(void *closure, int level, const char *file, int line, const char *fmt, va_list args)
+{
+       vverbose_cb(closure, level, file, line, "?", fmt, args);
+}
+
 static struct afb_event event_make_cb(void *closure, const char *name)
 {
        size_t plen, nlen;
@@ -99,6 +88,17 @@ static int rootdir_open_locale_cb(void *closure, const char *filename, int flags
        return afb_common_rootdir_open_locale(filename, flags, locale);
 }
 
+static const struct afb_daemon_itf daemon_itf = {
+       .vverbose = old_vverbose_cb,
+       .event_make = event_make_cb,
+       .event_broadcast = event_broadcast_cb,
+       .get_event_loop = afb_common_get_event_loop,
+       .get_user_bus = afb_common_get_user_bus,
+       .get_system_bus = afb_common_get_system_bus,
+       .rootdir_get_fd = afb_common_rootdir_get_fd,
+       .rootdir_open_locale = rootdir_open_locale_cb
+};
+
 void afb_ditf_init(struct afb_ditf *ditf, const char *prefix)
 {
        ditf->interface.verbosity = verbosity;
index 2045bde..88183c2 100644 (file)
@@ -29,14 +29,14 @@ int verbosity = 1;
 
 #include <syslog.h>
 
-void vverbose(int level, const char *file, int line, const char *fmt, va_list args)
+void vverbose(int level, const char *file, int line, const char *function, 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);
+               syslog(LEVEL(level), "%s [%s:%d, function]", p, file, line, function);
                free(p);
        }
 }
@@ -46,6 +46,34 @@ void verbose_set_name(const char *name, int authority)
        openlog(name, LOG_PERROR, authority ? LOG_AUTH : LOG_USER);
 }
 
+#elif defined(VERBOSE_WITH_SYSTEMD)
+
+#define SD_JOURNAL_SUPPRESS_LOCATION
+
+#include <systemd/sd-journal.h>
+
+static const char *appname;
+
+static int appauthority;
+
+void vverbose(int level, const char *file, int line, const char *function, const char *fmt, va_list args)
+{
+       char lino[20];
+
+       if (file == NULL) {
+               sd_journal_printv(level, fmt, args);
+       } else {
+               sprintf(lino, "%d", line);
+               sd_journal_printv_with_location(level, file, lino, function, fmt, args);
+       }
+}
+
+void verbose_set_name(const char *name, int authority)
+{
+       appname = name;
+       appauthority = authority;
+}
+
 #else
 
 #include <unistd.h>
@@ -65,14 +93,14 @@ static const char *prefixes[] = {
        "<7> DEBUG"
 };
 
-void vverbose(int level, const char *file, int line, const char *fmt, va_list args)
+void vverbose(int level, const char *file, int line, const char *function, const char *fmt, va_list args)
 {
        int tty = isatty(fileno(stderr));
 
        fprintf(stderr, "%s: ", prefixes[LEVEL(level)] + (tty ? 4 : 0));
        vfprintf(stderr, fmt, args);
        if (file != NULL && (!tty || verbosity >5))
-               fprintf(stderr, " [%s:%d]\n", file, line);
+               fprintf(stderr, " [%s:%d,%s]\n", file, line, function);
        else
                fprintf(stderr, "\n");
 }
@@ -85,12 +113,12 @@ void verbose_set_name(const char *name, int authority)
 
 #endif
 
-void verbose(int level, const char *file, int line, const char *fmt, ...)
+void verbose(int level, const char *file, int line, const char *function, const char *fmt, ...)
 {
        va_list ap;
 
        va_start(ap, fmt);
-       vverbose(level, file, line, fmt, ap);
+       vverbose(level, file, line, function, fmt, ap);
        va_end(ap);
 }
 
index 7e4d84f..71d5fe1 100644 (file)
 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, ...) __attribute__((format(printf, 4, 5)));
-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)
+extern void verbose(int level, const char *file, int line, const char *function, const char *fmt, ...) __attribute__((format(printf, 5, 6)));
+extern void vverbose(int level, const char *file, int line, const char *function, const char *fmt, va_list args);
+
+# define ERROR(...)   do{if(verbosity>=0)verbose(3,__FILE__,__LINE__,__func__,__VA_ARGS__);}while(0)
+# define WARNING(...) do{if(verbosity>=1)verbose(4,__FILE__,__LINE__,__func__,__VA_ARGS__);}while(0)
+# define NOTICE(...)  do{if(verbosity>=1)verbose(5,__FILE__,__LINE__,__func__,__VA_ARGS__);}while(0)
+# define INFO(...)    do{if(verbosity>=2)verbose(6,__FILE__,__LINE__,__func__,__VA_ARGS__);}while(0)
+# define DEBUG(...)   do{if(verbosity>=3)verbose(7,__FILE__,__LINE__,__func__,__VA_ARGS__);}while(0)
 # define LOGUSER(app) verbose_set_name(app,0)
 # define LOGAUTH(app) verbose_set_name(app,1)