Add a dedicated logging facility (keyd_log)

- Add support for NO_COLOR (#346)
 - Automatically disable colorized output when stdout is not a terminal.
master
Raheman Vaiya 3 years ago
parent 04c9e15d70
commit 03d4ce3339
  1. 4
      src/config.c
  2. 9
      src/error.c
  3. 26
      src/error.h
  4. 7
      src/keyd.c
  5. 80
      src/log.c
  6. 26
      src/log.h
  7. 24
      src/monitor.c

@ -20,7 +20,7 @@
#include "keyd.h"
#include "ini.h"
#include "keys.h"
#include "error.h"
#include "log.h"
#include "string.h"
#include "unicode.h"
@ -28,7 +28,7 @@
#define MAX_LINE_LEN 256
#undef warn
#define warn(fmt, ...) fprintf(stderr, "\t\033[31;1mERROR:\033[0m "fmt"\n", ##__VA_ARGS__)
#define warn(fmt, ...) keyd_log("\tr{ERROR:} "fmt"\n", ##__VA_ARGS__)
static struct {
const char *name;

@ -1,9 +0,0 @@
/*
* keyd - A key remapping daemon.
*
* © 2019 Raheman Vaiya (see also: LICENSE).
*/
#include "error.h"
char errstr[2048];
int debug_level;

@ -1,26 +0,0 @@
/*
* keyd - A key remapping daemon.
*
* © 2019 Raheman Vaiya (see also: LICENSE).
*/
#ifndef ERROR_H
#define ERROR_H
#include <stdio.h>
#define dbg(fmt, ...) { \
if (debug_level >= 1) \
fprintf(stderr, "DEBUG: %s:%d: "fmt"\n", __FILE__, __LINE__, ##__VA_ARGS__); \
}
#define dbg2(fmt, ...) { \
if (debug_level >= 2) \
fprintf(stderr, "DEBUG: %s:%d: "fmt"\n", __FILE__, __LINE__, ##__VA_ARGS__); \
}
#define err(fmt, ...) snprintf(errstr, sizeof(errstr), fmt, ##__VA_ARGS__);
extern int debug_level;
extern char errstr[2048];
#endif

@ -219,9 +219,14 @@ int main(int argc, char *argv[])
{
size_t i;
debug_level =
log_level =
atoi(getenv("KEYD_DEBUG") ? getenv("KEYD_DEBUG") : "");
if (isatty(1))
suppress_colours = getenv("NO_COLOR") ? 1 : 0;
else
suppress_colours = 1;
dbg("Debug mode activated");
signal(SIGTERM, exit);

@ -0,0 +1,80 @@
/*
* keyd - A key remapping daemon.
*
* © 2019 Raheman Vaiya (see also: LICENSE).
*/
#include "log.h"
#include <time.h>
char errstr[2048];
int log_level = 0;
int suppress_colours = 0;
static const char *colorize(const char *s)
{
int i;
static char buf[1024];
size_t n = 0;
int inside_escape = 0;
for (i = 0; s[i] != 0 && n < sizeof(buf); i++) {
if (s[i+1] == '{') {
int escape_num = 0;
switch (s[i]) {
case 'r': escape_num = 1; break;
case 'g': escape_num = 2; break;
case 'y': escape_num = 3; break;
case 'b': escape_num = 4; break;
case 'm': escape_num = 5; break;
case 'c': escape_num = 6; break;
case 'w': escape_num = 7; break;
default: break;
}
if (escape_num) {
if (!suppress_colours && (sizeof(buf)-n > 5)) {
buf[n++] = '\033';
buf[n++] = '[';
buf[n++] = '3';
buf[n++] = '0' + escape_num;
buf[n++] = 'm';
}
inside_escape = 1;
i++;
continue;
}
}
if (s[i] == '}' && inside_escape) {
if (!suppress_colours && (sizeof(buf)-n > 4)) {
memcpy(buf+n, "\033[0m", 4);
n += 4;
}
inside_escape = 0;
continue;
}
buf[n++] = s[i];
}
buf[n] = 0;
return buf;
}
void _keyd_log(int level, const char *fmt, ...)
{
if (level > log_level)
return;
va_list ap;
va_start(ap, fmt);
vprintf(colorize(fmt), ap);
va_end(ap);
}

@ -0,0 +1,26 @@
/*
* keyd - A key remapping daemon.
*
* © 2019 Raheman Vaiya (see also: LICENSE).
*/
#ifndef KEYD_LOG_H
#define KEYD_LOG_H
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#define keyd_log(fmt, ...) _keyd_log(0, fmt, ##__VA_ARGS__);
#define dbg(fmt, ...) _keyd_log(1, "r{DEBUG:} b{%s:%d:} "fmt"\n", __FILE__, __LINE__, ##__VA_ARGS__)
#define dbg2(fmt, ...) _keyd_log(2, "r{DEBUG:} b{%s:%d:} "fmt"\n", __FILE__, __LINE__, ##__VA_ARGS__)
#define err(fmt, ...) snprintf(errstr, sizeof(errstr), fmt, ##__VA_ARGS__);
void _keyd_log(int level, const char *fmt, ...);
extern int log_level;
extern int suppress_colours;
extern char errstr[2048];
#endif

@ -38,14 +38,14 @@ int event_handler(struct event *ev)
const char *name;
case EV_DEV_ADD:
printf("device added: %04x:%04x %s (%s)\n",
ev->dev->vendor_id, ev->dev->product_id,
ev->dev->name, ev->dev->path);
keyd_log("device added: %04x:%04x %s (%s)\n",
ev->dev->vendor_id, ev->dev->product_id,
ev->dev->name, ev->dev->path);
break;
case EV_DEV_REMOVE:
printf("device removed: %04x:%04x %s (%s)\n",
ev->dev->vendor_id, ev->dev->product_id,
ev->dev->name, ev->dev->path);
keyd_log("device removed: %04x:%04x %s (%s)\n",
ev->dev->vendor_id, ev->dev->product_id,
ev->dev->name, ev->dev->path);
break;
case EV_DEV_EVENT:
switch (ev->devev->type) {
@ -53,13 +53,13 @@ int event_handler(struct event *ev)
name = keycode_table[ev->devev->code].name;
if (time_flag)
printf("+%ld ms\t", ev->timestamp - last_time);
keyd_log("r{+%ld} ms\t", ev->timestamp - last_time);
printf("%s\t%04x:%04x\t%s %s\n",
ev->dev->name,
ev->dev->vendor_id,
ev->dev->product_id, name,
ev->devev->pressed ? "down" : "up");
keyd_log("%s\t%04x:%04x\t%s %s\n",
ev->dev->name,
ev->dev->vendor_id,
ev->dev->product_id, name,
ev->devev->pressed ? "down" : "up");
break;
default:

Loading…
Cancel
Save