Add xkb_map_mod_mask_remove_consumed
[platform/upstream/libxkbcommon.git] / test / log.c
1 /*
2  * Copyright © 2012 Ran Benita
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  */
23
24 #include <assert.h>
25 #include <stdarg.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28
29 #include "test.h"
30 #include "xkb-priv.h"
31
32 #pragma GCC diagnostic ignored "-Wmissing-format-attribute"
33
34 static const char *
35 priority_to_string(int priority)
36 {
37     switch (priority) {
38     case XKB_LOG_LEVEL_ERROR:
39         return "error";
40     case XKB_LOG_LEVEL_WARNING:
41         return "warning";
42     case XKB_LOG_LEVEL_INFO:
43         return "info";
44     case XKB_LOG_LEVEL_DEBUG:
45         return "debug";
46     }
47
48     return "unknown";
49 }
50
51 ATTR_PRINTF(3, 0) static void
52 log_fn(struct xkb_context *ctx, int priority, const char *fmt, va_list args)
53 {
54     char *s;
55     int size;
56     darray_char *ls = xkb_get_user_data(ctx);
57     assert(ls);
58
59     size = vasprintf(&s, fmt, args);
60     assert(size != -1);
61
62     darray_append_string(*ls, priority_to_string(priority));
63     darray_append_lit(*ls, ": ");
64     darray_append_string(*ls, s);
65     free(s);
66 }
67
68 int
69 main(void)
70 {
71     darray_char log_string;
72     struct xkb_context *ctx;
73     int ret;
74
75     ret = setenv("XKB_LOG", "warn", 1);
76     ret = setenv("XKB_VERBOSITY", "5", 1);
77     assert(ret == 0);
78     ctx = xkb_context_new(0);
79     assert(ctx);
80
81     darray_init(log_string);
82     xkb_set_user_data(ctx, &log_string);
83     xkb_set_log_fn(ctx, log_fn);
84
85     log_warn(ctx, "first warning: %d\n", 87);
86     log_info(ctx, "first info\n");
87     log_dbg(ctx, "first debug: %s\n", "hello");
88     log_err(ctx, "first error: %lu\n", 115415UL);
89     log_lvl(ctx, 5, "first verbose 5\n");
90
91     xkb_set_log_priority(ctx, XKB_LOG_LEVEL_DEBUG);
92     log_warn(ctx, "second warning: %d\n", 87);
93     log_dbg(ctx, "second debug: %s %s\n", "hello", "world");
94     log_info(ctx, "second info\n");
95     log_err(ctx, "second error: %lu\n", 115415UL);
96     log_lvl(ctx, 6, "second verbose 6\n");
97
98     xkb_set_log_verbosity(ctx, 0);
99     xkb_set_log_priority(ctx, XKB_LOG_LEVEL_CRITICAL);
100     log_warn(ctx, "third warning: %d\n", 87);
101     log_dbg(ctx, "third debug: %s %s\n", "hello", "world");
102     log_info(ctx, "third info\n");
103     log_err(ctx, "third error: %lu\n", 115415UL);
104     log_lvl(ctx, 0, "third verbose 0\n");
105
106     printf("%s", log_string.item);
107
108     assert(streq(log_string.item,
109                  "warning: first warning: 87\n"
110                  "error: first error: 115415\n"
111                  "warning: first verbose 5\n"
112                  "warning: second warning: 87\n"
113                  "debug: second debug: hello world\n"
114                  "info: second info\n"
115                  "error: second error: 115415\n"));
116
117     xkb_context_unref(ctx);
118     darray_free(log_string);
119     return 0;
120 }