78d97ee3c411dc4437ed3ceebc1c76fa2a4a4752
[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 #include <syslog.h>
29
30 #include "test.h"
31 #include "xkb-priv.h"
32
33 #pragma GCC diagnostic ignored "-Wmissing-format-attribute"
34
35 static const char *
36 priority_to_string(int priority)
37 {
38     switch (priority) {
39     case LOG_ERR:
40         return "error";
41     case LOG_WARNING:
42         return "warning";
43     case LOG_INFO:
44         return "info";
45     case LOG_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, int priority, const char *fmt, va_list args)
54 {
55     char *s;
56     int size;
57     darray_char *ls = xkb_get_user_data(ctx);
58     assert(ls);
59
60     size = vasprintf(&s, fmt, args);
61     assert(size != -1);
62
63     darray_append_string(*ls, priority_to_string(priority));
64     darray_append_lit(*ls, ": ");
65     darray_append_string(*ls, s);
66     free(s);
67 }
68
69 int
70 main(void)
71 {
72     darray_char log_string;
73     struct xkb_context *ctx;
74     int ret;
75
76     ret = setenv("XKB_LOG", "warn", 1);
77     ret = setenv("XKB_VERBOSITY", "5", 1);
78     assert(ret == 0);
79     ctx = test_get_context();
80     assert(ctx);
81
82     darray_init(log_string);
83     xkb_set_user_data(ctx, &log_string);
84     xkb_set_log_fn(ctx, log_fn);
85
86     log_warn(ctx, "first warning: %d\n", 87);
87     log_info(ctx, "first info\n");
88     log_dbg(ctx, "first debug: %s\n", "hello");
89     log_err(ctx, "first error: %lu\n", 115415UL);
90     log_lvl(ctx, 5, "first verbose 5\n");
91
92     xkb_set_log_priority(ctx, LOG_DEBUG);
93     log_warn(ctx, "second warning: %d\n", 87);
94     log_dbg(ctx, "second debug: %s %s\n", "hello", "world");
95     log_info(ctx, "second info\n");
96     log_err(ctx, "second error: %lu\n", 115415UL);
97     log_lvl(ctx, 6, "second verbose 6\n");
98
99     xkb_set_log_verbosity(ctx, 0);
100     xkb_set_log_priority(ctx, -1);
101     log_warn(ctx, "third warning: %d\n", 87);
102     log_dbg(ctx, "third debug: %s %s\n", "hello", "world");
103     log_info(ctx, "third info\n");
104     log_err(ctx, "third error: %lu\n", 115415UL);
105     log_lvl(ctx, 0, "third verbose 0\n");
106
107     printf("%s", log_string.item);
108
109     assert(streq(log_string.item,
110                  "warning: first warning: 87\n"
111                  "error: first error: 115415\n"
112                  "warning: first verbose 5\n"
113                  "warning: second warning: 87\n"
114                  "debug: second debug: hello world\n"
115                  "info: second info\n"
116                  "error: second error: 115415\n"));
117
118     xkb_context_unref(ctx);
119     darray_free(log_string);
120     return 0;
121 }