Skip to content

Commit

Permalink
logging: Use messages ID in registry
Browse files Browse the repository at this point in the history
  • Loading branch information
wismill committed Sep 25, 2024
1 parent cffbcdc commit 2703fb2
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 45 deletions.
2 changes: 1 addition & 1 deletion doc/message-registry.md
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ keysyms (`Unnnn`) if relevant.
<dt>Summary</dt><dd>A keysym name has been deprecated: use the corresponding canonical name instead</dd>
</dl>

Some keysym names have typos, unconventional names or historical aliases of a reference name.
Some keysym names have typos, unconventional names or are historical aliases.
Please consider migrating to the reference name reported in the warning message.


Expand Down
1 change: 1 addition & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,7 @@ if get_option('enable-xkbregistry')
deps_libxkbregistry = [dep_libxml]
libxkbregistry_sources = [
'src/registry.c',
'src/messages-codes.h',
'src/utils.h',
'src/utils.c',
'src/util-list.h',
Expand Down
29 changes: 0 additions & 29 deletions src/context.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,35 +109,6 @@ void
xkb_context_sanitize_rule_names(struct xkb_context *ctx,
struct xkb_rule_names *rmlvo);

/*
* Macro sorcery: PREPEND_MESSAGE_ID enables the log functions to format messages
* with the message ID only if the ID is not 0 (XKB_LOG_MESSAGE_NO_ID).
* This avoid checking the ID value at run time.
*
* The trick resides in CHECK_ID:
* • CHECK_ID(0) expands to:
* ‣ SECOND(MATCH0, WITH_ID, unused)
* ‣ SECOND(unused,WITHOUT_ID, WITH_ID, unused)
* ‣ WITHOUT_ID
* • CHECK_ID(123) expands to:
* ‣ SECOND(MATCH123, WITH_ID, unused)
* ‣ WITH_ID
*/
#define EXPAND(...) __VA_ARGS__ /* needed for MSVC compatibility */

#define JOIN_EXPAND(a, b) a##b
#define JOIN(a, b) JOIN_EXPAND(a, b)

#define SECOND_EXPAND(a, b, ...) b
#define SECOND(...) EXPAND(SECOND_EXPAND(__VA_ARGS__))

#define MATCH0 unused,WITHOUT_ID
#define CHECK_ID(value) SECOND(JOIN(MATCH, value), WITH_ID, unused)

#define FORMAT_MESSAGE_WITHOUT_ID(id, fmt) fmt
#define FORMAT_MESSAGE_WITH_ID(id, fmt) "[XKB-%03d] " fmt, id
#define PREPEND_MESSAGE_ID(id, fmt) JOIN(FORMAT_MESSAGE_, CHECK_ID(id))(id, fmt)

/*
* 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
Expand Down
29 changes: 29 additions & 0 deletions src/messages-codes.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,35 @@

#include <stdint.h>

/*
* Macro sorcery: PREPEND_MESSAGE_ID enables the log functions to format messages
* with the message ID only if the ID is not 0 (XKB_LOG_MESSAGE_NO_ID).
* This avoid checking the ID value at run time.
*
* The trick resides in CHECK_ID:
* • CHECK_ID(0) expands to:
* ‣ SECOND(MATCH0, WITH_ID, unused)
* ‣ SECOND(unused,WITHOUT_ID, WITH_ID, unused)
* ‣ WITHOUT_ID
* • CHECK_ID(123) expands to:
* ‣ SECOND(MATCH123, WITH_ID, unused)
* ‣ WITH_ID
*/
#define EXPAND(...) __VA_ARGS__ /* needed for MSVC compatibility */

#define JOIN_EXPAND(a, b) a##b
#define JOIN(a, b) JOIN_EXPAND(a, b)

#define SECOND_EXPAND(a, b, ...) b
#define SECOND(...) EXPAND(SECOND_EXPAND(__VA_ARGS__))

#define MATCH0 unused,WITHOUT_ID
#define CHECK_ID(value) SECOND(JOIN(MATCH, value), WITH_ID, unused)

#define FORMAT_MESSAGE_WITHOUT_ID(id, fmt) fmt
#define FORMAT_MESSAGE_WITH_ID(id, fmt) "[XKB-%03d] " fmt, id
#define PREPEND_MESSAGE_ID(id, fmt) JOIN(FORMAT_MESSAGE_, CHECK_ID(id))(id, fmt)

/**
* Special case when no message identifier is defined.
*/
Expand Down
29 changes: 29 additions & 0 deletions src/messages-codes.h.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,35 @@

#include <stdint.h>

/*
* Macro sorcery: PREPEND_MESSAGE_ID enables the log functions to format messages
* with the message ID only if the ID is not 0 (XKB_LOG_MESSAGE_NO_ID).
* This avoid checking the ID value at run time.
*
* The trick resides in CHECK_ID:
* • CHECK_ID(0) expands to:
* ‣ SECOND(MATCH0, WITH_ID, unused)
* ‣ SECOND(unused,WITHOUT_ID, WITH_ID, unused)
* ‣ WITHOUT_ID
* • CHECK_ID(123) expands to:
* ‣ SECOND(MATCH123, WITH_ID, unused)
* ‣ WITH_ID
*/
#define EXPAND(...) __VA_ARGS__ /* needed for MSVC compatibility */

#define JOIN_EXPAND(a, b) a##b
#define JOIN(a, b) JOIN_EXPAND(a, b)

#define SECOND_EXPAND(a, b, ...) b
#define SECOND(...) EXPAND(SECOND_EXPAND(__VA_ARGS__))

#define MATCH0 unused,WITHOUT_ID
#define CHECK_ID(value) SECOND(JOIN(MATCH, value), WITH_ID, unused)

#define FORMAT_MESSAGE_WITHOUT_ID(id, fmt) fmt
#define FORMAT_MESSAGE_WITH_ID(id, fmt) "[XKB-%03d] " fmt, id
#define PREPEND_MESSAGE_ID(id, fmt) JOIN(FORMAT_MESSAGE_, CHECK_ID(id))(id, fmt)

/**
* Special case when no message identifier is defined.
*/
Expand Down
39 changes: 24 additions & 15 deletions src/registry.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include <libxml/parser.h>

#include "xkbcommon/xkbregistry.h"
#include "messages-codes.h"
#include "utils.h"
#include "util-list.h"
#include "util-mem.h"
Expand Down Expand Up @@ -156,16 +157,18 @@ rxkb_log(struct rxkb_context *ctx, enum rxkb_log_level level,
* format is supplied without arguments. Not supplying it would still
* result in an error, though.
*/
#define rxkb_log_with_code(ctx, level, msg_id, fmt, ...) \
rxkb_log(ctx, level, PREPEND_MESSAGE_ID(msg_id, fmt), ##__VA_ARGS__)
#define log_dbg(ctx, ...) \
rxkb_log((ctx), RXKB_LOG_LEVEL_DEBUG, __VA_ARGS__)
#define log_info(ctx, ...) \
rxkb_log((ctx), RXKB_LOG_LEVEL_INFO, __VA_ARGS__)
#define log_warn(ctx, ...) \
rxkb_log((ctx), RXKB_LOG_LEVEL_WARNING, __VA_ARGS__)
#define log_err(ctx, ...) \
rxkb_log((ctx), RXKB_LOG_LEVEL_ERROR, __VA_ARGS__)
#define log_wsgo(ctx, ...) \
rxkb_log((ctx), RXKB_LOG_LEVEL_CRITICAL, __VA_ARGS__)
#define log_warn(ctx, id, ...) \
rxkb_log_with_code((ctx), RXKB_LOG_LEVEL_WARNING, id, __VA_ARGS__)
#define log_err(ctx, id, ...) \
rxkb_log_with_code((ctx), RXKB_LOG_LEVEL_ERROR, id, __VA_ARGS__)
#define log_wsgo(ctx, id, ...) \
rxkb_log_with_code((ctx), RXKB_LOG_LEVEL_CRITICAL, id, __VA_ARGS__)


#define DECLARE_REF_UNREF_FOR_TYPE(type_) \
Expand Down Expand Up @@ -561,7 +564,8 @@ rxkb_context_include_path_append(struct rxkb_context *ctx, const char *path)
char rules[PATH_MAX];

if (ctx->context_state != CONTEXT_NEW) {
log_err(ctx, "include paths can only be appended to a new context\n");
log_err(ctx, XKB_LOG_MESSAGE_NO_ID,
"include paths can only be appended to a new context\n");
return false;
}

Expand Down Expand Up @@ -599,7 +603,8 @@ rxkb_context_include_path_append_default(struct rxkb_context *ctx)
bool ret = false;

if (ctx->context_state != CONTEXT_NEW) {
log_err(ctx, "include paths can only be appended to a new context\n");
log_err(ctx, XKB_LOG_MESSAGE_NO_ID,
"include paths can only be appended to a new context\n");
return false;
}

Expand Down Expand Up @@ -648,7 +653,8 @@ rxkb_context_parse(struct rxkb_context *ctx, const char *ruleset)
bool success = false;

if (ctx->context_state != CONTEXT_NEW) {
log_err(ctx, "parse must only be called on a new context\n");
log_err(ctx, XKB_LOG_MESSAGE_NO_ID,
"parse must only be called on a new context\n");
return false;
}

Expand Down Expand Up @@ -751,7 +757,7 @@ parse_config_item(struct rxkb_context *ctx, xmlNode *parent,
else if (xmlStrEqual(raw_popularity, (const xmlChar*)"exotic"))
config->popularity = RXKB_POPULARITY_EXOTIC;
else
log_err(ctx,
log_err(ctx, XKB_LOG_MESSAGE_NO_ID,
"xml:%d: invalid popularity attribute: expected "
"'standard' or 'exotic', got: '%s'\n",
ci->line, raw_popularity);
Expand All @@ -773,7 +779,8 @@ parse_config_item(struct rxkb_context *ctx, xmlNode *parent,
}

if (!config->name || !strlen(config->name)) {
log_err(ctx, "xml:%d: missing required element 'name'\n",
log_err(ctx, XKB_LOG_MESSAGE_NO_ID,
"xml:%d: missing required element 'name'\n",
ci->line);
config_item_free(config);
return false;
Expand Down Expand Up @@ -1133,7 +1140,8 @@ xml_error_func(void *ctx, const char *msg, ...)

/* This shouldn't really happen */
if (rc < 0) {
log_err(ctx, "+++ out of cheese error. redo from start +++\n");
log_err(ctx, XKB_LOG_MESSAGE_NO_ID,
"+++ out of cheese error. redo from start +++\n");
slen = 0;
memset(buf, 0, sizeof(buf));
return;
Expand All @@ -1148,7 +1156,7 @@ xml_error_func(void *ctx, const char *msg, ...)

/* We're assuming here that the last character is \n. */
if (buf[slen - 1] == '\n') {
log_err(ctx, "%s", buf);
log_err(ctx, XKB_LOG_MESSAGE_NO_ID, "%s", buf);
memset(buf, 0, sizeof(buf));
slen = 0;
}
Expand Down Expand Up @@ -1204,7 +1212,7 @@ validate(struct rxkb_context *ctx, xmlDoc *doc)

dtd = xmlIOParseDTD(NULL, buf, XML_CHAR_ENCODING_UTF8);
if (!dtd) {
log_err(ctx, "Failed to load DTD\n");
log_err(ctx, XKB_LOG_MESSAGE_NO_ID, "Failed to load DTD\n");
return false;
}

Expand Down Expand Up @@ -1240,7 +1248,8 @@ parse(struct rxkb_context *ctx, const char *path,
return false;

if (!validate(ctx, doc)) {
log_err(ctx, "XML error: failed to validate document at %s\n", path);
log_err(ctx, XKB_LOG_MESSAGE_NO_ID,
"XML error: failed to validate document at %s\n", path);
goto error;
}

Expand Down

0 comments on commit 2703fb2

Please sign in to comment.