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