diff options
author | Daniel Stone <daniel@fooishbar.org> | 2012-08-08 14:01:46 +0200 |
---|---|---|
committer | Daniel Stone <daniel@fooishbar.org> | 2012-08-08 16:23:31 +0200 |
commit | 5e276adb9209749f1b5152853c640ec444df7051 (patch) | |
tree | d6c77df81fe9dc3c8d470903c059e05330b4824c | |
parent | ba8458a9fd580d80d127ed1768f8f1d4a51ef251 (diff) |
Add xkb_log_level enum rather than using syslog
Instead of relying on people including syslog.h, add our own
XKB_LOG_LEVEL_* defines.
Signed-off-by: Daniel Stone <daniel@fooishbar.org>
-rw-r--r-- | src/context.c | 31 | ||||
-rw-r--r-- | src/xkb-priv.h | 20 | ||||
-rw-r--r-- | test/common.c | 3 | ||||
-rw-r--r-- | test/log.c | 13 | ||||
-rw-r--r-- | test/rulescomp.c | 8 | ||||
-rw-r--r-- | xkbcommon/xkbcommon.h | 24 |
6 files changed, 59 insertions, 40 deletions
diff --git a/src/context.c b/src/context.c index 864c6de..942fcc2 100644 --- a/src/context.c +++ b/src/context.c @@ -30,7 +30,6 @@ #include <stdarg.h> #include <stdio.h> #include <unistd.h> -#include <syslog.h> #include "xkb-priv.h" #include "atom.h" @@ -180,18 +179,16 @@ static const char * priority_to_prefix(int priority) { switch (priority) { - case LOG_DEBUG: + case XKB_LOG_LEVEL_DEBUG: return "Debug:"; - case LOG_INFO: + case XKB_LOG_LEVEL_INFO: return "Info:"; - case LOG_WARNING: + case XKB_LOG_LEVEL_WARNING: return "Warning:"; - case LOG_ERR: + case XKB_LOG_LEVEL_ERROR: return "Error:"; - case LOG_CRIT: - case LOG_ALERT: - case LOG_EMERG: - return "Internal error:"; + case XKB_LOG_LEVEL_CRITICAL: + return "Internal error (critical):"; default: return NULL; } @@ -218,15 +215,15 @@ log_priority(const char *priority) { if (errno == 0 && (endptr[0] == '\0' || isspace(endptr[0]))) return prio; if (strncasecmp(priority, "err", 3) == 0) - return LOG_ERR; + return XKB_LOG_LEVEL_ERROR; if (strncasecmp(priority, "warn", 4) == 0) - return LOG_WARNING; + return XKB_LOG_LEVEL_WARNING; if (strncasecmp(priority, "info", 4) == 0) - return LOG_INFO; + return XKB_LOG_LEVEL_INFO; if (strncasecmp(priority, "debug", 5) == 0) - return LOG_DEBUG; + return XKB_LOG_LEVEL_DEBUG; - return LOG_ERR; + return XKB_LOG_LEVEL_ERROR; } static int @@ -256,7 +253,7 @@ xkb_context_new(enum xkb_context_flags flags) ctx->refcnt = 1; ctx->log_fn = default_log_fn; - ctx->log_priority = LOG_ERR; + ctx->log_priority = XKB_LOG_LEVEL_ERROR; ctx->log_verbosity = 0; /* Environment overwrites defaults. */ @@ -327,14 +324,14 @@ xkb_set_log_fn(struct xkb_context *ctx, ctx->log_fn = (log_fn ? log_fn : default_log_fn); } -XKB_EXPORT int +XKB_EXPORT enum xkb_log_level 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) +xkb_set_log_priority(struct xkb_context *ctx, enum xkb_log_level priority) { ctx->log_priority = priority; } diff --git a/src/xkb-priv.h b/src/xkb-priv.h index 42b7fa1..68bbcb9 100644 --- a/src/xkb-priv.h +++ b/src/xkb-priv.h @@ -82,7 +82,6 @@ #include <stdbool.h> #include <string.h> #include <strings.h> -#include <syslog.h> #include <X11/extensions/XKB.h> #include "xkbcommon/xkbcommon.h" @@ -115,7 +114,7 @@ struct xkb_context { ATTR_PRINTF(3, 0) void (*log_fn)(struct xkb_context *ctx, int priority, const char *fmt, va_list args); - int log_priority; + enum xkb_log_level log_priority; int log_verbosity; void *user_data; @@ -549,12 +548,17 @@ xkb_log(struct xkb_context *ctx, int priority, const char *fmt, ...); * 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_dbg(ctx, ...) \ + xkb_log_cond((ctx), XKB_LOG_LEVEL_DEBUG, __VA_ARGS__) +#define log_info(ctx, ...) \ + xkb_log_cond((ctx), XKB_LOG_LEVEL_INFO, __VA_ARGS__) +#define log_warn(ctx, ...) \ + xkb_log_cond((ctx), XKB_LOG_LEVEL_WARNING, __VA_ARGS__) +#define log_err(ctx, ...) \ + xkb_log_cond((ctx), XKB_LOG_LEVEL_ERROR, __VA_ARGS__) +#define log_wsgo(ctx, ...) \ + xkb_log_cond((ctx), XKB_LOG_LEVEL_CRITICAL, __VA_ARGS__) #define log_lvl(ctx, lvl, ...) \ - xkb_log_cond_lvl((ctx), LOG_WARNING, (lvl), __VA_ARGS__) + xkb_log_cond_lvl((ctx), XKB_LOG_LEVEL_WARNING, (lvl), __VA_ARGS__) #endif /* XKB_PRIV_H */ diff --git a/test/common.c b/test/common.c index c1f9786..2d49824 100644 --- a/test/common.c +++ b/test/common.c @@ -35,7 +35,6 @@ #include <unistd.h> #include <sys/types.h> #include <sys/stat.h> -#include <sys/syslog.h> #include "xkbcommon/xkbcommon.h" #include "test.h" @@ -102,7 +101,7 @@ test_get_context(void) xkb_context_include_path_append(ctx, test_get_path("")); - xkb_set_log_priority(ctx, LOG_DEBUG); + xkb_set_log_priority(ctx, XKB_LOG_LEVEL_DEBUG); xkb_set_log_verbosity(ctx, 101); return ctx; @@ -25,7 +25,6 @@ #include <stdarg.h> #include <stdio.h> #include <stdlib.h> -#include <syslog.h> #include "test.h" #include "xkb-priv.h" @@ -36,13 +35,13 @@ static const char * priority_to_string(int priority) { switch (priority) { - case LOG_ERR: + case XKB_LOG_LEVEL_ERROR: return "error"; - case LOG_WARNING: + case XKB_LOG_LEVEL_WARNING: return "warning"; - case LOG_INFO: + case XKB_LOG_LEVEL_INFO: return "info"; - case LOG_DEBUG: + case XKB_LOG_LEVEL_DEBUG: return "debug"; } @@ -89,7 +88,7 @@ main(void) log_err(ctx, "first error: %lu\n", 115415UL); log_lvl(ctx, 5, "first verbose 5\n"); - xkb_set_log_priority(ctx, LOG_DEBUG); + xkb_set_log_priority(ctx, XKB_LOG_LEVEL_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"); @@ -97,7 +96,7 @@ main(void) log_lvl(ctx, 6, "second verbose 6\n"); xkb_set_log_verbosity(ctx, 0); - xkb_set_log_priority(ctx, -1); + xkb_set_log_priority(ctx, XKB_LOG_LEVEL_CRITICAL); log_warn(ctx, "third warning: %d\n", 87); log_dbg(ctx, "third debug: %s %s\n", "hello", "world"); log_info(ctx, "third info\n"); diff --git a/test/rulescomp.c b/test/rulescomp.c index 8eb9464..bed0bb5 100644 --- a/test/rulescomp.c +++ b/test/rulescomp.c @@ -70,13 +70,21 @@ static void benchmark(struct xkb_context *context) { struct timespec start, stop, elapsed; + enum xkb_log_level old_prio = xkb_get_log_priority(context); + int old_verb = xkb_get_log_verbosity(context); int i; + xkb_set_log_priority(context, XKB_LOG_LEVEL_CRITICAL); + xkb_set_log_verbosity(context, 0); + clock_gettime(CLOCK_MONOTONIC, &start); for (i = 0; i < BENCHMARK_ITERATIONS; i++) assert(test_rmlvo_silent(context, "evdev", "", "us", "", "")); clock_gettime(CLOCK_MONOTONIC, &stop); + xkb_set_log_priority(context, old_prio); + xkb_set_log_verbosity(context, old_verb); + elapsed.tv_sec = stop.tv_sec - start.tv_sec; elapsed.tv_nsec = stop.tv_nsec - start.tv_nsec; if (elapsed.tv_nsec < 0) { diff --git a/xkbcommon/xkbcommon.h b/xkbcommon/xkbcommon.h index 58e4183..6afe5e1 100644 --- a/xkbcommon/xkbcommon.h +++ b/xkbcommon/xkbcommon.h @@ -256,6 +256,19 @@ xkb_context_unref(struct xkb_context *context); * @{ */ +enum xkb_log_level { + /** Log critical internal errors only */ + XKB_LOG_LEVEL_CRITICAL = 0, + /** Log all errors */ + XKB_LOG_LEVEL_ERROR = 1, + /** Log warnings and errors */ + XKB_LOG_LEVEL_WARNING = 2, + /** Log information, warnings, and errors */ + XKB_LOG_LEVEL_INFO = 3, + /** Log all the things */ + XKB_LOG_LEVEL_DEBUG = 4, +}; + /** * Sets the function to be called for logging messages. * Passing NULL restores the default function, which logs to stderr. @@ -266,20 +279,18 @@ xkb_set_log_fn(struct xkb_context *context, const char *format, va_list args)); /** * Sets the current logging priority. The value controls which messages - * are logged. + * are logged. The default priority is LOG_ERR. * - * 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); +xkb_set_log_priority(struct xkb_context *context, enum xkb_log_level priority); /** * Returns the current logging priority. */ -int +enum xkb_log_level xkb_get_log_priority(struct xkb_context *context); /** @@ -291,7 +302,8 @@ xkb_get_log_priority(struct xkb_context *context); * The environment variable XKB_VERBOSITY, if set, overrdies the default * value. * - * Note that most verbose messages are of priority LOG_WARNING or lower. + * Note that most verbose messages are of priority XKB_LOG_LEVEL_WARNING + * or lower. */ void xkb_set_log_verbosity(struct xkb_context *ctx, int verbosity); |