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