X-Git-Url: https://sigrok.org/gitweb/?a=blobdiff_plain;f=log.c;h=e8cebb1bda544c21cc5f97cd87571b299bb19013;hb=5a9660dd4e4977b9c3a86fe895e0b714e64a44a1;hp=22dcc107d020fa0a165834a030baff49bf5a0043;hpb=b08024a8363c7a019bebc05a25e2689e774326e8;p=libsigrok.git 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");