X-Git-Url: http://sigrok.org/gitweb/?a=blobdiff_plain;f=src%2Flog.c;h=cdf2293bdeb32b012d43548545c44c058eb15a1b;hb=HEAD;hp=caffa2a2d4900f8532b82455288a748aea6b4ba7;hpb=2c267f6819c2bd0e6f7101ffd4eec35ef6c2a734;p=libsigrok.git diff --git a/src/log.c b/src/log.c index caffa2a2..1b01afee 100644 --- a/src/log.c +++ b/src/log.c @@ -160,18 +160,48 @@ 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) { + int ret; uint64_t elapsed_us, minutes; unsigned int rest_us, seconds, microseconds; - char *raw_output, *output; - int raw_len, raw_idx, idx, ret; + char *raw_output, *output, c; + ssize_t print_len; + size_t raw_len; + const char *raw_ptr; + char *out_ptr; /* This specific log callback doesn't need the void pointer data. */ (void)cb_data; (void)loglevel; + /* Prefix with 'sr:'. Optionally prefix with timestamp. */ + ret = fputs("sr: ", stderr); + if (ret < 0) + return SR_ERR; if (cur_loglevel >= LOGLEVEL_TIMESTAMP) { elapsed_us = g_get_monotonic_time() - sr_log_start_time; @@ -180,30 +210,41 @@ static int sr_logv(void *cb_data, int loglevel, const char *format, va_list args seconds = rest_us / G_TIME_SPAN_SECOND; microseconds = rest_us % G_TIME_SPAN_SECOND; - ret = g_fprintf(stderr, "sr: [%.2" PRIu64 ":%.2u.%.6u] ", + ret = g_fprintf(stderr, "[%.2" PRIu64 ":%.2u.%.6u] ", minutes, seconds, microseconds); - } else { - ret = fputs("sr: ", stderr); + if (ret < 0) + return SR_ERR; } - if (ret < 0 || (raw_len = g_vasprintf(&raw_output, format, args)) < 0) + /* Print the caller's message into a local buffer. */ + raw_output = NULL; + print_len = g_vasprintf(&raw_output, format, args); + if (print_len < 0) { + g_free(raw_output); return SR_ERR; + } + raw_len = (size_t)print_len; - 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++; + /* Copy the string. Strip unwanted line breaks. */ + output = g_malloc(raw_len + 1); + if (!output) { + g_free(raw_output); + return SR_ERR; } + out_ptr = output; + raw_ptr = raw_output; + while (*raw_ptr) { + c = *raw_ptr++; + if (c == '\r' || c == '\n') + continue; + *out_ptr++ = c; + } + *out_ptr = '\0'; + g_free(raw_output); + /* Print the trimmed output text. */ g_fprintf(stderr, "%s\n", output); fflush(stderr); - g_free(raw_output); g_free(output); return SR_OK; @@ -219,6 +260,10 @@ SR_PRIV int sr_log(int loglevel, const char *format, ...) if (loglevel > cur_loglevel) return SR_OK; + /* Silently succeed when no logging callback is registered. */ + if (!sr_log_cb) + return SR_OK; + va_start(args, format); ret = sr_log_cb(sr_log_cb_data, loglevel, format, args); va_end(args);