From: Vincent Bernat Date: Sat, 16 May 2015 06:05:24 +0000 (+0200) Subject: lib: enable ability to change log level on-the-fly X-Git-Tag: 0.7.15~31 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=2389d2ccf7be6486596e517e82cc88a1b0d0d741;p=thirdparty%2Flldpd.git lib: enable ability to change log level on-the-fly And remove the previous commit, use internal logging instead. --- diff --git a/src/client/lldpcli.c b/src/client/lldpcli.c index 98d4267f..fc97c612 100644 --- a/src/client/lldpcli.c +++ b/src/client/lldpcli.c @@ -31,7 +31,6 @@ #include #include #include -#include #include "client.h" @@ -45,18 +44,6 @@ extern const char *__progname; static struct cmd_node *root = NULL; const char *ctlname = NULL; -static void -log_from_lib(int severity, const char *msg) -{ - switch (severity) { - case LOG_DEBUG: log_debug("liblldpctl", "%s", msg); break; - case LOG_INFO: - case LOG_NOTICE: log_info("liblldpctl", "%s", msg); break; - default: - log_warnx("liblldpctl", "%s", msg); break; - } -} - static int is_lldpctl(const char *name) { @@ -463,6 +450,7 @@ main(int argc, char *argv[]) } } log_init(debug, __progname); + lldpctl_log_level(debug); /* Get and parse command line options */ optind = 1; @@ -498,9 +486,6 @@ main(int argc, char *argv[]) /* Register commands */ root = register_commands(); - /* Initialize logging for liblldpctl */ - lldpctl_log_callback(log_from_lib); - /* Make a connection */ log_debug("lldpctl", "connect to lldpd"); conn = lldpctl_new_name(ctlname, NULL, NULL, NULL); diff --git a/src/lib/Makefile.am b/src/lib/Makefile.am index 1cc79805..a7709bf1 100644 --- a/src/lib/Makefile.am +++ b/src/lib/Makefile.am @@ -13,7 +13,7 @@ liblldpctl_la_SOURCES = \ atoms/config.c atoms/dot1.c atoms/dot3.c \ atoms/interface.c atoms/med.c atoms/mgmt.c atoms/port.c liblldpctl_la_LIBADD = $(top_builddir)/src/libcommon-daemon-lib.la libfixedpoint.la -liblldpctl_la_LDFLAGS = $(AM_LDFLAGS) -export-symbols-regex '^lldpctl_' -version-info 9:0:5 +liblldpctl_la_LDFLAGS = $(AM_LDFLAGS) -export-symbols-regex '^lldpctl_' -version-info 10:0:6 # -version-info format is `current`:`revision`:`age`. For more details, see: # http://sources.redhat.com/autobook/autobook/autobook_91.html#SEC91 diff --git a/src/lib/errors.c b/src/lib/errors.c index 33860b1b..0bd31f32 100644 --- a/src/lib/errors.c +++ b/src/lib/errors.c @@ -53,3 +53,9 @@ lldpctl_log_callback(void (*cb)(int severity, const char *msg)) { log_register(cb); } + +void +lldpctl_log_level(int level) +{ + log_level(level); +} diff --git a/src/lib/lldpctl.h b/src/lib/lldpctl.h index 9f154233..3f07e442 100644 --- a/src/lib/lldpctl.h +++ b/src/lib/lldpctl.h @@ -256,6 +256,17 @@ int lldpctl_release(lldpctl_conn_t *conn); */ void lldpctl_log_callback(void (*cb)(int severity, const char *msg)); +/** + * Setup log level. + * + * By default, liblldpctl will only log warnings. The following function allows + * to increase verbosity. This function has no effect if callbacks are + * registered with the previous function. + * + * @param level Level of verbosity (1 = warnings, 2 = info, 3 = debug). + */ +void lldpctl_log_level(int level); + /** * Possible error codes for functions that return negative integers on * this purpose or for @c lldpctl_last_error(). diff --git a/src/log.c b/src/log.c index 4f266bfc..d1106fa8 100644 --- a/src/log.c +++ b/src/log.c @@ -50,6 +50,13 @@ log_init(int n_debug, const char *progname) tzset(); } +void +log_level(int n_debug) +{ + if (debug > 0 && n_debug >= 1) + debug = n_debug; +} + void log_register(void (*cb)(int, const char*)) { diff --git a/src/log.h b/src/log.h index df985857..9f8707ed 100644 --- a/src/log.h +++ b/src/log.h @@ -29,5 +29,6 @@ void fatalx(const char *, const char *) __attribute__((__noreturn__) void log_register(void (*cb)(int, const char*)); void log_accept(const char *); +void log_level(int); #endif