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