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"
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 *data, int loglevel, const char *format, va_list args);
32 /* Pointer to the currently selected log handler. Default: srd_logv(). */
33 static srd_log_handler_t srd_handler = srd_logv;
36 * Pointer to private data that can be passed to the log handler.
37 * This can be used (for example) by C++ GUIs to pass a "this" pointer.
39 static void *srd_handler_data = NULL;
41 /* Log domain (a short string that is used as prefix for all messages). */
42 #define LOGDOMAIN_MAXLEN 30
43 #define LOGDOMAIN_DEFAULT "srd: "
44 static char srd_log_domain[LOGDOMAIN_MAXLEN + 1] = LOGDOMAIN_DEFAULT;
47 * Set the libsigrokdecode loglevel.
49 * This influences the amount of log messages (debug messages, error messages,
50 * and so on) libsigrokdecode will output. Using SRD_LOG_NONE disables all
53 * @param loglevel The loglevel to set (SRD_LOG_NONE, SRD_LOG_ERR,
54 * SRD_LOG_WARN, SRD_LOG_INFO, SRD_LOG_DBG, or SRD_LOG_SPEW).
55 * @return SRD_OK upon success, SRD_ERR_ARG upon invalid loglevel.
57 int srd_set_loglevel(int loglevel)
59 if (loglevel < SRD_LOG_NONE || loglevel > SRD_LOG_SPEW) {
60 srd_err("Invalid loglevel %d.", loglevel);
64 srd_loglevel = loglevel;
66 srd_dbg("libsigrokdecode loglevel set to %d.", loglevel);
72 * Get the libsigrokdecode loglevel.
74 * @return The currently configured libsigrokdecode loglevel.
76 int srd_get_loglevel(void)
84 * @param logdomain TODO
87 int srd_log_set_logdomain(const char *logdomain)
90 srd_err("log: %s: logdomain was NULL", __func__);
94 /* TODO: Error handling. */
95 snprintf((char *)&srd_log_domain, LOGDOMAIN_MAXLEN, "%s", logdomain);
97 srd_dbg("log domain set to '%s'", (const char *)&srd_log_domain);
107 char *srd_log_get_logdomain(void)
109 return g_strdup((char *)srd_log_domain);
113 * Set the libsigrokdecode log handler to the specified function.
115 * @param handler Function pointer to the log handler function to use.
117 * @param data Pointer to private data to be passed on. This can be used by
118 * the caller pass arbitrary data to the log functions. This
119 * pointer is only stored or passed on by libsigrokdecode, and
120 * is never used or interpreted in any way.
121 * @return SRD_OK upon success, SRD_ERR_ARG upon invalid arguments.
123 int srd_log_set_handler(srd_log_handler_t handler, void *data)
126 srd_err("log: %s: handler was NULL", __func__);
130 /* Note: 'data' is allowed to be NULL. */
132 srd_handler = handler;
133 srd_handler_data = data;
139 * Set the libsigrokdecode log handler to the default built-in one.
141 * Additionally, the internal 'srd_handler_data' pointer is set to NULL.
143 * @return SRD_OK upon success, a negative error code otherwise.
145 int srd_log_set_default_handler(void)
148 * Note: No log output in this function, as it should safely work
149 * even if the currently set log handler is buggy/broken.
151 srd_handler = srd_logv;
152 srd_handler_data = NULL;
157 static int srd_logv(void *data, int loglevel, const char *format, va_list args)
161 /* This specific log handler doesn't need the void pointer data. */
164 /* Only output messages of at least the selected loglevel(s). */
165 if (loglevel > srd_loglevel)
166 return SRD_OK; /* TODO? */
168 if (srd_log_domain[0] != '\0')
169 fprintf(stderr, "%s", srd_log_domain);
170 ret = vfprintf(stderr, format, args);
171 fprintf(stderr, "\n");
176 int srd_log(int loglevel, const char *format, ...)
181 va_start(args, format);
182 ret = srd_handler(srd_handler_data, loglevel, format, args);
188 int srd_spew(const char *format, ...)
193 va_start(args, format);
194 ret = srd_handler(srd_handler_data, SRD_LOG_SPEW, format, args);
200 int srd_dbg(const char *format, ...)
205 va_start(args, format);
206 ret = srd_handler(srd_handler_data, SRD_LOG_DBG, format, args);
212 int srd_info(const char *format, ...)
217 va_start(args, format);
218 ret = srd_handler(srd_handler_data, SRD_LOG_INFO, format, args);
224 int srd_warn(const char *format, ...)
229 va_start(args, format);
230 ret = srd_handler(srd_handler_data, SRD_LOG_WARN, format, args);
236 int srd_err(const char *format, ...)
241 va_start(args, format);
242 ret = srd_handler(srd_handler_data, SRD_LOG_ERR, format, args);