blob: 7b30497ee1226882b5e89ebe23bbefcf9198c596 [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
Uwe Hermann41106a02012-02-08 19:52:43 +010021#include "sigrokdecode.h" /* First, so we avoid a _POSIX_C_SOURCE warning. */
Uwe Hermann43c0a642011-12-21 18:57:04 +010022#include "sigrokdecode-internal.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 Hermann43c0a642011-12-21 18:57:04 +010041static int srd_loglevel = SRD_LOG_WARN; /* Show errors+warnings per default. */
42
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(). */
48static srd_log_callback_t srd_log_callback = 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 Hermann0082d592012-03-03 14:13:21 +010054static void *srd_log_callback_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 Hermann43c0a642011-12-21 18:57:04 +010078 */
Uwe Hermann55c3c5f2012-02-09 19:11:53 +010079SRD_API int srd_log_loglevel_set(int loglevel)
Uwe Hermann43c0a642011-12-21 18:57:04 +010080{
81 if (loglevel < SRD_LOG_NONE || loglevel > SRD_LOG_SPEW) {
Bert Vermeulend906d3f2012-01-22 02:51:49 +010082 srd_err("Invalid loglevel %d.", loglevel);
Uwe Hermanna62186e2011-12-21 19:09:46 +010083 return SRD_ERR_ARG;
Uwe Hermann43c0a642011-12-21 18:57:04 +010084 }
85
86 srd_loglevel = loglevel;
87
Uwe Hermann7a1712c2012-01-26 01:15:10 +010088 srd_dbg("libsigrokdecode loglevel set to %d.", loglevel);
Uwe Hermann43c0a642011-12-21 18:57:04 +010089
90 return SRD_OK;
91}
92
93/**
94 * Get the libsigrokdecode loglevel.
95 *
96 * @return The currently configured libsigrokdecode loglevel.
97 */
Uwe Hermann55c3c5f2012-02-09 19:11:53 +010098SRD_API int srd_log_loglevel_get(void)
Uwe Hermann43c0a642011-12-21 18:57:04 +010099{
100 return srd_loglevel;
101}
102
Uwe Hermann66e4c272012-01-25 02:52:27 +0100103/**
Uwe Hermann41106a02012-02-08 19:52:43 +0100104 * Set the libsigrokdecode logdomain string.
Uwe Hermann3a8b2e72012-01-25 11:00:04 +0100105 *
Uwe Hermann41106a02012-02-08 19:52:43 +0100106 * @param logdomain The string to use as logdomain for libsigrokdecode log
107 * messages from now on. Must not be NULL. The maximum
108 * length of the string is 30 characters (this does not
109 * include the trailing NUL-byte). Longer strings are
110 * silently truncated.
111 * In order to not use a logdomain, pass an empty string.
112 * The function makes its own copy of the input string, i.e.
113 * the caller does not need to keep it around.
Bert Vermeulen582c8472012-02-12 14:55:20 +0100114 *
Uwe Hermann41106a02012-02-08 19:52:43 +0100115 * @return SRD_OK upon success, SRD_ERR_ARG upon invalid logdomain.
Uwe Hermann3a8b2e72012-01-25 11:00:04 +0100116 */
Uwe Hermann55c3c5f2012-02-09 19:11:53 +0100117SRD_API int srd_log_logdomain_set(const char *logdomain)
Uwe Hermann3a8b2e72012-01-25 11:00:04 +0100118{
119 if (!logdomain) {
120 srd_err("log: %s: logdomain was NULL", __func__);
121 return SRD_ERR_ARG;
122 }
123
124 /* TODO: Error handling. */
125 snprintf((char *)&srd_log_domain, LOGDOMAIN_MAXLEN, "%s", logdomain);
126
Uwe Hermann41106a02012-02-08 19:52:43 +0100127 srd_dbg("Log domain set to '%s'.", (const char *)&srd_log_domain);
Uwe Hermann3a8b2e72012-01-25 11:00:04 +0100128
129 return SRD_OK;
130}
131
132/**
Uwe Hermann41106a02012-02-08 19:52:43 +0100133 * Get the currently configured libsigrokdecode logdomain.
Uwe Hermann3a8b2e72012-01-25 11:00:04 +0100134 *
Uwe Hermann41106a02012-02-08 19:52:43 +0100135 * @return A copy of the currently configured libsigrokdecode logdomain
136 * string. The caller is responsible for g_free()ing the string when
137 * it is no longer needed.
Uwe Hermann3a8b2e72012-01-25 11:00:04 +0100138 */
Uwe Hermann55c3c5f2012-02-09 19:11:53 +0100139SRD_API char *srd_log_logdomain_get(void)
Uwe Hermann3a8b2e72012-01-25 11:00:04 +0100140{
Uwe Hermann41106a02012-02-08 19:52:43 +0100141 return g_strdup((const char *)&srd_log_domain);
Uwe Hermann3a8b2e72012-01-25 11:00:04 +0100142}
143
144/**
Uwe Hermann0082d592012-03-03 14:13:21 +0100145 * Set the libsigrokdecode log callback to the specified function.
Uwe Hermann66e4c272012-01-25 02:52:27 +0100146 *
Uwe Hermann0082d592012-03-03 14:13:21 +0100147 * @param cb Function pointer to the log callback function to use.
148 * Must not be NULL.
Uwe Hermann41a5d962012-02-29 22:32:34 +0100149 * @param cb_data Pointer to private data to be passed on. This can be used
150 * by the caller to pass arbitrary data to the log functions.
151 * This pointer is only stored or passed on by libsigrokdecode,
152 * and is never used or interpreted in any way. The pointer
153 * is allowed to be NULL if the caller doesn't need/want to
154 * pass any data.
Bert Vermeulen582c8472012-02-12 14:55:20 +0100155 *
Uwe Hermann66e4c272012-01-25 02:52:27 +0100156 * @return SRD_OK upon success, SRD_ERR_ARG upon invalid arguments.
157 */
Uwe Hermann0082d592012-03-03 14:13:21 +0100158SRD_API int srd_log_callback_set(srd_log_callback_t cb, void *cb_data)
Uwe Hermann66e4c272012-01-25 02:52:27 +0100159{
Uwe Hermann0082d592012-03-03 14:13:21 +0100160 if (!cb) {
161 srd_err("log: %s: cb was NULL", __func__);
Uwe Hermann66e4c272012-01-25 02:52:27 +0100162 return SRD_ERR_ARG;
163 }
164
Uwe Hermann41a5d962012-02-29 22:32:34 +0100165 /* Note: 'cb_data' is allowed to be NULL. */
Uwe Hermann66e4c272012-01-25 02:52:27 +0100166
Uwe Hermann0082d592012-03-03 14:13:21 +0100167 srd_log_callback = cb;
168 srd_log_callback_data = cb_data;
Uwe Hermann66e4c272012-01-25 02:52:27 +0100169
170 return SRD_OK;
171}
172
173/**
Uwe Hermann0082d592012-03-03 14:13:21 +0100174 * Set the libsigrokdecode log callback to the default built-in one.
Uwe Hermann66e4c272012-01-25 02:52:27 +0100175 *
Uwe Hermann0082d592012-03-03 14:13:21 +0100176 * Additionally, the internal 'srd_log_callback_data' pointer is set to NULL.
Uwe Hermann66e4c272012-01-25 02:52:27 +0100177 *
Bert Vermeulen582c8472012-02-12 14:55:20 +0100178 * @return SRD_OK upon success, a (negative) error code otherwise.
Uwe Hermann66e4c272012-01-25 02:52:27 +0100179 */
Uwe Hermann0082d592012-03-03 14:13:21 +0100180SRD_API int srd_log_callback_set_default(void)
Uwe Hermann66e4c272012-01-25 02:52:27 +0100181{
182 /*
183 * Note: No log output in this function, as it should safely work
Uwe Hermann0082d592012-03-03 14:13:21 +0100184 * even if the currently set log callback is buggy/broken.
Uwe Hermann66e4c272012-01-25 02:52:27 +0100185 */
Uwe Hermann0082d592012-03-03 14:13:21 +0100186 srd_log_callback = srd_logv;
187 srd_log_callback_data = NULL;
Uwe Hermann66e4c272012-01-25 02:52:27 +0100188
189 return SRD_OK;
190}
191
Uwe Hermann41a5d962012-02-29 22:32:34 +0100192static int srd_logv(void *cb_data, int loglevel, const char *format,
Uwe Hermanne09023b2012-02-11 22:09:18 +0100193 va_list args)
Uwe Hermann43c0a642011-12-21 18:57:04 +0100194{
195 int ret;
196
Uwe Hermann0082d592012-03-03 14:13:21 +0100197 /* This specific log callback doesn't need the void pointer data. */
Uwe Hermann41a5d962012-02-29 22:32:34 +0100198 (void)cb_data;
Uwe Hermann66e4c272012-01-25 02:52:27 +0100199
Uwe Hermann43c0a642011-12-21 18:57:04 +0100200 /* Only output messages of at least the selected loglevel(s). */
201 if (loglevel > srd_loglevel)
202 return SRD_OK; /* TODO? */
203
Uwe Hermann3a8b2e72012-01-25 11:00:04 +0100204 if (srd_log_domain[0] != '\0')
Bert Vermeulen568112c2012-01-27 01:14:01 +0100205 fprintf(stderr, "%s", srd_log_domain);
Uwe Hermann43c0a642011-12-21 18:57:04 +0100206 ret = vfprintf(stderr, format, args);
207 fprintf(stderr, "\n");
208
209 return ret;
210}
211
Uwe Hermann57790bc2013-02-09 21:44:11 +0100212/** @private */
Uwe Hermann55c3c5f2012-02-09 19:11:53 +0100213SRD_PRIV int srd_log(int loglevel, const char *format, ...)
Uwe Hermann43c0a642011-12-21 18:57:04 +0100214{
215 int ret;
216 va_list args;
217
218 va_start(args, format);
Uwe Hermann0082d592012-03-03 14:13:21 +0100219 ret = srd_log_callback(srd_log_callback_data, loglevel, format, args);
Uwe Hermann43c0a642011-12-21 18:57:04 +0100220 va_end(args);
221
222 return ret;
223}
224
Uwe Hermann57790bc2013-02-09 21:44:11 +0100225/** @private */
Uwe Hermann55c3c5f2012-02-09 19:11:53 +0100226SRD_PRIV int srd_spew(const char *format, ...)
Uwe Hermann43c0a642011-12-21 18:57:04 +0100227{
228 int ret;
229 va_list args;
230
231 va_start(args, format);
Uwe Hermann0082d592012-03-03 14:13:21 +0100232 ret = srd_log_callback(srd_log_callback_data, SRD_LOG_SPEW,
233 format, args);
Uwe Hermann43c0a642011-12-21 18:57:04 +0100234 va_end(args);
235
236 return ret;
237}
238
Uwe Hermann57790bc2013-02-09 21:44:11 +0100239/** @private */
Uwe Hermann55c3c5f2012-02-09 19:11:53 +0100240SRD_PRIV int srd_dbg(const char *format, ...)
Uwe Hermann43c0a642011-12-21 18:57:04 +0100241{
242 int ret;
243 va_list args;
244
245 va_start(args, format);
Uwe Hermann0082d592012-03-03 14:13:21 +0100246 ret = srd_log_callback(srd_log_callback_data, SRD_LOG_DBG,
247 format, args);
Uwe Hermann43c0a642011-12-21 18:57:04 +0100248 va_end(args);
249
250 return ret;
251}
252
Uwe Hermann57790bc2013-02-09 21:44:11 +0100253/** @private */
Uwe Hermann55c3c5f2012-02-09 19:11:53 +0100254SRD_PRIV int srd_info(const char *format, ...)
Uwe Hermann43c0a642011-12-21 18:57:04 +0100255{
256 int ret;
257 va_list args;
258
259 va_start(args, format);
Uwe Hermann0082d592012-03-03 14:13:21 +0100260 ret = srd_log_callback(srd_log_callback_data, SRD_LOG_INFO,
261 format, args);
Uwe Hermann43c0a642011-12-21 18:57:04 +0100262 va_end(args);
263
264 return ret;
265}
266
Uwe Hermann57790bc2013-02-09 21:44:11 +0100267/** @private */
Uwe Hermann55c3c5f2012-02-09 19:11:53 +0100268SRD_PRIV int srd_warn(const char *format, ...)
Uwe Hermann43c0a642011-12-21 18:57:04 +0100269{
270 int ret;
271 va_list args;
272
273 va_start(args, format);
Uwe Hermann0082d592012-03-03 14:13:21 +0100274 ret = srd_log_callback(srd_log_callback_data, SRD_LOG_WARN,
275 format, args);
Uwe Hermann43c0a642011-12-21 18:57:04 +0100276 va_end(args);
277
278 return ret;
279}
280
Uwe Hermann57790bc2013-02-09 21:44:11 +0100281/** @private */
Uwe Hermann55c3c5f2012-02-09 19:11:53 +0100282SRD_PRIV int srd_err(const char *format, ...)
Uwe Hermann43c0a642011-12-21 18:57:04 +0100283{
284 int ret;
285 va_list args;
286
287 va_start(args, format);
Uwe Hermann0082d592012-03-03 14:13:21 +0100288 ret = srd_log_callback(srd_log_callback_data, SRD_LOG_ERR,
289 format, args);
Uwe Hermann43c0a642011-12-21 18:57:04 +0100290 va_end(args);
291
292 return ret;
293}
Uwe Hermann48954182013-02-09 22:25:15 +0100294
295/** @} */