Add a new warning for numeric keysyms
[platform/upstream/libxkbcommon.git] / test / log.c
1 /*
2  * Copyright © 2012 Ran Benita <ran234@gmail.com>
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 "config.h"
25
26 #include "test.h"
27 #include "context.h"
28
29 #ifdef __GNUC__
30 #pragma GCC diagnostic ignored "-Wmissing-format-attribute"
31 #endif
32
33 static const char *
34 log_level_to_string(enum xkb_log_level level)
35 {
36     switch (level) {
37     case XKB_LOG_LEVEL_CRITICAL:
38         return "critical";
39     case XKB_LOG_LEVEL_ERROR:
40         return "error";
41     case XKB_LOG_LEVEL_WARNING:
42         return "warning";
43     case XKB_LOG_LEVEL_INFO:
44         return "info";
45     case XKB_LOG_LEVEL_DEBUG:
46         return "debug";
47     }
48
49     return "unknown";
50 }
51
52 ATTR_PRINTF(3, 0) static void
53 log_fn(struct xkb_context *ctx, enum xkb_log_level level,
54        const char *fmt, va_list args)
55 {
56     char *s;
57     int size;
58     darray_char *ls = xkb_context_get_user_data(ctx);
59     assert(ls);
60
61     size = vasprintf(&s, fmt, args);
62     assert(size != -1);
63
64     darray_append_string(*ls, log_level_to_string(level));
65     darray_append_lit(*ls, ": ");
66     darray_append_string(*ls, s);
67     free(s);
68 }
69
70 int
71 main(void)
72 {
73     darray_char log_string;
74     struct xkb_context *ctx;
75     int ret;
76
77     ret = setenv("XKB_LOG_LEVEL", "warn", 1);
78     assert(ret == 0);
79     ret = setenv("XKB_LOG_VERBOSITY", "5", 1);
80     assert(ret == 0);
81     ctx = test_get_context(0);
82     assert(ctx);
83
84     darray_init(log_string);
85     xkb_context_set_user_data(ctx, &log_string);
86     xkb_context_set_log_fn(ctx, log_fn);
87
88     log_warn(ctx, "first warning: %d\n", 87);
89     log_info(ctx, "first info\n");
90     log_dbg(ctx, "first debug: %s\n", "hello");
91     log_err(ctx, "first error: %lu\n", 115415UL);
92     log_vrb(ctx, 5, "first verbose 5\n");
93
94     xkb_context_set_log_level(ctx, XKB_LOG_LEVEL_DEBUG);
95     log_warn(ctx, "second warning: %d\n", 87);
96     log_dbg(ctx, "second debug: %s %s\n", "hello", "world");
97     log_info(ctx, "second info\n");
98     log_err(ctx, "second error: %lu\n", 115415UL);
99     log_vrb(ctx, 6, "second verbose 6\n");
100
101     xkb_context_set_log_verbosity(ctx, 0);
102     xkb_context_set_log_level(ctx, XKB_LOG_LEVEL_CRITICAL);
103     log_warn(ctx, "third warning: %d\n", 87);
104     log_dbg(ctx, "third debug: %s %s\n", "hello", "world");
105     log_info(ctx, "third info\n");
106     log_err(ctx, "third error: %lu\n", 115415UL);
107     log_vrb(ctx, 0, "third verbose 0\n");
108
109     printf("%s", log_string.item);
110
111     assert(streq(log_string.item,
112                  "warning: first warning: 87\n"
113                  "error: first error: 115415\n"
114                  "warning: first verbose 5\n"
115                  "warning: second warning: 87\n"
116                  "debug: second debug: hello world\n"
117                  "info: second info\n"
118                  "error: second error: 115415\n"));
119
120     xkb_context_unref(ctx);
121     darray_free(log_string);
122     return 0;
123 }