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