]> sigrok.org Git - libsigrokdecode.git/blob - log.c
srd: s/output_new/add/ for all decoders.
[libsigrokdecode.git] / log.c
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 "sigrokdecode.h"
22 #include "sigrokdecode-internal.h"
23 #include <stdarg.h>
24 #include <stdio.h>
25
26 static int srd_loglevel = SRD_LOG_WARN; /* Show errors+warnings per default. */
27
28 /**
29  * Set the libsigrokdecode loglevel.
30  *
31  * This influences the amount of log messages (debug messages, error messages,
32  * and so on) libsigrokdecode will output. Using SRD_LOG_NONE disables all
33  * messages.
34  *
35  * @param loglevel The loglevel to set (SRD_LOG_NONE, SRD_LOG_ERR,
36  *                 SRD_LOG_WARN, SRD_LOG_INFO, SRD_LOG_DBG, or SRD_LOG_SPEW).
37  * @return SRD_OK upon success, SRD_ERR_ARG upon invalid loglevel.
38  */
39 int srd_set_loglevel(int loglevel)
40 {
41         if (loglevel < SRD_LOG_NONE || loglevel > SRD_LOG_SPEW) {
42                 srd_err("log: %s: invalid loglevel %d", __func__, loglevel);
43                 return SRD_ERR_ARG;
44         }
45
46         srd_loglevel = loglevel;
47
48         srd_dbg("log: %s: libsigrokdecode loglevel set to %d",
49                 __func__, loglevel);
50
51         return SRD_OK;
52 }
53
54 /**
55  * Get the libsigrokdecode loglevel.
56  *
57  * @return The currently configured libsigrokdecode loglevel.
58  */
59 int srd_get_loglevel(void)
60 {
61         return srd_loglevel;
62 }
63
64 static int srd_logv(int loglevel, const char *format, va_list args)
65 {
66         int ret;
67
68         /* Only output messages of at least the selected loglevel(s). */
69         if (loglevel > srd_loglevel)
70                 return SRD_OK; /* TODO? */
71
72         ret = vfprintf(stderr, format, args);
73         fprintf(stderr, "\n");
74
75         return ret;
76 }
77
78 int srd_log(int loglevel, const char *format, ...)
79 {
80         int ret;
81         va_list args;
82
83         va_start(args, format);
84         ret = srd_logv(loglevel, format, args);
85         va_end(args);
86
87         return ret;
88 }
89
90 int srd_spew(const char *format, ...)
91 {
92         int ret;
93         va_list args;
94
95         va_start(args, format);
96         ret = srd_logv(SRD_LOG_SPEW, format, args);
97         va_end(args);
98
99         return ret;
100 }
101
102 int srd_dbg(const char *format, ...)
103 {
104         int ret;
105         va_list args;
106
107         va_start(args, format);
108         ret = srd_logv(SRD_LOG_DBG, format, args);
109         va_end(args);
110
111         return ret;
112 }
113
114 int srd_info(const char *format, ...)
115 {
116         int ret;
117         va_list args;
118
119         va_start(args, format);
120         ret = srd_logv(SRD_LOG_INFO, format, args);
121         va_end(args);
122
123         return ret;
124 }
125
126 int srd_warn(const char *format, ...)
127 {
128         int ret;
129         va_list args;
130
131         va_start(args, format);
132         ret = srd_logv(SRD_LOG_WARN, format, args);
133         va_end(args);
134
135         return ret;
136 }
137
138 int srd_err(const char *format, ...)
139 {
140         int ret;
141         va_list args;
142
143         va_start(args, format);
144         ret = srd_logv(SRD_LOG_ERR, format, args);
145         va_end(args);
146
147         return ret;
148 }