]> sigrok.org Git - libsigrok.git/blame - log.c
libsigrok: Fix #includes.
[libsigrok.git] / log.c
CommitLineData
b08024a8
UH
1/*
2 * This file is part of the sigrok project.
3 *
4 * Copyright (C) 2011 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 <stdarg.h>
22#include <stdio.h>
b7f09cf8
UH
23#include "sigrok.h"
24#include "sigrok-internal.h"
b08024a8 25
1352eedd
UH
26static int sr_loglevel = SR_LOG_WARN; /* Show errors+warnings per default. */
27
28/**
29 * Set the libsigrok loglevel.
30 *
31 * This influences the amount of log messages (debug messages, error messages,
32 * and so on) libsigrok will output. Using SR_LOG_NONE disables all messages.
33 *
34 * @param loglevel The loglevel to set (SR_LOG_NONE, SR_LOG_ERR, SR_LOG_WARN,
06dd80d4 35 * SR_LOG_INFO, SR_LOG_DBG, or SR_LOG_SPEW).
1352eedd
UH
36 * @return SR_OK upon success, SR_ERR_ARG upon invalid loglevel.
37 */
38int sr_set_loglevel(int loglevel)
39{
06dd80d4 40 if (loglevel < SR_LOG_NONE || loglevel > SR_LOG_SPEW) {
1352eedd
UH
41 sr_err("log: %s: invalid loglevel %d", __func__, loglevel);
42 return SR_ERR_ARG;
43 }
44
45 sr_loglevel = loglevel;
46
47 sr_dbg("log: %s: libsigrok loglevel set to %d", __func__, loglevel);
48
49 return SR_OK;
50}
51
52/**
53 * Get the libsigrok loglevel.
54 *
55 * @return The currently configured libsigrok loglevel.
56 */
57int sr_get_loglevel(void)
58{
59 return sr_loglevel;
60}
61
b08024a8
UH
62static int sr_logv(int loglevel, const char *format, va_list args)
63{
64 int ret;
65
1352eedd
UH
66 /* Only output messages of at least the selected loglevel(s). */
67 if (loglevel > sr_loglevel)
68 return SR_OK; /* TODO? */
b08024a8
UH
69
70 ret = vfprintf(stderr, format, args);
71 fprintf(stderr, "\n");
72
73 return ret;
74}
75
76int sr_log(int loglevel, const char *format, ...)
77{
78 int ret;
79 va_list args;
80
81 va_start(args, format);
82 ret = sr_logv(loglevel, format, args);
83 va_end(args);
84
85 return ret;
86}
87
06dd80d4
UH
88int sr_spew(const char *format, ...)
89{
90 int ret;
91 va_list args;
92
93 va_start(args, format);
94 ret = sr_logv(SR_LOG_SPEW, format, args);
95 va_end(args);
96
97 return ret;
98}
99
b08024a8
UH
100int sr_dbg(const char *format, ...)
101{
102 int ret;
103 va_list args;
104
105 va_start(args, format);
106 ret = sr_logv(SR_LOG_DBG, format, args);
107 va_end(args);
108
109 return ret;
110}
111
112int sr_info(const char *format, ...)
113{
114 int ret;
115 va_list args;
116
117 va_start(args, format);
118 ret = sr_logv(SR_LOG_INFO, format, args);
119 va_end(args);
120
121 return ret;
122}
123
124int sr_warn(const char *format, ...)
125{
126 int ret;
127 va_list args;
128
129 va_start(args, format);
130 ret = sr_logv(SR_LOG_WARN, format, args);
131 va_end(args);
132
133 return ret;
134}
135
136int sr_err(const char *format, ...)
137{
138 int ret;
139 va_list args;
140
141 va_start(args, format);
142 ret = sr_logv(SR_LOG_ERR, format, args);
143 va_end(args);
144
145 return ret;
146}