iotivity 0.9.0
[platform/upstream/iotivity.git] / service / protocol-plugin / lib / cpluff / test / loggers.c
1 /*-------------------------------------------------------------------------
2  * C-Pluff, a plug-in framework for C
3  * Copyright 2007 Johannes Lehtinen
4  * 
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included
13  * in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22  *-----------------------------------------------------------------------*/
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include "test.h"
28
29 void errorlogger(void) {
30         cp_context_t *ctx;
31         cp_status_t status;
32         int errors;
33         
34         ctx = init_context(CP_LOG_ERROR + 1, &errors);
35         check(cp_load_plugin_descriptor(ctx, "nonexisting", &status) == NULL && status != CP_OK);
36         cp_destroy();
37         check(errors > 0);
38 }
39
40 struct log_count_t {
41         cp_log_severity_t max_severity;
42         int count_max;
43         int count_above_max;
44 };
45
46 static void counting_logger(cp_log_severity_t severity, const char *msg, const char *apid, void *user_data) {
47         struct log_count_t *lc = user_data;
48         
49         if (severity <= lc->max_severity) {
50                 lc->count_max++;
51         } else {
52                 lc->count_above_max++;
53         }
54 }
55
56 void warninglogger(void) {
57         cp_context_t *ctx;
58         struct log_count_t lc = { CP_LOG_WARNING, 0, 0 };
59         
60         ctx = init_context(CP_LOG_ERROR, NULL);
61         check(cp_register_logger(ctx, counting_logger, &lc, CP_LOG_WARNING) == CP_OK);
62         check(cp_start_plugin(ctx, "nonexisting") == CP_ERR_UNKNOWN);
63         cp_destroy();
64         check(lc.count_max > 0 && lc.count_above_max == 0);
65 }
66
67 void infologger(void) {
68         cp_context_t *ctx;
69         cp_plugin_info_t *plugin;
70         cp_status_t status;
71         struct log_count_t lc = { CP_LOG_INFO, 0, 0 };
72
73         ctx = init_context(CP_LOG_WARNING, NULL);
74         check(cp_register_logger(ctx, counting_logger, &lc, CP_LOG_INFO) == CP_OK);
75         check((plugin = cp_load_plugin_descriptor(ctx, plugindir("minimal"), &status)) != NULL && status == CP_OK);
76         check(cp_install_plugin(ctx, plugin) == CP_OK);
77         cp_release_info(ctx, plugin);
78         cp_destroy();
79         check(lc.count_max > 0 && lc.count_above_max == 0);
80 }
81
82 void debuglogger(void) {
83         cp_context_t *ctx;
84         struct log_count_t lc = { CP_LOG_DEBUG, 0, 0 };
85         
86         ctx = init_context(CP_LOG_INFO, NULL);
87         check(cp_register_logger(ctx, counting_logger, &lc, CP_LOG_DEBUG) == CP_OK);
88         cp_destroy();
89         check(lc.count_max > 0 && lc.count_above_max == 0);
90 }
91
92 static void increment_logger(cp_log_severity_t severity, const char *msg, const char *apid, void *user_data) {
93         (*((int *) user_data))++;
94 }
95
96 void twologgers(void) {
97         cp_context_t *ctx;
98         cp_plugin_info_t *plugin;
99         cp_status_t status;
100         struct log_count_t lc = { CP_LOG_DEBUG, 0, 0 };
101         int count = 0;
102         int errors;
103         
104         ctx = init_context(CP_LOG_ERROR, &errors);
105         check(cp_register_logger(ctx, counting_logger, &lc, CP_LOG_DEBUG) == CP_OK);
106         check(count == 0 && lc.count_max > 0 && lc.count_above_max == 0);
107         check(cp_register_logger(ctx, increment_logger, &count, CP_LOG_INFO) == CP_OK);
108         check(count == 0 && lc.count_max > 0 && lc.count_above_max == 0);
109         check((plugin = cp_load_plugin_descriptor(ctx, plugindir("minimal"), &status)) != NULL && status == CP_OK);
110         check(cp_install_plugin(ctx, plugin) == CP_OK);
111         cp_release_info(ctx, plugin);
112         check(count > 0 && lc.count_max > 0 && lc.count_above_max > 0);
113         cp_destroy();
114         check(errors == 0);
115 }
116
117 void unreglogger(void) {
118         cp_context_t *ctx;
119         cp_plugin_info_t *plugin;
120         cp_status_t status;
121         struct log_count_t lc = { CP_LOG_DEBUG, 0, 0 };
122         int count = 0;
123         int errors;
124         
125         ctx = init_context(CP_LOG_ERROR, &errors);
126         check(cp_register_logger(ctx, counting_logger, &lc, CP_LOG_DEBUG) == CP_OK);
127         check(count == 0 && lc.count_max > 0 && lc.count_above_max == 0);
128         check(cp_register_logger(ctx, increment_logger, &count, CP_LOG_INFO) == CP_OK);
129         check(count == 0 && lc.count_max > 0 && lc.count_above_max == 0);
130         cp_unregister_logger(ctx, counting_logger);
131         check((plugin = cp_load_plugin_descriptor(ctx, plugindir("minimal"), &status)) != NULL && status == CP_OK);
132         check(cp_install_plugin(ctx, plugin) == CP_OK);
133         cp_release_info(ctx, plugin);
134         check(count > 0 && lc.count_max > 0 && lc.count_above_max == 0);
135         cp_destroy();
136         check(errors == 0);
137 }
138
139 void updatelogger(void) {
140         cp_context_t *ctx;
141         cp_plugin_info_t *plugin;
142         cp_status_t status;
143         struct log_count_t lc = { CP_LOG_DEBUG, 0, 0 };
144         struct log_count_t lc2 = { CP_LOG_INFO, 0, 0 };
145         int count = 0;
146         int errors;
147         
148         ctx = init_context(CP_LOG_ERROR, &errors);
149         check(cp_register_logger(ctx, counting_logger, &lc, CP_LOG_DEBUG) == CP_OK);
150         check(count == 0 && lc.count_max > 0 && lc.count_above_max == 0);
151         check(cp_register_logger(ctx, increment_logger, &count, CP_LOG_INFO) == CP_OK);
152         check(count == 0 && lc.count_max > 0 && lc.count_above_max == 0);
153         check(cp_register_logger(ctx, counting_logger, &lc2, CP_LOG_DEBUG) == CP_OK);
154         check((plugin = cp_load_plugin_descriptor(ctx, plugindir("minimal"), &status)) != NULL && status == CP_OK);
155         check(cp_install_plugin(ctx, plugin) == CP_OK);
156         cp_release_info(ctx, plugin);
157         check(count > 0 && lc.count_max > 0 && lc.count_above_max == 0);
158         cp_destroy();
159         check(errors == 0);
160 }
161
162 struct log_info_t {
163         cp_log_severity_t severity;
164         char *msg;
165         char *apid;
166 };
167
168 static void store_logger(cp_log_severity_t severity, const char *msg, const char *apid, void *user_data) {
169         struct log_info_t *li = user_data;
170         
171         // Free previous data
172         if (li->msg != NULL) {
173                 free(li->msg);
174                 li->msg = NULL;
175         }
176         if (li->apid != NULL) {
177                 free(li->apid);
178                 li->apid = NULL;
179         }
180         
181         // Copy information
182         li->severity = severity;
183         if (msg != NULL) {
184                 check((li->msg = strdup(msg)) != NULL);
185         }
186         if (apid != NULL) {
187                 check((li->apid = strdup(apid)) != NULL);
188         }
189 }
190
191 static void logmsg_sev(cp_context_t *ctx, cp_log_severity_t severity, const char *msg) {
192         struct log_info_t li = { -1, NULL, NULL };
193
194         check(cp_register_logger(ctx, store_logger, &li, CP_LOG_DEBUG) == CP_OK);
195         cp_log(ctx, severity, msg);
196         check(li.severity == severity);
197         check(li.msg != NULL && !strcmp(li.msg, msg));
198         check(li.apid == NULL);
199         free(li.msg);
200         li.msg = NULL;
201         cp_unregister_logger(ctx, store_logger);
202 }
203
204 void logmsg(void) {
205         cp_context_t *ctx;
206         
207         ctx = init_context(CP_LOG_ERROR + 1, NULL);
208         logmsg_sev(ctx, CP_LOG_DEBUG, "debug");
209         logmsg_sev(ctx, CP_LOG_INFO, "info");
210         logmsg_sev(ctx, CP_LOG_WARNING, "warning");
211         logmsg_sev(ctx, CP_LOG_ERROR, "error");
212         cp_destroy();
213 }
214
215 static void islogged_sev(cp_context_t *ctx, cp_log_severity_t severity) {
216         int count = 0;
217         
218         check(!cp_is_logged(ctx, severity));
219         check(cp_register_logger(ctx, increment_logger, &count, severity) == CP_OK);
220         check(cp_is_logged(ctx, CP_LOG_ERROR));
221         check(cp_is_logged(ctx, severity));
222         switch (severity) {
223                 case CP_LOG_DEBUG:
224                         break;
225                 case CP_LOG_INFO:
226                         check(!cp_is_logged(ctx, CP_LOG_DEBUG));
227                         break;
228                 case CP_LOG_WARNING:
229                         check(!cp_is_logged(ctx, CP_LOG_INFO));
230                         break;
231                 case CP_LOG_ERROR:
232                         check(!cp_is_logged(ctx, CP_LOG_WARNING));
233                         break;
234         }
235         cp_unregister_logger(ctx, increment_logger);
236 }
237
238 void islogged(void) {
239         cp_context_t *ctx;
240         
241         ctx = init_context(CP_LOG_ERROR + 1, NULL);
242         islogged_sev(ctx, CP_LOG_DEBUG);
243         islogged_sev(ctx, CP_LOG_INFO);
244         islogged_sev(ctx, CP_LOG_WARNING);
245         islogged_sev(ctx, CP_LOG_ERROR);
246         cp_destroy();
247 }