Add logging API Add new public API to provide the library users with some options to control and customize the logging output from the library. It is based upon the skeleton from the libabc demo libray: https://git.kernel.org/?p=linux/kernel/git/kay/libabc.git which is public domain and works pretty well. This requires passing in the context object in every logging call, and thus the conversion is done file by file. We also remove the global warningLevel variable in favor of a verbosity level in the context, which can be set by the user and is silent by default. One issue is the ACTION calls, which, while nice, do not play very well with line- and priority-based logging, and would require some line continuation handling or keeping state or some other compromise. So instead remove these and just inline them with their respective warning/error. So instead of: ERROR("Memory allocation failed\n") ACTION("Removing all files on hardisk\n") its something like that: log_err("Memory allocation failed; Removing all files on harddisk\n") 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 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546
diff --git a/Makefile.am b/Makefile.am
index ca715fe..02c0f57 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -75,7 +75,6 @@ libxkbcommon_la_SOURCES = \
src/state.c \
src/text.c \
src/text.h \
- src/utils.c \
src/utils.h \
src/xkb-priv.h
diff --git a/src/context.c b/src/context.c
index a0bb9a9..18a8222 100644
--- a/src/context.c
+++ b/src/context.c
@@ -25,8 +25,12 @@
#include <sys/types.h>
#include <sys/stat.h>
+#include <ctype.h>
+#include <errno.h>
+#include <stdarg.h>
#include <stdio.h>
#include <unistd.h>
+#include <syslog.h>
#include "xkb-priv.h"
#include "atom.h"
@@ -34,6 +38,12 @@
struct xkb_context {
int refcnt;
+ ATTR_PRINTF(3, 0) void (*log_fn)(struct xkb_context *ctx, int priority,
+ const char *fmt, va_list args);
+ int log_priority;
+ int log_verbosity;
+ void *user_data;
+
darray(char *) includes;
/* xkbcomp needs to assign sequential IDs to XkbFile's it creates. */
@@ -175,18 +185,97 @@ xkb_context_unref(struct xkb_context *ctx)
free(ctx);
}
+static const char *
+priority_to_prefix(int priority)
+{
+ switch (priority) {
+ case LOG_DEBUG:
+ return "Debug:";
+ case LOG_INFO:
+ return "Info:";
+ case LOG_WARNING:
+ return "Warning:";
+ case LOG_ERR:
+ return "Error:";
+ case LOG_CRIT:
+ case LOG_ALERT:
+ case LOG_EMERG:
+ return "Internal error:";
+ default:
+ return "";
+ }
+}
+
+ATTR_PRINTF(3, 0) static void
+default_log_fn(struct xkb_context *ctx, int priority,
+ const char *fmt, va_list args)
+{
+ const char *prefix = priority_to_prefix(priority);
+
+ if (prefix)
+ fprintf(stderr, "%-15s", prefix);
+ vfprintf(stderr, fmt, args);
+}
+
+static int
+log_priority(const char *priority) {
+ char *endptr;
+ int prio;
+
+ errno = 0;
+ prio = strtol(priority, &endptr, 10);
+ if (errno == 0 && (endptr[0] == '\0' || isspace(endptr[0])))
+ return prio;
+ if (strncasecmp(priority, "err", 3) == 0)
+ return LOG_ERR;
+ if (strncasecmp(priority, "warn", 4) == 0)
+ return LOG_WARNING;
+ if (strncasecmp(priority, "info", 4) == 0)
+ return LOG_INFO;
+ if (strncasecmp(priority, "debug", 5) == 0)
+ return LOG_DEBUG;
+
+ return LOG_ERR;
+}
+
+static int
+log_verbosity(const char *verbosity) {
+ char *endptr;
+ int v;
+
+ errno = 0;
+ v = strtol(verbosity, &endptr, 10);
+ if (errno == 0)
+ return v;
+
+ return 0;
+}
+
/**
* Create a new context.
*/
XKB_EXPORT struct xkb_context *
xkb_context_new(enum xkb_context_flags flags)
{
+ const char *env;
struct xkb_context *ctx = calloc(1, sizeof(*ctx));
if (!ctx)
return NULL;
ctx->refcnt = 1;
+ ctx->log_fn = default_log_fn;
+ ctx->log_priority = LOG_ERR;
+ ctx->log_verbosity = 0;
+
+ /* Environment overwrites defaults. */
+ env = getenv("XKB_LOG");
+ if (env)
+ xkb_set_log_priority(ctx, log_priority(env));
+
+ env = getenv("XKB_VERBOSITY");
+ if (env)
+ xkb_set_log_verbosity(ctx, log_verbosity(env));
if (!(flags & XKB_CONTEXT_NO_DEFAULT_INCLUDES) &&
!xkb_context_include_path_append_default(ctx)) {
@@ -220,3 +309,63 @@ xkb_atom_text(struct xkb_context *ctx, xkb_atom_t atom)
{
return atom_text(ctx->atom_table, atom);
}
+
+void
+xkb_log(struct xkb_context *ctx, int priority, const char *fmt, ...)
+{
+ va_list args;
+
+ va_start(args, fmt);
+ /* NOTE: This test will be removed in a few commits. */
+ if (ctx)
+ ctx->log_fn(ctx, priority, fmt, args);
+ else
+ default_log_fn(ctx, priority, fmt, args);
+ va_end(args);
+}
+
+XKB_EXPORT void
+xkb_set_log_fn(struct xkb_context *ctx,
+ void (*log_fn)(struct xkb_context *ctx, int priority,
+ const char *fmt, va_list args))
+{
+ ctx->log_fn = log_fn;
+}
+
+XKB_EXPORT int
+xkb_get_log_priority(struct xkb_context *ctx)
+{
+ return ctx->log_priority;
+}
+
+XKB_EXPORT void
+xkb_set_log_priority(struct xkb_context *ctx, int priority)
+{
+ ctx->log_priority = priority;
+}
+
+XKB_EXPORT int
+xkb_get_log_verbosity(struct xkb_context *ctx)
+{
+ return ctx->log_verbosity;
+}
+
+XKB_EXPORT void
+xkb_set_log_verbosity(struct xkb_context *ctx, int verbosity)
+{
+ ctx->log_verbosity = verbosity;
+}
+
+XKB_EXPORT void *
+xkb_get_user_data(struct xkb_context *ctx)
+{
+ if (ctx)
+ return ctx->user_data;
+ return NULL;
+}
+
+XKB_EXPORT void
+xkb_set_user_data(struct xkb_context *ctx, void *user_data)
+{
+ ctx->user_data = user_data;
+}
diff --git a/src/utils.c b/src/utils.c
deleted file mode 100644
index d1f79bb..0000000
--- a/src/utils.c
+++ /dev/null
@@ -1,154 +0,0 @@
-/*\
- *
- * COPYRIGHT 1990
- * DIGITAL EQUIPMENT CORPORATION
- * MAYNARD, MASSACHUSETTS
- * ALL RIGHTS RESERVED.
- *
- * THE INFORMATION IN THIS SOFTWARE IS SUBJECT TO CHANGE WITHOUT NOTICE AND
- * SHOULD NOT BE CONSTRUED AS A COMMITMENT BY DIGITAL EQUIPMENT CORPORATION.
- * DIGITAL MAKES NO REPRESENTATIONS ABOUT THE SUITABILITY OF THIS SOFTWARE
- * FOR ANY PURPOSE. IT IS SUPPLIED "AS IS" WITHOUT EXPRESS OR IMPLIED
- * WARRANTY.
- *
- * IF THE SOFTWARE IS MODIFIED IN A MANNER CREATING DERIVATIVE COPYRIGHT
- * RIGHTS, APPROPRIATE LEGENDS MAY BE PLACED ON THE DERIVATIVE WORK IN
- * ADDITION TO THAT SET FORTH ABOVE.
- *
- * Permission to use, copy, modify, and distribute this software and its
- * documentation for any purpose and without fee is hereby granted, provided
- * that the above copyright notice appear in all copies and that both that
- * copyright notice and this permission notice appear in supporting
- * documentation, and that the name of Digital Equipment Corporation not be
- * used in advertising or publicity pertaining to distribution of the
- * software without specific, written prior permission.
- \*/
-
-#include <ctype.h>
-#include <stdlib.h>
-#include <stdarg.h>
-#include <string.h>
-
-#include "utils.h"
-
-static FILE *errorFile = NULL;
-static int outCount = 0;
-static char *preMsg = NULL;
-static char *prefix = NULL;
-
-bool
-uSetErrorFile(char *name)
-{
- if ((errorFile != NULL) && (errorFile != stderr)) {
- fprintf(errorFile, "switching to %s\n", name ? name : "stderr");
- fclose(errorFile);
- }
- if (name != NULL)
- errorFile = fopen(name, "w");
- else
- errorFile = stderr;
- if (errorFile == NULL) {
- errorFile = stderr;
- return false;
- }
- return true;
-}
-
-void
-uInformation(const char *s, ...)
-{
- va_list args;
-
- if (!errorFile)
- errorFile = stderr;
-
- va_start(args, s);
- vfprintf(errorFile, s, args);
- va_end(args);
- fflush(errorFile);
-}
-
-/***====================================================================***/
-
-void
-uAction(const char *s, ...)
-{
- va_list args;
-
- if (!errorFile)
- errorFile = stderr;
-
- if (prefix != NULL)
- fprintf(errorFile, "%s", prefix);
- fprintf(errorFile, " ");
- va_start(args, s);
- vfprintf(errorFile, s, args);
- va_end(args);
- fflush(errorFile);
-}
-
-/***====================================================================***/
-
-void
-uWarning(const char *s, ...)
-{
- va_list args;
-
- if (!errorFile)
- errorFile = stderr;
-
- if ((outCount == 0) && (preMsg != NULL))
- fprintf(errorFile, "%s\n", preMsg);
- if (prefix != NULL)
- fprintf(errorFile, "%s", prefix);
- fprintf(errorFile, "Warning: ");
- va_start(args, s);
- vfprintf(errorFile, s, args);
- va_end(args);
- fflush(errorFile);
- outCount++;
-}
-
-/***====================================================================***/
-
-void
-uError(const char *s, ...)
-{
- va_list args;
-
- if (!errorFile)
- errorFile = stderr;
-
- if ((outCount == 0) && (preMsg != NULL))
- fprintf(errorFile, "%s\n", preMsg);
- if (prefix != NULL)
- fprintf(errorFile, "%s", prefix);
- fprintf(errorFile, "Error: ");
- va_start(args, s);
- vfprintf(errorFile, s, args);
- va_end(args);
- fflush(errorFile);
- outCount++;
-}
-
-/***====================================================================***/
-
-void
-uInternalError(const char *s, ...)
-{
- va_list args;
-
- if (!errorFile)
- errorFile = stderr;
-
- if ((outCount == 0) && (preMsg != NULL))
- fprintf(errorFile, "%s\n", preMsg);
- if (prefix != NULL)
- fprintf(errorFile, "%s", prefix);
- fprintf(errorFile, "Internal error: ");
- va_start(args, s);
- vfprintf(errorFile, s, args);
- va_end(args);
- fflush(errorFile);
- outCount++;
-}
diff --git a/src/utils.h b/src/utils.h
index 4ea703a..7d4e612 100644
--- a/src/utils.h
+++ b/src/utils.h
@@ -29,10 +29,6 @@
/***====================================================================***/
-#include <stdbool.h>
-#include <stdio.h>
-#include <stdlib.h>
-
/*
* We sometimes malloc strings and then expose them as const char*'s. This
* macro is used when we free these strings in order to avoid -Wcast-qual
@@ -73,33 +69,10 @@
#define ATTR_MALLOC
#endif
-extern bool
-uSetErrorFile(char *name);
-
-#define INFO uInformation
-
-ATTR_PRINTF(1, 2) void
-uInformation(const char *s, ...);
-
-#define ACTION uAction
-
-ATTR_PRINTF(1, 2) void
-uAction(const char *s, ...);
-
-#define WARN uWarning
-
-ATTR_PRINTF(1, 2) void
-uWarning(const char *s, ...);
-
-#define ERROR uError
-
-ATTR_PRINTF(1, 2) void
-uError(const char *s, ...);
-
-/* WSGO stands for "Weird Stuff Going On" (wtf???) */
-#define WSGO uInternalError
-
-ATTR_PRINTF(1, 2) void
-uInternalError(const char *s, ...);
+#define INFO(...) xkb_log(NULL, LOG_INFO, __VA_ARGS__)
+#define WARN(...) xkb_log(NULL, LOG_WARNING, __VA_ARGS__)
+#define ERROR(...) xkb_log(NULL, LOG_ERR, __VA_ARGS__)
+#define WSGO(...) xkb_log(NULL, LOG_CRIT, __VA_ARGS__)
+#define ACTION(...) xkb_log(NULL, -1, __VA_ARGS__)
#endif /* UTILS_H */
diff --git a/src/xkb-priv.h b/src/xkb-priv.h
index 55a89f2..d1b135d 100644
--- a/src/xkb-priv.h
+++ b/src/xkb-priv.h
@@ -82,6 +82,7 @@
#include <stdbool.h>
#include <string.h>
#include <strings.h>
+#include <syslog.h>
#include <X11/extensions/XKB.h>
#include <X11/X.h>
@@ -518,4 +519,33 @@ xkb_keysym_is_upper(xkb_keysym_t keysym);
bool
xkb_keysym_is_keypad(xkb_keysym_t keysym);
+ATTR_PRINTF(3, 4) void
+xkb_log(struct xkb_context *ctx, int priority, const char *fmt, ...);
+
+#define xkb_log_cond(ctx, prio, ...) \
+ do { \
+ if (xkb_get_log_priority(ctx) >= (prio)) \
+ xkb_log((ctx), (prio), __VA_ARGS__); \
+ } while (0)
+
+#define xkb_log_cond_lvl(ctx, prio, lvl, ...) \
+ do { \
+ if (xkb_get_log_verbosity(ctx) >= (lvl)) \
+ xkb_log_cond((ctx), (prio), __VA_ARGS__); \
+ } while (0)
+
+/*
+ * The format is not part of the argument list in order to avoid the
+ * "ISO C99 requires rest arguments to be used" warning when only the
+ * format is supplied without arguments. Not supplying it would still
+ * result in an error, though.
+ */
+#define log_dbg(ctx, ...) xkb_log_cond((ctx), LOG_DEBUG, __VA_ARGS__)
+#define log_info(ctx, ...) xkb_log_cond((ctx), LOG_INFO, __VA_ARGS__)
+#define log_warn(ctx, ...) xkb_log_cond((ctx), LOG_WARNING, __VA_ARGS__)
+#define log_err(ctx, ...) xkb_log_cond((ctx), LOG_ERR, __VA_ARGS__)
+#define log_wsgo(ctx, ...) xkb_log_cond((ctx), LOG_CRIT, __VA_ARGS__)
+#define log_lvl(ctx, lvl, ...) \
+ xkb_log_cond_lvl((ctx), LOG_WARNING, (lvl), __VA_ARGS__)
+
#endif /* XKB_PRIV_H */
diff --git a/xkbcommon/xkbcommon.h b/xkbcommon/xkbcommon.h
index 42f70ad..8c772ae 100644
--- a/xkbcommon/xkbcommon.h
+++ b/xkbcommon/xkbcommon.h
@@ -249,6 +249,77 @@ xkb_context_unref(struct xkb_context *context);
/** @} */
/**
+ * @defgroup logging Logging handling
+ * These functions allow you to manipulate how logging from this library
+ * will be handled.
+ *
+ * @{
+ */
+
+/**
+ * Sets the function to be called for logging messages, instead of the
+ * default logger which writes to stderr.
+ **/
+void
+xkb_set_log_fn(struct xkb_context *context,
+ void (*log_fn)(struct xkb_context *context, int priority,
+ const char *format, va_list args));
+/**
+ * Sets the current logging priority. The value controls which messages
+ * are logged.
+ *
+ * The value should be one of LOG_ERR, LOG_WARNING, LOG_DEBUG, etc., see
+ * syslog(3) or syslog.h. The default priority is LOG_ERR.
+ * The environment variable XKB_LOG, if set, overrides the default value
+ * and may be specified as a priority number or name.
+ */
+void
+xkb_set_log_priority(struct xkb_context *context, int priority);
+
+/**
+ * Returns the current logging priority.
+ */
+int
+xkb_get_log_priority(struct xkb_context *context);
+
+/**
+ * Sets the current logging verbosity, a value from 0 to 10.
+ *
+ * The library can generate a number of warnings which are not helpful to
+ * ordinary users of the library. The verbosity may be increased if more
+ * information is desired (e.g. when developing a keymap). Defaults to 0.
+ * The environment variable XKB_VERBOSITY, if set, overrdies the default
+ * value.
+ *
+ * Note that most verbose messages are of priority LOG_WARNING or lower.
+ */
+void
+xkb_set_log_verbosity(struct xkb_context *ctx, int verbosity);
+
+/**
+ * Returns the current logging verbosity.
+ */
+int
+xkb_get_log_verbosity(struct xkb_context *ctx);
+
+/**
+ * Retrieves stored data pointer from the context. This might be useful
+ * to access from callbacks like a custom logging function.
+ *
+ * If context is NULL, returns NULL.
+ **/
+void *
+xkb_get_user_data(struct xkb_context *context);
+
+/**
+ * Store custom user data in the context.
+ */
+void
+xkb_set_user_data(struct xkb_context *context, void *user_data);
+
+/** @} */
+
+/**
* @defgroup map Keymap management
* These utility functions allow you to create and deallocate XKB keymaps.
*