]> 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 3d65cc4e20bb755d7550f87b85959990976916c9..701df645e76cf296dfe7c760368704668575a2d5 100644 (file)
--- a/src/log.c
+++ b/src/log.c
@@ -14,8 +14,7 @@
  * 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>
@@ -25,7 +24,9 @@
 #include <libsigrok/libsigrok.h>
 #include "libsigrok-internal.h"
 
+/** @cond PRIVATE */
 #define LOG_PREFIX "log"
+/** @endcond */
 
 /**
  * @file
@@ -159,18 +160,39 @@ SR_API int sr_log_callback_set_default(void)
        return SR_OK;
 }
 
+/**
+ * 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)
+{
+       if (cb)
+               *cb = sr_log_cb;
+       if (cb_data)
+               *cb_data = sr_log_cb_data;
+
+       return SR_OK;
+}
+
 static int sr_logv(void *cb_data, int loglevel, const char *format, va_list args)
 {
        uint64_t elapsed_us, minutes;
        unsigned int rest_us, seconds, microseconds;
-       int ret;
+       char *raw_output, *output;
+       int raw_len, raw_idx, idx, ret;
 
        /* 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;
+       (void)loglevel;
 
        if (cur_loglevel >= LOGLEVEL_TIMESTAMP) {
                elapsed_us = g_get_monotonic_time() - sr_log_start_time;
@@ -186,10 +208,26 @@ static int sr_logv(void *cb_data, int loglevel, const char *format, va_list args
                ret = fputs("sr: ", stderr);
        }
 
-       if (ret < 0 || g_vfprintf(stderr, format, args) < 0
-                       || putc('\n', stderr) < 0)
+       if (ret < 0 || (raw_len = g_vasprintf(&raw_output, format, args)) < 0)
                return SR_ERR;
 
+       output = g_malloc0(raw_len + 1);
+
+       /* 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++;
+       }
+
+       g_fprintf(stderr, "%s\n", output);
+       fflush(stderr);
+       g_free(raw_output);
+       g_free(output);
+
        return SR_OK;
 }
 
@@ -199,6 +237,10 @@ SR_PRIV int sr_log(int loglevel, 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, loglevel, format, args);
        va_end(args);