libei/src/util-logger.c
Peter Hutterer f7a24b2fbd Add the minimal implementation for a UNIX socket libeis server
This is the minimum framework to support new clients, added devices and
pointer relative motion events. It's missing a bunch of checks and
verification, most of the server hooks aren't there yet, the only
implementation is a UNIX socket and the protocol is plain text (but at least
the last two makes it netcat-compatible).

Protocol is plain text for now and interaction is like this (S is server, C is client):
S: hello
C: connect myclientname
S: connected
C: add 2 4
S: accept 2
C: rel 2 -1 1
C: rel 2 5 4

Where the last two lines are: add device with id 2 and capability mask 0x4,
send a relative pointer motion event for device 2 with coordinates -1/1, then
5/4.

The implementation relies heavily on some abstraction and macros galore, see
the various util-* files. These are largely copied from libinput, with a few
parts removed and a few other parts added.

Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
2020-07-28 19:33:34 +10:00

104 lines
2.9 KiB
C

/*
* Copyright © 2020 Red Hat, Inc.
*
* 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 "config.h"
#include <stdbool.h>
#include <stdio.h>
#include "util-logger.h"
#include "util-object.h"
struct logger {
struct object object;
enum logger_priority priority;
logger_log_func_t handler;
void *user_data;
};
_printf_(3, 0)
static void
logger_default_log_func(struct logger *logger,
enum logger_priority priority,
const char *format, va_list args)
{
const char *prefix;
switch(priority) {
case LOGGER_DEBUG: prefix = "debug"; break;
case LOGGER_INFO: prefix = "info"; break;
case LOGGER_WARN: prefix = "warn"; break;
case LOGGER_ERROR: prefix = "error"; break;
default: prefix="<invalid priority>"; break;
}
fprintf(stderr, "%s: ", prefix);
vfprintf(stderr, format, args);
}
void
log_msg_va(struct logger *logger,
enum logger_priority priority,
const char *format,
va_list args)
{
if (logger->handler && logger->priority <= priority)
logger->handler(logger, priority, format, args);
}
void
log_msg(struct logger *logger,
enum logger_priority priority,
const char *format, ...)
{
va_list args;
va_start(args, format);
log_msg_va(logger, priority, format, args);
va_end(args);
}
static void
logger_destroy(struct logger* logger)
{
/* nothing to do */
}
OBJECT_DECLARE_CREATE(logger);
OBJECT_DECLARE_UNREF(logger);
OBJECT_DECLARE_SETTER(logger, priority, enum logger_priority);
OBJECT_DECLARE_GETTER(logger, priority, enum logger_priority);
OBJECT_DECLARE_SETTER(logger, user_data, void *);
OBJECT_DECLARE_GETTER(logger, user_data, void *);
OBJECT_DECLARE_SETTER(logger, handler, logger_log_func_t);
struct logger *
logger_new(void *user_data)
{
struct logger *logger = logger_create(NULL);
logger->user_data = user_data;
logger->priority = LOGGER_WARN;
logger->handler = logger_default_log_func;
return logger;
}