2 * This file is part of the sigrok 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
21 #include "sigrokdecode.h" /* First, so we avoid a _POSIX_C_SOURCE warning. */
22 #include "sigrokdecode-internal.h"
26 /* Currently selected libsigrokdecode loglevel. Default: SRD_LOG_WARN. */
27 static int srd_loglevel = SRD_LOG_WARN; /* Show errors+warnings per default. */
29 /* Function prototype. */
30 static int srd_logv(void *cb_data, int loglevel, const char *format,
33 /* Pointer to the currently selected log callback. Default: srd_logv(). */
34 static srd_log_callback_t srd_log_callback = srd_logv;
37 * Pointer to private data that can be passed to the log callback.
38 * This can be used (for example) by C++ GUIs to pass a "this" pointer.
40 static void *srd_log_callback_data = NULL;
42 /* Log domain (a short string that is used as prefix for all messages). */
44 #define LOGDOMAIN_MAXLEN 30
45 #define LOGDOMAIN_DEFAULT "srd: "
47 static char srd_log_domain[LOGDOMAIN_MAXLEN + 1] = LOGDOMAIN_DEFAULT;
50 * Set the libsigrokdecode loglevel.
52 * This influences the amount of log messages (debug messages, error messages,
53 * and so on) libsigrokdecode will output. Using SRD_LOG_NONE disables all
56 * Note that this function itself will also output log messages. After the
57 * loglevel has changed, it will output a debug message with SRD_LOG_DBG for
58 * example. Whether this message is shown depends on the (new) loglevel.
60 * @param loglevel The loglevel to set (SRD_LOG_NONE, SRD_LOG_ERR,
61 * SRD_LOG_WARN, SRD_LOG_INFO, SRD_LOG_DBG, or SRD_LOG_SPEW).
63 * @return SRD_OK upon success, SRD_ERR_ARG upon invalid loglevel.
65 SRD_API int srd_log_loglevel_set(int loglevel)
67 if (loglevel < SRD_LOG_NONE || loglevel > SRD_LOG_SPEW) {
68 srd_err("Invalid loglevel %d.", loglevel);
72 srd_loglevel = loglevel;
74 srd_dbg("libsigrokdecode loglevel set to %d.", loglevel);
80 * Get the libsigrokdecode loglevel.
82 * @return The currently configured libsigrokdecode loglevel.
84 SRD_API int srd_log_loglevel_get(void)
90 * Set the libsigrokdecode logdomain string.
92 * @param logdomain The string to use as logdomain for libsigrokdecode log
93 * messages from now on. Must not be NULL. The maximum
94 * length of the string is 30 characters (this does not
95 * include the trailing NUL-byte). Longer strings are
97 * In order to not use a logdomain, pass an empty string.
98 * The function makes its own copy of the input string, i.e.
99 * the caller does not need to keep it around.
101 * @return SRD_OK upon success, SRD_ERR_ARG upon invalid logdomain.
103 SRD_API int srd_log_logdomain_set(const char *logdomain)
106 srd_err("log: %s: logdomain was NULL", __func__);
110 /* TODO: Error handling. */
111 snprintf((char *)&srd_log_domain, LOGDOMAIN_MAXLEN, "%s", logdomain);
113 srd_dbg("Log domain set to '%s'.", (const char *)&srd_log_domain);
119 * Get the currently configured libsigrokdecode logdomain.
121 * @return A copy of the currently configured libsigrokdecode logdomain
122 * string. The caller is responsible for g_free()ing the string when
123 * it is no longer needed.
125 SRD_API char *srd_log_logdomain_get(void)
127 return g_strdup((const char *)&srd_log_domain);
131 * Set the libsigrokdecode log callback to the specified function.
133 * @param cb Function pointer to the log callback function to use.
135 * @param cb_data Pointer to private data to be passed on. This can be used
136 * by the caller to pass arbitrary data to the log functions.
137 * This pointer is only stored or passed on by libsigrokdecode,
138 * and is never used or interpreted in any way. The pointer
139 * is allowed to be NULL if the caller doesn't need/want to
142 * @return SRD_OK upon success, SRD_ERR_ARG upon invalid arguments.
144 SRD_API int srd_log_callback_set(srd_log_callback_t cb, void *cb_data)
147 srd_err("log: %s: cb was NULL", __func__);
151 /* Note: 'cb_data' is allowed to be NULL. */
153 srd_log_callback = cb;
154 srd_log_callback_data = cb_data;
160 * Set the libsigrokdecode log callback to the default built-in one.
162 * Additionally, the internal 'srd_log_callback_data' pointer is set to NULL.
164 * @return SRD_OK upon success, a (negative) error code otherwise.
166 SRD_API int srd_log_callback_set_default(void)
169 * Note: No log output in this function, as it should safely work
170 * even if the currently set log callback is buggy/broken.
172 srd_log_callback = srd_logv;
173 srd_log_callback_data = NULL;
178 static int srd_logv(void *cb_data, int loglevel, const char *format,
183 /* This specific log callback doesn't need the void pointer data. */
186 /* Only output messages of at least the selected loglevel(s). */
187 if (loglevel > srd_loglevel)
188 return SRD_OK; /* TODO? */
190 if (srd_log_domain[0] != '\0')
191 fprintf(stderr, "%s", srd_log_domain);
192 ret = vfprintf(stderr, format, args);
193 fprintf(stderr, "\n");
199 SRD_PRIV int srd_log(int loglevel, const char *format, ...)
204 va_start(args, format);
205 ret = srd_log_callback(srd_log_callback_data, loglevel, format, args);
212 SRD_PRIV int srd_spew(const char *format, ...)
217 va_start(args, format);
218 ret = srd_log_callback(srd_log_callback_data, SRD_LOG_SPEW,
226 SRD_PRIV int srd_dbg(const char *format, ...)
231 va_start(args, format);
232 ret = srd_log_callback(srd_log_callback_data, SRD_LOG_DBG,
240 SRD_PRIV int srd_info(const char *format, ...)
245 va_start(args, format);
246 ret = srd_log_callback(srd_log_callback_data, SRD_LOG_INFO,
254 SRD_PRIV int srd_warn(const char *format, ...)
259 va_start(args, format);
260 ret = srd_log_callback(srd_log_callback_data, SRD_LOG_WARN,
268 SRD_PRIV int srd_err(const char *format, ...)
273 va_start(args, format);
274 ret = srd_log_callback(srd_log_callback_data, SRD_LOG_ERR,