Add test for logging functionality Just to make sure everything works properly. Signed-off-by: Ran Benita <ran234@gmail.com>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
diff --git a/Makefile.am b/Makefile.am
index 02c0f57..b9975e6 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -117,7 +117,8 @@ TESTS = \
test/rules-file \
test/dump \
test/stringcomp \
- test/keyseq
+ test/keyseq \
+ test/log
TESTS_LDADD = libtest.la
test_xkey_LDADD = $(TESTS_LDADD)
@@ -131,6 +132,7 @@ test_rules_file_LDADD = $(TESTS_LDADD)
test_dump_LDADD = $(TESTS_LDADD)
test_stringcomp_LDADD = $(TESTS_LDADD)
test_keyseq_LDADD = $(TESTS_LDADD)
+test_log_LDADD = $(TESTS_LDADD)
check_PROGRAMS = $(TESTS)
diff --git a/test/.gitignore b/test/.gitignore
index 0879e57..580c92f 100644
--- a/test/.gitignore
+++ b/test/.gitignore
@@ -11,3 +11,4 @@ rules-file
stringcomp
dump
keyseq
+log
diff --git a/test/log.c b/test/log.c
new file mode 100644
index 0000000..9fd9be8
--- /dev/null
+++ b/test/log.c
@@ -0,0 +1,121 @@
+/*
+ * Copyright © 2012 Ran Benita
+ *
+ * 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.
+ */
+
+#include <assert.h>
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <syslog.h>
+
+#include "test.h"
+#include "xkb-priv.h"
+
+#pragma GCC diagnostic ignored "-Wmissing-format-attribute"
+
+static const char *
+priority_to_string(int priority)
+{
+ switch (priority) {
+ case LOG_ERR:
+ return "error";
+ case LOG_WARNING:
+ return "warning";
+ case LOG_INFO:
+ return "info";
+ case LOG_DEBUG:
+ return "debug";
+ }
+
+ return "unknown";
+}
+
+ATTR_PRINTF(3, 0) static void
+log_fn(struct xkb_context *ctx, int priority, const char *fmt, va_list args)
+{
+ char *s;
+ int size;
+ darray_char *ls = xkb_get_user_data(ctx);
+ assert(ls);
+
+ size = vasprintf(&s, fmt, args);
+ assert(size != -1);
+
+ darray_append_string(*ls, priority_to_string(priority));
+ darray_append_lit(*ls, ": ");
+ darray_append_string(*ls, s);
+ free(s);
+}
+
+int
+main(void)
+{
+ darray_char log_string;
+ struct xkb_context *ctx;
+ int ret;
+
+ ret = setenv("XKB_LOG", "warn", 1);
+ ret = setenv("XKB_VERBOSITY", "5", 1);
+ assert(ret == 0);
+ ctx = test_get_context();
+ assert(ctx);
+
+ darray_init(log_string);
+ xkb_set_user_data(ctx, &log_string);
+ xkb_set_log_fn(ctx, log_fn);
+
+ log_warn(ctx, "first warning: %d\n", 87);
+ log_info(ctx, "first info\n");
+ log_dbg(ctx, "first debug: %s\n", "hello");
+ log_err(ctx, "first error: %lu\n", 115415UL);
+ log_lvl(ctx, 5, "first verbose 5\n");
+
+ xkb_set_log_priority(ctx, LOG_DEBUG);
+ log_warn(ctx, "second warning: %d\n", 87);
+ log_dbg(ctx, "second debug: %s %s\n", "hello", "world");
+ log_info(ctx, "second info\n");
+ log_err(ctx, "second error: %lu\n", 115415UL);
+ log_lvl(ctx, 6, "second verbose 6\n");
+
+ xkb_set_log_verbosity(ctx, 0);
+ xkb_set_log_priority(ctx, -1);
+ log_warn(ctx, "third warning: %d\n", 87);
+ log_dbg(ctx, "third debug: %s %s\n", "hello", "world");
+ log_info(ctx, "third info\n");
+ log_err(ctx, "third error: %lu\n", 115415UL);
+ log_lvl(ctx, 0, "third verbose 0\n");
+
+ printf("%s", log_string.item);
+
+ assert(strcmp(log_string.item,
+ "warning: first warning: 87\n"
+ "error: first error: 115415\n"
+ "warning: first verbose 5\n"
+ "warning: second warning: 87\n"
+ "debug: second debug: hello world\n"
+ "info: second info\n"
+ "error: second error: 115415\n") == 0);
+
+ xkb_context_unref(ctx);
+ darray_free(log_string);
+ return 0;
+}