]> sigrok.org Git - libsigrok.git/blob - src/log.c
demo: Retain the default of 8 digital channels for now.
[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, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include <config.h>
21 #include <stdarg.h>
22 #include <stdio.h>
23 #include <glib/gprintf.h>
24 #include <libsigrok/libsigrok.h>
25 #include "libsigrok-internal.h"
26
27 #define LOG_PREFIX "log"
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 /** @cond PRIVATE */
60 #define LOGLEVEL_TIMESTAMP SR_LOG_DBG
61 /** @endcond */
62 static int64_t sr_log_start_time = 0;
63
64 /**
65  * Set the libsigrok loglevel.
66  *
67  * This influences the amount of log messages (debug messages, error messages,
68  * and so on) libsigrok will output. Using SR_LOG_NONE disables all messages.
69  *
70  * Note that this function itself will also output log messages. After the
71  * loglevel has changed, it will output a debug message with SR_LOG_DBG for
72  * example. Whether this message is shown depends on the (new) loglevel.
73  *
74  * @param loglevel The loglevel to set (SR_LOG_NONE, SR_LOG_ERR, SR_LOG_WARN,
75  *                 SR_LOG_INFO, SR_LOG_DBG, or SR_LOG_SPEW).
76  *
77  * @return SR_OK upon success, SR_ERR_ARG upon invalid loglevel.
78  *
79  * @since 0.1.0
80  */
81 SR_API int sr_log_loglevel_set(int loglevel)
82 {
83         if (loglevel < SR_LOG_NONE || loglevel > SR_LOG_SPEW) {
84                 sr_err("Invalid loglevel %d.", loglevel);
85                 return SR_ERR_ARG;
86         }
87         /* Output time stamps relative to time at startup */
88         if (loglevel >= LOGLEVEL_TIMESTAMP && sr_log_start_time == 0)
89                 sr_log_start_time = g_get_monotonic_time();
90
91         cur_loglevel = loglevel;
92
93         sr_dbg("libsigrok loglevel set to %d.", loglevel);
94
95         return SR_OK;
96 }
97
98 /**
99  * Get the libsigrok loglevel.
100  *
101  * @return The currently configured libsigrok loglevel.
102  *
103  * @since 0.1.0
104  */
105 SR_API int sr_log_loglevel_get(void)
106 {
107         return cur_loglevel;
108 }
109
110 /**
111  * Set the libsigrok log callback to the specified function.
112  *
113  * @param cb Function pointer to the log callback function to use.
114  *           Must not be NULL.
115  * @param cb_data Pointer to private data to be passed on. This can be used by
116  *                the caller to pass arbitrary data to the log functions. This
117  *                pointer is only stored or passed on by libsigrok, and is
118  *                never used or interpreted in any way. The pointer is allowed
119  *                to be NULL if the caller doesn't need/want to pass any data.
120  *
121  * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments.
122  *
123  * @since 0.3.0
124  */
125 SR_API int sr_log_callback_set(sr_log_callback cb, void *cb_data)
126 {
127         if (!cb) {
128                 sr_err("%s: cb was NULL", __func__);
129                 return SR_ERR_ARG;
130         }
131
132         /* Note: 'cb_data' is allowed to be NULL. */
133
134         sr_log_cb = cb;
135         sr_log_cb_data = cb_data;
136
137         return SR_OK;
138 }
139
140 /**
141  * Set the libsigrok log callback to the default built-in one.
142  *
143  * Additionally, the internal 'sr_log_cb_data' pointer is set to NULL.
144  *
145  * @return SR_OK upon success, a negative error code otherwise.
146  *
147  * @since 0.1.0
148  */
149 SR_API int sr_log_callback_set_default(void)
150 {
151         /*
152          * Note: No log output in this function, as it should safely work
153          * even if the currently set log callback is buggy/broken.
154          */
155         sr_log_cb = sr_logv;
156         sr_log_cb_data = NULL;
157
158         return SR_OK;
159 }
160
161 static int sr_logv(void *cb_data, int loglevel, const char *format, va_list args)
162 {
163         uint64_t elapsed_us, minutes;
164         unsigned int rest_us, seconds, microseconds;
165         char *raw_output, *output;
166         int raw_len, raw_idx, idx, ret;
167
168         /* This specific log callback doesn't need the void pointer data. */
169         (void)cb_data;
170
171         /* Only output messages of at least the selected loglevel(s). */
172         if (loglevel > cur_loglevel)
173                 return SR_OK;
174
175         if (cur_loglevel >= LOGLEVEL_TIMESTAMP) {
176                 elapsed_us = g_get_monotonic_time() - sr_log_start_time;
177
178                 minutes = elapsed_us / G_TIME_SPAN_MINUTE;
179                 rest_us = elapsed_us % G_TIME_SPAN_MINUTE;
180                 seconds = rest_us / G_TIME_SPAN_SECOND;
181                 microseconds = rest_us % G_TIME_SPAN_SECOND;
182
183                 ret = g_fprintf(stderr, "sr: [%.2" PRIu64 ":%.2u.%.6u] ",
184                                 minutes, seconds, microseconds);
185         } else {
186                 ret = fputs("sr: ", stderr);
187         }
188
189         if (ret < 0 || (raw_len = g_vasprintf(&raw_output, format, args)) < 0)
190                 return SR_ERR;
191
192         output = g_malloc0(raw_len + 1);
193
194         /* Copy the string without any unwanted newlines. */
195         raw_idx = idx = 0;
196         while (raw_idx < raw_len) {
197                 if (raw_output[raw_idx] != '\n') {
198                         output[idx] = raw_output[raw_idx];
199                         idx++;
200                 }
201                 raw_idx++;
202         }
203
204         g_fprintf(stderr, "%s\n", output);
205         g_free(raw_output);
206         g_free(output);
207
208         return SR_OK;
209 }
210
211 /** @private */
212 SR_PRIV int sr_log(int loglevel, const char *format, ...)
213 {
214         int ret;
215         va_list args;
216
217         va_start(args, format);
218         ret = sr_log_cb(sr_log_cb_data, loglevel, format, args);
219         va_end(args);
220
221         return ret;
222 }
223
224 /** @} */