BadVPN – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | /** |
2 | * @file log.c |
||
3 | * @author Ambroz Bizjak <ambrop7@gmail.com> |
||
4 | * |
||
5 | * @section LICENSE |
||
6 | * |
||
7 | * Redistribution and use in source and binary forms, with or without |
||
8 | * modification, are permitted provided that the following conditions are met: |
||
9 | * 1. Redistributions of source code must retain the above copyright |
||
10 | * notice, this list of conditions and the following disclaimer. |
||
11 | * 2. Redistributions in binary form must reproduce the above copyright |
||
12 | * notice, this list of conditions and the following disclaimer in the |
||
13 | * documentation and/or other materials provided with the distribution. |
||
14 | * 3. Neither the name of the author nor the |
||
15 | * names of its contributors may be used to endorse or promote products |
||
16 | * derived from this software without specific prior written permission. |
||
17 | * |
||
18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
||
19 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
||
20 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
||
21 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
||
22 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
||
23 | * (INCLUDING, BUT NOT LIMITED TO OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
||
24 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
||
26 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||
27 | * |
||
28 | * @section DESCRIPTION |
||
29 | * |
||
30 | * Message logging using the BLog system provided by the BadVPN framework. |
||
31 | * Each message has an associated loglevel, which must be one of: "error, "warning", |
||
32 | * "notice", "info", "debug", or a numeric identifier (1=error to 5=debug). |
||
33 | * |
||
34 | * Synopsis: |
||
35 | * log(string level [, string ...]) |
||
36 | * |
||
37 | * Description: |
||
38 | * On init, logs the concatenation of the given strings. |
||
39 | * |
||
40 | * Synopsis: |
||
41 | * log_r(string level [, string ...]) |
||
42 | * |
||
43 | * Description: |
||
44 | * On deinit, logs the concatenation of the given strings. |
||
45 | * |
||
46 | * Synopsis: |
||
47 | * log_fr(string level, list(string) strings_init, list(string) strings_deinit) |
||
48 | * |
||
49 | * Description: |
||
50 | * On init, logs the concatenation of the strings in 'strings_init', |
||
51 | * and on deinit, logs the concatenation of the strings in 'strings_deinit'. |
||
52 | */ |
||
53 | |||
54 | #include <stdlib.h> |
||
55 | #include <stdio.h> |
||
56 | |||
57 | #include <misc/debug.h> |
||
58 | |||
59 | #include <ncd/module_common.h> |
||
60 | |||
61 | #include <generated/blog_channel_ncd_log.h> |
||
62 | |||
63 | struct rlog_instance { |
||
64 | NCDModuleInst *i; |
||
65 | int level; |
||
66 | NCDValRef list; |
||
67 | size_t start; |
||
68 | }; |
||
69 | |||
70 | enum {STRING_ERROR, STRING_WARNING, STRING_NOTICE, STRING_INFO, STRING_DEBUG}; |
||
71 | |||
72 | static const char *strings[] = { |
||
73 | "error", "warning", "notice", "info", "debug", NULL |
||
74 | }; |
||
75 | |||
76 | static int check_strings (NCDValRef list, size_t start) |
||
77 | { |
||
78 | ASSERT(NCDVal_IsList(list)) |
||
79 | |||
80 | size_t count = NCDVal_ListCount(list); |
||
81 | |||
82 | for (size_t j = start; j < count; j++) { |
||
83 | NCDValRef string = NCDVal_ListGet(list, j); |
||
84 | if (!NCDVal_IsString(string)) { |
||
85 | return 0; |
||
86 | } |
||
87 | } |
||
88 | |||
89 | return 1; |
||
90 | } |
||
91 | |||
92 | static void do_log (int level, NCDValRef list, size_t start) |
||
93 | { |
||
94 | ASSERT(level >= BLOG_ERROR) |
||
95 | ASSERT(level <= BLOG_DEBUG) |
||
96 | ASSERT(check_strings(list, start)) |
||
97 | |||
98 | if (!BLog_WouldLog(BLOG_CHANNEL_ncd_log_msg, level)) { |
||
99 | return; |
||
100 | } |
||
101 | |||
102 | size_t count = NCDVal_ListCount(list); |
||
103 | |||
104 | BLog_Begin(); |
||
105 | |||
106 | for (size_t j = start; j < count; j++) { |
||
107 | NCDValRef string = NCDVal_ListGet(list, j); |
||
108 | ASSERT(NCDVal_IsString(string)) |
||
109 | BLog_AppendBytes(NCDVal_StringMemRef(string)); |
||
110 | } |
||
111 | |||
112 | BLog_Finish(BLOG_CHANNEL_ncd_log_msg, level); |
||
113 | } |
||
114 | |||
115 | static int parse_level (NCDModuleInst *i, NCDValRef level_arg, int *out_level) |
||
116 | { |
||
117 | int found = 0; |
||
118 | if (NCDVal_IsString(level_arg)) { |
||
119 | found = 1; |
||
120 | |||
121 | if (NCDVal_StringEqualsId(level_arg, ModuleString(i, STRING_ERROR))) { |
||
122 | *out_level = BLOG_ERROR; |
||
123 | } |
||
124 | else if (NCDVal_StringEqualsId(level_arg, ModuleString(i, STRING_WARNING))) { |
||
125 | *out_level = BLOG_WARNING; |
||
126 | } |
||
127 | else if (NCDVal_StringEqualsId(level_arg, ModuleString(i, STRING_NOTICE))) { |
||
128 | *out_level = BLOG_NOTICE; |
||
129 | } |
||
130 | else if (NCDVal_StringEqualsId(level_arg, ModuleString(i, STRING_INFO))) { |
||
131 | *out_level = BLOG_INFO; |
||
132 | } |
||
133 | else if (NCDVal_StringEqualsId(level_arg, ModuleString(i, STRING_DEBUG))) { |
||
134 | *out_level = BLOG_DEBUG; |
||
135 | } |
||
136 | else { |
||
137 | found = 0; |
||
138 | } |
||
139 | } |
||
140 | |||
141 | if (!found) { |
||
142 | uintmax_t level_numeric; |
||
143 | if (!ncd_read_uintmax(level_arg, &level_numeric) || !(level_numeric >= BLOG_ERROR && level_numeric <= BLOG_DEBUG)) { |
||
144 | return 0; |
||
145 | } |
||
146 | *out_level = level_numeric; |
||
147 | } |
||
148 | |||
149 | return 1; |
||
150 | } |
||
151 | |||
152 | static void rlog_func_new_common (void *vo, NCDModuleInst *i, int level, NCDValRef list, size_t start) |
||
153 | { |
||
154 | ASSERT(level >= BLOG_ERROR) |
||
155 | ASSERT(level <= BLOG_DEBUG) |
||
156 | ASSERT(check_strings(list, start)) |
||
157 | |||
158 | struct rlog_instance *o = vo; |
||
159 | o->i = i; |
||
160 | o->level = level; |
||
161 | o->list = list; |
||
162 | o->start = start; |
||
163 | |||
164 | NCDModuleInst_Backend_Up(i); |
||
165 | } |
||
166 | |||
167 | static void rlog_func_die (void *vo) |
||
168 | { |
||
169 | struct rlog_instance *o = vo; |
||
170 | |||
171 | do_log(o->level, o->list, o->start); |
||
172 | |||
173 | NCDModuleInst_Backend_Dead(o->i); |
||
174 | } |
||
175 | |||
176 | static void log_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params) |
||
177 | { |
||
178 | if (NCDVal_ListCount(params->args) < 1) { |
||
179 | ModuleLog(i, BLOG_ERROR, "missing level argument"); |
||
180 | goto fail0; |
||
181 | } |
||
182 | |||
183 | int level; |
||
184 | if (!parse_level(i, NCDVal_ListGet(params->args, 0), &level)) { |
||
185 | ModuleLog(i, BLOG_ERROR, "wrong level argument"); |
||
186 | goto fail0; |
||
187 | } |
||
188 | |||
189 | if (!check_strings(params->args, 1)) { |
||
190 | ModuleLog(i, BLOG_ERROR, "wrong string arguments"); |
||
191 | goto fail0; |
||
192 | } |
||
193 | |||
194 | do_log(level, params->args, 1); |
||
195 | |||
196 | NCDModuleInst_Backend_Up(i); |
||
197 | return; |
||
198 | |||
199 | fail0: |
||
200 | NCDModuleInst_Backend_DeadError(i); |
||
201 | } |
||
202 | |||
203 | static void log_r_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params) |
||
204 | { |
||
205 | if (NCDVal_ListCount(params->args) < 1) { |
||
206 | ModuleLog(i, BLOG_ERROR, "missing level argument"); |
||
207 | goto fail0; |
||
208 | } |
||
209 | |||
210 | int level; |
||
211 | if (!parse_level(i, NCDVal_ListGet(params->args, 0), &level)) { |
||
212 | ModuleLog(i, BLOG_ERROR, "wrong level argument"); |
||
213 | goto fail0; |
||
214 | } |
||
215 | |||
216 | if (!check_strings(params->args, 1)) { |
||
217 | ModuleLog(i, BLOG_ERROR, "wrong string arguments"); |
||
218 | goto fail0; |
||
219 | } |
||
220 | |||
221 | rlog_func_new_common(vo, i, level, params->args, 1); |
||
222 | return; |
||
223 | |||
224 | fail0: |
||
225 | NCDModuleInst_Backend_DeadError(i); |
||
226 | } |
||
227 | |||
228 | static void log_fr_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params) |
||
229 | { |
||
230 | NCDValRef level_arg; |
||
231 | NCDValRef strings_init_arg; |
||
232 | NCDValRef strings_deinit_arg; |
||
233 | if (!NCDVal_ListRead(params->args, 3, &level_arg, &strings_init_arg, &strings_deinit_arg)) { |
||
234 | ModuleLog(i, BLOG_ERROR, "wrong arity"); |
||
235 | goto fail0; |
||
236 | } |
||
237 | |||
238 | int level; |
||
239 | if (!parse_level(i, level_arg, &level)) { |
||
240 | ModuleLog(i, BLOG_ERROR, "wrong level argument"); |
||
241 | goto fail0; |
||
242 | } |
||
243 | |||
244 | if (!NCDVal_IsList(strings_init_arg) || !check_strings(strings_init_arg, 0)) { |
||
245 | ModuleLog(i, BLOG_ERROR, "wrong string_init argument"); |
||
246 | goto fail0; |
||
247 | } |
||
248 | |||
249 | if (!NCDVal_IsList(strings_deinit_arg) || !check_strings(strings_deinit_arg, 0)) { |
||
250 | ModuleLog(i, BLOG_ERROR, "wrong strings_deinit argument"); |
||
251 | goto fail0; |
||
252 | } |
||
253 | |||
254 | do_log(level, strings_init_arg, 0); |
||
255 | |||
256 | rlog_func_new_common(vo, i, level, strings_deinit_arg, 0); |
||
257 | return; |
||
258 | |||
259 | fail0: |
||
260 | NCDModuleInst_Backend_DeadError(i); |
||
261 | } |
||
262 | |||
263 | static struct NCDModule modules[] = { |
||
264 | { |
||
265 | .type = "log", |
||
266 | .func_new2 = log_func_new |
||
267 | }, { |
||
268 | .type = "log_r", |
||
269 | .func_new2 = log_r_func_new, |
||
270 | .func_die = rlog_func_die, |
||
271 | .alloc_size = sizeof(struct rlog_instance) |
||
272 | }, { |
||
273 | .type = "log_fr", |
||
274 | .func_new2 = log_fr_func_new, |
||
275 | .func_die = rlog_func_die, |
||
276 | .alloc_size = sizeof(struct rlog_instance) |
||
277 | }, { |
||
278 | .type = NULL |
||
279 | } |
||
280 | }; |
||
281 | |||
282 | const struct NCDModuleGroup ncdmodule_log = { |
||
283 | .modules = modules, |
||
284 | .strings = strings |
||
285 | }; |