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