blob: 9d0bc73d5fb18891d12222c0d4e15f922d998533 [file] [log] [blame]
Uwe Hermann43c0a642011-12-21 18:57:04 +01001/*
Uwe Hermann50bd5d22013-04-23 22:27:20 +02002 * This file is part of the libsigrokdecode project.
Uwe Hermann43c0a642011-12-21 18:57:04 +01003 *
Uwe Hermann66e4c272012-01-25 02:52:27 +01004 * Copyright (C) 2011-2012 Uwe Hermann <uwe@hermann-uwe.de>
Uwe Hermann43c0a642011-12-21 18:57:04 +01005 *
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.
10 *
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.
15 *
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
19 */
20
Marcus Comstedtf6c7ead2014-07-01 00:07:40 +020021#include "libsigrokdecode-internal.h" /* First, so we avoid a _POSIX_C_SOURCE warning. */
22#include "libsigrokdecode.h"
Bert Vermeulenbc5f5a42012-01-05 03:31:36 +010023#include <stdarg.h>
24#include <stdio.h>
Uwe Hermann43c0a642011-12-21 18:57:04 +010025
Uwe Hermann54fdeee2013-02-09 22:10:57 +010026/**
27 * @file
28 *
29 * Controlling the libsigrokdecode message logging functionality.
30 */
31
Uwe Hermann48954182013-02-09 22:25:15 +010032/**
33 * @defgroup grp_logging Logging
34 *
35 * Controlling the libsigrokdecode message logging functionality.
36 *
37 * @{
38 */
39
Uwe Hermann66e4c272012-01-25 02:52:27 +010040/* Currently selected libsigrokdecode loglevel. Default: SRD_LOG_WARN. */
Uwe Hermannaa6506b2014-05-04 20:56:32 +020041static int cur_loglevel = SRD_LOG_WARN; /* Show errors+warnings per default. */
Uwe Hermann43c0a642011-12-21 18:57:04 +010042
Uwe Hermann66e4c272012-01-25 02:52:27 +010043/* Function prototype. */
Uwe Hermann41a5d962012-02-29 22:32:34 +010044static int srd_logv(void *cb_data, int loglevel, const char *format,
Uwe Hermanne09023b2012-02-11 22:09:18 +010045 va_list args);
Uwe Hermann66e4c272012-01-25 02:52:27 +010046
Uwe Hermann0082d592012-03-03 14:13:21 +010047/* Pointer to the currently selected log callback. Default: srd_logv(). */
Uwe Hermann2372b192014-05-03 22:45:50 +020048static srd_log_callback srd_log_cb = srd_logv;
Uwe Hermann66e4c272012-01-25 02:52:27 +010049
50/*
Uwe Hermann0082d592012-03-03 14:13:21 +010051 * Pointer to private data that can be passed to the log callback.
Uwe Hermann66e4c272012-01-25 02:52:27 +010052 * This can be used (for example) by C++ GUIs to pass a "this" pointer.
53 */
Uwe Hermann2372b192014-05-03 22:45:50 +020054static void *srd_log_cb_data = NULL;
Uwe Hermann66e4c272012-01-25 02:52:27 +010055
Uwe Hermann3a8b2e72012-01-25 11:00:04 +010056/* Log domain (a short string that is used as prefix for all messages). */
Uwe Hermann57790bc2013-02-09 21:44:11 +010057/** @cond PRIVATE */
Uwe Hermann3a8b2e72012-01-25 11:00:04 +010058#define LOGDOMAIN_MAXLEN 30
59#define LOGDOMAIN_DEFAULT "srd: "
Uwe Hermann57790bc2013-02-09 21:44:11 +010060/** @endcond */
Uwe Hermann3a8b2e72012-01-25 11:00:04 +010061static char srd_log_domain[LOGDOMAIN_MAXLEN + 1] = LOGDOMAIN_DEFAULT;
62
Uwe Hermann43c0a642011-12-21 18:57:04 +010063/**
64 * Set the libsigrokdecode loglevel.
65 *
66 * This influences the amount of log messages (debug messages, error messages,
67 * and so on) libsigrokdecode will output. Using SRD_LOG_NONE disables all
68 * messages.
69 *
Uwe Hermann41106a02012-02-08 19:52:43 +010070 * Note that this function itself will also output log messages. After the
71 * loglevel has changed, it will output a debug message with SRD_LOG_DBG for
72 * example. Whether this message is shown depends on the (new) loglevel.
73 *
Uwe Hermann43c0a642011-12-21 18:57:04 +010074 * @param loglevel The loglevel to set (SRD_LOG_NONE, SRD_LOG_ERR,
75 * SRD_LOG_WARN, SRD_LOG_INFO, SRD_LOG_DBG, or SRD_LOG_SPEW).
Bert Vermeulen582c8472012-02-12 14:55:20 +010076 *
Uwe Hermanna62186e2011-12-21 19:09:46 +010077 * @return SRD_OK upon success, SRD_ERR_ARG upon invalid loglevel.
Uwe Hermann8c664ca2013-05-03 14:45:49 +020078 *
79 * @since 0.1.0
Uwe Hermann43c0a642011-12-21 18:57:04 +010080 */
Uwe Hermann55c3c5f2012-02-09 19:11:53 +010081SRD_API int srd_log_loglevel_set(int loglevel)
Uwe Hermann43c0a642011-12-21 18:57:04 +010082{
83 if (loglevel < SRD_LOG_NONE || loglevel > SRD_LOG_SPEW) {
Bert Vermeulend906d3f2012-01-22 02:51:49 +010084 srd_err("Invalid loglevel %d.", loglevel);
Uwe Hermanna62186e2011-12-21 19:09:46 +010085 return SRD_ERR_ARG;
Uwe Hermann43c0a642011-12-21 18:57:04 +010086 }
87
Uwe Hermannaa6506b2014-05-04 20:56:32 +020088 cur_loglevel = loglevel;
Uwe Hermann43c0a642011-12-21 18:57:04 +010089
Uwe Hermann7a1712c2012-01-26 01:15:10 +010090 srd_dbg("libsigrokdecode loglevel set to %d.", loglevel);
Uwe Hermann43c0a642011-12-21 18:57:04 +010091
92 return SRD_OK;
93}
94
95/**
96 * Get the libsigrokdecode loglevel.
97 *
98 * @return The currently configured libsigrokdecode loglevel.
Uwe Hermann8c664ca2013-05-03 14:45:49 +020099 *
100 * @since 0.1.0
Uwe Hermann43c0a642011-12-21 18:57:04 +0100101 */
Uwe Hermann55c3c5f2012-02-09 19:11:53 +0100102SRD_API int srd_log_loglevel_get(void)
Uwe Hermann43c0a642011-12-21 18:57:04 +0100103{
Uwe Hermannaa6506b2014-05-04 20:56:32 +0200104 return cur_loglevel;
Uwe Hermann43c0a642011-12-21 18:57:04 +0100105}
106
Uwe Hermann66e4c272012-01-25 02:52:27 +0100107/**
Uwe Hermann41106a02012-02-08 19:52:43 +0100108 * Set the libsigrokdecode logdomain string.
Uwe Hermann3a8b2e72012-01-25 11:00:04 +0100109 *
Uwe Hermann41106a02012-02-08 19:52:43 +0100110 * @param logdomain The string to use as logdomain for libsigrokdecode log
111 * messages from now on. Must not be NULL. The maximum
112 * length of the string is 30 characters (this does not
113 * include the trailing NUL-byte). Longer strings are
114 * silently truncated.
115 * In order to not use a logdomain, pass an empty string.
116 * The function makes its own copy of the input string, i.e.
117 * the caller does not need to keep it around.
Bert Vermeulen582c8472012-02-12 14:55:20 +0100118 *
Uwe Hermann41106a02012-02-08 19:52:43 +0100119 * @return SRD_OK upon success, SRD_ERR_ARG upon invalid logdomain.
Uwe Hermann8c664ca2013-05-03 14:45:49 +0200120 *
121 * @since 0.1.0
Uwe Hermann3a8b2e72012-01-25 11:00:04 +0100122 */
Uwe Hermann55c3c5f2012-02-09 19:11:53 +0100123SRD_API int srd_log_logdomain_set(const char *logdomain)
Uwe Hermann3a8b2e72012-01-25 11:00:04 +0100124{
125 if (!logdomain) {
126 srd_err("log: %s: logdomain was NULL", __func__);
127 return SRD_ERR_ARG;
128 }
129
Uwe Hermann3a8b2e72012-01-25 11:00:04 +0100130 snprintf((char *)&srd_log_domain, LOGDOMAIN_MAXLEN, "%s", logdomain);
131
Uwe Hermann41106a02012-02-08 19:52:43 +0100132 srd_dbg("Log domain set to '%s'.", (const char *)&srd_log_domain);
Uwe Hermann3a8b2e72012-01-25 11:00:04 +0100133
134 return SRD_OK;
135}
136
137/**
Uwe Hermann41106a02012-02-08 19:52:43 +0100138 * Get the currently configured libsigrokdecode logdomain.
Uwe Hermann3a8b2e72012-01-25 11:00:04 +0100139 *
Uwe Hermann41106a02012-02-08 19:52:43 +0100140 * @return A copy of the currently configured libsigrokdecode logdomain
141 * string. The caller is responsible for g_free()ing the string when
142 * it is no longer needed.
Uwe Hermann8c664ca2013-05-03 14:45:49 +0200143 *
144 * @since 0.1.0
Uwe Hermann3a8b2e72012-01-25 11:00:04 +0100145 */
Uwe Hermann55c3c5f2012-02-09 19:11:53 +0100146SRD_API char *srd_log_logdomain_get(void)
Uwe Hermann3a8b2e72012-01-25 11:00:04 +0100147{
Uwe Hermann41106a02012-02-08 19:52:43 +0100148 return g_strdup((const char *)&srd_log_domain);
Uwe Hermann3a8b2e72012-01-25 11:00:04 +0100149}
150
151/**
Uwe Hermann0082d592012-03-03 14:13:21 +0100152 * Set the libsigrokdecode log callback to the specified function.
Uwe Hermann66e4c272012-01-25 02:52:27 +0100153 *
Uwe Hermann0082d592012-03-03 14:13:21 +0100154 * @param cb Function pointer to the log callback function to use.
155 * Must not be NULL.
Uwe Hermann41a5d962012-02-29 22:32:34 +0100156 * @param cb_data Pointer to private data to be passed on. This can be used
157 * by the caller to pass arbitrary data to the log functions.
158 * This pointer is only stored or passed on by libsigrokdecode,
159 * and is never used or interpreted in any way. The pointer
160 * is allowed to be NULL if the caller doesn't need/want to
161 * pass any data.
Bert Vermeulen582c8472012-02-12 14:55:20 +0100162 *
Uwe Hermann66e4c272012-01-25 02:52:27 +0100163 * @return SRD_OK upon success, SRD_ERR_ARG upon invalid arguments.
Uwe Hermann8c664ca2013-05-03 14:45:49 +0200164 *
Uwe Hermannc57d1012014-05-04 20:25:17 +0200165 * @since 0.3.0
Uwe Hermann66e4c272012-01-25 02:52:27 +0100166 */
Uwe Hermann2372b192014-05-03 22:45:50 +0200167SRD_API int srd_log_callback_set(srd_log_callback cb, void *cb_data)
Uwe Hermann66e4c272012-01-25 02:52:27 +0100168{
Uwe Hermann0082d592012-03-03 14:13:21 +0100169 if (!cb) {
170 srd_err("log: %s: cb was NULL", __func__);
Uwe Hermann66e4c272012-01-25 02:52:27 +0100171 return SRD_ERR_ARG;
172 }
173
Uwe Hermann41a5d962012-02-29 22:32:34 +0100174 /* Note: 'cb_data' is allowed to be NULL. */
Uwe Hermann66e4c272012-01-25 02:52:27 +0100175
Uwe Hermann2372b192014-05-03 22:45:50 +0200176 srd_log_cb = cb;
177 srd_log_cb_data = cb_data;
Uwe Hermann66e4c272012-01-25 02:52:27 +0100178
179 return SRD_OK;
180}
181
182/**
Uwe Hermann0082d592012-03-03 14:13:21 +0100183 * Set the libsigrokdecode log callback to the default built-in one.
Uwe Hermann66e4c272012-01-25 02:52:27 +0100184 *
Uwe Hermann2372b192014-05-03 22:45:50 +0200185 * Additionally, the internal 'srd_log_cb_data' pointer is set to NULL.
Uwe Hermann66e4c272012-01-25 02:52:27 +0100186 *
Bert Vermeulen582c8472012-02-12 14:55:20 +0100187 * @return SRD_OK upon success, a (negative) error code otherwise.
Uwe Hermann8c664ca2013-05-03 14:45:49 +0200188 *
189 * @since 0.1.0
Uwe Hermann66e4c272012-01-25 02:52:27 +0100190 */
Uwe Hermann0082d592012-03-03 14:13:21 +0100191SRD_API int srd_log_callback_set_default(void)
Uwe Hermann66e4c272012-01-25 02:52:27 +0100192{
193 /*
194 * Note: No log output in this function, as it should safely work
Uwe Hermann0082d592012-03-03 14:13:21 +0100195 * even if the currently set log callback is buggy/broken.
Uwe Hermann66e4c272012-01-25 02:52:27 +0100196 */
Uwe Hermann2372b192014-05-03 22:45:50 +0200197 srd_log_cb = srd_logv;
198 srd_log_cb_data = NULL;
Uwe Hermann66e4c272012-01-25 02:52:27 +0100199
200 return SRD_OK;
201}
202
Uwe Hermann41a5d962012-02-29 22:32:34 +0100203static int srd_logv(void *cb_data, int loglevel, const char *format,
Uwe Hermanne09023b2012-02-11 22:09:18 +0100204 va_list args)
Uwe Hermann43c0a642011-12-21 18:57:04 +0100205{
206 int ret;
207
Uwe Hermann0082d592012-03-03 14:13:21 +0100208 /* This specific log callback doesn't need the void pointer data. */
Uwe Hermann41a5d962012-02-29 22:32:34 +0100209 (void)cb_data;
Uwe Hermann66e4c272012-01-25 02:52:27 +0100210
Uwe Hermann43c0a642011-12-21 18:57:04 +0100211 /* Only output messages of at least the selected loglevel(s). */
Uwe Hermannaa6506b2014-05-04 20:56:32 +0200212 if (loglevel > cur_loglevel)
Bert Vermeulen1f2e00d2013-09-11 12:35:17 +0200213 return SRD_OK;
Uwe Hermann43c0a642011-12-21 18:57:04 +0100214
Uwe Hermann3a8b2e72012-01-25 11:00:04 +0100215 if (srd_log_domain[0] != '\0')
Bert Vermeulen568112c2012-01-27 01:14:01 +0100216 fprintf(stderr, "%s", srd_log_domain);
Uwe Hermann43c0a642011-12-21 18:57:04 +0100217 ret = vfprintf(stderr, format, args);
218 fprintf(stderr, "\n");
219
220 return ret;
221}
222
Uwe Hermann57790bc2013-02-09 21:44:11 +0100223/** @private */
Uwe Hermann55c3c5f2012-02-09 19:11:53 +0100224SRD_PRIV int srd_log(int loglevel, const char *format, ...)
Uwe Hermann43c0a642011-12-21 18:57:04 +0100225{
226 int ret;
227 va_list args;
228
229 va_start(args, format);
Uwe Hermann2372b192014-05-03 22:45:50 +0200230 ret = srd_log_cb(srd_log_cb_data, loglevel, format, args);
Uwe Hermann43c0a642011-12-21 18:57:04 +0100231 va_end(args);
232
233 return ret;
234}
235
Uwe Hermann57790bc2013-02-09 21:44:11 +0100236/** @private */
Uwe Hermann55c3c5f2012-02-09 19:11:53 +0100237SRD_PRIV int srd_spew(const char *format, ...)
Uwe Hermann43c0a642011-12-21 18:57:04 +0100238{
239 int ret;
240 va_list args;
241
242 va_start(args, format);
Uwe Hermann2372b192014-05-03 22:45:50 +0200243 ret = srd_log_cb(srd_log_cb_data, SRD_LOG_SPEW, format, args);
Uwe Hermann43c0a642011-12-21 18:57:04 +0100244 va_end(args);
245
246 return ret;
247}
248
Uwe Hermann57790bc2013-02-09 21:44:11 +0100249/** @private */
Uwe Hermann55c3c5f2012-02-09 19:11:53 +0100250SRD_PRIV int srd_dbg(const char *format, ...)
Uwe Hermann43c0a642011-12-21 18:57:04 +0100251{
252 int ret;
253 va_list args;
254
255 va_start(args, format);
Uwe Hermann2372b192014-05-03 22:45:50 +0200256 ret = srd_log_cb(srd_log_cb_data, SRD_LOG_DBG, format, args);
Uwe Hermann43c0a642011-12-21 18:57:04 +0100257 va_end(args);
258
259 return ret;
260}
261
Uwe Hermann57790bc2013-02-09 21:44:11 +0100262/** @private */
Uwe Hermann55c3c5f2012-02-09 19:11:53 +0100263SRD_PRIV int srd_info(const char *format, ...)
Uwe Hermann43c0a642011-12-21 18:57:04 +0100264{
265 int ret;
266 va_list args;
267
268 va_start(args, format);
Uwe Hermann2372b192014-05-03 22:45:50 +0200269 ret = srd_log_cb(srd_log_cb_data, SRD_LOG_INFO, format, args);
Uwe Hermann43c0a642011-12-21 18:57:04 +0100270 va_end(args);
271
272 return ret;
273}
274
Uwe Hermann57790bc2013-02-09 21:44:11 +0100275/** @private */
Uwe Hermann55c3c5f2012-02-09 19:11:53 +0100276SRD_PRIV int srd_warn(const char *format, ...)
Uwe Hermann43c0a642011-12-21 18:57:04 +0100277{
278 int ret;
279 va_list args;
280
281 va_start(args, format);
Uwe Hermann2372b192014-05-03 22:45:50 +0200282 ret = srd_log_cb(srd_log_cb_data, SRD_LOG_WARN, format, args);
Uwe Hermann43c0a642011-12-21 18:57:04 +0100283 va_end(args);
284
285 return ret;
286}
287
Uwe Hermann57790bc2013-02-09 21:44:11 +0100288/** @private */
Uwe Hermann55c3c5f2012-02-09 19:11:53 +0100289SRD_PRIV int srd_err(const char *format, ...)
Uwe Hermann43c0a642011-12-21 18:57:04 +0100290{
291 int ret;
292 va_list args;
293
294 va_start(args, format);
Uwe Hermann2372b192014-05-03 22:45:50 +0200295 ret = srd_log_cb(srd_log_cb_data, SRD_LOG_ERR, format, args);
Uwe Hermann43c0a642011-12-21 18:57:04 +0100296 va_end(args);
297
298 return ret;
299}
Uwe Hermann48954182013-02-09 22:25:15 +0100300
301/** @} */