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