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