* 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 LOG_PREFIX "log"
+/** @endcond */
/**
* @file
{
uint64_t elapsed_us, minutes;
unsigned int rest_us, seconds, microseconds;
+ 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;
seconds = rest_us / G_TIME_SPAN_SECOND;
microseconds = rest_us % G_TIME_SPAN_SECOND;
- if (fprintf(stderr, "[%.2" PRIu64 ":%.2u.%.6u] ",
- minutes, seconds, microseconds) < 0)
- return SR_ERR;
+ ret = g_fprintf(stderr, "sr: [%.2" PRIu64 ":%.2u.%.6u] ",
+ minutes, seconds, microseconds);
+ } else {
+ ret = fputs("sr: ", stderr);
}
- if (vfprintf(stderr, format, args) < 0)
- return SR_ERR;
- if (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;
}