]>
Commit | Line | Data |
---|---|---|
b08024a8 | 1 | /* |
50985c20 | 2 | * This file is part of the libsigrok project. |
b08024a8 | 3 | * |
0ae67ff7 | 4 | * Copyright (C) 2011-2012 Uwe Hermann <uwe@hermann-uwe.de> |
b08024a8 UH |
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 | |
2ea1fdf1 | 17 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
b08024a8 UH |
18 | */ |
19 | ||
6ec6c43b | 20 | #include <config.h> |
b08024a8 UH |
21 | #include <stdarg.h> |
22 | #include <stdio.h> | |
7419638d | 23 | #include <glib/gprintf.h> |
c1aae900 | 24 | #include <libsigrok/libsigrok.h> |
45c59c8b | 25 | #include "libsigrok-internal.h" |
b08024a8 | 26 | |
f200d59e | 27 | /** @cond PRIVATE */ |
be92d5b4 | 28 | #define LOG_PREFIX "log" |
f200d59e | 29 | /** @endcond */ |
be92d5b4 | 30 | |
b4bd7088 UH |
31 | /** |
32 | * @file | |
33 | * | |
393fb9cb | 34 | * Controlling the libsigrok message logging functionality. |
b4bd7088 UH |
35 | */ |
36 | ||
7b870c38 UH |
37 | /** |
38 | * @defgroup grp_logging Logging | |
39 | * | |
40 | * Controlling the libsigrok message logging functionality. | |
41 | * | |
42 | * @{ | |
43 | */ | |
44 | ||
0ae67ff7 | 45 | /* Currently selected libsigrok loglevel. Default: SR_LOG_WARN. */ |
f897acae | 46 | static int cur_loglevel = SR_LOG_WARN; /* Show errors+warnings per default. */ |
1352eedd | 47 | |
0ae67ff7 | 48 | /* Function prototype. */ |
1f9813eb UH |
49 | static int sr_logv(void *cb_data, int loglevel, const char *format, |
50 | va_list args); | |
0ae67ff7 | 51 | |
b5118d6c | 52 | /* Pointer to the currently selected log callback. Default: sr_logv(). */ |
144f6660 | 53 | static sr_log_callback sr_log_cb = sr_logv; |
0ae67ff7 UH |
54 | |
55 | /* | |
b5118d6c | 56 | * Pointer to private data that can be passed to the log callback. |
0ae67ff7 UH |
57 | * This can be used (for example) by C++ GUIs to pass a "this" pointer. |
58 | */ | |
144f6660 | 59 | static void *sr_log_cb_data = NULL; |
0ae67ff7 | 60 | |
6d9da8ef DE |
61 | /** @cond PRIVATE */ |
62 | #define LOGLEVEL_TIMESTAMP SR_LOG_DBG | |
63 | /** @endcond */ | |
15408b51 DE |
64 | static int64_t sr_log_start_time = 0; |
65 | ||
1352eedd UH |
66 | /** |
67 | * Set the libsigrok loglevel. | |
68 | * | |
69 | * This influences the amount of log messages (debug messages, error messages, | |
70 | * and so on) libsigrok will output. Using SR_LOG_NONE disables all messages. | |
71 | * | |
0ae67ff7 UH |
72 | * Note that this function itself will also output log messages. After the |
73 | * loglevel has changed, it will output a debug message with SR_LOG_DBG for | |
74 | * example. Whether this message is shown depends on the (new) loglevel. | |
75 | * | |
1352eedd | 76 | * @param loglevel The loglevel to set (SR_LOG_NONE, SR_LOG_ERR, SR_LOG_WARN, |
06dd80d4 | 77 | * SR_LOG_INFO, SR_LOG_DBG, or SR_LOG_SPEW). |
44dae539 | 78 | * |
1352eedd | 79 | * @return SR_OK upon success, SR_ERR_ARG upon invalid loglevel. |
9fb5f2df UH |
80 | * |
81 | * @since 0.1.0 | |
1352eedd | 82 | */ |
0ae67ff7 | 83 | SR_API int sr_log_loglevel_set(int loglevel) |
1352eedd | 84 | { |
06dd80d4 | 85 | if (loglevel < SR_LOG_NONE || loglevel > SR_LOG_SPEW) { |
0ae67ff7 | 86 | sr_err("Invalid loglevel %d.", loglevel); |
1352eedd UH |
87 | return SR_ERR_ARG; |
88 | } | |
15408b51 | 89 | /* Output time stamps relative to time at startup */ |
6d9da8ef | 90 | if (loglevel >= LOGLEVEL_TIMESTAMP && sr_log_start_time == 0) |
15408b51 | 91 | sr_log_start_time = g_get_monotonic_time(); |
1352eedd | 92 | |
f897acae | 93 | cur_loglevel = loglevel; |
1352eedd | 94 | |
0ae67ff7 | 95 | sr_dbg("libsigrok loglevel set to %d.", loglevel); |
1352eedd UH |
96 | |
97 | return SR_OK; | |
98 | } | |
99 | ||
100 | /** | |
101 | * Get the libsigrok loglevel. | |
102 | * | |
103 | * @return The currently configured libsigrok loglevel. | |
9fb5f2df UH |
104 | * |
105 | * @since 0.1.0 | |
1352eedd | 106 | */ |
0ae67ff7 | 107 | SR_API int sr_log_loglevel_get(void) |
1352eedd | 108 | { |
f897acae | 109 | return cur_loglevel; |
1352eedd UH |
110 | } |
111 | ||
0ae67ff7 | 112 | /** |
b5118d6c | 113 | * Set the libsigrok log callback to the specified function. |
0ae67ff7 | 114 | * |
b5118d6c UH |
115 | * @param cb Function pointer to the log callback function to use. |
116 | * Must not be NULL. | |
1f9813eb UH |
117 | * @param cb_data Pointer to private data to be passed on. This can be used by |
118 | * the caller to pass arbitrary data to the log functions. This | |
119 | * pointer is only stored or passed on by libsigrok, and is | |
120 | * never used or interpreted in any way. The pointer is allowed | |
121 | * to be NULL if the caller doesn't need/want to pass any data. | |
44dae539 | 122 | * |
0ae67ff7 | 123 | * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments. |
9fb5f2df | 124 | * |
47117241 | 125 | * @since 0.3.0 |
0ae67ff7 | 126 | */ |
144f6660 | 127 | SR_API int sr_log_callback_set(sr_log_callback cb, void *cb_data) |
0ae67ff7 | 128 | { |
b5118d6c | 129 | if (!cb) { |
be92d5b4 | 130 | sr_err("%s: cb was NULL", __func__); |
0ae67ff7 UH |
131 | return SR_ERR_ARG; |
132 | } | |
133 | ||
1f9813eb | 134 | /* Note: 'cb_data' is allowed to be NULL. */ |
0ae67ff7 | 135 | |
144f6660 UH |
136 | sr_log_cb = cb; |
137 | sr_log_cb_data = cb_data; | |
0ae67ff7 UH |
138 | |
139 | return SR_OK; | |
140 | } | |
141 | ||
142 | /** | |
b5118d6c | 143 | * Set the libsigrok log callback to the default built-in one. |
0ae67ff7 | 144 | * |
144f6660 | 145 | * Additionally, the internal 'sr_log_cb_data' pointer is set to NULL. |
0ae67ff7 UH |
146 | * |
147 | * @return SR_OK upon success, a negative error code otherwise. | |
9fb5f2df UH |
148 | * |
149 | * @since 0.1.0 | |
0ae67ff7 | 150 | */ |
b5118d6c | 151 | SR_API int sr_log_callback_set_default(void) |
0ae67ff7 UH |
152 | { |
153 | /* | |
154 | * Note: No log output in this function, as it should safely work | |
b5118d6c | 155 | * even if the currently set log callback is buggy/broken. |
0ae67ff7 | 156 | */ |
144f6660 UH |
157 | sr_log_cb = sr_logv; |
158 | sr_log_cb_data = NULL; | |
0ae67ff7 UH |
159 | |
160 | return SR_OK; | |
161 | } | |
162 | ||
8cd15dd4 UH |
163 | /** |
164 | * Get the libsigrok log callback routine and callback data. | |
165 | * | |
166 | * @param[out] cb Pointer to a function pointer to receive the log callback | |
167 | * function. Optional, can be NULL. | |
168 | * @param[out] cb_data Pointer to a void pointer to receive the log callback's | |
169 | * additional arguments. Optional, can be NULL. | |
170 | * | |
171 | * @return SR_OK upon success. | |
172 | * | |
173 | * @since 0.6.0 | |
174 | */ | |
175 | SR_API int sr_log_callback_get(sr_log_callback *cb, void **cb_data) | |
176 | { | |
177 | if (cb) | |
178 | *cb = sr_log_cb; | |
179 | if (cb_data) | |
180 | *cb_data = sr_log_cb_data; | |
181 | ||
182 | return SR_OK; | |
183 | } | |
184 | ||
1f9813eb | 185 | static int sr_logv(void *cb_data, int loglevel, const char *format, va_list args) |
b08024a8 | 186 | { |
6d9da8ef DE |
187 | uint64_t elapsed_us, minutes; |
188 | unsigned int rest_us, seconds, microseconds; | |
4d6d660b SA |
189 | char *raw_output, *output; |
190 | int raw_len, raw_idx, idx, ret; | |
b08024a8 | 191 | |
b5118d6c | 192 | /* This specific log callback doesn't need the void pointer data. */ |
1f9813eb | 193 | (void)cb_data; |
0ae67ff7 | 194 | |
8cd15dd4 | 195 | (void)loglevel; |
b08024a8 | 196 | |
6d9da8ef DE |
197 | if (cur_loglevel >= LOGLEVEL_TIMESTAMP) { |
198 | elapsed_us = g_get_monotonic_time() - sr_log_start_time; | |
199 | ||
200 | minutes = elapsed_us / G_TIME_SPAN_MINUTE; | |
201 | rest_us = elapsed_us % G_TIME_SPAN_MINUTE; | |
202 | seconds = rest_us / G_TIME_SPAN_SECOND; | |
203 | microseconds = rest_us % G_TIME_SPAN_SECOND; | |
b08024a8 | 204 | |
7419638d | 205 | ret = g_fprintf(stderr, "sr: [%.2" PRIu64 ":%.2u.%.6u] ", |
22c50ed9 DE |
206 | minutes, seconds, microseconds); |
207 | } else { | |
208 | ret = fputs("sr: ", stderr); | |
15408b51 | 209 | } |
22c50ed9 | 210 | |
4d6d660b | 211 | if (ret < 0 || (raw_len = g_vasprintf(&raw_output, format, args)) < 0) |
15408b51 DE |
212 | return SR_ERR; |
213 | ||
4d6d660b SA |
214 | output = g_malloc0(raw_len + 1); |
215 | ||
216 | /* Copy the string without any unwanted newlines. */ | |
217 | raw_idx = idx = 0; | |
218 | while (raw_idx < raw_len) { | |
219 | if (raw_output[raw_idx] != '\n') { | |
220 | output[idx] = raw_output[raw_idx]; | |
221 | idx++; | |
222 | } | |
223 | raw_idx++; | |
224 | } | |
225 | ||
226 | g_fprintf(stderr, "%s\n", output); | |
8cd15dd4 | 227 | fflush(stderr); |
4d6d660b SA |
228 | g_free(raw_output); |
229 | g_free(output); | |
230 | ||
15408b51 | 231 | return SR_OK; |
b08024a8 UH |
232 | } |
233 | ||
b4bd7088 | 234 | /** @private */ |
1a081ca6 | 235 | SR_PRIV int sr_log(int loglevel, const char *format, ...) |
b08024a8 UH |
236 | { |
237 | int ret; | |
238 | va_list args; | |
239 | ||
8cd15dd4 UH |
240 | /* Only output messages of at least the selected loglevel(s). */ |
241 | if (loglevel > cur_loglevel) | |
242 | return SR_OK; | |
243 | ||
b08024a8 | 244 | va_start(args, format); |
144f6660 | 245 | ret = sr_log_cb(sr_log_cb_data, loglevel, format, args); |
b08024a8 UH |
246 | va_end(args); |
247 | ||
248 | return ret; | |
249 | } | |
250 | ||
7b870c38 | 251 | /** @} */ |