]>
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 | |
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> | |
c1aae900 | 23 | #include <libsigrok/libsigrok.h> |
45c59c8b | 24 | #include "libsigrok-internal.h" |
b08024a8 | 25 | |
be92d5b4 DE |
26 | #define LOG_PREFIX "log" |
27 | ||
b4bd7088 UH |
28 | /** |
29 | * @file | |
30 | * | |
393fb9cb | 31 | * Controlling the libsigrok message logging functionality. |
b4bd7088 UH |
32 | */ |
33 | ||
7b870c38 UH |
34 | /** |
35 | * @defgroup grp_logging Logging | |
36 | * | |
37 | * Controlling the libsigrok message logging functionality. | |
38 | * | |
39 | * @{ | |
40 | */ | |
41 | ||
0ae67ff7 | 42 | /* Currently selected libsigrok loglevel. Default: SR_LOG_WARN. */ |
f897acae | 43 | static int cur_loglevel = SR_LOG_WARN; /* Show errors+warnings per default. */ |
1352eedd | 44 | |
0ae67ff7 | 45 | /* Function prototype. */ |
1f9813eb UH |
46 | static int sr_logv(void *cb_data, int loglevel, const char *format, |
47 | va_list args); | |
0ae67ff7 | 48 | |
b5118d6c | 49 | /* Pointer to the currently selected log callback. Default: sr_logv(). */ |
144f6660 | 50 | static sr_log_callback sr_log_cb = sr_logv; |
0ae67ff7 UH |
51 | |
52 | /* | |
b5118d6c | 53 | * Pointer to private data that can be passed to the log callback. |
0ae67ff7 UH |
54 | * This can be used (for example) by C++ GUIs to pass a "this" pointer. |
55 | */ | |
144f6660 | 56 | static void *sr_log_cb_data = NULL; |
0ae67ff7 | 57 | |
6d9da8ef DE |
58 | /** @cond PRIVATE */ |
59 | #define LOGLEVEL_TIMESTAMP SR_LOG_DBG | |
60 | /** @endcond */ | |
15408b51 DE |
61 | static int64_t sr_log_start_time = 0; |
62 | ||
1352eedd UH |
63 | /** |
64 | * Set the libsigrok loglevel. | |
65 | * | |
66 | * This influences the amount of log messages (debug messages, error messages, | |
67 | * and so on) libsigrok will output. Using SR_LOG_NONE disables all messages. | |
68 | * | |
0ae67ff7 UH |
69 | * Note that this function itself will also output log messages. After the |
70 | * loglevel has changed, it will output a debug message with SR_LOG_DBG for | |
71 | * example. Whether this message is shown depends on the (new) loglevel. | |
72 | * | |
1352eedd | 73 | * @param loglevel The loglevel to set (SR_LOG_NONE, SR_LOG_ERR, SR_LOG_WARN, |
06dd80d4 | 74 | * SR_LOG_INFO, SR_LOG_DBG, or SR_LOG_SPEW). |
44dae539 | 75 | * |
1352eedd | 76 | * @return SR_OK upon success, SR_ERR_ARG upon invalid loglevel. |
9fb5f2df UH |
77 | * |
78 | * @since 0.1.0 | |
1352eedd | 79 | */ |
0ae67ff7 | 80 | SR_API int sr_log_loglevel_set(int loglevel) |
1352eedd | 81 | { |
06dd80d4 | 82 | if (loglevel < SR_LOG_NONE || loglevel > SR_LOG_SPEW) { |
0ae67ff7 | 83 | sr_err("Invalid loglevel %d.", loglevel); |
1352eedd UH |
84 | return SR_ERR_ARG; |
85 | } | |
15408b51 | 86 | /* Output time stamps relative to time at startup */ |
6d9da8ef | 87 | if (loglevel >= LOGLEVEL_TIMESTAMP && sr_log_start_time == 0) |
15408b51 | 88 | sr_log_start_time = g_get_monotonic_time(); |
1352eedd | 89 | |
f897acae | 90 | cur_loglevel = loglevel; |
1352eedd | 91 | |
0ae67ff7 | 92 | sr_dbg("libsigrok loglevel set to %d.", loglevel); |
1352eedd UH |
93 | |
94 | return SR_OK; | |
95 | } | |
96 | ||
97 | /** | |
98 | * Get the libsigrok loglevel. | |
99 | * | |
100 | * @return The currently configured libsigrok loglevel. | |
9fb5f2df UH |
101 | * |
102 | * @since 0.1.0 | |
1352eedd | 103 | */ |
0ae67ff7 | 104 | SR_API int sr_log_loglevel_get(void) |
1352eedd | 105 | { |
f897acae | 106 | return cur_loglevel; |
1352eedd UH |
107 | } |
108 | ||
0ae67ff7 | 109 | /** |
b5118d6c | 110 | * Set the libsigrok log callback to the specified function. |
0ae67ff7 | 111 | * |
b5118d6c UH |
112 | * @param cb Function pointer to the log callback function to use. |
113 | * Must not be NULL. | |
1f9813eb UH |
114 | * @param cb_data Pointer to private data to be passed on. This can be used by |
115 | * the caller to pass arbitrary data to the log functions. This | |
116 | * pointer is only stored or passed on by libsigrok, and is | |
117 | * never used or interpreted in any way. The pointer is allowed | |
118 | * to be NULL if the caller doesn't need/want to pass any data. | |
44dae539 | 119 | * |
0ae67ff7 | 120 | * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments. |
9fb5f2df | 121 | * |
47117241 | 122 | * @since 0.3.0 |
0ae67ff7 | 123 | */ |
144f6660 | 124 | SR_API int sr_log_callback_set(sr_log_callback cb, void *cb_data) |
0ae67ff7 | 125 | { |
b5118d6c | 126 | if (!cb) { |
be92d5b4 | 127 | sr_err("%s: cb was NULL", __func__); |
0ae67ff7 UH |
128 | return SR_ERR_ARG; |
129 | } | |
130 | ||
1f9813eb | 131 | /* Note: 'cb_data' is allowed to be NULL. */ |
0ae67ff7 | 132 | |
144f6660 UH |
133 | sr_log_cb = cb; |
134 | sr_log_cb_data = cb_data; | |
0ae67ff7 UH |
135 | |
136 | return SR_OK; | |
137 | } | |
138 | ||
139 | /** | |
b5118d6c | 140 | * Set the libsigrok log callback to the default built-in one. |
0ae67ff7 | 141 | * |
144f6660 | 142 | * Additionally, the internal 'sr_log_cb_data' pointer is set to NULL. |
0ae67ff7 UH |
143 | * |
144 | * @return SR_OK upon success, a negative error code otherwise. | |
9fb5f2df UH |
145 | * |
146 | * @since 0.1.0 | |
0ae67ff7 | 147 | */ |
b5118d6c | 148 | SR_API int sr_log_callback_set_default(void) |
0ae67ff7 UH |
149 | { |
150 | /* | |
151 | * Note: No log output in this function, as it should safely work | |
b5118d6c | 152 | * even if the currently set log callback is buggy/broken. |
0ae67ff7 | 153 | */ |
144f6660 UH |
154 | sr_log_cb = sr_logv; |
155 | sr_log_cb_data = NULL; | |
0ae67ff7 UH |
156 | |
157 | return SR_OK; | |
158 | } | |
159 | ||
1f9813eb | 160 | static int sr_logv(void *cb_data, int loglevel, const char *format, va_list args) |
b08024a8 | 161 | { |
6d9da8ef DE |
162 | uint64_t elapsed_us, minutes; |
163 | unsigned int rest_us, seconds, microseconds; | |
b08024a8 | 164 | |
b5118d6c | 165 | /* This specific log callback doesn't need the void pointer data. */ |
1f9813eb | 166 | (void)cb_data; |
0ae67ff7 | 167 | |
1352eedd | 168 | /* Only output messages of at least the selected loglevel(s). */ |
f897acae | 169 | if (loglevel > cur_loglevel) |
15408b51 | 170 | return SR_OK; |
b08024a8 | 171 | |
6d9da8ef DE |
172 | if (cur_loglevel >= LOGLEVEL_TIMESTAMP) { |
173 | elapsed_us = g_get_monotonic_time() - sr_log_start_time; | |
174 | ||
175 | minutes = elapsed_us / G_TIME_SPAN_MINUTE; | |
176 | rest_us = elapsed_us % G_TIME_SPAN_MINUTE; | |
177 | seconds = rest_us / G_TIME_SPAN_SECOND; | |
178 | microseconds = rest_us % G_TIME_SPAN_SECOND; | |
b08024a8 | 179 | |
6d9da8ef DE |
180 | if (fprintf(stderr, "[%.2" PRIu64 ":%.2u.%.6u] ", |
181 | minutes, seconds, microseconds) < 0) | |
15408b51 DE |
182 | return SR_ERR; |
183 | } | |
15408b51 DE |
184 | if (vfprintf(stderr, format, args) < 0) |
185 | return SR_ERR; | |
186 | if (putc('\n', stderr) < 0) | |
187 | return SR_ERR; | |
188 | ||
189 | return SR_OK; | |
b08024a8 UH |
190 | } |
191 | ||
b4bd7088 | 192 | /** @private */ |
1a081ca6 | 193 | SR_PRIV int sr_log(int loglevel, const char *format, ...) |
b08024a8 UH |
194 | { |
195 | int ret; | |
196 | va_list args; | |
197 | ||
198 | va_start(args, format); | |
144f6660 | 199 | ret = sr_log_cb(sr_log_cb_data, loglevel, format, args); |
b08024a8 UH |
200 | va_end(args); |
201 | ||
202 | return ret; | |
203 | } | |
204 | ||
7b870c38 | 205 | /** @} */ |