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