+/**
+ * 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.
+ */
+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((char *)&sr_log_domain, LOGDOMAIN_MAXLEN, "%s", logdomain);
+
+ sr_dbg("Log domain set to '%s'.", (const char *)&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.
+ */
+SR_API char *sr_log_logdomain_get(void)
+{
+ return g_strdup((const char *)&sr_log_domain);
+}
+
+/**
+ * Set the libsigrok log callback to the specified function.
+ *
+ * @param cb Function pointer to the log callback function to use.
+ * Must not be NULL.
+ * @param cb_data Pointer to private data to be passed on. This can be used by
+ * the caller to pass arbitrary data to the log functions. This
+ * pointer is only stored or passed on by libsigrok, and is
+ * never used or interpreted in any way. The pointer is allowed
+ * to be NULL if the caller doesn't need/want to pass any data.
+ *
+ * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments.
+ */
+SR_API int sr_log_callback_set(sr_log_callback_t cb, void *cb_data)
+{
+ if (!cb) {
+ sr_err("log: %s: cb was NULL", __func__);
+ return SR_ERR_ARG;
+ }
+
+ /* Note: 'cb_data' is allowed to be NULL. */
+
+ sr_log_callback = cb;
+ sr_log_callback_data = cb_data;
+
+ return SR_OK;
+}
+
+/**
+ * Set the libsigrok log callback to the default built-in one.
+ *
+ * Additionally, the internal 'sr_log_callback_data' pointer is set to NULL.
+ *
+ * @return SR_OK upon success, a negative error code otherwise.
+ */
+SR_API int sr_log_callback_set_default(void)
+{
+ /*
+ * Note: No log output in this function, as it should safely work
+ * even if the currently set log callback is buggy/broken.
+ */
+ sr_log_callback = sr_logv;
+ sr_log_callback_data = NULL;
+
+ return SR_OK;
+}
+
+static int sr_logv(void *cb_data, int loglevel, const char *format, va_list args)