libsigrokdecode
 All Data Structures Files Functions Variables Typedefs Enumerator Macros
log.c
Go to the documentation of this file.
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 #define LOGDOMAIN_MAXLEN 30
44 #define LOGDOMAIN_DEFAULT "srd: "
45 static char srd_log_domain[LOGDOMAIN_MAXLEN + 1] = LOGDOMAIN_DEFAULT;
46 
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  *
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  *
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).
60  *
61  * @return SRD_OK upon success, SRD_ERR_ARG upon invalid loglevel.
62  */
63 SRD_API int srd_log_loglevel_set(int loglevel)
64 {
65  if (loglevel < SRD_LOG_NONE || loglevel > SRD_LOG_SPEW) {
66  srd_err("Invalid loglevel %d.", loglevel);
67  return SRD_ERR_ARG;
68  }
69 
70  srd_loglevel = loglevel;
71 
72  srd_dbg("libsigrokdecode loglevel set to %d.", loglevel);
73 
74  return SRD_OK;
75 }
76 
77 /**
78  * Get the libsigrokdecode loglevel.
79  *
80  * @return The currently configured libsigrokdecode loglevel.
81  */
83 {
84  return srd_loglevel;
85 }
86 
87 /**
88  * Set the libsigrokdecode logdomain string.
89  *
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.
98  *
99  * @return SRD_OK upon success, SRD_ERR_ARG upon invalid logdomain.
100  */
101 SRD_API int srd_log_logdomain_set(const char *logdomain)
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 
111  srd_dbg("Log domain set to '%s'.", (const char *)&srd_log_domain);
112 
113  return SRD_OK;
114 }
115 
116 /**
117  * Get the currently configured libsigrokdecode logdomain.
118  *
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.
122  */
124 {
125  return g_strdup((const char *)&srd_log_domain);
126 }
127 
128 /**
129  * Set the libsigrokdecode log callback to the specified function.
130  *
131  * @param cb Function pointer to the log callback function to use.
132  * Must not be NULL.
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.
139  *
140  * @return SRD_OK upon success, SRD_ERR_ARG upon invalid arguments.
141  */
143 {
144  if (!cb) {
145  srd_err("log: %s: cb was NULL", __func__);
146  return SRD_ERR_ARG;
147  }
148 
149  /* Note: 'cb_data' is allowed to be NULL. */
150 
151  srd_log_callback = cb;
152  srd_log_callback_data = cb_data;
153 
154  return SRD_OK;
155 }
156 
157 /**
158  * Set the libsigrokdecode log callback to the default built-in one.
159  *
160  * Additionally, the internal 'srd_log_callback_data' pointer is set to NULL.
161  *
162  * @return SRD_OK upon success, a (negative) error code otherwise.
163  */
165 {
166  /*
167  * Note: No log output in this function, as it should safely work
168  * even if the currently set log callback is buggy/broken.
169  */
170  srd_log_callback = srd_logv;
171  srd_log_callback_data = NULL;
172 
173  return SRD_OK;
174 }
175 
176 static int srd_logv(void *cb_data, int loglevel, const char *format,
177  va_list args)
178 {
179  int ret;
180 
181  /* This specific log callback doesn't need the void pointer data. */
182  (void)cb_data;
183 
184  /* Only output messages of at least the selected loglevel(s). */
185  if (loglevel > srd_loglevel)
186  return SRD_OK; /* TODO? */
187 
188  if (srd_log_domain[0] != '\0')
189  fprintf(stderr, "%s", srd_log_domain);
190  ret = vfprintf(stderr, format, args);
191  fprintf(stderr, "\n");
192 
193  return ret;
194 }
195 
196 SRD_PRIV int srd_log(int loglevel, const char *format, ...)
197 {
198  int ret;
199  va_list args;
200 
201  va_start(args, format);
202  ret = srd_log_callback(srd_log_callback_data, loglevel, format, args);
203  va_end(args);
204 
205  return ret;
206 }
207 
208 SRD_PRIV int srd_spew(const char *format, ...)
209 {
210  int ret;
211  va_list args;
212 
213  va_start(args, format);
214  ret = srd_log_callback(srd_log_callback_data, SRD_LOG_SPEW,
215  format, args);
216  va_end(args);
217 
218  return ret;
219 }
220 
221 SRD_PRIV int srd_dbg(const char *format, ...)
222 {
223  int ret;
224  va_list args;
225 
226  va_start(args, format);
227  ret = srd_log_callback(srd_log_callback_data, SRD_LOG_DBG,
228  format, args);
229  va_end(args);
230 
231  return ret;
232 }
233 
234 SRD_PRIV int srd_info(const char *format, ...)
235 {
236  int ret;
237  va_list args;
238 
239  va_start(args, format);
240  ret = srd_log_callback(srd_log_callback_data, SRD_LOG_INFO,
241  format, args);
242  va_end(args);
243 
244  return ret;
245 }
246 
247 SRD_PRIV int srd_warn(const char *format, ...)
248 {
249  int ret;
250  va_list args;
251 
252  va_start(args, format);
253  ret = srd_log_callback(srd_log_callback_data, SRD_LOG_WARN,
254  format, args);
255  va_end(args);
256 
257  return ret;
258 }
259 
260 SRD_PRIV int srd_err(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_ERR,
267  format, args);
268  va_end(args);
269 
270  return ret;
271 }