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
23 #include <libsigrok/libsigrok.h>
25 #define NO_LOG_WRAPPERS
27 #include "libsigrok-internal.h"
32 * Controlling the libsigrok message logging functionality.
36 * @defgroup grp_logging Logging
38 * Controlling the libsigrok message logging functionality.
43 /* Currently selected libsigrok loglevel. Default: SR_LOG_WARN. */
44 static int cur_loglevel = SR_LOG_WARN; /* Show errors+warnings per default. */
46 /* Function prototype. */
47 static int sr_logv(void *cb_data, int loglevel, const char *format,
50 /* Pointer to the currently selected log callback. Default: sr_logv(). */
51 static sr_log_callback sr_log_cb = sr_logv;
54 * Pointer to private data that can be passed to the log callback.
55 * This can be used (for example) by C++ GUIs to pass a "this" pointer.
57 static void *sr_log_cb_data = NULL;
59 /* Log domain (a short string that is used as prefix for all messages). */
61 #define LOGDOMAIN_MAXLEN 30
62 #define LOGDOMAIN_DEFAULT "sr: "
64 static char sr_log_domain[LOGDOMAIN_MAXLEN + 1] = LOGDOMAIN_DEFAULT;
66 static int64_t sr_log_start_time = 0;
69 * Set the libsigrok loglevel.
71 * This influences the amount of log messages (debug messages, error messages,
72 * and so on) libsigrok will output. Using SR_LOG_NONE disables all messages.
74 * Note that this function itself will also output log messages. After the
75 * loglevel has changed, it will output a debug message with SR_LOG_DBG for
76 * example. Whether this message is shown depends on the (new) loglevel.
78 * @param loglevel The loglevel to set (SR_LOG_NONE, SR_LOG_ERR, SR_LOG_WARN,
79 * SR_LOG_INFO, SR_LOG_DBG, or SR_LOG_SPEW).
81 * @return SR_OK upon success, SR_ERR_ARG upon invalid loglevel.
85 SR_API int sr_log_loglevel_set(int loglevel)
87 if (loglevel < SR_LOG_NONE || loglevel > SR_LOG_SPEW) {
88 sr_err("Invalid loglevel %d.", loglevel);
91 /* Output time stamps relative to time at startup */
92 if (loglevel >= SR_LOG_SPEW && sr_log_start_time == 0)
93 sr_log_start_time = g_get_monotonic_time();
95 cur_loglevel = loglevel;
97 sr_dbg("libsigrok loglevel set to %d.", loglevel);
103 * Get the libsigrok loglevel.
105 * @return The currently configured libsigrok loglevel.
109 SR_API int sr_log_loglevel_get(void)
115 * Set the libsigrok logdomain string.
117 * @param logdomain The string to use as logdomain for libsigrok log
118 * messages from now on. Must not be NULL. The maximum
119 * length of the string is 30 characters (this does not
120 * include the trailing NUL-byte). Longer strings are
121 * silently truncated.
122 * In order to not use a logdomain, pass an empty string.
123 * The function makes its own copy of the input string, i.e.
124 * the caller does not need to keep it around.
126 * @return SR_OK upon success, SR_ERR_ARG upon invalid logdomain.
130 SR_API int sr_log_logdomain_set(const char *logdomain)
133 sr_err("log: %s: logdomain was NULL", __func__);
137 /* TODO: Error handling. */
138 snprintf(sr_log_domain, LOGDOMAIN_MAXLEN, "%s", logdomain);
140 sr_dbg("Log domain set to '%s'.", sr_log_domain);
146 * Get the currently configured libsigrok logdomain.
148 * @return A copy of the currently configured libsigrok logdomain
149 * string. The caller is responsible for g_free()ing the string when
150 * it is no longer needed.
154 SR_API char *sr_log_logdomain_get(void)
156 return g_strdup(sr_log_domain);
160 * Set the libsigrok log callback to the specified function.
162 * @param cb Function pointer to the log callback function to use.
164 * @param cb_data Pointer to private data to be passed on. This can be used by
165 * the caller to pass arbitrary data to the log functions. This
166 * pointer is only stored or passed on by libsigrok, and is
167 * never used or interpreted in any way. The pointer is allowed
168 * to be NULL if the caller doesn't need/want to pass any data.
170 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments.
174 SR_API int sr_log_callback_set(sr_log_callback cb, void *cb_data)
177 sr_err("log: %s: cb was NULL", __func__);
181 /* Note: 'cb_data' is allowed to be NULL. */
184 sr_log_cb_data = cb_data;
190 * Set the libsigrok log callback to the default built-in one.
192 * Additionally, the internal 'sr_log_cb_data' pointer is set to NULL.
194 * @return SR_OK upon success, a negative error code otherwise.
198 SR_API int sr_log_callback_set_default(void)
201 * Note: No log output in this function, as it should safely work
202 * even if the currently set log callback is buggy/broken.
205 sr_log_cb_data = NULL;
210 static int sr_logv(void *cb_data, int loglevel, const char *format, va_list args)
217 /* This specific log callback doesn't need the void pointer data. */
220 /* Only output messages of at least the selected loglevel(s). */
221 if (loglevel > cur_loglevel)
224 if (cur_loglevel >= SR_LOG_SPEW) {
225 elapsed = g_get_monotonic_time() - sr_log_start_time;
226 min = elapsed / G_TIME_SPAN_MINUTE;
227 sec = (elapsed % G_TIME_SPAN_MINUTE) / G_TIME_SPAN_SECOND;
228 usec = elapsed % G_TIME_SPAN_SECOND;
230 if (fprintf(stderr, "[%.2" PRIi64 ":%.2d.%.6d] ", min, sec, usec) < 0)
233 if (sr_log_domain[0] != '\0' && fputs(sr_log_domain, stderr) < 0)
235 if (vfprintf(stderr, format, args) < 0)
237 if (putc('\n', stderr) < 0)
244 SR_PRIV int sr_log(int loglevel, const char *format, ...)
249 va_start(args, format);
250 ret = sr_log_cb(sr_log_cb_data, loglevel, format, args);
257 SR_PRIV int sr_spew(const char *format, ...)
262 va_start(args, format);
263 ret = sr_log_cb(sr_log_cb_data, SR_LOG_SPEW, format, args);
270 SR_PRIV int sr_dbg(const char *format, ...)
275 va_start(args, format);
276 ret = sr_log_cb(sr_log_cb_data, SR_LOG_DBG, format, args);
283 SR_PRIV int sr_info(const char *format, ...)
288 va_start(args, format);
289 ret = sr_log_cb(sr_log_cb_data, SR_LOG_INFO, format, args);
296 SR_PRIV int sr_warn(const char *format, ...)
301 va_start(args, format);
302 ret = sr_log_cb(sr_log_cb_data, SR_LOG_WARN, format, args);
309 SR_PRIV int sr_err(const char *format, ...)
314 va_start(args, format);
315 ret = sr_log_cb(sr_log_cb_data, SR_LOG_ERR, format, args);