]> sigrok.org Git - libsigrokdecode.git/blob - log.c
6b9a20d81fd1d653b5fd6b759d97c7605afe70e7
[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("Invalid loglevel %d.", loglevel);
43                 return SRD_ERR_ARG;
44         }
45
46         srd_loglevel = loglevel;
47
48         srd_dbg("srd: loglevel set to %d", loglevel);
49
50         return SRD_OK;
51 }
52
53 /**
54  * Get the libsigrokdecode loglevel.
55  *
56  * @return The currently configured libsigrokdecode loglevel.
57  */
58 int srd_get_loglevel(void)
59 {
60         return srd_loglevel;
61 }
62
63 static int srd_logv(int loglevel, const char *format, va_list args)
64 {
65         int ret;
66
67         /* Only output messages of at least the selected loglevel(s). */
68         if (loglevel > srd_loglevel)
69                 return SRD_OK; /* TODO? */
70
71         ret = vfprintf(stderr, format, args);
72         fprintf(stderr, "\n");
73
74         return ret;
75 }
76
77 int srd_log(int loglevel, const char *format, ...)
78 {
79         int ret;
80         va_list args;
81
82         va_start(args, format);
83         ret = srd_logv(loglevel, format, args);
84         va_end(args);
85
86         return ret;
87 }
88
89 int srd_spew(const char *format, ...)
90 {
91         int ret;
92         va_list args;
93
94         va_start(args, format);
95         ret = srd_logv(SRD_LOG_SPEW, format, args);
96         va_end(args);
97
98         return ret;
99 }
100
101 int srd_dbg(const char *format, ...)
102 {
103         int ret;
104         va_list args;
105
106         va_start(args, format);
107         ret = srd_logv(SRD_LOG_DBG, format, args);
108         va_end(args);
109
110         return ret;
111 }
112
113 int srd_info(const char *format, ...)
114 {
115         int ret;
116         va_list args;
117
118         va_start(args, format);
119         ret = srd_logv(SRD_LOG_INFO, format, args);
120         va_end(args);
121
122         return ret;
123 }
124
125 int srd_warn(const char *format, ...)
126 {
127         int ret;
128         va_list args;
129
130         va_start(args, format);
131         ret = srd_logv(SRD_LOG_WARN, format, args);
132         va_end(args);
133
134         return ret;
135 }
136
137 int srd_err(const char *format, ...)
138 {
139         int ret;
140         va_list args;
141
142         va_start(args, format);
143         ret = srd_logv(SRD_LOG_ERR, format, args);
144         va_end(args);
145
146         return ret;
147 }