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