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;
42 * Set the libsigrokdecode loglevel.
44 * This influences the amount of log messages (debug messages, error messages,
45 * and so on) libsigrokdecode will output. Using SRD_LOG_NONE disables all
48 * @param loglevel The loglevel to set (SRD_LOG_NONE, SRD_LOG_ERR,
49 * SRD_LOG_WARN, SRD_LOG_INFO, SRD_LOG_DBG, or SRD_LOG_SPEW).
50 * @return SRD_OK upon success, SRD_ERR_ARG upon invalid loglevel.
52 int srd_set_loglevel(int loglevel)
54 if (loglevel < SRD_LOG_NONE || loglevel > SRD_LOG_SPEW) {
55 srd_err("Invalid loglevel %d.", loglevel);
59 srd_loglevel = loglevel;
61 srd_dbg("srd: loglevel set to %d", loglevel);
67 * Get the libsigrokdecode loglevel.
69 * @return The currently configured libsigrokdecode loglevel.
71 int srd_get_loglevel(void)
77 * Set the libsigrokdecode log handler to the specified function.
79 * @param handler Function pointer to the log handler function to use.
81 * @param data Pointer to private data to be passed on. This can be used by
82 * the caller pass arbitrary data to the log functions. This
83 * pointer is only stored or passed on by libsigrokdecode, and
84 * is never used or interpreted in any way.
85 * @return SRD_OK upon success, SRD_ERR_ARG upon invalid arguments.
87 int srd_log_set_handler(srd_log_handler_t handler, void *data)
90 srd_err("log: %s: handler was NULL", __func__);
94 /* Note: 'data' is allowed to be NULL. */
96 srd_handler = handler;
97 srd_handler_data = data;
103 * Set the libsigrokdecode log handler to the default built-in one.
105 * Additionally, the internal 'srd_handler_data' pointer is set to NULL.
107 * @return SRD_OK upon success, a negative error code otherwise.
109 int srd_log_set_default_handler(void)
112 * Note: No log output in this function, as it should safely work
113 * even if the currently set log handler is buggy/broken.
115 srd_handler = srd_logv;
116 srd_handler_data = NULL;
121 static int srd_logv(void *data, int loglevel, const char *format, va_list args)
125 /* This specific log handler doesn't need the void pointer data. */
128 /* Only output messages of at least the selected loglevel(s). */
129 if (loglevel > srd_loglevel)
130 return SRD_OK; /* TODO? */
132 ret = vfprintf(stderr, format, args);
133 fprintf(stderr, "\n");
138 int srd_log(int loglevel, const char *format, ...)
143 va_start(args, format);
144 ret = srd_handler(srd_handler_data, loglevel, format, args);
150 int srd_spew(const char *format, ...)
155 va_start(args, format);
156 ret = srd_handler(srd_handler_data, SRD_LOG_SPEW, format, args);
162 int srd_dbg(const char *format, ...)
167 va_start(args, format);
168 ret = srd_handler(srd_handler_data, SRD_LOG_DBG, format, args);
174 int srd_info(const char *format, ...)
179 va_start(args, format);
180 ret = srd_handler(srd_handler_data, SRD_LOG_INFO, format, args);
186 int srd_warn(const char *format, ...)
191 va_start(args, format);
192 ret = srd_handler(srd_handler_data, SRD_LOG_WARN, format, args);
198 int srd_err(const char *format, ...)
203 va_start(args, format);
204 ret = srd_handler(srd_handler_data, SRD_LOG_ERR, format, args);