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