2012-07-22 10:38:14 -06:00
|
|
|
/*
|
2012-09-12 09:54:07 -06:00
|
|
|
* Copyright © 2012 Ran Benita <ran234@gmail.com>
|
2012-07-22 10:38:14 -06:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
* copy of this software and associated documentation files (the "Software"),
|
|
|
|
* to deal in the Software without restriction, including without limitation
|
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
* Software is furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice (including the next
|
|
|
|
* paragraph) shall be included in all copies or substantial portions of the
|
|
|
|
* Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
* DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2019-12-27 04:03:20 -07:00
|
|
|
#include "config.h"
|
|
|
|
|
2012-07-22 10:38:14 -06:00
|
|
|
#include "test.h"
|
2012-09-16 05:45:32 -06:00
|
|
|
#include "context.h"
|
2023-09-21 12:06:27 -06:00
|
|
|
#include "messages-codes.h"
|
2012-07-22 10:38:14 -06:00
|
|
|
|
2019-12-27 06:03:10 -07:00
|
|
|
#ifdef __GNUC__
|
2012-07-22 10:38:14 -06:00
|
|
|
#pragma GCC diagnostic ignored "-Wmissing-format-attribute"
|
2019-12-27 06:03:10 -07:00
|
|
|
#endif
|
2012-07-22 10:38:14 -06:00
|
|
|
|
|
|
|
static const char *
|
2012-08-27 15:42:59 -06:00
|
|
|
log_level_to_string(enum xkb_log_level level)
|
2012-07-22 10:38:14 -06:00
|
|
|
{
|
2012-08-27 15:42:59 -06:00
|
|
|
switch (level) {
|
|
|
|
case XKB_LOG_LEVEL_CRITICAL:
|
|
|
|
return "critical";
|
2012-08-08 06:01:46 -06:00
|
|
|
case XKB_LOG_LEVEL_ERROR:
|
2012-07-22 10:38:14 -06:00
|
|
|
return "error";
|
2012-08-08 06:01:46 -06:00
|
|
|
case XKB_LOG_LEVEL_WARNING:
|
2012-07-22 10:38:14 -06:00
|
|
|
return "warning";
|
2012-08-08 06:01:46 -06:00
|
|
|
case XKB_LOG_LEVEL_INFO:
|
2012-07-22 10:38:14 -06:00
|
|
|
return "info";
|
2012-08-08 06:01:46 -06:00
|
|
|
case XKB_LOG_LEVEL_DEBUG:
|
2012-07-22 10:38:14 -06:00
|
|
|
return "debug";
|
|
|
|
}
|
|
|
|
|
|
|
|
return "unknown";
|
|
|
|
}
|
|
|
|
|
|
|
|
ATTR_PRINTF(3, 0) static void
|
2012-08-27 15:42:59 -06:00
|
|
|
log_fn(struct xkb_context *ctx, enum xkb_log_level level,
|
|
|
|
const char *fmt, va_list args)
|
2012-07-22 10:38:14 -06:00
|
|
|
{
|
|
|
|
char *s;
|
|
|
|
int size;
|
2012-09-22 01:58:00 -06:00
|
|
|
darray_char *ls = xkb_context_get_user_data(ctx);
|
2012-07-22 10:38:14 -06:00
|
|
|
assert(ls);
|
|
|
|
|
|
|
|
size = vasprintf(&s, fmt, args);
|
|
|
|
assert(size != -1);
|
|
|
|
|
2012-08-27 15:42:59 -06:00
|
|
|
darray_append_string(*ls, log_level_to_string(level));
|
2012-07-22 10:38:14 -06:00
|
|
|
darray_append_lit(*ls, ": ");
|
|
|
|
darray_append_string(*ls, s);
|
|
|
|
free(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(void)
|
|
|
|
{
|
|
|
|
darray_char log_string;
|
|
|
|
struct xkb_context *ctx;
|
|
|
|
int ret;
|
|
|
|
|
2012-10-12 02:15:43 -06:00
|
|
|
ret = setenv("XKB_LOG_LEVEL", "warn", 1);
|
2012-08-27 15:42:59 -06:00
|
|
|
assert(ret == 0);
|
2012-10-12 02:15:43 -06:00
|
|
|
ret = setenv("XKB_LOG_VERBOSITY", "5", 1);
|
2012-07-22 10:38:14 -06:00
|
|
|
assert(ret == 0);
|
2013-03-18 15:03:00 -06:00
|
|
|
ctx = test_get_context(0);
|
2012-07-22 10:38:14 -06:00
|
|
|
assert(ctx);
|
|
|
|
|
|
|
|
darray_init(log_string);
|
2012-09-22 01:58:00 -06:00
|
|
|
xkb_context_set_user_data(ctx, &log_string);
|
|
|
|
xkb_context_set_log_fn(ctx, log_fn);
|
2012-07-22 10:38:14 -06:00
|
|
|
|
2023-09-21 12:06:27 -06:00
|
|
|
log_warn(ctx, XKB_LOG_MESSAGE_NO_ID, "first warning: %d\n", 87);
|
|
|
|
log_info(ctx, XKB_LOG_MESSAGE_NO_ID, "first info\n");
|
|
|
|
log_dbg(ctx, XKB_LOG_MESSAGE_NO_ID, "first debug: %s\n", "hello");
|
|
|
|
log_err(ctx, XKB_LOG_MESSAGE_NO_ID, "first error: %lu\n", 115415UL);
|
2023-09-21 12:06:27 -06:00
|
|
|
log_vrb(ctx, 5, XKB_LOG_MESSAGE_NO_ID, "first verbose 5\n");
|
2012-07-22 10:38:14 -06:00
|
|
|
|
2012-09-22 01:58:00 -06:00
|
|
|
xkb_context_set_log_level(ctx, XKB_LOG_LEVEL_DEBUG);
|
2023-09-21 12:06:27 -06:00
|
|
|
log_warn(ctx, XKB_LOG_MESSAGE_NO_ID, "second warning: %d\n", 87);
|
|
|
|
log_dbg(ctx, XKB_LOG_MESSAGE_NO_ID, "second debug: %s %s\n", "hello", "world");
|
|
|
|
log_info(ctx, XKB_LOG_MESSAGE_NO_ID, "second info\n");
|
|
|
|
log_err(ctx, XKB_ERROR_MALFORMED_NUMBER_LITERAL, "second error: %lu\n", 115415UL);
|
2023-09-21 12:06:27 -06:00
|
|
|
log_vrb(ctx, 6, XKB_LOG_MESSAGE_NO_ID, "second verbose 6\n");
|
2012-07-22 10:38:14 -06:00
|
|
|
|
2012-09-22 01:58:00 -06:00
|
|
|
xkb_context_set_log_verbosity(ctx, 0);
|
|
|
|
xkb_context_set_log_level(ctx, XKB_LOG_LEVEL_CRITICAL);
|
2023-09-21 12:06:27 -06:00
|
|
|
log_warn(ctx, XKB_LOG_MESSAGE_NO_ID, "third warning: %d\n", 87);
|
|
|
|
log_dbg(ctx, XKB_LOG_MESSAGE_NO_ID, "third debug: %s %s\n", "hello", "world");
|
|
|
|
log_info(ctx, XKB_LOG_MESSAGE_NO_ID, "third info\n");
|
|
|
|
log_err(ctx, XKB_LOG_MESSAGE_NO_ID, "third error: %lu\n", 115415UL);
|
2023-09-21 12:06:27 -06:00
|
|
|
log_vrb(ctx, 0, XKB_LOG_MESSAGE_NO_ID, "third verbose 0\n");
|
2012-07-22 10:38:14 -06:00
|
|
|
|
|
|
|
printf("%s", log_string.item);
|
|
|
|
|
2012-07-24 10:54:14 -06:00
|
|
|
assert(streq(log_string.item,
|
|
|
|
"warning: first warning: 87\n"
|
|
|
|
"error: first error: 115415\n"
|
2023-09-21 12:06:27 -06:00
|
|
|
"warning: first verbose 5\n"
|
2012-07-24 10:54:14 -06:00
|
|
|
"warning: second warning: 87\n"
|
|
|
|
"debug: second debug: hello world\n"
|
|
|
|
"info: second info\n"
|
2023-09-21 12:06:27 -06:00
|
|
|
"error: [XKB-034] second error: 115415\n"));
|
2012-07-22 10:38:14 -06:00
|
|
|
|
|
|
|
xkb_context_unref(ctx);
|
|
|
|
darray_free(log_string);
|
|
|
|
return 0;
|
|
|
|
}
|