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>
master
Daniel Stone 2012-08-08 14:01:46 +02:00
parent ba8458a9fd
commit 5e276adb92
6 changed files with 59 additions and 40 deletions

View File

@ -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;
}

View File

@ -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 */

View File

@ -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;

View File

@ -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");

View File

@ -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) {

View File

@ -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);