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
This commit is contained in:
Uwe Hermann 2011-05-04 19:03:01 +02:00
parent 548b08e55c
commit 1352eeddd4
4 changed files with 51 additions and 9 deletions

41
log.c
View File

@ -23,12 +23,49 @@
#include <sigrok.h>
#include <sigrok-internal.h>
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");

View File

@ -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, ...);

View File

@ -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);

View File

@ -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() */