From: Uwe Hermann Date: Wed, 4 May 2011 17:03:01 +0000 (+0200) Subject: libsigrok/cli: Implement loglevel support. X-Git-Tag: libsigrok-0.1.0~265 X-Git-Url: https://sigrok.org/gitweb/?p=libsigrok.git;a=commitdiff_plain;h=1352eeddd4d164b58f2233ae109432b73faab539 libsigrok/cli: Implement loglevel support. libsigrok can now be told at which loglevel to work, i.e., how many debug/error/warning output to generate. You can also query the current loglevel. In sigrok-cli it is now possible to set the loglevel via -l. For example: - Disable all output: sigrok-cli -l 0 - Only show errors: sigrok-cli -l 1 - Show errors, warnings, info, and debug messages: sigrok-cli -l 4 --- diff --git a/log.c b/log.c index 22dcc107..e8cebb1b 100644 --- a/log.c +++ b/log.c @@ -23,12 +23,49 @@ #include #include +static int sr_loglevel = SR_LOG_WARN; /* Show errors+warnings per default. */ + +/** + * Set the libsigrok loglevel. + * + * This influences the amount of log messages (debug messages, error messages, + * and so on) libsigrok will output. Using SR_LOG_NONE disables all messages. + * + * @param loglevel The loglevel to set (SR_LOG_NONE, SR_LOG_ERR, SR_LOG_WARN, + * SR_LOG_INFO, or SR_LOG_DBG). + * @return SR_OK upon success, SR_ERR_ARG upon invalid loglevel. + */ +int sr_set_loglevel(int loglevel) +{ + if (loglevel < SR_LOG_NONE || loglevel > SR_LOG_DBG) { + sr_err("log: %s: invalid loglevel %d", __func__, loglevel); + return SR_ERR_ARG; + } + + sr_loglevel = loglevel; + + sr_dbg("log: %s: libsigrok loglevel set to %d", __func__, loglevel); + + return SR_OK; +} + +/** + * Get the libsigrok loglevel. + * + * @return The currently configured libsigrok loglevel. + */ +int sr_get_loglevel(void) +{ + return sr_loglevel; +} + static int sr_logv(int loglevel, const char *format, va_list args) { int ret; - /* Avoid compiler warnings. */ - loglevel = loglevel; + /* Only output messages of at least the selected loglevel(s). */ + if (loglevel > sr_loglevel) + return SR_OK; /* TODO? */ ret = vfprintf(stderr, format, args); fprintf(stderr, "\n"); diff --git a/sigrok-internal.h b/sigrok-internal.h index d3853403..db8aef61 100644 --- a/sigrok-internal.h +++ b/sigrok-internal.h @@ -41,13 +41,6 @@ int load_hwplugins(void); /*--- log.c -----------------------------------------------------------------*/ -/* Log levels for sr_log() and friends. */ -#define SR_LOG_NONE 0 -#define SR_LOG_DBG 1 -#define SR_LOG_INFO 2 -#define SR_LOG_WARN 3 -#define SR_LOG_ERR 4 - int sr_log(int loglevel, const char *format, ...); int sr_dbg(const char *format, ...); int sr_info(const char *format, ...); diff --git a/sigrok-proto.h b/sigrok-proto.h index 36c4c651..fce35c09 100644 --- a/sigrok-proto.h +++ b/sigrok-proto.h @@ -25,6 +25,11 @@ int sr_init(void); int sr_exit(void); +/*--- log.c -----------------------------------------------------------------*/ + +int sr_set_loglevel(int loglevel); +int sr_get_loglevel(void); + /*--- datastore.c -----------------------------------------------------------*/ int sr_datastore_new(int unitsize, struct sr_datastore **ds); diff --git a/sigrok.h b/sigrok.h index 2fd17119..13641bfd 100644 --- a/sigrok.h +++ b/sigrok.h @@ -67,6 +67,13 @@ extern "C" { #define SR_HZ_TO_NS(n) (1000000000 / (n)) +/* libsigrok loglevels. */ +#define SR_LOG_NONE 0 +#define SR_LOG_ERR 1 +#define SR_LOG_WARN 2 +#define SR_LOG_INFO 3 +#define SR_LOG_DBG 4 + typedef int (*sr_receive_data_callback) (int fd, int revents, void *user_data); /* Data types used by hardware plugins for set_configuration() */