]> sigrok.org Git - libsigrok.git/blobdiff - src/log.c
uni-t-ut181a: silence compiler warning, use of uninitialized variable
[libsigrok.git] / src / log.c
index 36aa5c0070c10659b6ef6f0a6d7a46f275996197..701df645e76cf296dfe7c760368704668575a2d5 100644 (file)
--- a/src/log.c
+++ b/src/log.c
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
+ * along with this program; if not, see <http://www.gnu.org/licenses/>.
  */
 
+#include <config.h>
 #include <stdarg.h>
 #include <stdio.h>
+#include <glib/gprintf.h>
 #include <libsigrok/libsigrok.h>
+#include "libsigrok-internal.h"
+
 /** @cond PRIVATE */
-#define NO_LOG_WRAPPERS
+#define LOG_PREFIX "log"
 /** @endcond */
-#include "libsigrok-internal.h"
 
 /**
  * @file
@@ -56,13 +58,9 @@ static sr_log_callback sr_log_cb = sr_logv;
  */
 static void *sr_log_cb_data = NULL;
 
-/* Log domain (a short string that is used as prefix for all messages). */
 /** @cond PRIVATE */
-#define LOGDOMAIN_MAXLEN 30
-#define LOGDOMAIN_DEFAULT "sr: "
+#define LOGLEVEL_TIMESTAMP SR_LOG_DBG
 /** @endcond */
-static char sr_log_domain[LOGDOMAIN_MAXLEN + 1] = LOGDOMAIN_DEFAULT;
-
 static int64_t sr_log_start_time = 0;
 
 /**
@@ -89,7 +87,7 @@ SR_API int sr_log_loglevel_set(int loglevel)
                return SR_ERR_ARG;
        }
        /* Output time stamps relative to time at startup */
-       if (loglevel >= SR_LOG_SPEW && sr_log_start_time == 0)
+       if (loglevel >= LOGLEVEL_TIMESTAMP && sr_log_start_time == 0)
                sr_log_start_time = g_get_monotonic_time();
 
        cur_loglevel = loglevel;
@@ -111,51 +109,6 @@ SR_API int sr_log_loglevel_get(void)
        return cur_loglevel;
 }
 
-/**
- * Set the libsigrok logdomain string.
- *
- * @param logdomain The string to use as logdomain for libsigrok log
- *                  messages from now on. Must not be NULL. The maximum
- *                  length of the string is 30 characters (this does not
- *                  include the trailing NUL-byte). Longer strings are
- *                  silently truncated.
- *                  In order to not use a logdomain, pass an empty string.
- *                  The function makes its own copy of the input string, i.e.
- *                  the caller does not need to keep it around.
- *
- * @return SR_OK upon success, SR_ERR_ARG upon invalid logdomain.
- *
- * @since 0.1.0
- */
-SR_API int sr_log_logdomain_set(const char *logdomain)
-{
-       if (!logdomain) {
-               sr_err("log: %s: logdomain was NULL", __func__);
-               return SR_ERR_ARG;
-       }
-
-       /* TODO: Error handling. */
-       snprintf(sr_log_domain, LOGDOMAIN_MAXLEN, "%s", logdomain);
-
-       sr_dbg("Log domain set to '%s'.", sr_log_domain);
-
-       return SR_OK;
-}
-
-/**
- * Get the currently configured libsigrok logdomain.
- *
- * @return A copy of the currently configured libsigrok logdomain
- *         string. The caller is responsible for g_free()ing the string when
- *         it is no longer needed.
- *
- * @since 0.1.0
- */
-SR_API char *sr_log_logdomain_get(void)
-{
-       return g_strdup(sr_log_domain);
-}
-
 /**
  * Set the libsigrok log callback to the specified function.
  *
@@ -174,7 +127,7 @@ SR_API char *sr_log_logdomain_get(void)
 SR_API int sr_log_callback_set(sr_log_callback cb, void *cb_data)
 {
        if (!cb) {
-               sr_err("log: %s: cb was NULL", __func__);
+               sr_err("%s: cb was NULL", __func__);
                return SR_ERR_ARG;
        }
 
@@ -207,112 +160,89 @@ SR_API int sr_log_callback_set_default(void)
        return SR_OK;
 }
 
-static int sr_logv(void *cb_data, int loglevel, const char *format, va_list args)
+/**
+ * Get the libsigrok log callback routine and callback data.
+ *
+ * @param[out] cb Pointer to a function pointer to receive the log callback
+ *     function. Optional, can be NULL.
+ * @param[out] cb_data Pointer to a void pointer to receive the log callback's
+ *     additional arguments. Optional, can be NULL.
+ *
+ * @return SR_OK upon success.
+ *
+ * @since 0.6.0
+ */
+SR_API int sr_log_callback_get(sr_log_callback *cb, void **cb_data)
 {
-       int64_t elapsed;
-       int64_t min;
-       int sec;
-       int usec;
-
-       /* This specific log callback doesn't need the void pointer data. */
-       (void)cb_data;
-
-       /* Only output messages of at least the selected loglevel(s). */
-       if (loglevel > cur_loglevel)
-               return SR_OK;
-
-       if (cur_loglevel >= SR_LOG_SPEW) {
-               elapsed = g_get_monotonic_time() - sr_log_start_time;
-               min = elapsed / G_TIME_SPAN_MINUTE;
-               sec = (elapsed % G_TIME_SPAN_MINUTE) / G_TIME_SPAN_SECOND;
-               usec = elapsed % G_TIME_SPAN_SECOND;
-
-               if (fprintf(stderr, "[%.2" PRIi64 ":%.2d.%.6d] ", min, sec, usec) < 0)
-                       return SR_ERR;
-       }
-       if (sr_log_domain[0] != '\0' && fputs(sr_log_domain, stderr) < 0)
-               return SR_ERR;
-       if (vfprintf(stderr, format, args) < 0)
-               return SR_ERR;
-       if (putc('\n', stderr) < 0)
-               return SR_ERR;
+       if (cb)
+               *cb = sr_log_cb;
+       if (cb_data)
+               *cb_data = sr_log_cb_data;
 
        return SR_OK;
 }
 
-/** @private */
-SR_PRIV int sr_log(int loglevel, const char *format, ...)
+static int sr_logv(void *cb_data, int loglevel, const char *format, va_list args)
 {
-       int ret;
-       va_list args;
+       uint64_t elapsed_us, minutes;
+       unsigned int rest_us, seconds, microseconds;
+       char *raw_output, *output;
+       int raw_len, raw_idx, idx, ret;
 
-       va_start(args, format);
-       ret = sr_log_cb(sr_log_cb_data, loglevel, format, args);
-       va_end(args);
+       /* This specific log callback doesn't need the void pointer data. */
+       (void)cb_data;
 
-       return ret;
-}
+       (void)loglevel;
 
-/** @private */
-SR_PRIV int sr_spew(const char *format, ...)
-{
-       int ret;
-       va_list args;
+       if (cur_loglevel >= LOGLEVEL_TIMESTAMP) {
+               elapsed_us = g_get_monotonic_time() - sr_log_start_time;
 
-       va_start(args, format);
-       ret = sr_log_cb(sr_log_cb_data, SR_LOG_SPEW, format, args);
-       va_end(args);
+               minutes = elapsed_us / G_TIME_SPAN_MINUTE;
+               rest_us = elapsed_us % G_TIME_SPAN_MINUTE;
+               seconds = rest_us / G_TIME_SPAN_SECOND;
+               microseconds = rest_us % G_TIME_SPAN_SECOND;
 
-       return ret;
-}
-
-/** @private */
-SR_PRIV int sr_dbg(const char *format, ...)
-{
-       int ret;
-       va_list args;
+               ret = g_fprintf(stderr, "sr: [%.2" PRIu64 ":%.2u.%.6u] ",
+                               minutes, seconds, microseconds);
+       } else {
+               ret = fputs("sr: ", stderr);
+       }
 
-       va_start(args, format);
-       ret = sr_log_cb(sr_log_cb_data, SR_LOG_DBG, format, args);
-       va_end(args);
+       if (ret < 0 || (raw_len = g_vasprintf(&raw_output, format, args)) < 0)
+               return SR_ERR;
 
-       return ret;
-}
+       output = g_malloc0(raw_len + 1);
 
-/** @private */
-SR_PRIV int sr_info(const char *format, ...)
-{
-       int ret;
-       va_list args;
+       /* Copy the string without any unwanted newlines. */
+       raw_idx = idx = 0;
+       while (raw_idx < raw_len) {
+               if (raw_output[raw_idx] != '\n') {
+                       output[idx] = raw_output[raw_idx];
+                       idx++;
+               }
+               raw_idx++;
+       }
 
-       va_start(args, format);
-       ret = sr_log_cb(sr_log_cb_data, SR_LOG_INFO, format, args);
-       va_end(args);
+       g_fprintf(stderr, "%s\n", output);
+       fflush(stderr);
+       g_free(raw_output);
+       g_free(output);
 
-       return ret;
+       return SR_OK;
 }
 
 /** @private */
-SR_PRIV int sr_warn(const char *format, ...)
+SR_PRIV int sr_log(int loglevel, const char *format, ...)
 {
        int ret;
        va_list args;
 
-       va_start(args, format);
-       ret = sr_log_cb(sr_log_cb_data, SR_LOG_WARN, format, args);
-       va_end(args);
-
-       return ret;
-}
-
-/** @private */
-SR_PRIV int sr_err(const char *format, ...)
-{
-       int ret;
-       va_list args;
+       /* Only output messages of at least the selected loglevel(s). */
+       if (loglevel > cur_loglevel)
+               return SR_OK;
 
        va_start(args, format);
-       ret = sr_log_cb(sr_log_cb_data, SR_LOG_ERR, format, args);
+       ret = sr_log_cb(sr_log_cb_data, loglevel, format, args);
        va_end(args);
 
        return ret;