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