log.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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, write to the Free Software
  18. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include <stdarg.h>
  21. #include <stdio.h>
  22. #include "libsigrok.h"
  23. /** @cond PRIVATE */
  24. #define NO_LOG_WRAPPERS
  25. /** @endcond */
  26. #include "libsigrok-internal.h"
  27. /**
  28. * @file
  29. *
  30. * Controlling the libsigrok message logging functionality.
  31. */
  32. /**
  33. * @defgroup grp_logging Logging
  34. *
  35. * Controlling the libsigrok message logging functionality.
  36. *
  37. * @{
  38. */
  39. /* Currently selected libsigrok loglevel. Default: SR_LOG_WARN. */
  40. static int cur_loglevel = SR_LOG_WARN; /* Show errors+warnings per default. */
  41. /* Function prototype. */
  42. static int sr_logv(void *cb_data, int loglevel, const char *format,
  43. va_list args);
  44. /* Pointer to the currently selected log callback. Default: sr_logv(). */
  45. static sr_log_callback sr_log_cb = sr_logv;
  46. /*
  47. * Pointer to private data that can be passed to the log callback.
  48. * This can be used (for example) by C++ GUIs to pass a "this" pointer.
  49. */
  50. static void *sr_log_cb_data = NULL;
  51. /* Log domain (a short string that is used as prefix for all messages). */
  52. /** @cond PRIVATE */
  53. #define LOGDOMAIN_MAXLEN 30
  54. #define LOGDOMAIN_DEFAULT "sr: "
  55. /** @endcond */
  56. static char sr_log_domain[LOGDOMAIN_MAXLEN + 1] = LOGDOMAIN_DEFAULT;
  57. /**
  58. * Set the libsigrok loglevel.
  59. *
  60. * This influences the amount of log messages (debug messages, error messages,
  61. * and so on) libsigrok will output. Using SR_LOG_NONE disables all messages.
  62. *
  63. * Note that this function itself will also output log messages. After the
  64. * loglevel has changed, it will output a debug message with SR_LOG_DBG for
  65. * example. Whether this message is shown depends on the (new) loglevel.
  66. *
  67. * @param loglevel The loglevel to set (SR_LOG_NONE, SR_LOG_ERR, SR_LOG_WARN,
  68. * SR_LOG_INFO, SR_LOG_DBG, or SR_LOG_SPEW).
  69. *
  70. * @return SR_OK upon success, SR_ERR_ARG upon invalid loglevel.
  71. *
  72. * @since 0.1.0
  73. */
  74. SR_API int sr_log_loglevel_set(int loglevel)
  75. {
  76. if (loglevel < SR_LOG_NONE || loglevel > SR_LOG_SPEW) {
  77. sr_err("Invalid loglevel %d.", loglevel);
  78. return SR_ERR_ARG;
  79. }
  80. cur_loglevel = loglevel;
  81. sr_dbg("libsigrok loglevel set to %d.", loglevel);
  82. return SR_OK;
  83. }
  84. /**
  85. * Get the libsigrok loglevel.
  86. *
  87. * @return The currently configured libsigrok loglevel.
  88. *
  89. * @since 0.1.0
  90. */
  91. SR_API int sr_log_loglevel_get(void)
  92. {
  93. return cur_loglevel;
  94. }
  95. /**
  96. * Set the libsigrok logdomain string.
  97. *
  98. * @param logdomain The string to use as logdomain for libsigrok log
  99. * messages from now on. Must not be NULL. The maximum
  100. * length of the string is 30 characters (this does not
  101. * include the trailing NUL-byte). Longer strings are
  102. * silently truncated.
  103. * In order to not use a logdomain, pass an empty string.
  104. * The function makes its own copy of the input string, i.e.
  105. * the caller does not need to keep it around.
  106. *
  107. * @return SR_OK upon success, SR_ERR_ARG upon invalid logdomain.
  108. *
  109. * @since 0.1.0
  110. */
  111. SR_API int sr_log_logdomain_set(const char *logdomain)
  112. {
  113. if (!logdomain) {
  114. sr_err("log: %s: logdomain was NULL", __func__);
  115. return SR_ERR_ARG;
  116. }
  117. /* TODO: Error handling. */
  118. snprintf((char *)&sr_log_domain, LOGDOMAIN_MAXLEN, "%s", logdomain);
  119. sr_dbg("Log domain set to '%s'.", (const char *)&sr_log_domain);
  120. return SR_OK;
  121. }
  122. /**
  123. * Get the currently configured libsigrok logdomain.
  124. *
  125. * @return A copy of the currently configured libsigrok logdomain
  126. * string. The caller is responsible for g_free()ing the string when
  127. * it is no longer needed.
  128. *
  129. * @since 0.1.0
  130. */
  131. SR_API char *sr_log_logdomain_get(void)
  132. {
  133. return g_strdup((const char *)&sr_log_domain);
  134. }
  135. /**
  136. * Set the libsigrok log callback to the specified function.
  137. *
  138. * @param cb Function pointer to the log callback function to use.
  139. * Must not be NULL.
  140. * @param cb_data Pointer to private data to be passed on. This can be used by
  141. * the caller to pass arbitrary data to the log functions. This
  142. * pointer is only stored or passed on by libsigrok, and is
  143. * never used or interpreted in any way. The pointer is allowed
  144. * to be NULL if the caller doesn't need/want to pass any data.
  145. *
  146. * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments.
  147. *
  148. * @since 0.3.0
  149. */
  150. SR_API int sr_log_callback_set(sr_log_callback cb, void *cb_data)
  151. {
  152. if (!cb) {
  153. sr_err("log: %s: cb was NULL", __func__);
  154. return SR_ERR_ARG;
  155. }
  156. /* Note: 'cb_data' is allowed to be NULL. */
  157. sr_log_cb = cb;
  158. sr_log_cb_data = cb_data;
  159. return SR_OK;
  160. }
  161. /**
  162. * Set the libsigrok log callback to the default built-in one.
  163. *
  164. * Additionally, the internal 'sr_log_cb_data' pointer is set to NULL.
  165. *
  166. * @return SR_OK upon success, a negative error code otherwise.
  167. *
  168. * @since 0.1.0
  169. */
  170. SR_API int sr_log_callback_set_default(void)
  171. {
  172. /*
  173. * Note: No log output in this function, as it should safely work
  174. * even if the currently set log callback is buggy/broken.
  175. */
  176. sr_log_cb = sr_logv;
  177. sr_log_cb_data = NULL;
  178. return SR_OK;
  179. }
  180. static int sr_logv(void *cb_data, int loglevel, const char *format, va_list args)
  181. {
  182. int ret;
  183. /* This specific log callback doesn't need the void pointer data. */
  184. (void)cb_data;
  185. /* Only output messages of at least the selected loglevel(s). */
  186. if (loglevel > cur_loglevel)
  187. return SR_OK; /* TODO? */
  188. if (sr_log_domain[0] != '\0')
  189. fprintf(stderr, "%s", sr_log_domain);
  190. ret = vfprintf(stderr, format, args);
  191. fprintf(stderr, "\n");
  192. return ret;
  193. }
  194. /** @private */
  195. SR_PRIV int sr_log(int loglevel, const char *format, ...)
  196. {
  197. int ret;
  198. va_list args;
  199. va_start(args, format);
  200. ret = sr_log_cb(sr_log_cb_data, loglevel, format, args);
  201. va_end(args);
  202. return ret;
  203. }
  204. /** @private */
  205. SR_PRIV int sr_spew(const char *format, ...)
  206. {
  207. int ret;
  208. va_list args;
  209. va_start(args, format);
  210. ret = sr_log_cb(sr_log_cb_data, SR_LOG_SPEW, format, args);
  211. va_end(args);
  212. return ret;
  213. }
  214. /** @private */
  215. SR_PRIV int sr_dbg(const char *format, ...)
  216. {
  217. int ret;
  218. va_list args;
  219. va_start(args, format);
  220. ret = sr_log_cb(sr_log_cb_data, SR_LOG_DBG, format, args);
  221. va_end(args);
  222. return ret;
  223. }
  224. /** @private */
  225. SR_PRIV int sr_info(const char *format, ...)
  226. {
  227. int ret;
  228. va_list args;
  229. va_start(args, format);
  230. ret = sr_log_cb(sr_log_cb_data, SR_LOG_INFO, format, args);
  231. va_end(args);
  232. return ret;
  233. }
  234. /** @private */
  235. SR_PRIV int sr_warn(const char *format, ...)
  236. {
  237. int ret;
  238. va_list args;
  239. va_start(args, format);
  240. ret = sr_log_cb(sr_log_cb_data, SR_LOG_WARN, format, args);
  241. va_end(args);
  242. return ret;
  243. }
  244. /** @private */
  245. SR_PRIV int sr_err(const char *format, ...)
  246. {
  247. int ret;
  248. va_list args;
  249. va_start(args, format);
  250. ret = sr_log_cb(sr_log_cb_data, SR_LOG_ERR, format, args);
  251. va_end(args);
  252. return ret;
  253. }
  254. /** @} */