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