]>
Commit | Line | Data |
---|---|---|
43c0a640 | 1 | /* |
50bd5d25 | 2 | * This file is part of the libsigrokdecode project. |
43c0a640 | 3 | * |
66e4c27c | 4 | * Copyright (C) 2011-2012 Uwe Hermann <uwe@hermann-uwe.de> |
43c0a640 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 | ||
36784362 | 21 | #include <config.h> |
f6c7eade MC |
22 | #include "libsigrokdecode-internal.h" /* First, so we avoid a _POSIX_C_SOURCE warning. */ |
23 | #include "libsigrokdecode.h" | |
bc5f5a43 BV |
24 | #include <stdarg.h> |
25 | #include <stdio.h> | |
ef3a6629 | 26 | #include <glib/gprintf.h> |
43c0a640 | 27 | |
54fdeeef UH |
28 | /** |
29 | * @file | |
30 | * | |
31 | * Controlling the libsigrokdecode message logging functionality. | |
32 | */ | |
33 | ||
4895418c UH |
34 | /** |
35 | * @defgroup grp_logging Logging | |
36 | * | |
37 | * Controlling the libsigrokdecode message logging functionality. | |
38 | * | |
39 | * @{ | |
40 | */ | |
41 | ||
66e4c27c | 42 | /* Currently selected libsigrokdecode loglevel. Default: SRD_LOG_WARN. */ |
aa6506b8 | 43 | static int cur_loglevel = SRD_LOG_WARN; /* Show errors+warnings per default. */ |
43c0a640 | 44 | |
66e4c27c | 45 | /* Function prototype. */ |
41a5d962 | 46 | static int srd_logv(void *cb_data, int loglevel, const char *format, |
e09023b9 | 47 | va_list args); |
66e4c27c | 48 | |
0082d592 | 49 | /* Pointer to the currently selected log callback. Default: srd_logv(). */ |
2372b199 | 50 | static srd_log_callback srd_log_cb = srd_logv; |
66e4c27c UH |
51 | |
52 | /* | |
0082d592 | 53 | * Pointer to private data that can be passed to the log callback. |
66e4c27c UH |
54 | * This can be used (for example) by C++ GUIs to pass a "this" pointer. |
55 | */ | |
2372b199 | 56 | static void *srd_log_cb_data = NULL; |
66e4c27c | 57 | |
43c0a640 UH |
58 | /** |
59 | * Set the libsigrokdecode loglevel. | |
60 | * | |
61 | * This influences the amount of log messages (debug messages, error messages, | |
62 | * and so on) libsigrokdecode will output. Using SRD_LOG_NONE disables all | |
63 | * messages. | |
64 | * | |
41106a07 UH |
65 | * Note that this function itself will also output log messages. After the |
66 | * loglevel has changed, it will output a debug message with SRD_LOG_DBG for | |
67 | * example. Whether this message is shown depends on the (new) loglevel. | |
68 | * | |
43c0a640 UH |
69 | * @param loglevel The loglevel to set (SRD_LOG_NONE, SRD_LOG_ERR, |
70 | * SRD_LOG_WARN, SRD_LOG_INFO, SRD_LOG_DBG, or SRD_LOG_SPEW). | |
582c8473 | 71 | * |
a62186e4 | 72 | * @return SRD_OK upon success, SRD_ERR_ARG upon invalid loglevel. |
8c664ca2 UH |
73 | * |
74 | * @since 0.1.0 | |
43c0a640 | 75 | */ |
55c3c5f4 | 76 | SRD_API int srd_log_loglevel_set(int loglevel) |
43c0a640 UH |
77 | { |
78 | if (loglevel < SRD_LOG_NONE || loglevel > SRD_LOG_SPEW) { | |
d906d3f9 | 79 | srd_err("Invalid loglevel %d.", loglevel); |
a62186e4 | 80 | return SRD_ERR_ARG; |
43c0a640 UH |
81 | } |
82 | ||
aa6506b8 | 83 | cur_loglevel = loglevel; |
43c0a640 | 84 | |
7a1712c4 | 85 | srd_dbg("libsigrokdecode loglevel set to %d.", loglevel); |
43c0a640 UH |
86 | |
87 | return SRD_OK; | |
88 | } | |
89 | ||
90 | /** | |
91 | * Get the libsigrokdecode loglevel. | |
92 | * | |
93 | * @return The currently configured libsigrokdecode loglevel. | |
8c664ca2 UH |
94 | * |
95 | * @since 0.1.0 | |
43c0a640 | 96 | */ |
55c3c5f4 | 97 | SRD_API int srd_log_loglevel_get(void) |
43c0a640 | 98 | { |
aa6506b8 | 99 | return cur_loglevel; |
43c0a640 UH |
100 | } |
101 | ||
66e4c27c | 102 | /** |
0082d592 | 103 | * Set the libsigrokdecode log callback to the specified function. |
66e4c27c | 104 | * |
0082d592 UH |
105 | * @param cb Function pointer to the log callback function to use. |
106 | * Must not be NULL. | |
41a5d962 UH |
107 | * @param cb_data Pointer to private data to be passed on. This can be used |
108 | * by the caller to pass arbitrary data to the log functions. | |
109 | * This pointer is only stored or passed on by libsigrokdecode, | |
110 | * and is never used or interpreted in any way. The pointer | |
111 | * is allowed to be NULL if the caller doesn't need/want to | |
112 | * pass any data. | |
582c8473 | 113 | * |
66e4c27c | 114 | * @return SRD_OK upon success, SRD_ERR_ARG upon invalid arguments. |
8c664ca2 | 115 | * |
c57d1013 | 116 | * @since 0.3.0 |
66e4c27c | 117 | */ |
2372b199 | 118 | SRD_API int srd_log_callback_set(srd_log_callback cb, void *cb_data) |
66e4c27c | 119 | { |
0082d592 UH |
120 | if (!cb) { |
121 | srd_err("log: %s: cb was NULL", __func__); | |
66e4c27c UH |
122 | return SRD_ERR_ARG; |
123 | } | |
124 | ||
41a5d962 | 125 | /* Note: 'cb_data' is allowed to be NULL. */ |
66e4c27c | 126 | |
2372b199 UH |
127 | srd_log_cb = cb; |
128 | srd_log_cb_data = cb_data; | |
66e4c27c UH |
129 | |
130 | return SRD_OK; | |
131 | } | |
132 | ||
133 | /** | |
0082d592 | 134 | * Set the libsigrokdecode log callback to the default built-in one. |
66e4c27c | 135 | * |
2372b199 | 136 | * Additionally, the internal 'srd_log_cb_data' pointer is set to NULL. |
66e4c27c | 137 | * |
582c8473 | 138 | * @return SRD_OK upon success, a (negative) error code otherwise. |
8c664ca2 UH |
139 | * |
140 | * @since 0.1.0 | |
66e4c27c | 141 | */ |
0082d592 | 142 | SRD_API int srd_log_callback_set_default(void) |
66e4c27c UH |
143 | { |
144 | /* | |
145 | * Note: No log output in this function, as it should safely work | |
0082d592 | 146 | * even if the currently set log callback is buggy/broken. |
66e4c27c | 147 | */ |
2372b199 UH |
148 | srd_log_cb = srd_logv; |
149 | srd_log_cb_data = NULL; | |
66e4c27c UH |
150 | |
151 | return SRD_OK; | |
152 | } | |
153 | ||
41a5d962 | 154 | static int srd_logv(void *cb_data, int loglevel, const char *format, |
e09023b9 | 155 | va_list args) |
43c0a640 | 156 | { |
0082d592 | 157 | /* This specific log callback doesn't need the void pointer data. */ |
41a5d962 | 158 | (void)cb_data; |
66e4c27c | 159 | |
43c0a640 | 160 | /* Only output messages of at least the selected loglevel(s). */ |
aa6506b8 | 161 | if (loglevel > cur_loglevel) |
1f2e00d1 | 162 | return SRD_OK; |
43c0a640 | 163 | |
ef3a6629 DE |
164 | if (fputs("srd: ", stderr) < 0 |
165 | || g_vfprintf(stderr, format, args) < 0 | |
166 | || putc('\n', stderr) < 0) | |
167 | return SRD_ERR; | |
43c0a640 | 168 | |
ef3a6629 | 169 | return SRD_OK; |
43c0a640 UH |
170 | } |
171 | ||
57790bc8 | 172 | /** @private */ |
55c3c5f4 | 173 | SRD_PRIV int srd_log(int loglevel, const char *format, ...) |
43c0a640 UH |
174 | { |
175 | int ret; | |
176 | va_list args; | |
177 | ||
178 | va_start(args, format); | |
2372b199 | 179 | ret = srd_log_cb(srd_log_cb_data, loglevel, format, args); |
43c0a640 UH |
180 | va_end(args); |
181 | ||
182 | return ret; | |
183 | } | |
184 | ||
4895418c | 185 | /** @} */ |