libsigrok  unreleased development snapshot
sigrok hardware access and backend library
log.c
Go to the documentation of this file.
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 /** @cond PRIVATE */
28 #define LOG_PREFIX "log"
29 /** @endcond */
30 
31 /**
32  * @file
33  *
34  * Controlling the libsigrok message logging functionality.
35  */
36 
37 /**
38  * @defgroup grp_logging Logging
39  *
40  * Controlling the libsigrok message logging functionality.
41  *
42  * @{
43  */
44 
45 /* Currently selected libsigrok loglevel. Default: SR_LOG_WARN. */
46 static int cur_loglevel = SR_LOG_WARN; /* Show errors+warnings per default. */
47 
48 /* Function prototype. */
49 static int sr_logv(void *cb_data, int loglevel, const char *format,
50  va_list args);
51 
52 /* Pointer to the currently selected log callback. Default: sr_logv(). */
53 static sr_log_callback sr_log_cb = sr_logv;
54 
55 /*
56  * Pointer to private data that can be passed to the log callback.
57  * This can be used (for example) by C++ GUIs to pass a "this" pointer.
58  */
59 static void *sr_log_cb_data = NULL;
60 
61 /** @cond PRIVATE */
62 #define LOGLEVEL_TIMESTAMP SR_LOG_DBG
63 /** @endcond */
64 static int64_t sr_log_start_time = 0;
65 
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  *
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  *
76  * @param loglevel The loglevel to set (SR_LOG_NONE, SR_LOG_ERR, SR_LOG_WARN,
77  * SR_LOG_INFO, SR_LOG_DBG, or SR_LOG_SPEW).
78  *
79  * @return SR_OK upon success, SR_ERR_ARG upon invalid loglevel.
80  *
81  * @since 0.1.0
82  */
83 SR_API int sr_log_loglevel_set(int loglevel)
84 {
85  if (loglevel < SR_LOG_NONE || loglevel > SR_LOG_SPEW) {
86  sr_err("Invalid loglevel %d.", loglevel);
87  return SR_ERR_ARG;
88  }
89  /* Output time stamps relative to time at startup */
90  if (loglevel >= LOGLEVEL_TIMESTAMP && sr_log_start_time == 0)
91  sr_log_start_time = g_get_monotonic_time();
92 
93  cur_loglevel = loglevel;
94 
95  sr_dbg("libsigrok loglevel set to %d.", loglevel);
96 
97  return SR_OK;
98 }
99 
100 /**
101  * Get the libsigrok loglevel.
102  *
103  * @return The currently configured libsigrok loglevel.
104  *
105  * @since 0.1.0
106  */
108 {
109  return cur_loglevel;
110 }
111 
112 /**
113  * Set the libsigrok log callback to the specified function.
114  *
115  * @param cb Function pointer to the log callback function to use.
116  * Must not be NULL.
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.
122  *
123  * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments.
124  *
125  * @since 0.3.0
126  */
128 {
129  if (!cb) {
130  sr_err("%s: cb was NULL", __func__);
131  return SR_ERR_ARG;
132  }
133 
134  /* Note: 'cb_data' is allowed to be NULL. */
135 
136  sr_log_cb = cb;
137  sr_log_cb_data = cb_data;
138 
139  return SR_OK;
140 }
141 
142 /**
143  * Set the libsigrok log callback to the default built-in one.
144  *
145  * Additionally, the internal 'sr_log_cb_data' pointer is set to NULL.
146  *
147  * @return SR_OK upon success, a negative error code otherwise.
148  *
149  * @since 0.1.0
150  */
152 {
153  /*
154  * Note: No log output in this function, as it should safely work
155  * even if the currently set log callback is buggy/broken.
156  */
157  sr_log_cb = sr_logv;
158  sr_log_cb_data = NULL;
159 
160  return SR_OK;
161 }
162 
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  */
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 
185 static int sr_logv(void *cb_data, int loglevel, const char *format, va_list args)
186 {
187  int ret;
188  uint64_t elapsed_us, minutes;
189  unsigned int rest_us, seconds, microseconds;
190  char *raw_output, *output, c;
191  ssize_t print_len;
192  size_t raw_len;
193  const char *raw_ptr;
194  char *out_ptr;
195 
196  /* This specific log callback doesn't need the void pointer data. */
197  (void)cb_data;
198 
199  (void)loglevel;
200 
201  /* Prefix with 'sr:'. Optionally prefix with timestamp. */
202  ret = fputs("sr: ", stderr);
203  if (ret < 0)
204  return SR_ERR;
205  if (cur_loglevel >= LOGLEVEL_TIMESTAMP) {
206  elapsed_us = g_get_monotonic_time() - sr_log_start_time;
207 
208  minutes = elapsed_us / G_TIME_SPAN_MINUTE;
209  rest_us = elapsed_us % G_TIME_SPAN_MINUTE;
210  seconds = rest_us / G_TIME_SPAN_SECOND;
211  microseconds = rest_us % G_TIME_SPAN_SECOND;
212 
213  ret = g_fprintf(stderr, "[%.2" PRIu64 ":%.2u.%.6u] ",
214  minutes, seconds, microseconds);
215  if (ret < 0)
216  return SR_ERR;
217  }
218 
219  /* Print the caller's message into a local buffer. */
220  raw_output = NULL;
221  print_len = g_vasprintf(&raw_output, format, args);
222  if (print_len < 0) {
223  g_free(raw_output);
224  return SR_ERR;
225  }
226  raw_len = (size_t)print_len;
227 
228  /* Copy the string. Strip unwanted line breaks. */
229  output = g_malloc(raw_len + 1);
230  if (!output) {
231  g_free(raw_output);
232  return SR_ERR;
233  }
234  out_ptr = output;
235  raw_ptr = raw_output;
236  while (*raw_ptr) {
237  c = *raw_ptr++;
238  if (c == '\r' || c == '\n')
239  continue;
240  *out_ptr++ = c;
241  }
242  *out_ptr = '\0';
243  g_free(raw_output);
244 
245  /* Print the trimmed output text. */
246  g_fprintf(stderr, "%s\n", output);
247  fflush(stderr);
248  g_free(output);
249 
250  return SR_OK;
251 }
252 
253 /** @private */
254 SR_PRIV int sr_log(int loglevel, const char *format, ...)
255 {
256  int ret;
257  va_list args;
258 
259  /* Only output messages of at least the selected loglevel(s). */
260  if (loglevel > cur_loglevel)
261  return SR_OK;
262 
263  /* Silently succeed when no logging callback is registered. */
264  if (!sr_log_cb)
265  return SR_OK;
266 
267  va_start(args, format);
268  ret = sr_log_cb(sr_log_cb_data, loglevel, format, args);
269  va_end(args);
270 
271  return ret;
272 }
273 
274 /** @} */
No error.
Definition: libsigrok.h:67
#define SR_API
Definition: libsigrok.h:128
Output very noisy debug messages.
Definition: libsigrok.h:107
int sr_log_loglevel_get(void)
Get the libsigrok loglevel.
Definition: log.c:107
typedef void(__LZO_CDECL *lzo_free_func_t)(lzo_callback_p self
Output warnings.
Definition: libsigrok.h:104
Generic/unspecified error.
Definition: libsigrok.h:68
int sr_log_callback_set_default(void)
Set the libsigrok log callback to the default built-in one.
Definition: log.c:151
int sr_log_callback_set(sr_log_callback cb, void *cb_data)
Set the libsigrok log callback to the specified function.
Definition: log.c:127
Function argument error.
Definition: libsigrok.h:70
int sr_log_loglevel_set(int loglevel)
Set the libsigrok loglevel.
Definition: log.c:83
The public libsigrok header file to be used by frontends.
#define SR_PRIV
Definition: libsigrok.h:135
int sr_log_callback_get(sr_log_callback *cb, void **cb_data)
Get the libsigrok log callback routine and callback data.
Definition: log.c:175
int(* sr_log_callback)(void *cb_data, int loglevel, const char *format, va_list args)
Definition: proto.h:63