From 59db7c73c9d98414be9edf7056d7afe025512b3e Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jos=C3=A9=20Bollo?= Date: Fri, 7 Apr 2017 12:52:58 +0200 Subject: [PATCH] Improve log messages MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Add ability to discuss directly with systemd journal. Add report of functions name when logging. Change-Id: Ia7c5836e387b621b47e3700a7abca40bc0e481c8 Signed-off-by: José Bollo --- src/afb-ditf.c | 38 +++++++++++++++++++------------------- src/verbose.c | 40 ++++++++++++++++++++++++++++++++++------ src/verbose.h | 16 ++++++++-------- 3 files changed, 61 insertions(+), 33 deletions(-) diff --git a/src/afb-ditf.c b/src/afb-ditf.c index 414824f5..3eedbdb8 100644 --- a/src/afb-ditf.c +++ b/src/afb-ditf.c @@ -29,35 +29,24 @@ #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; diff --git a/src/verbose.c b/src/verbose.c index 2045bde1..88183c27 100644 --- a/src/verbose.c +++ b/src/verbose.c @@ -29,14 +29,14 @@ int verbosity = 1; #include -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 + +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 @@ -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); } diff --git a/src/verbose.h b/src/verbose.h index 7e4d84f7..71d5fe1e 100644 --- a/src/verbose.h +++ b/src/verbose.h @@ -23,14 +23,14 @@ 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) -- 2.16.6