2 * This file is part of the libsigrok project.
4 * Copyright (C) 2011-2012 Uwe Hermann <uwe@hermann-uwe.de>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 #include <glib/gprintf.h>
25 #include <libsigrok/libsigrok.h>
26 #include "libsigrok-internal.h"
28 #define LOG_PREFIX "log"
33 * Controlling the libsigrok message logging functionality.
37 * @defgroup grp_logging Logging
39 * Controlling the libsigrok message logging functionality.
44 /* Currently selected libsigrok loglevel. Default: SR_LOG_WARN. */
45 static int cur_loglevel = SR_LOG_WARN; /* Show errors+warnings per default. */
47 /* Function prototype. */
48 static int sr_logv(void *cb_data, int loglevel, const char *format,
51 /* Pointer to the currently selected log callback. Default: sr_logv(). */
52 static sr_log_callback sr_log_cb = sr_logv;
55 * Pointer to private data that can be passed to the log callback.
56 * This can be used (for example) by C++ GUIs to pass a "this" pointer.
58 static void *sr_log_cb_data = NULL;
61 #define LOGLEVEL_TIMESTAMP SR_LOG_DBG
63 static int64_t sr_log_start_time = 0;
66 * Set the libsigrok loglevel.
68 * This influences the amount of log messages (debug messages, error messages,
69 * and so on) libsigrok will output. Using SR_LOG_NONE disables all messages.
71 * Note that this function itself will also output log messages. After the
72 * loglevel has changed, it will output a debug message with SR_LOG_DBG for
73 * example. Whether this message is shown depends on the (new) loglevel.
75 * @param loglevel The loglevel to set (SR_LOG_NONE, SR_LOG_ERR, SR_LOG_WARN,
76 * SR_LOG_INFO, SR_LOG_DBG, or SR_LOG_SPEW).
78 * @return SR_OK upon success, SR_ERR_ARG upon invalid loglevel.
82 SR_API int sr_log_loglevel_set(int loglevel)
84 if (loglevel < SR_LOG_NONE || loglevel > SR_LOG_SPEW) {
85 sr_err("Invalid loglevel %d.", loglevel);
88 /* Output time stamps relative to time at startup */
89 if (loglevel >= LOGLEVEL_TIMESTAMP && sr_log_start_time == 0)
90 sr_log_start_time = g_get_monotonic_time();
92 cur_loglevel = loglevel;
94 sr_dbg("libsigrok loglevel set to %d.", loglevel);
100 * Get the libsigrok loglevel.
102 * @return The currently configured libsigrok loglevel.
106 SR_API int sr_log_loglevel_get(void)
112 * Set the libsigrok log callback to the specified function.
114 * @param cb Function pointer to the log callback function to use.
116 * @param cb_data Pointer to private data to be passed on. This can be used by
117 * the caller to pass arbitrary data to the log functions. This
118 * pointer is only stored or passed on by libsigrok, and is
119 * never used or interpreted in any way. The pointer is allowed
120 * to be NULL if the caller doesn't need/want to pass any data.
122 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments.
126 SR_API int sr_log_callback_set(sr_log_callback cb, void *cb_data)
129 sr_err("%s: cb was NULL", __func__);
133 /* Note: 'cb_data' is allowed to be NULL. */
136 sr_log_cb_data = cb_data;
142 * Set the libsigrok log callback to the default built-in one.
144 * Additionally, the internal 'sr_log_cb_data' pointer is set to NULL.
146 * @return SR_OK upon success, a negative error code otherwise.
150 SR_API int sr_log_callback_set_default(void)
153 * Note: No log output in this function, as it should safely work
154 * even if the currently set log callback is buggy/broken.
157 sr_log_cb_data = NULL;
162 static int sr_logv(void *cb_data, int loglevel, const char *format, va_list args)
164 uint64_t elapsed_us, minutes;
165 unsigned int rest_us, seconds, microseconds;
166 char *raw_output, *output;
167 int raw_len, raw_idx, idx, ret;
169 /* This specific log callback doesn't need the void pointer data. */
172 /* Only output messages of at least the selected loglevel(s). */
173 if (loglevel > cur_loglevel)
176 if (cur_loglevel >= LOGLEVEL_TIMESTAMP) {
177 elapsed_us = g_get_monotonic_time() - sr_log_start_time;
179 minutes = elapsed_us / G_TIME_SPAN_MINUTE;
180 rest_us = elapsed_us % G_TIME_SPAN_MINUTE;
181 seconds = rest_us / G_TIME_SPAN_SECOND;
182 microseconds = rest_us % G_TIME_SPAN_SECOND;
184 ret = g_fprintf(stderr, "sr: [%.2" PRIu64 ":%.2u.%.6u] ",
185 minutes, seconds, microseconds);
187 ret = fputs("sr: ", stderr);
190 if (ret < 0 || (raw_len = g_vasprintf(&raw_output, format, args)) < 0)
193 output = g_malloc0(raw_len + 1);
195 /* Copy the string without any unwanted newlines. */
197 while (raw_idx < raw_len) {
198 if (raw_output[raw_idx] != '\n') {
199 output[idx] = raw_output[raw_idx];
205 g_fprintf(stderr, "%s\n", output);
213 SR_PRIV int sr_log(int loglevel, const char *format, ...)
218 va_start(args, format);
219 ret = sr_log_cb(sr_log_cb_data, loglevel, format, args);