1 /*-------------------------------------------------------------------------
2 * C-Pluff, a plug-in framework for C
3 * Copyright 2007 Johannes Lehtinen
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:
12 * The above copyright notice and this permission notice shall be included
13 * in all copies or substantial portions of the Software.
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 *-----------------------------------------------------------------------*/
29 void errorlogger(void) {
34 ctx = init_context(CP_LOG_ERROR + 1, &errors);
35 check(cp_load_plugin_descriptor(ctx, "nonexisting", &status) == NULL && status != CP_OK);
41 cp_log_severity_t max_severity;
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;
49 if (severity <= lc->max_severity) {
52 lc->count_above_max++;
56 void warninglogger(void) {
58 struct log_count_t lc = { CP_LOG_WARNING, 0, 0 };
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);
64 check(lc.count_max > 0 && lc.count_above_max == 0);
67 void infologger(void) {
69 cp_plugin_info_t *plugin;
71 struct log_count_t lc = { CP_LOG_INFO, 0, 0 };
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);
79 check(lc.count_max > 0 && lc.count_above_max == 0);
82 void debuglogger(void) {
84 struct log_count_t lc = { CP_LOG_DEBUG, 0, 0 };
86 ctx = init_context(CP_LOG_INFO, NULL);
87 check(cp_register_logger(ctx, counting_logger, &lc, CP_LOG_DEBUG) == CP_OK);
89 check(lc.count_max > 0 && lc.count_above_max == 0);
92 static void increment_logger(cp_log_severity_t severity, const char *msg, const char *apid, void *user_data) {
93 (*((int *) user_data))++;
96 void twologgers(void) {
98 cp_plugin_info_t *plugin;
100 struct log_count_t lc = { CP_LOG_DEBUG, 0, 0 };
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);
117 void unreglogger(void) {
119 cp_plugin_info_t *plugin;
121 struct log_count_t lc = { CP_LOG_DEBUG, 0, 0 };
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);
139 void updatelogger(void) {
141 cp_plugin_info_t *plugin;
143 struct log_count_t lc = { CP_LOG_DEBUG, 0, 0 };
144 struct log_count_t lc2 = { CP_LOG_INFO, 0, 0 };
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);
163 cp_log_severity_t severity;
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;
171 // Free previous data
172 if (li->msg != NULL) {
176 if (li->apid != NULL) {
182 li->severity = severity;
184 check((li->msg = strdup(msg)) != NULL);
187 check((li->apid = strdup(apid)) != NULL);
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 };
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);
201 cp_unregister_logger(ctx, store_logger);
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");
215 static void islogged_sev(cp_context_t *ctx, cp_log_severity_t severity) {
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));
226 check(!cp_is_logged(ctx, CP_LOG_DEBUG));
229 check(!cp_is_logged(ctx, CP_LOG_INFO));
232 check(!cp_is_logged(ctx, CP_LOG_WARNING));
235 cp_unregister_logger(ctx, increment_logger);
238 void islogged(void) {
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);