]> sigrok.org Git - libsigrokdecode.git/blame - log.c
sr/srd: s/handler/callback/.
[libsigrokdecode.git] / log.c
CommitLineData
43c0a640
UH
1/*
2 * This file is part of the sigrok project.
3 *
66e4c27c 4 * Copyright (C) 2011-2012 Uwe Hermann <uwe@hermann-uwe.de>
43c0a640
UH
5 *
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
41106a07 21#include "sigrokdecode.h" /* First, so we avoid a _POSIX_C_SOURCE warning. */
43c0a640 22#include "sigrokdecode-internal.h"
bc5f5a43
BV
23#include <stdarg.h>
24#include <stdio.h>
43c0a640 25
66e4c27c 26/* Currently selected libsigrokdecode loglevel. Default: SRD_LOG_WARN. */
43c0a640
UH
27static int srd_loglevel = SRD_LOG_WARN; /* Show errors+warnings per default. */
28
66e4c27c 29/* Function prototype. */
41a5d962 30static int srd_logv(void *cb_data, int loglevel, const char *format,
e09023b9 31 va_list args);
66e4c27c 32
0082d592
UH
33/* Pointer to the currently selected log callback. Default: srd_logv(). */
34static srd_log_callback_t srd_log_callback = srd_logv;
66e4c27c
UH
35
36/*
0082d592 37 * Pointer to private data that can be passed to the log callback.
66e4c27c
UH
38 * This can be used (for example) by C++ GUIs to pass a "this" pointer.
39 */
0082d592 40static void *srd_log_callback_data = NULL;
66e4c27c 41
3a8b2e78
UH
42/* Log domain (a short string that is used as prefix for all messages). */
43#define LOGDOMAIN_MAXLEN 30
44#define LOGDOMAIN_DEFAULT "srd: "
45static char srd_log_domain[LOGDOMAIN_MAXLEN + 1] = LOGDOMAIN_DEFAULT;
46
43c0a640
UH
47/**
48 * Set the libsigrokdecode loglevel.
49 *
50 * This influences the amount of log messages (debug messages, error messages,
51 * and so on) libsigrokdecode will output. Using SRD_LOG_NONE disables all
52 * messages.
53 *
41106a07
UH
54 * Note that this function itself will also output log messages. After the
55 * loglevel has changed, it will output a debug message with SRD_LOG_DBG for
56 * example. Whether this message is shown depends on the (new) loglevel.
57 *
43c0a640
UH
58 * @param loglevel The loglevel to set (SRD_LOG_NONE, SRD_LOG_ERR,
59 * SRD_LOG_WARN, SRD_LOG_INFO, SRD_LOG_DBG, or SRD_LOG_SPEW).
582c8473 60 *
a62186e4 61 * @return SRD_OK upon success, SRD_ERR_ARG upon invalid loglevel.
43c0a640 62 */
55c3c5f4 63SRD_API int srd_log_loglevel_set(int loglevel)
43c0a640
UH
64{
65 if (loglevel < SRD_LOG_NONE || loglevel > SRD_LOG_SPEW) {
d906d3f9 66 srd_err("Invalid loglevel %d.", loglevel);
a62186e4 67 return SRD_ERR_ARG;
43c0a640
UH
68 }
69
70 srd_loglevel = loglevel;
71
7a1712c4 72 srd_dbg("libsigrokdecode loglevel set to %d.", loglevel);
43c0a640
UH
73
74 return SRD_OK;
75}
76
77/**
78 * Get the libsigrokdecode loglevel.
79 *
80 * @return The currently configured libsigrokdecode loglevel.
81 */
55c3c5f4 82SRD_API int srd_log_loglevel_get(void)
43c0a640
UH
83{
84 return srd_loglevel;
85}
86
3a8b2e78 87/**
41106a07 88 * Set the libsigrokdecode logdomain string.
3a8b2e78 89 *
41106a07
UH
90 * @param logdomain The string to use as logdomain for libsigrokdecode log
91 * messages from now on. Must not be NULL. The maximum
92 * length of the string is 30 characters (this does not
93 * include the trailing NUL-byte). Longer strings are
94 * silently truncated.
95 * In order to not use a logdomain, pass an empty string.
96 * The function makes its own copy of the input string, i.e.
97 * the caller does not need to keep it around.
582c8473 98 *
41106a07 99 * @return SRD_OK upon success, SRD_ERR_ARG upon invalid logdomain.
3a8b2e78 100 */
55c3c5f4 101SRD_API int srd_log_logdomain_set(const char *logdomain)
3a8b2e78
UH
102{
103 if (!logdomain) {
104 srd_err("log: %s: logdomain was NULL", __func__);
105 return SRD_ERR_ARG;
106 }
107
108 /* TODO: Error handling. */
109 snprintf((char *)&srd_log_domain, LOGDOMAIN_MAXLEN, "%s", logdomain);
110
41106a07 111 srd_dbg("Log domain set to '%s'.", (const char *)&srd_log_domain);
3a8b2e78
UH
112
113 return SRD_OK;
114}
115
116/**
41106a07 117 * Get the currently configured libsigrokdecode logdomain.
3a8b2e78 118 *
41106a07
UH
119 * @return A copy of the currently configured libsigrokdecode logdomain
120 * string. The caller is responsible for g_free()ing the string when
121 * it is no longer needed.
3a8b2e78 122 */
55c3c5f4 123SRD_API char *srd_log_logdomain_get(void)
3a8b2e78 124{
41106a07 125 return g_strdup((const char *)&srd_log_domain);
3a8b2e78
UH
126}
127
66e4c27c 128/**
0082d592 129 * Set the libsigrokdecode log callback to the specified function.
66e4c27c 130 *
0082d592
UH
131 * @param cb Function pointer to the log callback function to use.
132 * Must not be NULL.
41a5d962
UH
133 * @param cb_data Pointer to private data to be passed on. This can be used
134 * by the caller to pass arbitrary data to the log functions.
135 * This pointer is only stored or passed on by libsigrokdecode,
136 * and is never used or interpreted in any way. The pointer
137 * is allowed to be NULL if the caller doesn't need/want to
138 * pass any data.
582c8473 139 *
66e4c27c
UH
140 * @return SRD_OK upon success, SRD_ERR_ARG upon invalid arguments.
141 */
0082d592 142SRD_API int srd_log_callback_set(srd_log_callback_t cb, void *cb_data)
66e4c27c 143{
0082d592
UH
144 if (!cb) {
145 srd_err("log: %s: cb was NULL", __func__);
66e4c27c
UH
146 return SRD_ERR_ARG;
147 }
148
41a5d962 149 /* Note: 'cb_data' is allowed to be NULL. */
66e4c27c 150
0082d592
UH
151 srd_log_callback = cb;
152 srd_log_callback_data = cb_data;
66e4c27c
UH
153
154 return SRD_OK;
155}
156
157/**
0082d592 158 * Set the libsigrokdecode log callback to the default built-in one.
66e4c27c 159 *
0082d592 160 * Additionally, the internal 'srd_log_callback_data' pointer is set to NULL.
66e4c27c 161 *
582c8473 162 * @return SRD_OK upon success, a (negative) error code otherwise.
66e4c27c 163 */
0082d592 164SRD_API int srd_log_callback_set_default(void)
66e4c27c
UH
165{
166 /*
167 * Note: No log output in this function, as it should safely work
0082d592 168 * even if the currently set log callback is buggy/broken.
66e4c27c 169 */
0082d592
UH
170 srd_log_callback = srd_logv;
171 srd_log_callback_data = NULL;
66e4c27c
UH
172
173 return SRD_OK;
174}
175
41a5d962 176static int srd_logv(void *cb_data, int loglevel, const char *format,
e09023b9 177 va_list args)
43c0a640
UH
178{
179 int ret;
180
0082d592 181 /* This specific log callback doesn't need the void pointer data. */
41a5d962 182 (void)cb_data;
66e4c27c 183
43c0a640
UH
184 /* Only output messages of at least the selected loglevel(s). */
185 if (loglevel > srd_loglevel)
186 return SRD_OK; /* TODO? */
187
3a8b2e78 188 if (srd_log_domain[0] != '\0')
568112ce 189 fprintf(stderr, "%s", srd_log_domain);
43c0a640
UH
190 ret = vfprintf(stderr, format, args);
191 fprintf(stderr, "\n");
192
193 return ret;
194}
195
55c3c5f4 196SRD_PRIV int srd_log(int loglevel, const char *format, ...)
43c0a640
UH
197{
198 int ret;
199 va_list args;
200
201 va_start(args, format);
0082d592 202 ret = srd_log_callback(srd_log_callback_data, loglevel, format, args);
43c0a640
UH
203 va_end(args);
204
205 return ret;
206}
207
55c3c5f4 208SRD_PRIV int srd_spew(const char *format, ...)
43c0a640
UH
209{
210 int ret;
211 va_list args;
212
213 va_start(args, format);
0082d592
UH
214 ret = srd_log_callback(srd_log_callback_data, SRD_LOG_SPEW,
215 format, args);
43c0a640
UH
216 va_end(args);
217
218 return ret;
219}
220
55c3c5f4 221SRD_PRIV int srd_dbg(const char *format, ...)
43c0a640
UH
222{
223 int ret;
224 va_list args;
225
226 va_start(args, format);
0082d592
UH
227 ret = srd_log_callback(srd_log_callback_data, SRD_LOG_DBG,
228 format, args);
43c0a640
UH
229 va_end(args);
230
231 return ret;
232}
233
55c3c5f4 234SRD_PRIV int srd_info(const char *format, ...)
43c0a640
UH
235{
236 int ret;
237 va_list args;
238
239 va_start(args, format);
0082d592
UH
240 ret = srd_log_callback(srd_log_callback_data, SRD_LOG_INFO,
241 format, args);
43c0a640
UH
242 va_end(args);
243
244 return ret;
245}
246
55c3c5f4 247SRD_PRIV int srd_warn(const char *format, ...)
43c0a640
UH
248{
249 int ret;
250 va_list args;
251
252 va_start(args, format);
0082d592
UH
253 ret = srd_log_callback(srd_log_callback_data, SRD_LOG_WARN,
254 format, args);
43c0a640
UH
255 va_end(args);
256
257 return ret;
258}
259
55c3c5f4 260SRD_PRIV int srd_err(const char *format, ...)
43c0a640
UH
261{
262 int ret;
263 va_list args;
264
265 va_start(args, format);
0082d592
UH
266 ret = srd_log_callback(srd_log_callback_data, SRD_LOG_ERR,
267 format, args);
43c0a640
UH
268 va_end(args);
269
270 return ret;
271}