From: Soeren Apel Date: Thu, 9 Jun 2016 02:36:35 +0000 (+0200) Subject: Logging: Filter out unwanted newlines X-Git-Tag: libsigrok-0.5.0~298 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=4d6d660b8306abdbc77a47e48d7f527234ce9a4f;p=libsigrok.git Logging: Filter out unwanted newlines 055804e89ea0f6b4145040a0eeb2f4e6951692fe changed the outgoing SCPI message termination by always adding a newline. This results in the following log output: sr: [00:00.003102] scpi: Opening VXI device vxi/192.168.178.43. sr: [00:00.005648] scpi_vxi: Successfully sent SCPI command: '*IDN? '. sr: [00:00.005931] scpi: Got response: 'YOKOGAWA,710130,91HC30402,F3.73', length 31. This patch restores the previous unterminated SCPI message logging: sr: [00:00.005462] scpi: Opening VXI device vxi/192.168.178.43. sr: [00:00.007515] scpi_vxi: Successfully sent SCPI command: '*IDN?'. sr: [00:00.007860] scpi: Got response: 'YOKOGAWA,710130,91HC30402,F3.73', length 31. As it's located in the general logging mechanism, we deal with any additional (and unwanted) newlines this way. --- diff --git a/src/log.c b/src/log.c index 3d65cc4e..3cbbe17b 100644 --- a/src/log.c +++ b/src/log.c @@ -163,7 +163,8 @@ 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; @@ -186,10 +187,25 @@ 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); + g_free(raw_output); + g_free(output); + return SR_OK; }