]> sigrok.org Git - libsigrok.git/blame - src/log.c
libsigrok-internal.h: Remove unused prototypes
[libsigrok.git] / src / log.c
CommitLineData
b08024a8 1/*
50985c20 2 * This file is part of the libsigrok project.
b08024a8 3 *
0ae67ff7 4 * Copyright (C) 2011-2012 Uwe Hermann <uwe@hermann-uwe.de>
b08024a8
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
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 <stdarg.h>
22#include <stdio.h>
7419638d 23#include <glib/gprintf.h>
c1aae900 24#include <libsigrok/libsigrok.h>
45c59c8b 25#include "libsigrok-internal.h"
b08024a8 26
be92d5b4
DE
27#define LOG_PREFIX "log"
28
b4bd7088
UH
29/**
30 * @file
31 *
393fb9cb 32 * Controlling the libsigrok message logging functionality.
b4bd7088
UH
33 */
34
7b870c38
UH
35/**
36 * @defgroup grp_logging Logging
37 *
38 * Controlling the libsigrok message logging functionality.
39 *
40 * @{
41 */
42
0ae67ff7 43/* Currently selected libsigrok loglevel. Default: SR_LOG_WARN. */
f897acae 44static int cur_loglevel = SR_LOG_WARN; /* Show errors+warnings per default. */
1352eedd 45
0ae67ff7 46/* Function prototype. */
1f9813eb
UH
47static int sr_logv(void *cb_data, int loglevel, const char *format,
48 va_list args);
0ae67ff7 49
b5118d6c 50/* Pointer to the currently selected log callback. Default: sr_logv(). */
144f6660 51static sr_log_callback sr_log_cb = sr_logv;
0ae67ff7
UH
52
53/*
b5118d6c 54 * Pointer to private data that can be passed to the log callback.
0ae67ff7
UH
55 * This can be used (for example) by C++ GUIs to pass a "this" pointer.
56 */
144f6660 57static void *sr_log_cb_data = NULL;
0ae67ff7 58
6d9da8ef
DE
59/** @cond PRIVATE */
60#define LOGLEVEL_TIMESTAMP SR_LOG_DBG
61/** @endcond */
15408b51
DE
62static int64_t sr_log_start_time = 0;
63
1352eedd
UH
64/**
65 * Set the libsigrok loglevel.
66 *
67 * This influences the amount of log messages (debug messages, error messages,
68 * and so on) libsigrok will output. Using SR_LOG_NONE disables all messages.
69 *
0ae67ff7
UH
70 * Note that this function itself will also output log messages. After the
71 * loglevel has changed, it will output a debug message with SR_LOG_DBG for
72 * example. Whether this message is shown depends on the (new) loglevel.
73 *
1352eedd 74 * @param loglevel The loglevel to set (SR_LOG_NONE, SR_LOG_ERR, SR_LOG_WARN,
06dd80d4 75 * SR_LOG_INFO, SR_LOG_DBG, or SR_LOG_SPEW).
44dae539 76 *
1352eedd 77 * @return SR_OK upon success, SR_ERR_ARG upon invalid loglevel.
9fb5f2df
UH
78 *
79 * @since 0.1.0
1352eedd 80 */
0ae67ff7 81SR_API int sr_log_loglevel_set(int loglevel)
1352eedd 82{
06dd80d4 83 if (loglevel < SR_LOG_NONE || loglevel > SR_LOG_SPEW) {
0ae67ff7 84 sr_err("Invalid loglevel %d.", loglevel);
1352eedd
UH
85 return SR_ERR_ARG;
86 }
15408b51 87 /* Output time stamps relative to time at startup */
6d9da8ef 88 if (loglevel >= LOGLEVEL_TIMESTAMP && sr_log_start_time == 0)
15408b51 89 sr_log_start_time = g_get_monotonic_time();
1352eedd 90
f897acae 91 cur_loglevel = loglevel;
1352eedd 92
0ae67ff7 93 sr_dbg("libsigrok loglevel set to %d.", loglevel);
1352eedd
UH
94
95 return SR_OK;
96}
97
98/**
99 * Get the libsigrok loglevel.
100 *
101 * @return The currently configured libsigrok loglevel.
9fb5f2df
UH
102 *
103 * @since 0.1.0
1352eedd 104 */
0ae67ff7 105SR_API int sr_log_loglevel_get(void)
1352eedd 106{
f897acae 107 return cur_loglevel;
1352eedd
UH
108}
109
0ae67ff7 110/**
b5118d6c 111 * Set the libsigrok log callback to the specified function.
0ae67ff7 112 *
b5118d6c
UH
113 * @param cb Function pointer to the log callback function to use.
114 * Must not be NULL.
1f9813eb
UH
115 * @param cb_data Pointer to private data to be passed on. This can be used by
116 * the caller to pass arbitrary data to the log functions. This
117 * pointer is only stored or passed on by libsigrok, and is
118 * never used or interpreted in any way. The pointer is allowed
119 * to be NULL if the caller doesn't need/want to pass any data.
44dae539 120 *
0ae67ff7 121 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments.
9fb5f2df 122 *
47117241 123 * @since 0.3.0
0ae67ff7 124 */
144f6660 125SR_API int sr_log_callback_set(sr_log_callback cb, void *cb_data)
0ae67ff7 126{
b5118d6c 127 if (!cb) {
be92d5b4 128 sr_err("%s: cb was NULL", __func__);
0ae67ff7
UH
129 return SR_ERR_ARG;
130 }
131
1f9813eb 132 /* Note: 'cb_data' is allowed to be NULL. */
0ae67ff7 133
144f6660
UH
134 sr_log_cb = cb;
135 sr_log_cb_data = cb_data;
0ae67ff7
UH
136
137 return SR_OK;
138}
139
140/**
b5118d6c 141 * Set the libsigrok log callback to the default built-in one.
0ae67ff7 142 *
144f6660 143 * Additionally, the internal 'sr_log_cb_data' pointer is set to NULL.
0ae67ff7
UH
144 *
145 * @return SR_OK upon success, a negative error code otherwise.
9fb5f2df
UH
146 *
147 * @since 0.1.0
0ae67ff7 148 */
b5118d6c 149SR_API int sr_log_callback_set_default(void)
0ae67ff7
UH
150{
151 /*
152 * Note: No log output in this function, as it should safely work
b5118d6c 153 * even if the currently set log callback is buggy/broken.
0ae67ff7 154 */
144f6660
UH
155 sr_log_cb = sr_logv;
156 sr_log_cb_data = NULL;
0ae67ff7
UH
157
158 return SR_OK;
159}
160
1f9813eb 161static int sr_logv(void *cb_data, int loglevel, const char *format, va_list args)
b08024a8 162{
6d9da8ef
DE
163 uint64_t elapsed_us, minutes;
164 unsigned int rest_us, seconds, microseconds;
22c50ed9 165 int ret;
b08024a8 166
b5118d6c 167 /* This specific log callback doesn't need the void pointer data. */
1f9813eb 168 (void)cb_data;
0ae67ff7 169
1352eedd 170 /* Only output messages of at least the selected loglevel(s). */
f897acae 171 if (loglevel > cur_loglevel)
15408b51 172 return SR_OK;
b08024a8 173
6d9da8ef
DE
174 if (cur_loglevel >= LOGLEVEL_TIMESTAMP) {
175 elapsed_us = g_get_monotonic_time() - sr_log_start_time;
176
177 minutes = elapsed_us / G_TIME_SPAN_MINUTE;
178 rest_us = elapsed_us % G_TIME_SPAN_MINUTE;
179 seconds = rest_us / G_TIME_SPAN_SECOND;
180 microseconds = rest_us % G_TIME_SPAN_SECOND;
b08024a8 181
7419638d 182 ret = g_fprintf(stderr, "sr: [%.2" PRIu64 ":%.2u.%.6u] ",
22c50ed9
DE
183 minutes, seconds, microseconds);
184 } else {
185 ret = fputs("sr: ", stderr);
15408b51 186 }
22c50ed9 187
7419638d 188 if (ret < 0 || g_vfprintf(stderr, format, args) < 0
22c50ed9 189 || putc('\n', stderr) < 0)
15408b51
DE
190 return SR_ERR;
191
192 return SR_OK;
b08024a8
UH
193}
194
b4bd7088 195/** @private */
1a081ca6 196SR_PRIV int sr_log(int loglevel, const char *format, ...)
b08024a8
UH
197{
198 int ret;
199 va_list args;
200
201 va_start(args, format);
144f6660 202 ret = sr_log_cb(sr_log_cb_data, loglevel, format, args);
b08024a8
UH
203 va_end(args);
204
205 return ret;
206}
207
7b870c38 208/** @} */