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